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:
我已经阅读了很多问题和答案,但没有找到任何解决方案。可能是我的问题不对,但我需要一些指导。我在Linux中使用串口,它从我的Arduino设备读取数据。每当我想将数据从Arduino发送到Linux时,我首先发送两个字节,表示来自Arduino的总字节数。我将这两个字节转换为整数值并开始从串行端口读取数据。说,我想从Ardiuno向Linux发送300个字节,我将先写入{1,44}然后通过以下公式将此1和44字节转换为int:
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..
这将给我300 int值,这就像魅力一样。但是当我必须读取少于255的数据时出现问题。假设我想要读取100个字节,那么前两个字节将是{0,100}。 0是空字符,串口不处理它(我手动写0到串口,它总是给我写0字节),我的所有序列都出错了。所以我的问题是我可以从串口读取空字符或有人请给我更好的解决方案..
thanks in Advance.
提前致谢。
1 个解决方案
#1
1
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:
我解决了问题。处理C中的字节时,不要将字节(char)与字符串混淆,就像我正在处理字节数组(char data [])和当我尝试使用strlen长度的write方法在串行端口上写这些字节时(数据) ),我只得到那些非空的字节。 strlen在看到第一个0字符的空字符后返回数据长度,因此我得不到我想要的输出。我做的是,如果我想写数据char data [] = {0,4},那么我会做这样的事情:
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:
告诉write函数写2个字节。这将写0和4,如果我写这样的东西:
char data[] = {0, 4}
write(serial_port_fd, data, strlen(data));
this will write NOTHING
.
这将写下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:
还有一件事,如果要在串行端口上写入不可打印的字符(从字节值0到32),请确保已为原始输入和输出配置了串行端口。看看本指南:
http://www.cmrr.umn.edu/~strupp/serial.html#config
http://www.cmrr.umn.edu/~strupp/serial.html#config
#1
1
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:
我解决了问题。处理C中的字节时,不要将字节(char)与字符串混淆,就像我正在处理字节数组(char data [])和当我尝试使用strlen长度的write方法在串行端口上写这些字节时(数据) ),我只得到那些非空的字节。 strlen在看到第一个0字符的空字符后返回数据长度,因此我得不到我想要的输出。我做的是,如果我想写数据char data [] = {0,4},那么我会做这样的事情:
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:
告诉write函数写2个字节。这将写0和4,如果我写这样的东西:
char data[] = {0, 4}
write(serial_port_fd, data, strlen(data));
this will write NOTHING
.
这将写下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:
还有一件事,如果要在串行端口上写入不可打印的字符(从字节值0到32),请确保已为原始输入和输出配置了串行端口。看看本指南:
http://www.cmrr.umn.edu/~strupp/serial.html#config
http://www.cmrr.umn.edu/~strupp/serial.html#config