简体   繁体   English

如何获取串行 COM 端口的状态

[英]How to get the status of a serial COM port

What I mean is that, when I code a project, I need to communicate with the serial port like COM1 , COM2 ... but sometimes there is no device connected and I also can use the function CreateFile to get the COM port handle.我的意思是,当我编码一个项目时,我需要与COM1COM2串口通信......但有时没有连接设备,我也可以使用函数CreateFile来获取COM端口句柄。

When I use the WriteFile function to send a string to the COM port the software blocks.当我使用WriteFile函数将字符串发送到COM端口时,软件会阻止。

After I dig into the problem I find another function GetCommModemStatus which can get status of the COM port but when I use the usb-rs232 transition line, the second parameter always returns 0 .深入研究问题后,我找到了另一个函数GetCommModemStatus ,它可以获取COM端口的状态,但是当我使用usb-rs232转换线时,第二个参数始终返回0

How can I get the COM port status so that I can check if is there some devices connected to the computer?如何获取COM端口状态,以便检查是否有某些设备连接到计算机?

If I understand correctly, you want to detect if a device is connected to your COM port and ready to accept packets.如果我理解正确,您想检测设备是否已连接到您的 COM 端口并准备好接受数据包。 If that's the case, you need to check control signals (DTR/DSR and CTS/RTS) before sending data, assuming your device is aware of them and sets the appropriate PIN on your DB-9 or DB-25 connector.如果是这种情况,您需要在发送数据之前检查控制信号(DTR/DSR 和 CTS/RTS),假设您的设备知道它们并在 DB-9 或 DB-25 连接器上设置适当的 PIN。 Some devices rely on software handshaking instead (XON/XOFF) and do not set control signals.某些设备改为依赖软件握手(XON/XOFF)并且不设置控制信号。 Your best bet would be to consult documentation of your device.最好的办法是查阅设备的文档。

I have been using ComPort Library version 4.10 by Dejan Crnila.我一直在使用 Dejan Crnila 的 ComPort 库 4.10 版。 It does support both hardware and software handshakings, so you can focus on your own code instead of reinventing the wheel .它确实支持硬件和软件握手,因此您可以专注于自己的代码而不是重新发明轮子

As several people have already pointed out, it is not a good idea to try to "re-invent the wheel."正如一些人已经指出的那样,尝试“重新发明轮子”并不是一个好主意。 Except for "quick and dirty" testing, your code will have to handle the com port in a separate thread and the available solutions all make this much easier.除了“快速而肮脏”的测试外,您的代码必须在单独的线程中处理 com 端口,而可用的解决方案都使这变得更加容易。

BUT, if you Really want to do it, I'll give you some pointers.但是,如果你真的想这样做,我会给你一些建议。

If you are using "WriteFile" then you have probably already figured out the "CreateFile" part of the procedure and how complicated things can get depends upon what kind of IO you specified in that procedure, Overlapped or not.如果您正在使用“WriteFile”,那么您可能已经弄清楚了过程的“CreateFile”部分,事情的复杂程度取决于您在该过程中指定的 IO 类型,是否重叠。 Overlapped IO is more complicated but does let the OS handle some of the burden.重叠 IO 更复杂,但确实让操作系统处理了一些负担。

You mentioned that your call to "WriteFile" hangs.您提到您对“WriteFile”的调用挂起。 You should look up the "SetCommTimeOuts" function.您应该查找“SetCommTimeOuts”函数。 Setting both the WriteTotalTimeoutMultiplier and WriteTotalTimeoutConstant members of TCommTimeouts to zero will tell the OS to return immediately.将 TCommTimeouts 的 WriteTotalTimeoutMultiplier 和 WriteTotalTimeoutConstant 成员都设置为零将告诉操作系统立即返回。 You may also need to "SetCommMask" if your target uses handshaking.如果您的目标使用握手,您可能还需要“SetCommMask”。

What happens next really depends on what your target is supposed to do.接下来会发生什么实际上取决于您的目标应该做什么。 The GetCommMask function can be used to check the status of the handshake lines. GetCommMask 函数可用于检查握手线的状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM