Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
21 years ago

uart nonblocking

How can I do nonblocking io with uart

I have a singlethread enviroment and need nonblocking io

for uart

I tried the following code, but it does not work correctly

int fd;

fd = open("/dev/uart_0", O_RDWR | O_NONBLOCK);

while(1) {

res=read(fd,test,25);

// res=fgets(test,25,fd);

// res=fread(test,1,25,fd);

if(res>0) {

for(i=0;i<res;i++) {

if(test==&#39;\r&#39;) {

*prxbuff=0;

printf("rx:count:%d:<%s>\r\n",rxcount,rxbuff);

rxcount=0;

prxbuff=rxbuff;

}

else {

*prxbuff++=test;

rxcount++;

}

}

}

Sorry I´ve forgot to append the output of the code.

the output of the above code is sometimes missing some characters, however

the uart works correct, echo of uart is ok.

output in terminal:

TestRS232

RX:COUNT:8:<TesRS232>

TestRS232

RX:COUNT:9:<TestRS232>

TestRS232

RX:COUNT:7:<estRS32>

TestRS232

RX:COUNT:8:<TestR232>

TestRS232

RX:COUNT:8:<TestS232>

TestRS232

RX:COUNT:9:<TestRS232>

TestRS232

RX:COUNT:8:<TestRS22>

TestRS232

RX:COUNT:8:<TestR232>

TestRS232

RX:COUNT:7:<TstRS22>

TestRS232

RX:COUNT:7:<TstRS22>

TestRS232

RX:COUNT:8:<TesRS232>

TestRS232

RX:COUNT:8:<TesRS232>

3 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Ummm.... I see where you&#39;re opening your file handle, but I don&#39;t see that you use it anywhere, unless I&#39;m missing something.

    I&#39;m no C expert, but I believe that *only* your "fd" file descriptor will have the non-blocking option set.

    Also, do you happen to have the "Reduced device drivers" box checked on the system library project associated with this code?

    If so, the following comment from the uart device driver could be exactly what you&#39;re seeing:

     * This implementation polls the device waiting for characters. At most it can
     * only return one character, regardless of how many are requested. If the 
     * device is being accessed in non-blocking mode then it is possible for this
     * function to return without reading any characters. In this case errno is
     * set to EWOULDBLOCK.
     */

    FYI: You can find the source code for this driver in the Nios II Device Drivers project from within the IDE...

    Nios II Device Drivers/altera_avalon_uart/HAL/src/altera_avalon_uart.c

    Cheers,

    - slacker
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    The filedescriptor is used in the "read" function and the read function returns

    immediatly with res=0, if no characters are available. I don&#39;t want to use

    the reduced drivers. I found no example and no register description in the

    altera documentation. I&#39; have read the "linux serial programming howto",

    but there must be some differences in programming with NIOSII ???.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    !!! I tested the code on a NIOSII cyclone dev.kit board and it works. !!!

    It seems that I have a hardware problem with my "testboard EP1C6"

    Thanks