Windows下的串口编程

发布于 2024-10-26 02:37:45 字数 112 浏览 2 评论 0原文

我正在帮助一位朋友完成他的电气工程项目。他正在构建一种使用串行端口与某些软件进行通信的设备。在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 技术交流群。

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

发布评论

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

评论(2

寄意 2024-11-02 02:37:45

是的,本质上您打开一个具有特殊名称的串行端口设备,例如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.

挽梦忆笙歌 2024-11-02 02:37:45

您可以使用WaitCommEvent函数来监视特定引脚。假设电压变化触发CTS信号,可以是这样的

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

来自 MSDN 的 WaitCommEvent

You can use WaitCommEvent function to monitor a specific pin. Suppose the voltage change triggers CTS signal, it can be like this

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

WaitCommEvent from MSDN

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