select() on serial port

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

select() on serial port

Jump to solution
1,624 Views
pozz
Contributor III

I'm using MQX 4.0 with Tower system (Kinetis K60).

I'm writing a task that communicates to another device through the serial port of TWR-SER board. I'm able to open the port and send/receive bytes on that port without problems.

Now I'd like to have a function similar to the classical select() function: I want to put the task in blocked state until a character on the serial port has arrived or a timeout expires. I noticed the select() function is implemented only in RTCS, but not for simple I/O serial port.

The serial protocol I'm implemented is master/slave, where the MQX application is the master. So it sends a packet and waits for the answer. The wait can't be indefinitely, because the other device couldn't be actually connected, so I have to do something if this condition occurs.

I can open the serial port in polling non-blocking mode and poll the serial port with fgetc() and make some ticks calculations to detect the timeout:

while(1) {

  send_packet();

  end = start = _time_ticks();

  while(_time_diff(end, start) < TIMEOUT) {

    c = fgetc(serial_port);

    if (c >= 0) {

      add_to_rx(c);

      if (rxpacket_complete()) {

        break;

      }

  }

  if (rxpacket_complete()) {

    /* Process the incoming packet */

    clear_rx();

  } else {

    /* No answer */

  }

}


But I'd like to avoid this. The task is always running and never goes in blocked mode.

Tags (3)
1 Solution
547 Views
DavidS
NXP Employee
NXP Employee

Hi Giuseppe,

Please have a look at an example of timeout for a read() with timeout.

   result_custom = read_custom(fh_ptr,(pointer)bptr,MAX_NUMER_OF_CHARACTERS,INPUT_CHARACTER_DELAY_MILLISECONDS);  //DES read 7 character or timeout in X seconds

Not certain this addresses your needs totally but hopefully gets you moving in right direction.

Regards,

David

View solution in original post

0 Kudos
3 Replies
547 Views
Monica
Senior Contributor III

Giuseppe, is this working now?

Let us know :smileywink:

Regards!

0 Kudos
547 Views
pozz
Contributor III

I'm sorry, but I'm not working on that project now. When I come back to it, I will try the suggestion of David and let you know the result.

0 Kudos
548 Views
DavidS
NXP Employee
NXP Employee

Hi Giuseppe,

Please have a look at an example of timeout for a read() with timeout.

   result_custom = read_custom(fh_ptr,(pointer)bptr,MAX_NUMER_OF_CHARACTERS,INPUT_CHARACTER_DELAY_MILLISECONDS);  //DES read 7 character or timeout in X seconds

Not certain this addresses your needs totally but hopefully gets you moving in right direction.

Regards,

David

0 Kudos