简体   繁体   English

从串行端口Linux C读取NULL字符

[英]Reading NULL character from Serial Port Linux C

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. 我在Linux中使用串行端口,该端口正在从Arduino设备读取数据。 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. 每当我想将数据从Arduino发送到Linux时,我都会首先发送两个字节,这些字节指示将来自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: 假设我想将300个字节从Ardiuno发送到Linux,我将首先写入{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. 这将给我300 int值,这就像魅力一样。 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}. 但是问题出在我必须读取小于255的数据时。比如说我要读取100个字节,那么前两个字节将为{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. 0是空字符,串行端口不处理它(我手动将0写入串行端口,它总是向我写入0字节),并且我的所有顺序都出错了。 So my question is can I read null characters from serial port OR someone please give me better solution.. 所以我的问题是我可以从串口读取空字符还是有人可以给我更好的解决方案。

thanks in Advance. 提前致谢。

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. 在C语言中处理字节时,请勿将字节(char)与字符串混淆,就像我正在处理字节数组(char data [])一样,当我尝试使用长度为strlen(data)的write方法在串行端口上写入这些字节时),我只会得到不为空的那些字节。 strlen returns the length of data after seeing first null character that is \\0 , because of this I was not getting my desired output. 在看到第一个空字符\\0strlen返回数据长度,因此我没有得到想要的输出。 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}那么我将执行以下操作:

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

told the write function to write 2 bytes. 告诉write函数写2个字节。 This will write 0 and 4, If I write something like this: 如果我写这样的话,它将写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. 还有一件事,如果要在串行端口上写入不可打印的字符(从字节值0到32),那么请确保已将串行端口配置为原始输入和输出。 Have a look on this guide: 看一下这个指南:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM