简体   繁体   English

从Windows中的串行端口读取

[英]Read from serial port in Windows

I'm trying to read serial data from rs232. 我正在尝试从rs232读取串行数据。 My rs232 is connected in comport COM11. 我的rs232已连接到COMCOM11端口。 I specified COM11 in my serial.h file. 我在serial.h文件中指定了COM11。 below is the code. 下面是代码。

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

My main c code is 我的主要C代码是

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

After run the c code, im getting... 运行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 . . .

I think my program cant able to find the comport. 我认为我的程序无法找到该端口。 How to resolve this? 如何解决呢? Thanks 谢谢

To get access to serial ports with port number larger then 9 you should use 要访问端口号大于9的串行端口,您应该使用

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

More info here . 更多信息在这里

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

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