无法访问c中的串口

时间:2017-01-06 23:24:48

标签: c windows serial-port

试图掌握C并访问串口,这是相当新的,所以使用了我在教程中找到的代码:

   #include<windows.h>
   #include<stdio.h>
   int main()
   {
    HANDLE hComm;

    hComm = CreateFile("COM3",                //port name
        GENERIC_READ | GENERIC_WRITE, //Read/Write
        0,                            // No Sharing
        NULL,                         // No Security
        OPEN_EXISTING,// Open existing port only
        0,            // Non Overlapped I/O
        NULL);        // Null for Comm Devices

    if (hComm == INVALID_HANDLE_VALUE) {
        printf("Error in opening serial port");
    }
    else {
        printf("Opening serial port successful");
        printf("It works!!!");
    }


    CloseHandle(hComm);//Closing the Serial Port

    return 0;
}

然而,hComm不断返回INVALID_HANDLE_VALUE。我检查了我的串口是COM3,并尝试使用\\\\.\\COM3COM:3,但两者都没有区别。我的驱动程序已更新,我的串口正在运行。我错过了一些明显的东西吗?

0 个答案:

没有答案