Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int read_com_buffer(const int toReadBytes, char *serInBuf, const int stream_status){
- unsigned int idx=0; //index of the serInBuf
- char b1,b2; //receive 2 bytes in one swipe of this procedure
- fd_set rfds; // File pointer set for select
- struct timeval serInWait; // Time to wait for serial input
- int stream=stream_status; //receiving 1st (START_STREAM), or following bytes (CONT_STREAM)
- int selRetVal; // Return value of select
- //inits
- serInWait.tv_sec = SERIAL_TIMEOUT_SEC;
- serInWait.tv_usec = SERIAL_TIMEOUT_USEC;
- FD_ZERO(&rfds);
- FD_SET(fd, &rfds);
- DEBUG_SERIAL(printf("SERIAL: will read bytes %d\n",toReadBytes));
- //main fn
- for(idx=0;idx<toReadBytes;idx++) {
- if (stream==CONT_STREAM){//add (or not) a timeout on the select()
- selRetVal = select(fd+1, &rfds, NULL, NULL, &serInWait);
- } else if (stream==START_STREAM) { //waiting for 1st byte
- selRetVal = select(fd+1, &rfds, NULL, NULL, NULL);
- pthread_mutex_lock(&serial_lock_mutex);
- stream=CONT_STREAM;
- }
- if (selRetVal==0){
- return SERIAL_TIMEOUT;
- } else if (selRetVal>0){
- read(fd,&b1,1);
- if((b1 == '#') || (b1 == '\n')) { // Skip comment lines
- while( b1 != '\n')
- read(fd,&b1,1);
- } else if (b1==' '){ //Skip spaces
- } else if (b1 == EOF) {
- return ERROR; // Nothing else to read
- } else { //go on to read 2nd byte
- selRetVal = select(fd+1, &rfds, NULL, NULL, &serInWait);
- if (selRetVal==0){
- return SERIAL_TIMEOUT;
- } else if (selRetVal>0){
- read(fd,&b2,1);
- //convert asciiHex to Hex
- if (asciiToHex(&b1)==ERROR) return ERROR;
- if (asciiToHex(&b2)==ERROR) return ERROR;
- //merge bytes b1 & b2
- b1 = b1 << 4;
- b1 = b1 | b2;
- serInBuf[idx] = b1;
- }
- }
- }
- }//end for [idx] loop
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement