从串行打印中读取垃圾

发布于 2024-11-08 06:24:12 字数 810 浏览 1 评论 0原文

我正在尝试配置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十年不长 2024-11-15 06:24:13

我强烈怀疑这些是由 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 problem

In no case is this problem attributable to serial port configuration.

自由范儿 2024-11-15 06:24:13

哈哈,我很抱歉,问题的原因在其他地方,我

unsigned short buffer[4];
n = read(..., buffer, ...); // n in bytes !

当时

for (int i = 0; i < n; ++i) 
    dump << buffer[i] << std::endl;  // index in 16-bits !

正在做,所以我总是在读取 4 个项目 + 4 个值的缓冲区(我猜是来自堆栈)...

不过还是感谢您的回答...

Lol I'm so sorry, the cause of the problem was elsewhere, I was doing

unsigned short buffer[4];
n = read(..., buffer, ...); // n in bytes !

Then

for (int i = 0; i < n; ++i) 
    dump << buffer[i] << std::endl;  // index in 16-bits !

So I was always reading my buffer of 4 items + 4 values (from the stack I guess)...

Thanks for your answers though...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文