Note that there are some explanatory texts on larger screens.

plurals
  1. POExtract wrong data from a frame in C?
    primarykey
    data
    text
    <p>I am writing a program that reads the data from the serial port on Linux. The data are sent by another device with the following frame format:</p> <pre> <code> |start | Command | Data | CRC | End | |0x02 | 0x41 | (0-127 octets) | | 0x03| ---------------------------------------------------- </code> </pre> <p>The Data field contains 127 octets as shown and octet 1,2 contains one type of data; octet 3,4 contains another data. I need to get these data.</p> <p>Because in <strong>C</strong>, one byte can only holds one character and in the start field of the frame, it is 0x02 which means STX which is 3 characters.</p> <p>So, in order to test my program,</p> <p>On the sender side, I construct an array as the frame formatted above like:</p> <pre> <code> char frame[254]; frame[0] = 0x02; // starting field frame[1] = 0x41; // command field which is character 'A' ..so on.. </code> </pre> <p>And, then On the receiver side, I take out the fields like:</p> <pre> char result[254]; // read data read(result); printf("command = %c", result[1]); // get the command field of the frame // get other field's values </pre> <p>the command field value (result[1]) is not character 'A'. </p> <p>I think, this because the first field value of the frame is 0x02 (STX) occupying 3 first places in the array frame and leading to the wrong results on the receiver side.</p> <p>How can I correct the issue or am I doing something wrong at the sender side?</p> <p>related questions: <br /> <a href="https://stackoverflow.com/questions/2500567/parse-and-read-data-frame-in-c">Parse and read data frame in C?</a> <br /> <a href="https://stackoverflow.com/questions/2531779/clear-data-at-serial-port-in-linux-in-c">Clear data at serial port in Linux in C?</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload