tags:

views:

57

answers:

2

i want to read serial port when there is some data present i mean on the event when data arrives only then i will read serial port instead of continuously reading the port i have this code for continuous reading the port how can i make it event based.

thanx in advance.

while(1)
{
   bReadRC = ReadFile(m_hCom, &byte, 6, &iBytesRead, NULL);
   printf("Data Recieved Through Serial port and no. of Bytes Recieved is    %d",iBytesRead);
 }
A: 

This looks like WinAPI, and I'm not exactly an expert at that, but you should be able to set the port to blocking mode. This will cause the read call to block (wait on that line) until there's data available.

Matti Virkkunen
+2  A: 

According to MSDN you can use the WaitCommEvent() operation on your serial port handle. Also, this article gives a nice introduction into the topic.

jopa