我正在尝试从 rs232 读取串行数据。我的rs232连接在comport 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 . . .
我认为我的程序无法找到该端口。如何解决这个问题?谢谢
最佳答案
要访问端口号大于 9 的串行端口,您应该使用
CreateFile("\\\\.\\COM11",GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
更多信息here .
关于c - 在 Windows 中从串口读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29620290/