1
votes

I have read many questions and answers but didn't find any solution. May be my question is not right but I need some guidance. I am using serial port in Linux which is reading data from my Arduino device. Whenever I want to send data from Arduino to Linux, I first send two bytes which indicate the total bytes which will come from Arduino. I convert these two bytes to integer value and start reading data from Serial Port. Say, I want to send 300 bytes from Ardiuno to Linux, I will just write {1, 44} first and then convert this 1 and 44 byte into int by the following formula:

char data[] = {1, 44};
int to_read = data[0]
to_read = to_read << 8;
to_read = to_read | data[1];
return to_read;

this will give me 300 int value, this is working like charm. but problem comes when I have to read data less then 255. Say I want to read 100 bytes, then first two bytes will be {0, 100}. 0 is null character, serial port doesn't process it (I manually wrote 0s to serial port, it always give me 0 bytes written), and my all sequence goes wrong. So my question is can I read null characters from serial port OR someone please give me better solution..

thanks in Advance.

1
Reason #18 why i hate it when people say "null" when talking about characters. "Null" basically means "no value" -- ie: it sits outside the range of allowed values -- while a "null character" is the character with code 0. IE: an allowable, if somewhat odd, value. Please, use NUL instead; that's what's in the ASCII tables. :PcHao
Point being, character 0 is still a normal character. It just happens to also have special meaning to C, when working with nul-terminated byte strings. The serial port, on the other hand, couldn't care less.cHao
You might like to show us the code how you setup the serial devices on both sides, and how you then send and receive data via those devices.alk
Yes - code. Serial ports have no intrinsic problem with the NUL char, or byte value 0, octet 0 or whatever you wish to call it. This is surely an issue with C-style null-terminated strings, as suggested by the other commenters.Martin James
finally I got my problem solved. Serial port is configured for 1N8 option. actually I was treating Char value as string, and sending data with size computing with function strlen(char*), I just start sending data like char data[] = {0, 0, 0}; write(serial_fd, data, 3); as I entered total byte size as 3, after that I am able to send NULL (:-P) characters also. And besides, 0 is also a character. I hope it will be helpful for some like me :-pmoonzai

1 Answers

1
votes

I got my problem solved. When working on bytes in C, don't confuse bytes (char) with string like I was treating bytes array (char data[]) and When I was trying to write these bytes on serial port with write method with length of strlen(data), I was only getting those bytes which were not null. strlen returns the length of data after seeing first null character that is \0, because of this I was not getting my desired output. What I did is that, If I want to write data char data[] = {0, 4} then I will do something like this:

char data[] = {0, 4};
write(serial_port_fd, data, 2);

told the write function to write 2 bytes. This will write 0 and 4, If I write something like this:

char data[] = {0, 4}
write(serial_port_fd, data, strlen(data));

this will write NOTHING.

One more thing, If you want to write non printable characters (which are from byte value 0 to 32) on Serial Port, then make sure that you have configured your Serial Port for raw input and ouput. Have a look on this guide:

http://www.cmrr.umn.edu/~strupp/serial.html#config