This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

failed to read on /dev/ttyUSB0

Other Parts Discussed in Thread: AM3352

Hello

On my AM3352 based custom board i have a Microchip USB2513 wich is well recognized by linux :

[    1.507446] usb 1-1.1: new high-speed USB device number 3 using musb-hdrc    
[    1.631622] usb 1-1.1: New USB device found, idVendor=0403, idProduct=6011   
[    1.638610] usb 1-1.1: New USB device strings: Mfr=1, Product=2, SerialNumber
=3                                                                              
[    1.646026] usb 1-1.1: Product: USB <-> Serial Converter                     
[    1.651397] usb 1-1.1: Manufacturer: FTDI                                    
[    1.655456] usb 1-1.1: SerialNumber: FTXO9FIB                                
[    1.663330] ftdi_sio 1-1.1:1.0: FTDI USB Serial Device converter detected    
[    1.670562] usb 1-1.1: Detected FT4232H                                      
[    1.674468] usb 1-1.1: Number of endpoints 2                                 
[    1.678802] usb 1-1.1: Endpoint 1 MaxPacketSize 512                          
[    1.683746] usb 1-1.1: Endpoint 2 MaxPacketSize 512                          
[    1.688690] usb 1-1.1: Setting MaxPacketSize 512                             
[    1.693847] usb 1-1.1: FTDI USB Serial Device converter now attached to ttyUS
B0                                                                              
[    1.704223] ftdi_sio 1-1.1:1.1: FTDI USB Serial Device converter detected    
[    1.711486] usb 1-1.1: Detected FT4232H                                      
[    1.715362] usb 1-1.1: Number of endpoints 2                                 
[    1.719696] usb 1-1.1: Endpoint 1 MaxPacketSize 512                          
[    1.724639] usb 1-1.1: Endpoint 2 MaxPacketSize 512                          
[    1.729583] usb 1-1.1: Setting MaxPacketSize 512                             
[    1.734710] usb 1-1.1: FTDI USB Serial Device converter now attached to ttyUS
B1                                                                              
[    1.744995] ftdi_sio 1-1.1:1.2: FTDI USB Serial Device converter detected    
[    1.752197] usb 1-1.1: Detected FT4232H                                      
[    1.756072] usb 1-1.1: Number of endpoints 2                                 
[    1.760406] usb 1-1.1: Endpoint 1 MaxPacketSize 512                          
[    1.765350] usb 1-1.1: Endpoint 2 MaxPacketSize 512                          
[    1.770324] usb 1-1.1: Setting MaxPacketSize 512                             
[    1.775390] usb 1-1.1: FTDI USB Serial Device converter now attached to ttyUS
B2                                                                              
[    1.785644] ftdi_sio 1-1.1:1.3: FTDI USB Serial Device converter detected    
[    1.792816] usb 1-1.1: Detected FT4232H                                      
[    1.796722] usb 1-1.1: Number of endpoints 2                                 
[    1.801055] usb 1-1.1: Endpoint 1 MaxPacketSize 512                          
[    1.805999] usb 1-1.1: Endpoint 2 MaxPacketSize 512                          
[    1.810943] usb 1-1.1: Setting MaxPacketSize 512                             
[    1.816009] usb 1-1.1: FTDI USB Serial Device converter now attached to ttyUS
B3                                                                              
[    1.917449] usb 1-1.2: new high-speed USB device number 4 using musb-hdrc    
[    2.041595] usb 1-1.2: New USB device found, idVendor=0403, idProduct=6011   
[    2.048583] usb 1-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber
=3                                                                              
[    2.055969] usb 1-1.2: Product: USB <-> Serial Converter                     
[    2.061370] usb 1-1.2: Manufacturer: FTDI                                    
[    2.065429] usb 1-1.2: SerialNumber: FTXO9FIB                                
[    2.073120] ftdi_sio 1-1.2:1.0: FTDI USB Serial Device converter detected    
[    2.080352] usb 1-1.2: Detected FT4232H                                      
[    2.084259] usb 1-1.2: Number of endpoints 2                                 
[    2.088592] usb 1-1.2: Endpoint 1 MaxPacketSize 512                          
[    2.093536] usb 1-1.2: Endpoint 2 MaxPacketSize 512                          
[    2.098480] usb 1-1.2: Setting MaxPacketSize 512                             
[    2.103576] usb 1-1.2: FTDI USB Serial Device converter now attached to ttyUS
B4                                                                              
[    2.113861] ftdi_sio 1-1.2:1.1: FTDI USB Serial Device converter detected    
[    2.121093] usb 1-1.2: Detected FT4232H                                      
[    2.124969] usb 1-1.2: Number of endpoints 2                                 
[    2.129333] usb 1-1.2: Endpoint 1 MaxPacketSize 512                          
[    2.134277] usb 1-1.2: Endpoint 2 MaxPacketSize 512                          
[    2.139221] usb 1-1.2: Setting MaxPacketSize 512                             
[    2.144287] usb 1-1.2: FTDI USB Serial Device converter now attached to ttyUS
B5                                                                              
[    2.154571] ftdi_sio 1-1.2:1.2: FTDI USB Serial Device converter detected    
[    2.161834] usb 1-1.2: Detected FT4232H                                      
[    2.165740] usb 1-1.2: Number of endpoints 2                                 
[    2.170104] usb 1-1.2: Endpoint 1 MaxPacketSize 512                          
[    2.175048] usb 1-1.2: Endpoint 2 MaxPacketSize 512                          
[    2.180023] usb 1-1.2: Setting MaxPacketSize 512                             
[    2.185150] usb 1-1.2: FTDI USB Serial Device converter now attached to ttyUS
B6                                                                              
[    2.195465] ftdi_sio 1-1.2:1.3: FTDI USB Serial Device converter detected    
[    2.202667] usb 1-1.2: Detected FT4232H                                      
[    2.206542] usb 1-1.2: Number of endpoints 2                                 
[    2.210906] usb 1-1.2: Endpoint 1 MaxPacketSize 512                          
[    2.215850] usb 1-1.2: Endpoint 2 MaxPacketSize 512                          
[    2.220794] usb 1-1.2: Setting MaxPacketSize 512                             
[    2.225891] usb 1-1.2: FTDI USB Serial Device converter now attached to ttyUS
B7       

