Forum Discussion

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

"Resource temporarily unavailable" while reading from "dev/ttyS0"

Hi all, i have ported uClinux on DE2 2c35 board and i want to use RS232 port on the board to communicate with a PC. I understand that I can use "dev/ttyS0" for that purpose.

I can open and write data to PC by using write function. But the problem is I can't read from "dev/ttyS0". Every time I use read function, I get "Resource temporarily unavailable". Can u guys tell me how to solve this problem?

anyways, I enabled both "Altera JTARG UART support" and "Altera UART support". Was I wrong?

here my C code:


# include <string.h>
# include <unistd.h>  
# include <fcntl.h>   
# include <errno.h>   
# include <termios.h> 
int main(void)
{
	char buf;	
	int fd1, count;
	
 	fd1 = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
	
      	if (fd1 == -1)
      	{
       		perror("open_port: Unable to open /dev/ttyS0 - ");
                       exit(1);
	}
      	 fcntl(fd, F_SETFL, FNDELAY);
            for(count = 0; count < 65556; count ++); // delay
	if(count = read(fd1, buf, 10) < 0)
           {
               perror("reading failed ");
               exit(2);
           }
	fprintf(stdout, "%s", buf);
	close(fd1);
	
return 0;
     
}

9 Replies

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

    You can send data to UART with

    ls > /dev/ttyS0

    Or

    cat "any_file" > /dev/ttyS0
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    yes, i was successful using cat application and write function to send data through "/dev/ttyS0" but i can not read data using read function

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

    #include <string.h># include <unistd.h># include <fcntl.h># include <errno.h># include <termios.h>
    int main(void)
    {
            char buf;
            int fd1;
            volatile int count;
            fd1 = open("/dev/urandom", O_RDWR | O_NOCTTY | O_NDELAY); //urandom for test.
            if (fd1 == -1)
            {
                    perror("open_port: Unable to open /dev/ttyS0 - ");
                           exit(1);
            }
             fcntl(fd1, F_SETFL, FNDELAY);
                for(count = 0; count < 65556; count ++); // delay //it will be delay if count is volatile. And it's a bad way to set delay. 
            if(count = read(fd1, buf, 10) < 0)
               {
                   perror("reading failed ");
                   exit(2);
               }
            printf("%s", buf);
            close(fd1);
    return 0;
    }
    ~  

    And 65556 is very short delay.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi ARTEM_BOND,

    thanks for replying!

    I know using delay is a bad way. Can u plz tell me another way to wait data from the host?

    I tried fcntl(fd1, F_SETFL, 0);, but i had to a long time without receive any data.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    Hi ARTEM_BOND,

    thanks for replying!

    I know using delay is a bad way. Can u plz tell me another way to wait data from the host?

    I tried fcntl(fd1, F_SETFL, 0);, but i had to a long time without receive any data.

    --- Quote End ---

    Set timeout.

    http://linux.die.net/man/2/select

    http://www.linux.org/docs/ldp/howto/serial-programming-howto/x115.html

    Also you can make you own module (driver) for UART. And write you own UART with hardware time-out timer on Verilog.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    hi ARTEM_BOND,

    thanks for ur instructions!

    I have set the timeout and wait for a long time. But but I still got no data. specially, every time i try to get data sent from the host by using read function or cat application, both rxd and txd leds on de2 board flash concurrently. I really do not understand the reason. I do not send data from the board.:confused:
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    thanks for replying and reading everybody,

    I finally solved this problem by the instructions in this link: http://www.linux.org/docs/ldp/howto/serial-programming-howto/x115.html

    --- Quote End ---

    Dear Nguyen, The website you told us you found in it instructions to solve this problem does not exist anymore. Would you like please tell us how you solved this problem because I have the same problem in linux ubuntu 12.04.

    Thank you, Rachid