2009-10-21 10:11:59     Uart question

Document created by Aaronwu Employee on Aug 19, 2013
Version 1Show Document
  • View in full screen mode

2009-10-21 10:11:59     Uart question

Daniele Pagani (ITALY)

Message: 81533   

 

Dear Sirs,

 

in my application I have a thread that sends and receives data to and from an external microcontrollers.

 

I use only Tx and Rx signal.

 

I need to send exactly 2 bytes and I need to receive exactly 10 bytes.

 

I want to use a blocking read in my thread.

 

So, I do:

 

int init_uart0(void){

    extern int fd2; // for uart0

    struct termios options;

   

    fd2=open(DEVICE_UART0,O_RDWR|O_NOCTTY /* |O_NDELAY */  );

    if(fd2<0){

     return -5;

    }

    bzero(&options,sizeof(options));

   

    cfsetispeed(&options,B19200);

    cfsetospeed(&options,B19200);

    options.c_cflag |= CS8 | CLOCAL | CREAD;

 

    options.c_cc[VTIME]=30; // timeout 3 sec

    options.c_cc[VMIN]=10;

 

    tcflush(fd2, TCIOFLUSH);

    tcsetattr(fd2,TCSANOW,&options);

    return 0;

} // end init_uart0

 

 

 

And then, in my thread, I have:

 

...

 

while(1){

 

    write(fd2,richiesta_uart0,2);

    read(fd2,ricezione_uart0,10);

}

 

...

 

In this configuration all works fine, but I have a timeout for the packet of 10 bytes.

 

So, I set:

 

    options.c_cc[VTIME]=30; // timeout 3 sec

    options.c_cc[VMIN]=0;

 

in order to have a timeout for each byte.

 

In this configuration, I see that typically Blackfin operates 2 write before waiting the answer from the other micro. That is, I use oscilloscope in order to see Rx and Tx and often I see two write without an answer from the other micro.

 

Then, if I use another while in my thread:

 

while(1){

 

    write(fd2,richiesta_uart0,2);

    while(read(fd2,ricezione_uart0,10)!=10){}

}

 

then, I see that send/receive works for 2 / 3 times, then it doesn't work.

 

If I use a count, like:

 

while(1){

 

    write(fd2,richiesta_uart0,2);

 

    write_counter++;

 

    while(read(fd2,ricezione_uart0,10)!=10){}

 

     read_counter++;

}

 

I see that when the comunication is blocked, write_counter is always read_counter + 1.

 

This thread is a part of an application.

 

I use Blackfin BF537,  release 2009R1-RC6.

 

Regards,

 

Daniele.

TranslateQuoteReplyEditDelete

 

 

2009-10-22 06:17:58     Re: Uart question

Sonic Zhang (CHINA)

Message: 81588   

 

If you want to "in order to have a timeout for each byte.", you have to change nothing to the options, instead run

 

for(i = 0; i < 10; i++)

 

    if (read(fd2,ricezione_uart0,1) == 0) {

 

        printf("error read from ricezione_uart0\n");

 

        break;

 

    }

Attachments

    Outcomes