when i write on /dev/ttyUSB0, that's OK, i get my characters on an hyperterminal, but when i want to read, the caracters received aren't those emited by the hyperterminal. (for example a 'm' becomes'I', h becomes 'J'...)

Have you any idea of what can happen ?

                         

thanks

Here is the source code to open COM :

            if ( (com_fd1=open("/dev/ttyUSB0",O_RDWR )) == -1 )
            {
                    perror("open tty232USB0\r\n");
                }
            }

    //read COM parameter
    if (tcgetattr(fd,&termios_p) != 0)
    {
        printf ("error %d from tcgetattr\r\n", errno);
    }

/
    //set baud rate
    termios_p.c_cflag = B9600  | CS8;
//    termios_p.c_cflag = B115200  | CS8;

    termios_p.c_lflag = 0;                    // no signaling chars, no echo,
    termios_p.c_oflag = 0;                    // no remapping, no delays
    termios_p.c_cc[VMIN]  = 0;                // read doesn't block
    termios_p.c_cc[VTIME] = 0;               // no timeout
    //TODO
    if ((comport==5)||(comport==6))
        termios_p.c_cc[VTIME] = 5;

    //shut off parity
    //termios_p.c_cflag &= ~(PARENB | PARODD);

    //one stop bit
    //termios_p.c_cflag &= ~CSTOPB;

    //save new parameter
    if (tcsetattr(fd,TCSANOW,&termios_p) != 0)
    {
        printf ("error %d from tcsetattr\r\n", errno);
    }

    printf ("COM%d initailized\r\n",comport);

