简体   繁体   English

串口不发送NULL值

[英]Serial port doesn't send NULL values

I'm playing with an XBEE radio, I'm using Linux (Ubuntu 9.10) and the XBEE doesn't appear to send NULL values through the serial port when using MY code. 我正在使用XBEE无线电,正在使用Linux(Ubuntu 9.10),使用MY代码时,XBEE似乎没有通过串行端口发送NULL值。 When I use the XCTU program(stock term emulator that comes with the XBEE on a seperate windows box), I see this output through the serial port when a new XBEE joins the network: 当我使用XCTU程序(在单独的Windows框上XBEE附带的股票术语模拟器)时,当新的XBEE加入网络时,我会通过串行端口看到此输出:

7E 00 20 95 00 13 A2 00 40 3B

etc... perfect. 等...完美。 But, using MY code, when a new XBEE joins the network I see this: 但是,使用MY代码,当新的XBEE加入网络时,我看到以下内容:

7E 20 95 13 A2 40 3B

Here is how I'm opening the serial port 这是我打开串口的方式

struct termios options;
int port;

port = open("/dev/ttyUSB0", O_RDWR | O_NONBLOCK);
tcgetattr(port, &options);
bzero(&options, sizeof(options));

options.c_cflag = B9600 | ~CRTSCTS | CS8 | CLOCAL | CREAD;

tcsetattr(port, TCSANOW, &options);

I have my theories about what that code does, but my theories are obviously wrong. 我对代码的功能有自己的理论,但我的理论显然是错误的。 I'm trying to open the port with 9600, 8N1, No Flow control. 我正在尝试使用9600、8N1,无流量控制打开端口。 You can see I'm also using the serial->USB driver, but since I do seem to get data I'm pretty sure that part is working. 您可以看到我也在使用serial-> USB驱动程序,但是由于我似乎确实在获取数据,因此我可以肯定那部分工作正常。

My guess is when I bzero options, I'm making 0x00 a control char? 我的猜测是当我使用bzero选项时,我是否将0x00用作控制字符? I'm not sure. 我不确定。 When I DON'T bzero options I can only read 5 bytes at a time and I lose data. 当我不设置零选项时,一次只能读取5个字节,并且会丢失数据。 It feels like I'm having a flow control or a baud rate problem, so I bzero() and now I don't get NULLs. 感觉好像我在遇到流控制或波特率问题,所以我使用了bzero(),现在却没有NULL。

I've also just used Minicom on my Linux system and captured the output. 我还刚刚在Linux系统上使用了Minicom并捕获了输出。 I get the same information, no NULLs(this really messes up packet sizes for those unfamiliar with the protocol). 我得到的信息是相同的,没有NULL(对于那些不熟悉协议的人,这确实弄乱了数据包的大小)。 Could my code have set the serial port into a state that minicom isn't changing? 我的代码能否将串行端口设置为minicom不变的状态? I'm lost. 我迷路了。

Thanks for the help! 谢谢您的帮助!

The initialization of the c_cflags looks wrong, by using bitwise negation you basically set all the bits beside CRTSCTS : c_cflags的初始化看起来是错误的,通过使用按位取反,您基本上设置了CRTSCTS旁边的所有位:

options.c_cflag = ... | ~CRTSCTS | ...;

You should just leave out CRTSCTS if that flag should not be set. 如果不应该设置CRTSCTS则应该CRTSCTS该标志。

Digi has released xbee_ansic_library , an Open Source (MPL 2.0) library of ANSI C code for communicating with XBee modules in API mode. Digi已发布xbee_ansic_library ,这是一个ANSI C代码的开源(MPL 2.0)库,用于以API模式与XBee模块进行通信。 It supports POSIX (Linux, BSD, Mac OS X, Cygwin), Windows (MinGW/MSYS), DOS (OpenWatcom) and some embedded platforms. 它支持POSIX(Linux,BSD,Mac OS X,Cygwin),Windows(MinGW / MSYS),DOS(OpenWatcom)和某些嵌入式平台。

It might be of interest if you're still writing C code in Ubuntu. 如果您仍然在Ubuntu中编写C代码,可能会很感兴趣。

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

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