串口:无法写入大块数据

发布于 2024-08-24 04:02:18 字数 285 浏览 5 评论 0原文

我正在尝试使用串行电缆将文本数据从一台电脑发送到另一台电脑。其中一台 PC 运行 Linux,我使用 write(2) 系统调用从它发送数据。日志大小约为 65K 字节,但 write(2) 系统调用返回约 4K 字节(即正在传输这么多数据量)。我尝试将数据分成 4K 块,但 write(2) 返回 -1。

我的问题是“在串行端口上写入数据是否有缓冲区限制?或者我可以发送任何大小的数据吗?另外,当我写入4K数据块时,我是否需要不断从其他PC读取数据”

我需要termios 结构中是否有任何特殊配置用于发送(巨大)数据?

I am trying to send text data from one PC to other using Serial cable. One of the PC is running linux and I am sending data from it using write(2) system call. The log size is approx 65K bytes but the write(2) system call returns some 4K bytes (i.e. this much amount of data is getting transferred). I tried breaking the data in chunks of 4K but write(2) returns -1.

My question is that "Is there any buffer limit for writing data on serial port? or can I send data of any size?. Also do I need to continously read data from other PC as I write 4K chunk of data"

Do I need to do any special configuration in termios structure for sending (huge) data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

梓梦 2024-08-31 04:02:19

传输缓冲区是一页(查看了 Linux 2.6.18 源代码) - 在大多数(如果不是全部)情况下为 4K。

另一端必须读取(不知道接收缓冲区的大小),但更重要的是你的写入速度不应该比串行端口可以传输的速度快,如果你使用115200 bps 8-N-1你可以写入4K块大约每秒 3 次。 (115200 / 9 / 4096 = 3.125)

The transmit buffer is one page (took a look at Linux 2.6.18 sources) - which is 4K in most (if not all) cases.

The other end must read (don't know the size of the receive buffer), but more importantly you should not write faster than the serial port can transmit, if you are using 115200 bps 8-N-1 you can write the 4K chunk approximately 3 times a second. (115200 / 9 / 4096 = 3.125)

多情癖 2024-08-31 04:02:19

是的,有一个缓冲区限制 - 但当达到该限制时,write() 应该阻塞。

write()返回-1时,errno设置为多少?

Yes, there is a buffer limit - but when you reach that limit, the write() should block.

When write() returns -1, what is errno set to?

快乐很简单 2024-08-31 04:02:19

确保接收器正在读取。

您应该从 write() 更新缓冲区的当前位置,并从那里继续下一个写入。 (适用于所有 writes(),无论 fd 是串行端口、tcp 套接字还是文件。)

如果后续写入返回错误。从联机帮助页来看,重试以下错误的写入是安全的:EAGAIN、EINTR,可能还有 ENOSPC。使用 perror() 看看你会得到什么。 (..并发布它,我很好奇。)

EFBIG 似乎表明您正在尝试使用太大的缓冲区(或者更确切地说计数)进行写入,但这可能比 64k 大得多。

如果内部缓冲区已满,因为您写入速度太快,请尝试在写入之间(纳米)睡眠一点。有几种巧妙的方法可以做到这一点(如 tcp 所做的),但如果速率已知,则只需以固定速率写入即可。

如果您认为接收器实际上正在读取,但没有发生太多情况,请查看串行端口流量控制选项以及电缆是否连接用于 DTS/RTS。

Make sure that the receiver is reading.

You should update the current position it your buffer from the write(), and continue the next write from there. (Applies to all writes(), regardless if the fd is a serial port, tcp socket or a file.)

If you get an error back for subsequent writes. Judging by the manpage, its safe to retry the writes for the following errnos: EAGAIN, EINTR, and probably ENOSPC. Use perror() to see what you get. (..and post it, I am curious.)

EFBIG would seem to indicate that you are trying to write using a buffer (or rather count) that is too large, but that is probably much larger than 64k.

If the internal buffer is filled up, because you are writing to fast, try to (nano)sleep a little between the writes. There are several clever ways of doing this (like tcp does), but if the rate is known, just write at a fixed rate.

If you think the receiver is actually reading, but not much happens, have a look at the serial ports flow-control options and if the cable is wired for DTS/RTS.

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