Reading data from serial port

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anu29dolly
    New Member
    • Oct 2008
    • 6

    #1

    Reading data from serial port

    Hello everyone...

    I have written a program to write and read data from serial port....
    I am able to write 80(in binary)..and is expecting 1B but i am uable to read it...

    My code goes as follows,,....

    Code:
     
    int main()
     
    {
     
    int mainfd=0;
     
     
     
     
    mainfd = open_port();
    close(mainfd);
     
    fd = open( "/dev/ttyS0" ,O_RDWR |O_NOCTTY | O_NDELAY);
    if (fd == -1) {
    perror("open_port: Unable to open /dev/ttyS0 - ");
    return 1;
    } else {
    fcntl(fd, F_SETFL,0);
    }
    /* Clear the line */
    tcflush(fd,TCIFLUSH);
     
    initport(fd);
     
     
    char sCmd[1];
    sCmd[0] =0010;//0010;
    sCmd[1] =0000;//0000;
     
    if (!writeport(fd, sCmd)) {
    printf("write failed\n");
    close(fd);
    return 1;
    }
     
     
    printf("written: %d%d\n", sCmd[0],sCmd[1]);
    close(fd);
     
    usleep(30000);
    fd = 0;
    fd = open( "/dev/ttyS0" ,O_RDONLY |O_NOCTTY | O_NDELAY);
    char sResult[1];
    fcntl(fd, F_SETFL, FNDELAY); // don't block serial read
     
    if (!readport(fd,sResult)) {
    printf("read failed\n");
    close(fd);
    return 1;
    }
    else
    close(fd);
    return 0;
    }
     
     
    the read write functions are as follows....
     
     
    int fd;
    char iIn[1];
     
    int open_port (void)
    {
     
     
    int ser_cmd, ser_stat;
    ser_cmd = TIOCM_RTS;
     
    fd = open("/dev/ttyS0", O_RDONLY); // Open the serial port.
     
    if (fd == -1)
    {
    perror("open_port: Unable to open /dev/ttyS0 -%s\n "); 
    return 1; 
    } else{
    printf("Port 0 opened\n");
    fcntl(fd, F_SETFL, 0);
    }
     
    // Read the RTS pin status.....
     
    ioctl(fd, TIOCMGET, &ser_stat);
    if (ser_stat & TIOCM_RTS)
    {
    printf("RTS pin is set.\n");
    }
    else
    {
    printf("RTS pin is reset.\n");
    ioctl(fd, TIOCMBIC, &ser_cmd); // set the RTS pin.
    if (ser_stat & TIOCM_RTS)
    printf("RTS pin is set.\n");
    else
    printf("RTS pin is reset.\n");
     
    }
    return (fd);
    }
     
     
     
     
    int writeport(int fd, char *chars) 
     
    {
    int len =strlen(chars);
     
    int n = write(fd, chars,strlen(chars));
    if (n < 0)
    {
    fputs("write failed!\n", stderr);
     
    return 0;
    }
    fcntl(fd, F_SETFL,0);//changed
    return 1;
    }
     
    int readport(int fd, char *result) {
    int iIn = read(fd, result, 1);
    printf("Value Received: %x\n",*result);
     
     
    return 1;
    }
     
     
    **************************************
    here it gives 40...
    ************************************
    int initport(int fd){
     
    struct termios options;
    printf("Setting Parameters...\n");
    tcgetattr(fd, &options);
    cfsetispeed(&options, B9600); /* Set the baud rates to 9600 */
    cfsetospeed(&options, B9600);
     
    /* Enable the receiver and set local mode */
    options.c_cflag |= (CLOCAL | CREAD);
    options.c_cflag &= ~PARENB; /* Mask the character size to 8 bits, no parity */
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8; /* Select 8 data bits */
    options.c_cflag &= ~CRTSCTS; /* Disable hardware flow control */ 
     
    /* Enable data to be processed as raw input */
    options.c_lflag &= ~(ICANON | ECHO | ISIG);
     
    options.c_cc[VMIN]=0;//it will wait for one byte at a time.
    options.c_cc[VTIME]=10;// it will wait for 0.1s at a time.
    /* Set the new options for the port */
    tcsetattr(fd, TCSANOW, &options);
    printf("Parameters SET!!!...\n");
    return 1;
    }
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Hi,
    whenever you are reading froma serial port try to read in chunks.
    After reading a certain chunk check whther you have read desired bytes or else read the remaining bytes.
    Do this in a loop always.

    Thanks
    Raghu

    Comment

    • anu29dolly
      New Member
      • Oct 2008
      • 6

      #3
      okk but can u plz guide me how to do this...
      can u suggest modifications for the same in my program...

      thanks
      in advance

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        I have made some changes to the code . I wont say that this is perfectly right and this is only to make you understand the idea
        [CODE=cpp]
        int readport(int fd, char *result)
        {
        //Have a while loop here
        int lnReadLength=0;
        while(lnReadLen gth <= 100)//I am assuming u want to read 100 bytes
        {
        int iIn = read(fd, result, 10);
        lnReadLength += iln;
        result = (result+lnReadL ength);
        }
        //printf("Value Received: %x\n",*result);
        return 1;
        }
        [/CODE]

        Raghu

        Comment

        • anu29dolly
          New Member
          • Oct 2008
          • 6

          #5
          ya thank you very much...for ur help...

          i hope u read my code i think i making a mistake in writing the data..as i mentioned that i have to send 80(hex value) to the serial port and then read..

          i think the write function is not working and hence the read function...

          the write function i am pasting again.......... .
          can u plz have a look at it and confirm its correctness...

          thanks

          code as follows...

          ...
          ...
          int main()
          {

          ...
          ...
          ...

          initport(fd);

          int sCmd[1];

          sCmd[0]=0200;

          (this value is gives given in octal as we cannot give 80 in hex directly...
          and serial port accepts only hex or octal values)



          if (!writeport(fd, sCmd)) {
          printf("write failed\n");
          close(fd);
          return 1;
          }

          printf("written : %x\n", sCmd[0]);
          close(fd);

          *************** *************** *************** *************** *************
          and this is the writeport function.....




          int writeport(int fd, char *chars)

          {
          int len =strlen(chars);

          int n = write(fd, chars,strlen(ch ars));
          if (n < 0)
          {
          fputs("write failed!\n", stderr);

          return 0;
          }
          fcntl(fd, F_SETFL,0);//changed
          return 1;
          }

          Comment

          • gpraghuram
            Recognized Expert Top Contributor
            • Mar 2007
            • 1275

            #6
            i think u shuld follow the same approach like writing in chunks and then return the status.

            Thanks
            Raghu

            Comment

            • anu29dolly
              New Member
              • Oct 2008
              • 6

              #7
              i have tried that but dnot know the program is not reading data from port...now it is writing correctly....

              can u please guide me..

              thanks

              Comment

              Working...