Here is the source code to read  COM :

    len = read(fd1,&c,1);
    if (len < 1 ) //TODO && errno != EAGAIN)
    {
        printf("Rx error%d: %s\n",com1, strerror(errno));
    }
    else if (len==1)
    {
       printf("Rx %c %d\r\n",c,c);
       break;
    }

  • Hi,

    What are you trying to do ?

    Are you testing the device "USB2513" ?

  • Hello

    yes i try to read/write on dev/ttyUSB0, wich is an USB->serial device

    thanks for your reply

    regards

  • Hi,

    Try the below code.

    Ex:

    To generate "read" app.

    arm-arago-linux-gnueabi-gcc serial_testing.c -o read

    To generate "write" app.

    arm-arago-linux-gnueabi-gcc serial_testing.c -o write

    To Run:

    ./read  /dev/ttyUSB0 "" 115200 > /root/log_usb.txt &

    ./write  /dev/ttyUSB0 "Testing" 115200

    Note:

    Short the Rx & Tx pins (2,3) of serial port

    /******************************************************************************
     *
     * This code used to test the serial communication
     *
     *
     * Usage : ./serial /dev/ttyS0 "Hello World" 9600
     *
     ******************************************************************************/
    
    /* Ver 1.0
     *
     * Created on: 21-May-2014
     * Author : Titusrathinaraj Stalin
     * E-mail & Feedbacks to : x0213399@ti.com
     *
     ******************************/
    
    /* Include linux kernel header files */
    #include <stdio.h>
    #include<fcntl.h>
    #include<termios.h>
    #include<string.h>
    
    int fd_serial;
    
    //char tes[] = "Testing";
    
    int Open(char a[10],int baudrate)
    {
    	// Object For Struct Termios .
    	struct termios options;
    
    	// ser1 || ttyS0=> Com Port1 File Present In /dev Directory .
    	// open => Function Com Port 1 .
    	fd_serial=open(a, O_RDWR|O_NOCTTY|O_NDELAY);
    	// Error Handle .
    	if(fd_serial==-1)
    	{
    		printf(" Cannot Open Port : %s \n",a);
    		return -1;
    	}
    	fcntl(fd_serial,F_SETFL,0); 
    	tcgetattr(fd_serial,&options);
    	cfsetispeed(&options,baudrate);// Function To Set Baud Rate Of Input Stream .
    	cfsetospeed(&options,baudrate);// Function To Set Baud Rate Of Output Stream .
    	options.c_cflag |= (CLOCAL | CREAD );// Options For Reading From Devices .
    	options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);//Option For Desable Canonical Input .
    	options.c_oflag &= ~OPOST;//Option For Desable Canonical Output .
    	options.c_cc[VMIN]=1;// Options For Time Outs .
    	options.c_cc[VTIME]=10;
    	tcsetattr(fd_serial,TCSANOW,&options);// Update Changes .
    	//printf("\n Succeeded In  Openning Port : %s\n",a);
    	return fd_serial;// Return Device Ptr .
    }
    
    Write(int fd,char *data)
    {
    	int i,k;
    	char ch;
    	for (i=0;i<strlen(data);i++)
    	{
    		ch = data[i];
    		k = write(fd,&ch,1);
    	}
    	i = 13;
    	k = write(fd,&i,1);
    //	printf("writing %d  %c\n",k,i);
    }
    
    Read(int fd ,char *data)
    {
    	int i;
    	char ch ;
    	while(read(fd,&ch,1))
    	printf(" %c",ch);
    }
    
    int main ( int argc, char *argv[] )
    {
    
    if ( argc != 4 ) /* argc should be 3 for correct execution */
    	{
            /* We print argv[0] assuming it is the program name */
            printf( "Usage: %s <COM port name>  <msg> <baud rate>\n Ex: %s /dev/ttyS0 \"Testing\" 9600 \n", argv[0],argv[0] );
    	}
    else
    	{
    	Open(argv[1],argv[3]);
    //	Write(fd_serial,argv[2]);	//Comment this line to generate 'read' app
    	Read(fd_serial,argv[2]);	//Comment this line to generate 'write' app
    	}
    
    return 0;
    }

  • Hi,

    when i write on /dev/ttyUSB0, that's OK, i get my characters on an hyperterminal, but when i want to read, the caracters received aren't those emited by the hyperterminal. (for example a 'm' becomes'I', h becomes 'J'...)

    I hope your code might have problem,

    Please try the above code and update the result to us.

  • Hi,

    Try to run like below.

    Ex:

    To generate "read" app.

    host$ arm-arago-linux-gnueabi-gcc serial_testing.c -o read

    To generate "write" app.

    host$ arm-arago-linux-gnueabi-gcc serial_testing.c -o write

    To Run:

    Open telnet connection or ssh connection :

    telnet#./read  /dev/ttyUSB0 "" 115200

    In Console

    target# ./write  /dev/ttyUSB0 "Testing" 115200

  • Hello

    i tried your code

    but get same problem reading /dev/ttyUSB0 :

    sending K (fro hyperterminal) : we read  J=0x 74    (on custom board)                                                           
    sending  : we read  0x  18                                                             
    sending : we read  I =0x73

  • Hi,

    Which string have you send to "ttyUSB0" ?

    single letter or string ?

    Is the string one letter got shifted?

    Ex:

    If you sent "Hi" then Are you receiving "i" letter alone instead "Hi" ?

    Please provide your logs with commands what you executed.

  • Hi

    thanks for your reply

    there is no real rules : for example if i send a 'm' from PC i receive a 'I', a 'k'-> 'J', 'n'->'$'...

    'Hi' sent will gives me 0x11, 0x4b

    I can transmit well to PC

    Is there something special to do with FTDI driver for embedded linux on ARM 3352 ?

    Thanks

    Here is my com initialization code

    void COM_init(int comport)
    {
        struct     termios    termios_p;
        int        fd=-1;
        //open serial COM
        switch(comport)
        {
            case 1:
                //if ( (com_fd1=open(COM_USB0,O_RDWR)) == -1 )
                if ( (com_fd1=open(COM_USB0,O_RDWR|O_NOCTTY|O_NDELAY )) == -1 )
                {
    //                if ( (com_fd1=open(COM_USB4,O_RDWR )) == -1 )
                    {
    //                    perror("open tty232USB0\r\n");
                    }
                }
                fd=com_fd1;
                break;
            case 2:
                if ( (com_fd2=open(COM_USB1,O_RDWR )) == -1 )
                {
    //                if ( (com_fd2=open(COM_USB5,O_RDWR )) == -1 )
                    {
    //                    perror("open tty232USB1\r\n");
                    }
                }
                fd=com_fd2;
                break;
            case 3:
                if ( (com_fd3=open(COM_USB2,O_RDWR )) == -1 )
                {
    //                if ( (com_fd3=open(COM_USB6,O_RDWR )) == -1 )
                    {
    //                    perror("open tty232USB2\r\n");
                    }
                }
                fd=com_fd3;
                break;
            case 4:
                if ( (com_fd4=open(COM_USB3,O_RDWR )) == -1 )
                {
    //                if ( (com_fd4=open(COM_USB7,O_RDWR )) == -1 )
                    {
    //                    perror("open tty232USB3\r\n");
                    }
                }
                fd=com_fd4;
                break;
            case 5:
                if ( (com_fd5=open(COM_USB4,O_RDWR )) == -1 )
                {
    //                if ( (com_fd5=open("/dev/tty485USB4",O_RDWR )) == -1 )
                    {
    //                    perror("open tty485USB0\r\n");
                    }
                }
                fd=com_fd5;
                break;
            case 6:
                if ( (com_fd6=open(COM_USB5,O_RDWR )) == -1 )
                {
    //                if ( (com_fd6=open("/dev/tty485USB5",O_RDWR )) == -1 )
                    {
    //                    perror("open tty485USB1\r\n");
                    }
                }
                fd=com_fd6;
                break;
            case 7:
                if ( (com_fd7=open(COM_USB6,O_RDWR )) == -1 )
                {
    //                if ( (com_fd6=open("/dev/tty485USB5",O_RDWR )) == -1 )
                    {
    //                    perror("open tty485USB1\r\n");
                    }
                }
                fd=com_fd7;
                break;
            case 8:
                if ( (com_fd8=open(COM_USB7,O_RDWR )) == -1 )
                {
    //                if ( (com_fd6=open("/dev/tty485USB5",O_RDWR )) == -1 )
                    {
    //                    perror("open tty485USB1\r\n");
                    }
                }
                fd=com_fd8;
                break;
            default:
                printf("wrong port COM %d\r\n",comport);
                break;
        }


        //read COM parameter
        if (tcgetattr(fd,&termios_p) != 0)
        {
            printf ("error %d from tcgetattr\r\n", errno);
        }

        //set baud rate
        termios_p.c_cflag = B9600  | CS8;
    //    termios_p.c_cflag = B115200  | CS8;

        termios_p.c_lflag = 0;                    // no signaling chars, no echo,
        termios_p.c_oflag = 0;                    // no remapping, no delays
        termios_p.c_cc[VMIN]  = 0;                // read doesn't block
        termios_p.c_cc[VTIME] = 0;               // no timeout
        //TODO
        if ((comport==5)||(comport==6))
            termios_p.c_cc[VTIME] = 5;

        //shut off parity
        termios_p.c_cflag &= ~(PARENB | PARODD);

        //one stop bit
        termios_p.c_cflag &= ~CSTOPB;

        //save new parameter
        if (tcsetattr(fd,TCSANOW,&termios_p) != 0)
        {
            printf ("error %d from tcsetattr\r\n", errno);
        }


        printf ("COM%d initailized\r\n",comport);
    }

    void COMxxx_test_loop(int loop)
    {
        char    c;
        int       len=0;
        char    rx_buffer[COM_COM_TEST_BUFFER_LENGTH];
        char    tx_buffer[COM_COM_TEST_BUFFER_LENGTH];
        int     nb_rx_char=0;
        int     result=0;
        struct     timeval tp;
        double     startsecs,secs;
        int        fd1,fd2;
        int        com1,com2,i;
        switch(loop)
        {
            case 1:
                fd1=com_fd1;
                break;
            case 2:
                fd1=com_fd2;
                break;
            case 3:
                fd1=com_fd3;
                break;
            case 4:
                fd1=com_fd4;
                break;
            case 5:
                fd1=com_fd5;
                break;
            case 6:
                fd1=com_fd6;
                break;
            case 7:
                fd1=com_fd7;
                break;
            case 8:
                fd1=com_fd8;
                break;
            default:
                printf("wrong port COM %d\r\n",loop);
                return;
                break;
        }


        //from COM1->COM2
        memset(rx_buffer,0,COM_COM_TEST_BUFFER_LENGTH);
        memset(tx_buffer,0,COM_COM_TEST_BUFFER_LENGTH);
        strcpy(&tx_buffer[0],COM_UCR_RX_TX_TEST_STRING);
        tcflush(fd1,TCIFLUSH);


    while (1)
    {
        len = read(fd1,&c,1);
        if (len < 1 ) //TODO && errno != EAGAIN)
        {
            printf("Rx error%d: %s\n",com1, strerror(errno));
        }
        else if (len==1)
        {
           printf("Rx %c Ox%x\r\n",c,c);
           break;
        }
    }
    }


    best regards

  • Hi,

    Have you tried code which I have attached early and followed steps ?

  • HI

    yes i have tried this in my initialization function :

        {
            // Object For Struct Termios .
            struct termios options;

            fcntl(fd,F_SETFL,0);
            tcgetattr(fd,&options);
            cfsetispeed(&options,9600);// Function To Set Baud Rate Of Input Stream .
            cfsetospeed(&options,9600);// Function To Set Baud Rate Of Output Stream .
            options.c_cflag |= (CLOCAL | CREAD );// Options For Reading From Devices .
            options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);//Option For Desable Canonical Input .
            options.c_oflag &= ~OPOST;//Option For Desable Canonical Output .
            options.c_cc[VMIN]=1;// Options For Time Outs .
            options.c_cc[VTIME]=10;
            tcsetattr(fd,TCSANOW,&options);// Update Changes .
            //printf("\n Succeeded In  Openning Port : %s\n",a);

        }

    thanks