Windows下的串口编程
我正在帮助一位朋友完成他的电气工程项目。他正在构建一种使用串行端口与某些软件进行通信的设备。在Windows平台(Win7)上,如何直接读写串口上的特定引脚? Windows 是否有针对此类事情公开的 API?
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? Is there an API that Windows exposes for this sort of thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,本质上您打开一个具有特殊名称的串行端口设备,例如
COM1
,然后像读取文件一样对其进行读写。使用的引脚(自然)将是串行发送和接收引脚。如果您想控制特定引脚但不一定以串行方式,那么最好使用并行端口。并行端口电压通常对TTL电平逻辑更友好,并且通常可以直接驱动。
更新:如果您只需要根据您的评论切换一个引脚,您可以使用 DTR 线来实现此目的。请参阅
EscapeCommFunction
< /a> 函数文档了解如何执行此操作。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. 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.
Update: If you just need to toggle one pin as per your comment, you may be able to use the DTR line for this. See the
EscapeCommFunction
function documentation for how to do this.您可以使用WaitCommEvent函数来监视特定引脚。假设电压变化触发CTS信号,可以是这样的
来自 MSDN 的 WaitCommEvent
You can use WaitCommEvent function to monitor a specific pin. Suppose the voltage change triggers CTS signal, it can be like this
WaitCommEvent from MSDN