【问题标题】:Read from serial port in Windows从 Windows 中的串口读取
【发布时间】:2015-04-14 06:11:07
【问题描述】:

我正在尝试从 rs232 读取串行数据。我的 rs232 连接在 COM11 端口。我在我的 serial.h 文件中指定了 COM11。下面是代码。

void port_open()
{
hSerial = 
CreateFile("COM11",GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
..

我的主要c代码是

#include <stdio.h>
#include "serial.h"
void main()
{
while(1)
    {
    port_open();
    printf("%s\n",port_read());
    }
}

运行c代码后,我得到...

serial port does not exist
some other error occured. Inform user.
error getting state
error setting serial port state
error setting port state
error reading from input buffer


--------------------------------
Process exited after 0.4768 seconds with return value 0
Press any key to continue . . .

我认为我的程序无法找到该 comport。如何解决这个问题?谢谢

【问题讨论】:

  • 请提供完整的函数代码port_openport_read
  • 在无限循环中调用port_open() 是没有意义的。如果您的其余代码正常工作,您很快就会用完文件句柄。

标签: c windows serial-port


【解决方案1】:

要访问端口号大于 9 的串行端口,您应该使用

CreateFile("\\\\.\\COM11",GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

更多信息here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    • 2011-12-24
    • 2011-08-27
    • 1970-01-01
    相关资源
    最近更新 更多