c++ - Serial port not reading complete transmission -
i attempted modify teunis van beelen's rs232 library, polling event driven , non-overlapped suit project. rs232 library
i expect receive blocks of data (roughly 100 200 chars) every 200ms.
problem having received data inconsistent, cut off @ random points, , incomplete.
i readfile() return after reading 1 whole block of data.( or effect)
i feel problem time out settings, because altering figures different results, cant right, best result far has been set time out values 0 , let readfile() expect 150 bytes, way readfile() dose not return unless reads 150 chars, go out of sync after few transmissions, have no idea how data expect.
these main changes polling function in teunis's code , besides time out settings, other settings unchanged:
//using ev_rxchar flag notify thread byte arrived @ port dword dwerror = 0; //use setcommmask , waitcommevent see if byte has arrived @ port //setcommmask sets desired events cause notification. if(!setcommmask(cport[comport_number],ev_rxchar)){ printf("setcommmask error"); dwerror = getlasterror(); // error setting com mask return false; } //waitcommevent function detects occurrence of events. dword dwcommevent; for( ; ; ) { //wait event happen if (waitcommevent(cport[comport_number],&dwcommevent,null)) { if(readfile(cport[comport_number], buf, 1, (lpdword)((void *)&n), null)){ //byte has been read, buf processed in main } else{ //error occoured in readfile call dwerror = getlasterror(); break; } else{ //error in waitcommevent break; } break; //break after read file }
attempt 2 suggested msdn article on serial com using while cycle through every character in buffer, method did not yield results either.
dword dwerror = 0; /* using ev_rxchar flag notify thread byte arrived @ port */ //use setcommmask , waitcommevent see if byte has arrived @ port //setcommmask sets desired events cause notification. if(!setcommmask(cport[comport_number],ev_rxchar)){ printf("setcommmask error"); dwerror = getlasterror(); // error setting com mask return false; } //waitcommevent function detects occurrence of events. dword dwcommevent; for( ; ; ) { //wait event happen if (waitcommevent(cport[comport_number],&dwcommevent,null)) { //do while loop cycle readfile until bytes-read reach 0, do{ if(readfile(cport[comport_number], buf, size, (lpdword)((void *)&n), null)){ //byte has been read, buf processed in main } else{ //error occoured in readfile call dwerror = getlasterror(); break; } }while(n); } else{ //error in waitcommevent break; } break; //break after read file }
i wondering if rewriting code in overlapped mode improve things, dont see advantages have no need multi threading. suggestions great!
thank you.
readfile has no way detect "block of data" is. should not expect understand data or timing of data. fix issue process whatever gives you, using own knowledge of data divide "blocks" further processing. if partial block keep it, , append next read.
there no need call waitcommevent data. readfile wait data. give suitably sized buffer , ask lot more 1 byte @ time. it's extremely inefficient call 1 byte. select requested count , timeouts readfile return within acceptable time, whether there data or not.
Comments
Post a Comment