简体   繁体   English

Windows中的串口编程

[英]Serial port programming in Windows

I'm helping out a friend with his Electrical Engineering project. 我正在帮助他的电气工程项目的朋友。 He is building a device that will use a serial port to communicate to some software. 他正在构建一种设备,该设备将使用串行端口与某些软件进行通信。 On a windows platform (Win7), how would one read and write directly to a specific pin on the serial port? 在Windows平台(Win7)上,如何直接读写串行端口上的特定引脚? Is there an API that Windows exposes for this sort of thing? Windows是否提供针对此类情况的API?

Yes, essentially you open a serial port device with a special name, such as COM1 , and read and write to it much as you would a file. 是的,从本质上讲,您使用特殊名称(例如COM1打开串行端口设备,并对其进行读写操作就像处理文件一样。 The pins used will (naturally) be the serial transmit and receive pins. 使用的引脚(自然)将是串行发送和接收引脚。

If you want to control specific pins but not necessarily in a serial way, you might be better off working with a parallel port instead. 如果要控制特定的引脚,但不一定要以串行方式控制,则最好使用并行端口。 The parallel port voltages are usually more friendly to TTL level logic and can often be driven directly. 并行端口电压通常对TTL电平逻辑更友好,并且通常可以直接驱动。

Update : If you just need to toggle one pin as per your comment, you may be able to use the DTR line for this. 更新 :如果您只需要根据注释切换一个引脚,则可以使用DTR线。 See the EscapeCommFunction function documentation for how to do this. 有关如何执行此操作,请参阅EscapeCommFunction函数文档。

You can use WaitCommEvent function to monitor a specific pin. 您可以使用WaitCommEvent函数来监视特定的引脚。 Suppose the voltage change triggers CTS signal, it can be like this 假设电压变化触发CTS信号,可以这样

hCommn = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
......
 WaitCommEvent(hCommn, EV_CTS, NULL);
......

WaitCommEvent from MSDN 来自MSDN的WaitCommEvent

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

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