在 Windows 中从串口读取
Read from serial port in Windows
我正在尝试从 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 . . .
我想我的程序找不到comport。如何解决这个问题?谢谢
要访问端口号大于 9 的串行端口,您应该使用
CreateFile("\\.\COM11",GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
更多信息here。
我正在尝试从 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 . . .
我想我的程序找不到comport。如何解决这个问题?谢谢
要访问端口号大于 9 的串行端口,您应该使用
CreateFile("\\.\COM11",GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
更多信息here。