How to do non-blocking reads from serial port

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to do non-blocking reads from serial port

3,947 Views
getuptime
Contributor I

Our application is structured to send a packet of data and wait for a response. Using blocking reads won't work because if data isn't returned for some reason then the task effectively dead-ends at the read command. How can the port be configured such that reads are non-blocking? Is there some source of MQX serial port usage beyond what is in the IO User's Guide?

0 Kudos
Reply
2 Replies

1,246 Views
eGuy
Contributor IV

use fstatus() to check serial port status.

 

refer to demo.c at Freescale MQX 3.2\demo\telnet_to_serial\demo.c for example.

 

the code snip is below:

 

      if (fstatus( serial_fd )) {
         c = fgetc( serial_fd );
         if (c==IO_ERROR)
            break;
         putchar((char)c);
      }
 

Hope this helps

0 Kudos
Reply

1,246 Views
CarlFST60L
Senior Contributor II

We do this to, what you should do is use itty (interrupt based serial).

 

What I do is run an ESP 'expect send pairs' type arrangment. This way you can have a dedicated RX_TASK which pass's messages back via a message queue. I have developed a full set of routines which we can quickly put into anything that requires two way comm's which works well.

 

The core of what you are asking happens here:

 

inside ESP_Function()...

 //Get start time so we know where we are _time_get(&StartTime);  //DEBUG, Display time. printf("Start Seconds %u MilliSeconds %u \n", StartTime.SECONDS, StartTime.MILLISECONDS);  //Run this until we get a message from the TX routines while(_msgq_peek(ps_User_Comm_ESP->QID_of_TX_Task) == NULL) {  _time_get(&CurrentTime);   _time_diff(&StartTime, &CurrentTime, &TimeDifference);  if((TimeDifference.SECONDS >= ps_User_Comm_ESP->tCommandTimeOut.SECONDS) && (TimeDifference.MILLISECONDS >= ps_User_Comm_ESP->tCommandTimeOut.MILLISECONDS))  {   //DEBUG printf("Timer Experied in ESP while waiting for message from TX queue\n");   return ESPR_Timeout;  } }  //DEBUG, Display time it took to get message.  printf("Time Taken = %u.%u Seconds \n", TimeDifference.SECONDS, TimeDifference.MILLISECONDS);  //Get the message that we recieved (from above peek) msg_ptr = _msgq_receive(ps_User_Comm_ESP->QID_of_TX_Task, 0);

 

 

 

Obviously this is only the part of the code which allows you to create your little timeout / error / ok messages.

 

Hope this gives you some ideas to move forward

0 Kudos
Reply