串行端口不发送 NULL 值
我正在使用 XBEE 无线电,我使用的是 Linux(Ubuntu 9.10),并且在使用我的代码时,XBEE 似乎没有通过串行端口发送 NULL 值。当我使用 XCTU 程序(单独的 Windows 盒子上的 XBEE 附带的常用术语模拟器)时,当新的 XBEE 加入网络时,我通过串行端口看到此输出:
7E 00 20 95 00 13 A2 00 40 3B
等等......完美。但是,使用我的代码,当新的 XBEE 加入网络时,我会看到以下内容:
7E 20 95 13 A2 40 3B
这是我打开串行端口的方式
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);
我有关于该代码的作用的理论,但我的理论显然是错误的。我正在尝试使用 9600、8N1、无流量控制打开端口。您可以看到我也在使用串行->USB 驱动程序,但由于我似乎确实获得了数据,所以我很确定该部分正在工作。
我的猜测是,当我 bzero 选项时,我将 0x00 设为控制字符?我不知道。当我不选择 bzero 选项时,我一次只能读取 5 个字节,并且会丢失数据。感觉就像我遇到了流量控制或波特率问题,所以我 bzero() 现在我没有得到 NULL。
我刚刚在我的 Linux 系统上使用了 Minicom 并捕获了输出。我得到相同的信息,没有 NULL(对于那些不熟悉协议的人来说,这确实弄乱了数据包大小)。我的代码是否可以将串行端口设置为 minicom 不改变的状态?我迷路了。
感谢您的帮助!
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. 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:
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:
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. 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.
My guess is when I bzero options, I'm making 0x00 a control char? I'm not sure. When I DON'T bzero options I can only read 5 bytes at a time and I lose data. It feels like I'm having a flow control or a baud rate problem, so I bzero() and now I don't get NULLs.
I've also just used Minicom on my Linux system and captured the output. I get the same information, no NULLs(this really messes up packet sizes for those unfamiliar with the protocol). Could my code have set the serial port into a state that minicom isn't changing? I'm lost.
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
c_cflags
的初始化看起来是错误的,通过使用按位否定,您基本上设置了CRTSCTS
旁边的所有位:您应该忽略
CRTSCTS
如果不应设置该标志。The initialization of the
c_cflags
looks wrong, by using bitwise negation you basically set all the bits besideCRTSCTS
:You should just leave out
CRTSCTS
if that flag should not be set.Digi 发布了 xbee_ansic_library,这是一个开源 (MPL 2.0) ANSI C 代码库,用于与 XBee 模块进行通信API模式。它支持 POSIX(Linux、BSD、Mac OS X、Cygwin)、Windows(MinGW/MSYS)、DOS(OpenWatcom)和一些嵌入式平台。
如果您仍在 Ubuntu 中编写 C 代码,那么它可能会感兴趣。
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. It supports POSIX (Linux, BSD, Mac OS X, Cygwin), Windows (MinGW/MSYS), DOS (OpenWatcom) and some embedded platforms.
It might be of interest if you're still writing C code in Ubuntu.