How to implement CRC using C language [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am bit new to programming and i want to send a float data to eeprom from a controller and i want to implement crc for the error detection of the data when ever i tries to read or write.
This is my intention.But i found many algorithms for implementing crc and its been quite confusing for me to follow which one.
The compiler provides 4 bytes of space for all data and its function sends each byte one at a time.So i trust i would be required to do the crc of each 8bit data or can implement it as a whole.
And i am using C language.
In brief:
i just need to do a
1.Crc implementation.
2.Common Data size =32 bits
3.Compiler function sends one byte at a time.
4.And how do we fix which algorithm to go for.
Thank you very much

Try Reading this article(It Contains C Codes and Diagrams Which you need) :
http://www.barrgroup.com/Embedded-Systems/How-To/CRC-Calculation-C-Code
CRCs are among the best checksums available to detect and/or correct errors in communications transmissions. Unfortunately, the modulo-2 arithmetic used to compute CRCs doesn't map easily into software. This article shows how to implement an efficient CRC in C.

Related

Data structures layout [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I have a disagreement with my colleague about sending/receiving a data structure between two machines (Also with different compilers) by UART.
Our data structure has several simple variable types as its fields (like int32, uint8 and etc).
In his opinion, to have a data structure with the same sequence and alignment in their fields, we have to use serializer and deserializer. Otherwise, Our code has the potential of different struct layout between two sides.
But I did it without using serializer/deserializer many times and never saw any problem.
I think using from the #pragma pack(...), guarantee our purpose. Because of most differences in each compiler (In data structures compiling) occurs in fields alignment due to padding for speedup or size optimization. (Ignore the different of endianness).
For more details, We want to send/receive a struct between a Cortex-M4 (IAR) and PC (Qt in windows) by UART currently.
Am I in a wrong way? Or my friend?!
This is, I'm afraid, fundamentally a question of opinion, that can never be fully resolved.
For what it's worth, I am adamantly, vociferously with your colleague. I believe in writing explicit serializers and deserializers. I don't believe in blatting out an in-memory data structure and hoping that the other side can slurp it down without error. I don't believe in ignoring endianness differences. I believe that "blatting it out" will inevitably fail, in the end, somewhere, and I don't want to run that risk. I believe that although the explicit de/serializers may seem to be more trouble to write up front, they save time in the long run because of all the fussing and debugging you don't have to do later.
But there are also huge swaths of programmers (I suspect a significant majority) who agree entirely with you: that given enough hacking, and suitable pragmas and packing directives, you can get the "blat it out" technique to work at least most of the time, and it may be more efficient, to boot. So you're in good company, and with as many people as there are out there who obviously agree with you, I can't tell you that you're wrong.

Getting started with coding unix commands [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have been learning c and data structures for quite some time now and I wanted to see whether I could apply what I have learnt. I searched a bit and found out that I could start with util linux but, before I could do so, I thought I'd check and perhaps dabble a bit with the code for basic unix commands like "cat". I was able to understand what a part of the code might have been trying to do, but I was not able to understand the entire code as a unit.
For example, in the "cat" code, a pointer to the output buffer and input buffer is declared and is appropriately used, which I could understand. What i could not understand, are parts of code like io_blksize (stat_buf) which has no description whatsoever, on what it does. Or how two pointers declared as pointers to the input and output buffers, actually correspond to the input and output buffers ?
So my question being, how do I approach these type of code, how can I understand something that has no description to what it does (in the example given above) and how can I make and implement changes in the code, so that I can see the changes when i run a command ?
(Would really appreciate references or topics I should start with, so that I can relate what I have learnt to how command code's can be modified. I also apologize if the question is to abstract.)
This is a bit of a subjective question so my answers will just be my opinion of course.
A good place to start when you run into something you don't recognise while reading source code is the manpages. Each function will generally have a manpage, e.g. man 2 read or man 3 printf. Beyond that, I feel perhaps you should get more of a foundation in Unix before attempting to read the straight source code, a good book is Advanced Programming in the Unix Environment. I've been working through it myself and am finding my Unix knowledge improving considerably.
Just my two cents.

Recursive coroutines in C (C99) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
While implementing a communication protocol, we have an encoder that traverses some structs recursively and encodes them into a binary message.
So far so good, but now the buffer has to split out into multiple chunks of fixed size, e.g. the upper size of receiving buffer. Since allocating memory for the full message and cutting it consequently seems to be too wasteful (the size of the message is --in theory-- not bounded), the idea is now to implement a coroutine with means of setjmp/longjmp.
At the moment, I have a prototype with two jump buffers - one buffer for resuming the encode function and the second one for simulating the return behavior of the function to jump back to its caller.
Well, it seems to work, but the code looks like coming straight from hell. Are there any 'conventions' for implementing interruptible recursive functions, maybe a set of macros or something? I would like to use only standardized functions, no inline asm in order to stay portable.
Addition:
The prototype is here: https://github.com/open62541/open62541/compare/master...chunking_longjmp
The 'usage' is shown inside of the unit-test.
Currently, coroutine behavior is implemented for a non-recursive function Array_encodeBinary. However, the 'coroutine' behavior should be extended to the general recursive UA_encodeBinary function located here: https://github.com/open62541/open62541/blob/master/src/ua_types_encoding_binary.c#L1029
As pointed out by Olaf the easiest way would be to use an iterative algorithm. However, if for some reason this is difficult, you can always simulate the recursive algorithm with a stack container and a while loop. This at least makes the function easier to interrupt. Pretty good article of how to implement this can be found here. The article is written for c++, but it should not be difficult to convert it to c.

What are some common uses for the tcpdump -dd option? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
In reading the man pages for tcpdump, I saw that the -dd arguement would output the dump as a fragment of a C file. In what situations is that useful? I take it this is to quickly include and compile the fragment in a program that will be used to process the data according to code we write ourselves? Does this have its utility with unknown or new protocols? Is there some other common, standing situation in which this is needed? Just curious.
It's useful if you're writing a program using libpcap/WinPcap that would use a filter but that, for whatever reason, wouldn't run pcap_compile() to translate a filter string into BPF machine code; it lets you do the compilation with tcpdump and generate some text that you could use in the initialization of an array of struct bpf_insn (a pointer to which, and a count of elements in which, you'd put in a struct bpf_program).
I'm not sure who would do that, however.

How to write bytes to disc in C? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I want to write a program that formats a disc to a new format (not NTFS or FAT32) and can read from and write to the disc, In order to do this, I have to have a way to write single bytes to a disc (without creating files). How do I do that?
EDIT: I found this. But I'm not sure if using CreateFile() as Eli Bendersky said in the first answer (when organized in order of votes) let's you write one byte (or even one bit) at a time, or do you have to write full sectors at a time.
Answer: Since I can't post an answer the question because it was closed, I will answer it write here. You don't need need API functions to do this. All you need to do is open the disk like you open any other file. Like this:
int hFile;
hFile=open("/dev/sdb",0_RDWR);
You program has to directly talk to the driver as you will be by-passing the file-system.

Resources