串口决定论

发布于 2024-08-25 15:11:20 字数 314 浏览 6 评论 0原文

这看似简单的问题,却很难寻找。我需要通过串行端口与设备连接。如果我的程序(或其他程序)未完成向设备写入命令,如何确保程序的下一次运行可以成功发送命令?

示例:

  1. foo 程序运行并开始写入“A_VERY_LONG_COMMAND”
  2. 用户终止程序,但程序仅写入“A_VERY”
  3. 用户再次运行程序,并重新发送命令。除此之外,设备看到“A_VERYA_VERY_LONG_COMMAND”,这不是我们想要的。

有什么办法可以让这个更加确定吗?由于这样的问题,串行端口编程感觉非常失控。

This seems like a simple question, but it is difficult to search for. I need to interface with a device over the serial port. In the event my program (or another) does not finish writing a command to the device, how do I ensure the next run of the program can successfully send a command?

Example:

  1. The foo program runs and begins writing "A_VERY_LONG_COMMAND"
  2. The user terminates the program, but the program has only written, "A_VERY"
  3. The user runs the program again, and the command is resent. Except, the device sees "A_VERYA_VERY_LONG_COMMAND," which isn't what we want.

Is there any way to make this more deterministic? Serial port programming feels very out-of-control due to issues like this.

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

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

发布评论

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

评论(3

忆沫 2024-09-01 15:11:21

我猜想,如果您调用 write("A_VERY_LONG_COMMAND"),然后用户在字节在线传出时按 Ctrl+C,则驱动程序层应该完成发送完整缓冲区。如果用户在调用过程中中断,驱动程序层可能会忽略整个事情。

以防万一,当您打开新的 COM 端口时,清除该端口总是明智的。

你对设备端有控制权吗?实现超时以使设备忽略未完成或以其他方式损坏的数据包可能是有意义的。

I would guess that if you call write("A_VERY_LONG_COMMAND"), and then the user hits Ctrl+C while the bytes are going out on the line, the driver layer should finish sending the full buffer. And if the user interrupts in the middle of the call, the driver layer will probably just ignore the whole thing.

Just in case, when you open a new COM port, it's always wise to clear the port.

Do you have control over the device end? It might make sense to implement a timeout to make the device ignore unfinished or otherwise corrupt packets.

离不开的别离 2024-09-01 15:11:21

应该实现嵌入式设备,以便您可以发送中止/清除/中断字符,该字符将转储其命令缓冲区的内容,并在客户端应用程序启动时为您提供一个干净的状态。

否则它应该提供一个软件重置字符,它将重置命令缓冲区和所有状态。

否则,它的设计方式是您可以发送命令终止(可能是换行符等,具体取决于命令协议),并且可能在解析其缓冲区中的部分乱码命令时生成错误,查询/清除错误,然后就可以开始了。

在连接客户端程序时,重复发送一些运行状况/状态/错误查询,直到获得良好的响应,然后才开始发送配置或操作命令,这不是一个坏主意。除非您可以通过查询确定设备处于合适的状态,否则您可能不想采取任何假设并在配置重置(如果可用)后从头开始配置它。

The embedded device should be implemented such that you can either send an abort/clear/break character that will dump the contents of its command buffer and give you a clean slate on your client app startup.

Or else it should provide a software reset character which will reset the command buffer and all state.

Or else it so be designed so that you can send a command termination (perhaps a newline, etc, depending on command protocol) and possibly have an error generated on the parsing of a garbled partial command that was in its buffer, query/clear the error, and then be good to go.

It wouldn't be a bad idea upon connection of your client program to send some health/status/error query repeatedly until you get a sound response, and only then commence sending configuration or operation commands. Unless you can via a query determine that the device was left in a suitable state, you probably want to assume nothing and configure it from scratch, after a configuration reset if available.

人心善变 2024-09-01 15:11:20

所需的方法取决于设备。

  • 串口除了串行数据线外,还有额外的控制信号线;也许其中之一会重置设备的输入。我从未做过串行端口编程,但我认为 ioctl() 可以处理这个问题。
  • 可能有一个字节将被重置,例如某个控制字符。
  • 可能存在基于定时的信号,例如Hayes命令集调制解调器使用“暂停+++暂停”。
  • 在固定时间后未收到完整命令后,它可能会重置。

了解设备最初是否旨在支持交互式使用(串行终端)、程序控制或两者兼而有之,可能会很有用。

The required method depends on the device.

  • Serial ports have additional control signal lines as well as the serial data line; perhaps one of them will reset the device's input. I've never done serial port programming but I think ioctl() handles this.
  • There may be a single byte which will reset, e.g. some control character.
  • There might be a timing-based signal, e.g. Hayes command set modems use “pause +++ pause”.
  • It might just reset after not receiving a complete command after a fixed time.

It might be useful to know whether the device was originally intended to support interactive use (serial terminal), control by a program, or both.

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