winapi - C++: How to ignore ReadFile() if there is no new data from the serial port? -


i working on c++ program can read serial port , write serial port. have problem @ reading data. if there no new data, readfile() waiting until receive new data.

my code read data:

while (!_kbhit())     {         if (!_kbhit())         {             if (readfile(hserial, &c, 1, &dwbytesread, null))             {                 cout << c;             }         }     } 

how can check if there no new data , skip readfile() line?

edit:

i able fix it. changed readfunction this:

do {     if (readfile(hserial, &c, 1, &dwbytesread, null))     {         if (isascii(c))         {             cout << c;         }     }     if (_kbhit())     {         key = _getch();     } } while (key != 27); 

and added timeouts this:

serialhandle = createfile(lcomport, generic_read | generic_write, 0, 0, open_existing, file_attribute_normal, 0); commtimeouts timeouts;        timeouts.readintervaltimeout = 1;     timeouts.readtotaltimeoutmultiplier = 1;     timeouts.readtotaltimeoutconstant = 1;     timeouts.writetotaltimeoutmultiplier = 1;     timeouts.writetotaltimeoutconstant = 1;     setcommtimeouts(serialhandle, &timeouts);  // call function read ... 

 if there no new data, readfile() is waiting until receive new data.

you can use setcommtimeouts() configure reading timeout readfile() exit if no data arrives within timeout interval.


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -