Thread: my serial port data reading program isn't working

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    4

    my serial port data reading program isn't working

    Hey all,

    I run linux, and my serial port is on /dev/ttyS0. I have a GPS receiver hooked up to it, which puts out lines like:
    $PSRF104,0,0,0,96000,237759,1946,12,4*1D

    I can't seem to read those lines. When i fireup cutecom and have the program connect to the serial port at 4800 8N1, it reads the lines perfectly from the device... so I know the device and serial port work.

    Here is the code I have:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <termios.h>
    
    int main() {
      struct termios tp;
      int fd;
      char buffer[100];
      long baud = 4800;
    
      if ((fd = open("/dev/ttyS0", O_RDWR)) < 0) {
        perror("bad terminal device, try another");
        exit(-1);
      }
    
      tp.c_cflag = CS8|CLOCAL|baud;
      tp.c_oflag = 0;
      tp.c_iflag = IXON|IGNBRK|IGNCR|IGNPAR;
      tp.c_lflag = 0;
      cfsetospeed(&tp, baud);
      cfsetispeed(&tp, baud);
      if (tcsetattr(fd, TCSANOW, &tp) < 0) {
        perror("Couldn't set term attributes");
        exit(-1);
      }
    
      memset(buffer, '\0', sizeof(buffer));
      read(fd, buffer, 10);
    
      printf("Buffer: %s\n", buffer);
    
      return 1;
    }
    and here is the output I get, it is very random output:
    Code:
    hedpe@hedlinux serial $ ./serial
    Buffer: <?3xÀ˜
    hedpe@hedlinux serial $ ./serial
    Buffer: `
             ~0
    hedpe@hedlinux serial $ ./serial
    Buffer: <˜††
    any help would be greatly appreciated...thanks!
    George

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It looks like buffer hasn't been initialized. But memset() initializes it.

    Try this code instead of your printf() to see if anything is working:

    Code:
    int x;
    for(x = 0; x < sizeof(buffer); x ++) {
        putchar(buffer[x]);
    }
    printf("\n");
    That ouput is if you send the same data over the port, right?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or better yet, why don't you pay attention to the return value of read?

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Now why didn't I see that.

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    4
    the return value of read is never -1 or 0

    any other ideas?

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by gnychis
    the return value of read is never -1 or 0

    any other ideas?

    If you want the bit rate to be 4800, then use B4800, (whose integer value may not be 4800).

    Code:
      speed_t baud = B4800;
    Also, you must set the CREAD bit in c_cflag (the value integer 4800 that you had used just happened to set the CREAD bit, otherwise nothing would have been received).

    so the setting for c_cflag would be something like this:

    Code:
     tp.c_cflag = CREAD|CS8|CLOCAL|baud;
    Regards,

    Dave
    Last edited by Dave Evans; 06-02-2005 at 09:54 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data structure for storing serial port data in firmware
    By james457 in forum C Programming
    Replies: 4
    Last Post: 06-15-2009, 09:28 AM
  2. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  3. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  4. read() is not reading the whole data
    By maven in forum C Programming
    Replies: 6
    Last Post: 02-18-2006, 07:15 AM
  5. opening and reading from a serial port...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 06-20-2002, 09:18 PM