从串行打印中读取垃圾
我正在尝试配置 DSP 和我的计算机之间的串行通信。
DSP 发送 16 位计数器的值,每次发送该值都会递增。 所以它只是在计数......
这是我在计算机上得到的:
3335 3336 3337 3338 36388 46920 16372 46919 3339 3340 3341 3342 36388 46920 16372 46919 3343 3344 4621 3341 36388 46920 ...
所以我们可以看到计数器被这4个不知从何而来的值中断...
我的程序配置为
fd = open(device.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
fcntl(fd, F_SETFL, FNDELAY);
termios options;
tcgetattr(fd, &options);
cfsetispeed(&options, B4800);
cfsetospeed(&options, B4800);
options.c_cflag |= (CLOCAL | CREAD | CS8);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_iflag |= (IXON | IXOFF | IXANY);
tcsetattr(fd, TCSANOW, &options);
DSP发送4800/8N1。
有什么想法吗?
I'm trying to configure a serial communication between a dsp and my computer.
The dsp sends the value of a 16-bit counter which increments each time it is sent.
So it's just counting...
Here is what I get on my computer :
3335
3336
3337
3338
36388
46920
16372
46919
3339
3340
3341
3342
36388
46920
16372
46919
3343
3344
4621
3341
36388
46920
...
So we can see the counter which is interrupted by those 4 values that comes out of nowhere...
My program is configured with
fd = open(device.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
fcntl(fd, F_SETFL, FNDELAY);
termios options;
tcgetattr(fd, &options);
cfsetispeed(&options, B4800);
cfsetospeed(&options, B4800);
options.c_cflag |= (CLOCAL | CREAD | CS8);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_iflag |= (IXON | IXOFF | IXANY);
tcsetattr(fd, TCSANOW, &options);
The DSP sends 4800/8N1.
Any idea ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我强烈怀疑这些是由 DSP 发送的,串行端口驱动程序现在已经调试得很好。
其他可能性是您同时从多个线程读取串行端口(也许在同一个进程中,也许不是)。仅打开端口以确保这不是您的问题。
或者,您可以从串行端口读取预期值,并在程序内的某些缓冲/队列中添加垃圾。您可能会收到部分读取(由于接收超时)。
请记住,
read
的返回值以字节为单位,而不是16 位值。如果您接收 8 个字节,然后打印 8 个 Shorts,您会看到这种行为。(但是,“序列”中最后两个值 4621 3341 的问题不能用这种方式解释.) 根据 OP 的评论,这似乎是问题的确切原因在任何情况下,此问题都不能归因于串行端口配置。
I strongly suspect those are being sent by the DSP, the serial port drivers are pretty well debugged by now.
Other possibilities are that you're reading the serial port from multiple threads at once (maybe in the same process, maybe not). Open the port exclusively to make sure that isn't your problem.
Or, you could be reading the expected values from the serial port, and adding garbage in some buffering/queuing inside your program. Possibly you're getting partial reads (due to receive timeout).
Remember that the return value of
read
is measured in bytes, not 16-bit values. If you were receiving 8 bytes and then printing 8 shorts, you would see this sort of behavior. (However, the problems with the last two values, 4621 3341, in the "sequence" can't be explained this way.) Based on comments from the OP, this appears to be the exact cause of the problemIn no case is this problem attributable to serial port configuration.
哈哈,我很抱歉,问题的原因在其他地方,我
当时
正在做,所以我总是在读取 4 个项目 + 4 个值的缓冲区(我猜是来自堆栈)...
不过还是感谢您的回答...
Lol I'm so sorry, the cause of the problem was elsewhere, I was doing
Then
So I was always reading my buffer of 4 items + 4 values (from the stack I guess)...
Thanks for your answers though...