c - 在 Windows 中从串口读取

标签 c windows serial-port

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

相关文章:

Python Noob - 愚蠢的问题?在 Python 解释器中工作,而不是在 CLI 中工作

c++ - 通过 C 函数调用引发 C++ 异常

Windows 7 的 PHP 4/5 安装

c++ - 使用 GetOverlappedResult 丢失数据?

vb.net - 跨线程操作无效

android - 在带有 Hyperv 的 Windows 8 上使用 Android x86 HAXM 加速模拟器的更好方法

java - 通过套接字从 Java 程序向 C 程序发送数据

c - 未初始化的 _Bool 变量或结构字段行为异常

c - 使用C返回数组

c# - 使用回调检查鼠标指针状态