无法从Windows 7中的串行COM端口连续读取数据

时间:2016-06-30 16:47:34

标签: c winapi serial-port serial-communication

这是我的代码,基本上是打开串口并从串口com 5读取数据。

`/ ----------------------------------打开串口---- --------------------------------------- /

        hComm = CreateFile( ComPortName,                  // Name of the Port to be Opened
                            GENERIC_READ | GENERIC_WRITE, // Read/Write Access
                            0,                            // No Sharing, ports cant be shared
                            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("\n    Error! - Port %s can't be opened. Please check port is free or not.\n", ComPortName);
        else
            printf("\n    Port %s is Opened\n ", ComPortName);

/ -------------------------------设置SerialPort的参数------ ------------------------ /

        DCB dcbSerialParams = { 0 };                         // Initializing DCB structure
        dcbSerialParams.DCBlength = sizeof(dcbSerialParams);

        Status = GetCommState(hComm, &dcbSerialParams);      //retreives  the current settings

        if (Status == FALSE)
            printf("\n    Error! in GetCommState()");

        dcbSerialParams.BaudRate = CBR_9600;      // Setting BaudRate = 9600
        dcbSerialParams.ByteSize = 8;             // Setting ByteSize = 8
        dcbSerialParams.StopBits = ONESTOPBIT;    // Setting StopBits = 1
        dcbSerialParams.Parity = NOPARITY;        // Setting Parity = None

        Status = SetCommState(hComm, &dcbSerialParams);  //Configuring the port according to settings in DCB

        if (Status == FALSE)
            {
                printf("\n    Error! in Setting DCB Structure");
            }
        else //If Successfull display the contents of the DCB Structure
            {
                printf("\n\n    Setting DCB Structure Successfull\n");
                printf("\n       Baudrate = %d", dcbSerialParams.BaudRate);
                printf("\n       ByteSize = %d", dcbSerialParams.ByteSize);
                printf("\n       StopBits = %d", dcbSerialParams.StopBits);
                printf("\n       Parity   = %d\n", dcbSerialParams.Parity);
            }

/ ------------------------------------设置接收掩码---- ------------------------------------------ /

        Status = SetCommMask(hComm, EV_RXCHAR); //Configure Windows to Monitor the serial device for Character Reception

        if (Status == FALSE)
            printf("\n\n    Error! in Setting CommMask");
        else
            printf("\n\n    Setting CommMask successfull");

/ ------------------------------------设置WaitComm()事件 - -------------------------------------- /

        printf("\n\n    Waiting for Data Reception");

        Status = WaitCommEvent(hComm, &dwEventMask, NULL); //Wait for the character to be received

/ --------------------------程序将在此等待,直到收到一个角色-------- ---------------- /

        if (Status == FALSE)
            {
                printf("\n    Error! in Setting WaitCommEvent()");
            }
        else //If  WaitCommEvent()==True Read the RXed data using ReadFile();
            {
                printf("\n\n    Characters Received");
                    do
                    {
                        Status = ReadFile(hComm, &TempChar, sizeof(TempChar), &NoBytesRead, NULL);
                        SerialBuffer[i] = TempChar;
                        i++;
                    }

                    while (NoBytesRead > 0);


                /*------------Printing the recieved value to Console----------------------*/

                printf("\n\n    ");
                int j =0;
                for (j = 0; j < i-1; j++)       // j < i-1 to ( '0' ), null or  last character
                    printf("%c", SerialBuffer[j]);
            }
            getchar();
            CloseHandle(hComm);//Closing the Serial Port
            return 0;

    }//End of Main()

` 因为设置接收掩码和设置WaitComm()事件。

Status = SetCommMask(hComm, EV_RXCHAR); Status = WaitCommEvent(hComm, &dwEventMask, NULL);

我只接收一次的值。如图。image showing results from device, which is sending data on com 5.

所示

请给我任何建议如何连续从串口接收数据。我没有得到这个问题来自这些函数或缓冲区大小。任何建议将不胜感激。提前谢谢。

0 个答案:

没有答案