且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

双向C ++在串行连接进行通信

更新时间:2023-02-06 20:51:11

有三点:

第一:您不会初始化在Linux中的串行端口(TTY)。没有人知道它是什么状态。

First: You don't initialize the serial port (TTY) on the Linux side. Nobody knows in what state it is.

在你的程序这样做,你必须使用 tcgetattr(3) tcsetattr(3)。您可以通过使用这些关键字在这个网站,Arduino的网站或谷歌上找到所需要的参数。但是的只是为了快速测试的我建议你打电话给自己的命令之前,发出以下命令:

Doing this in your program you must use tcgetattr(3) and tcsetattr(3). You can find the required parameters by using these keywords at this site, the Arduino site or on Google. But just for quick testing I propose to issue this command before you call your own command:

stty -F /dev/tty.usbmodem641 sane raw pass8 -echo -hupcl clocal 9600

特别是缺少 CLOCAL 可能prevent您打开TTY。

Especially the the missing clocal might prevent you opening the TTY.

二::当该设备是开放的,你应该等待发送任何东西之前一点点。默认串行线路被打开或关闭时阿尔杜伊诺重置。你必须考虑到这一点。

Second: When the device is open, you should wait a little before sending anything. By default the Arduino resets when the serial line is opened or closed. You have to take this into account.

-hupcl 部分将prevent这个重置的大部分时间。但至少有一个复位总是必要的,因为 -hupcl 只能在TTY已打开设定和此时的Arduino的已接收已经复位信号。因此, -hupcl 将只有prevent未来复位。

The -hupcl part will prevent this reset most of the time. But at least one reset is always necessary, because -hupcl can be set only when the TTY is already open and at that time the Arduino has received the reset signal already. So -hupcl will "only" prevent future resets.

第三: NO 的错误在code处理。请加code上检测错误和TTY每个IO操作之后 - 最重要的部分 - 利用输出有用的错误信息 PERROR(3)或类似的功能。

Third: There is NO error handling in your code. Please add code after each IO operation on the TTY which checks for errors and - the most important part - prints helpful error messages using perror(3) or similar functions.