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.

Error In Receiving characters on UART !!!!!!!!!!!

Dear Team !

Please refer the attachedcode for Transmitting and Receiving characters on /dev/ttyS0 :


I am able to transmit the String on terminal. But there is an Error in Receiving the String.

The read function returns -1 .

m i wrong with some receive configuration.

Please Guide me !!!!

Prag !!!!

  • The attachment does not app#include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/ioctl.h>
    #include <sys/stat.h>
    #include <termios.h>

    #define BAUDRATE    B115200
    #define SER_DEVICE  "/dev/ttyS0"
    #define FALSE       0
    #define TRUE        1


    int main()
    {
    int fd,c,res,i,n;
    struct termios oldtio,newtio;
    unsigned char buf[255] = "WELCOME TO THE WORLD OF LINUX PROGRAMMING\n";
    unsigned char buf2[255]= {"\0"};
    //Opening a Device for Reading Writing.
    //O_NOCTTY : - The Port Never Becomes the Controlling Terminal of the Process.
    //O_NDELAY : - Use NON-Blocking IO. on some system this also means Deactivating the DCD line.

    fd=open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_NDELAY);

    if(fd<0)
    {
    printf("\nError in opening the File\n");
    exit(0);
    }
    else
    {
    printf("File Opened  SuccessFull..HurraYYY !!!!1\n");
    }

    //printf("--------------Test Begin---------------\n");

    //Save Current Serial Port Settings

    tcgetattr(fd,&oldtio);
    //clear the struct for New port settings
    memset(&newtio,0,sizeof(newtio));

    //Baud rate : Set bps rate .
    //You could also use cfsetispeed and cfsetospeed.
    //CRTSCTS : Output Hardware Flow control
    //CS8 : 8n1(8bit No Parity 1 Stopbit)
    //CLOCAL : local connection no modem control
    //CREAD : Enable Receiving character
    //printf("Setting Configuration for Port");
    newtio.c_cflag |= (BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD);

    //IGNPAR : Ignore bytes with parity error.
    //ICRNL : map CR to NL
    //printf("Setting Parity\n");
    newtio.c_cflag |= (IGNPAR | ICRNL);

    //RAW output
    //printf("Raw Output\n");
    newtio.c_oflag = 0;
    //printf("Enabling Canonical format \n");
    //ICANON : Enable canonical input.
    newtio.c_lflag |= ICANON;
    //printf("Initialising Char\n");
    //Initialise all characters
    newtio.c_cc[VMIN] = 1;   /*Blocking read until one character arrives*/
    newtio.c_cc[VTIME] = 0;  /*Inter character timer unused*/

    /*
    Now clean the Modem Line and Activate the Settings for the Port.
    */
    tcflush(fd,TCIFLUSH);
    printf("Flushing Lines\n");
    tcsetattr(fd,TCSANOW,&newtio);


    n=write(fd,&buf,42);
    printf("n=%d",n);


    //SOME DELAY

    n=0;
    n = read(fd,&buf2,42);
    if(n==-1)
    {
    printf("\nError in Receiving");
    }
    else
    printf("Received String = %s",buf2);

    /*
    Restore the Old Port Setting
    */

    tcsetattr(fd,TCSANOW,&oldtio);

    printf("==============TEST END==============\n");

    close(fd);
    }