Weird characters after device read

This is a discussion on Weird characters after device read within the Networking/Device Communication forums, part of the General Programming Boards category; Hi all. I am writing a simple program that reads from the serial port. I have got the communications working ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    10

    Weird characters after device read

    Hi all. I am writing a simple program that reads from the serial port. I have got the communications working file, but get weird characters after using the read command such as below:

    ׵sӵ������׹���������

    Any ideas what might cause this? I am not that familiar with C (from java background) and to me this looks like I should read this input stream as something else than character array. However from all documentation I have seen for the read function, it always reads the data to character array.

    Any pointers anyone?

    Regards,

    Olli

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    Do yo you get actual data prior to that? If so you need to null terminate the buffer after you've read it in order to treat it as a string.

    pseudocode:
    Code:
    bytesread=read into buffer
    if bytesread > 0
        buffer[bytesread] = 0
        print buffer

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    10
    My code is as below:

    Code:
    char data[160];
    int readbytes;
    
    readbytes = read(fd,data,160);
    while(readbytes>0)
    {
      cout << data << "\n";
      readbytes = read(fd,data,160);
    }
    I modified this to:

    Code:
    readbytes = read(fd,data,160);
    while(readbytes>0)
    {
      data[readbytes]= '\0';
      cout << data << "\n";
      readbytes = read(fd,data,160);
    }
    But still the same result.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,918
    What operating system are you using? Also insure the sending and receiving serial ports are configured for the same settings. Does your incoming data have any sort of delimiter? ie( '\r' or '\n'). Also you might want to print the character array char by char converting to int.
    Code:
       for(int i = 0; i < data.length(); ++i)
          printf("%d ",data[i]);
    You might then be able to determine what is going on.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    10
    Yeah it should return something like this:

    Plugwise protocol unleashed part 1: introduction | Maartendamen&#039;s blog

    So the data has "\r\n\0" as terminator. If I convert all characters to int one by one I seem to get range of negative values.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you get negative values back, presumably that means that all your data has the high bit set (extended ASCII, or characters > 128). (EDIT: Of course, hopefully that's what you want, otherwise you'll have to figure out how your device is sending the data.) (ANOTHER EDIT: If you want to post the first few, maybe someone will be able to recognize the characters, or see some pattern in what you're getting.)
    Last edited by tabstop; 05-28-2010 at 10:56 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    > readbytes = read(fd,data,160);
    160 is a maximum limit, not a demand.

    Unless you're reading from a fast device like a disk, chances are the OS will return whenever there is SOME data to be read.

    This typically means your buffer will only be partly filled.
    Also, few low level routines explicitly add a \0 to the end of the buffer for you (so you can't immediately treat the result as a proper C-string).

    Try
    Code:
    while ( (readbytes = read(fd,data,160)) > 0 ) {
      for ( i = 0 ; i < readBytes ; i++ ) {
        printf("%c", data[i] );  // if you're sure all the data is printable, otherwise use say %d or %x
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    10
    thank you all for your input.

    It seems like I get a different result back everytime(when converting characters to d or x), so starting to think that either there is something wrong with the device or maybe the way I write first to it in order to make it to respond.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    2,918
    You may have to cast your char to an int in the printf
    Code:
       printf("%d ", int(data[i]));

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    10
    Figured it out in the end. Was passing the port the wrong tty settings by accident, hence the bad character encoding.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    Nice job, and thanks for the update
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File read in problem
    By DimensionX in forum C Programming
    Replies: 5
    Last Post: 12-02-2009, 12:37 PM
  2. Using fgetc() to read control characters
    By Bnchs in forum C Programming
    Replies: 8
    Last Post: 03-22-2008, 06:23 AM
  3. confused with read()
    By _EAX in forum Linux Programming
    Replies: 2
    Last Post: 03-17-2008, 05:14 PM
  4. read characters
    By modec in forum C Programming
    Replies: 2
    Last Post: 08-19-2003, 12:58 PM
  5. Replies: 4
    Last Post: 06-21-2002, 03:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21