gpt4 book ai didi

c# - 检测 SerialPort 何时准备好执行下一个命令

转载 作者:太空宇宙 更新时间:2023-11-03 16:30:38 25 4
gpt4 key购买 nike

我有一个硬件,我要通过 RS232 使用 .NET SerialPort 类连接到该硬件。根据其文档,它不支持任何类型的流量控制。

我可以向设备写入查询和命令,对于查询,它会立即响应读取,如果设备的回显打开并且我必须在写入命令后进行读取,那么命令写入工作正常也。命令和响应最多 7 个字符。

但是,如果 echo 关闭并且我快速连续写入 2 个命令,则不会执行第二个命令,除非我在写入之间放入 Thread.Sleep(15),大概复制执行 ReadLine 所需的时间当回声打开时。

我不能保证 echo 一定会打开,显式打开它也不是一个真正的选项,所以我基本上需要在我发现它时处理它。

串口设置如下:

SerialPort _serialPort = new SerialPort
{
PortName = "COM1",
BaudRate = 9600,
Parity = Parity.None,
DataBits = 8,
ReadTimeout = 5000,
WriteTimeout = 5000,
NewLine = "\x0D"
};

我正在使用 WriteLine 和 ReadLine 与其通信。

休眠线程感觉就像一个黑客,我还没有找到我所有的谷歌搜索的替代方案。
那么,我是不是错过了什么,有没有什么方法可以检查何时可以执行写入串行端口的下一个命令,或者 Thread.Sleep 是我的全部吗?

最佳答案

http://msmvps.com/blogs/coad/archive/2005/03/23/SerialPort-_2800_RS_2D00_232-Serial-COM-Port_2900_-in-C_2300_-.NET.aspx

将握手属性设置为 RTSCTS 并在 CTS 变为真时处理 PinChanged 事件?

Lines marked ‘Output’ in the table can be set by the serial port to a high or low state (True or > > False) and the ‘Input’ lines can be read as high or low. You can use the PinChanged event to be > > notified when one of the input pins changes. The Handshake property can be set to use RTS/CTS > > > (uses hardware control lines) or XON/XOFF (uses software) or both.

Control Line Name DB9 Pin SerialPort Property I/ODTR Data Terminal Ready 4 DtrEnable OutputRTS Request to Send 7 RtsEnable OutputCTS Clear to Send 8 CtsHolding InputDSR Data Set Ready 6 DsrHolding InputCD (or DCD) Data Carrier Detect 1 CDHolding Input

关于c# - 检测 SerialPort 何时准备好执行下一个命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10900983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com