Hello,
I am doing embedded system programming. i want to read data from device through the COM1 port.
I have already a software that for access the device through the COM1 port and generate data for the device. I wrote a program in c++ that could open the port and waiting for data to read. But problem is that, when i run two programs, it will conflict to open port. i could able to solve the problem. But when i read data from the port, my program could not read because of "hPort" is INVALID. Another program opens the COM1 port so that it is INVALID. My program just skip to open the port. please help me, how can i read data from COM port. See my code bellow (for read data).
// Specify a set of events to be monitored for the port.
SetCommMask (hPort, EV_RXCHAR | EV_CTS | EV_DSR | EV_RLSD | EV_RING);
while (hPort != INVALID_HANDLE_VALUE)
{
cout<<"Successfully open COM1 port!!!" <<endl <<"Waiting for Events...";
// Wait for an event to occur for the port.
WaitCommEvent (hPort, &dwCommModemStatus, 0);
// Re-specify the set of events to be monitored for the port.
SetCommMask (hPort, EV_RXCHAR | EV_CTS | EV_DSR | EV_RING);
if (dwCommModemStatus & EV_RXCHAR)
{
// Loop for waiting for the data.
do
{
// Read the data from the serial port.
ReadFile (hPort, &Byte, 1, &dwBytesTransferred, 0);
// Display the data read.
if (dwBytesTransferred == 1)
ProcessChar (Byte);
} while (dwBytesTransferred == 1);
}
}
Thanks in advance.