从Windows中的串行端口读取

时间:2015-04-14 06:11:07

标签: c windows serial-port

我正在尝试从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 . . .

我认为我的程序无法找到该程序。怎么解决这个?感谢

1 个答案:

答案 0 :(得分:1)

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

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

更多信息here