Read from serial port only works in main... ??

This is a discussion on Read from serial port only works in main... ?? within the Linux Programming forums, part of the Platform Specific Boards category; I am writing code to read from the serial port on my linux machine. I go through the initialization and ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    26

    Read from serial port only works in main... ??

    I am writing code to read from the serial port on my linux machine. I go through the initialization and then when i call read(fd...) in the main function I can read the data correctly. Once I try to pass the fd to another function, so main is short and sweet, the data i get is garbage. I have been struggling with this error for so long that I have even tried doing all the init and reading in the function I want to handle the data and after everything is initialized properly, the read still gives me garbage.

    Code:
    void sample(int fd)
    {
    	int i;
    	char* buf = (char*)malloc(128);
    	for(i=0;i<100;i++) { read(fd, buf, 1); printf("%s", buf); fflush(stdout); }
    	
    	
    } // sample
    
    int init(int* fd)
    {
            struct termios oldtio, newtio;
            *fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
    	if (*fd <0) { perror(MODEMDEVICE); exit(-1); }
    	
    	tcgetattr(*fd,&oldtio); /* save current modem settings */
    	newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
    	newtio.c_iflag = IGNPAR;
    	newtio.c_oflag = 0;
    	newtio.c_lflag = 0;
    	newtio.c_cc[VMIN]=1;
    	newtio.c_cc[VTIME]=0;
    	tcflush(*fd, TCIFLUSH);
    	tcsetattr(*fd,TCSANOW,&newtio);
    
           return(1);
    } // init
    
    int main( int argc , char * argv[] )
    {
        int fd;
        char* buf = (char*)malloc(128);
        if (!init(&fd))
          return 1;
        for(i=0;i<100;i++) { read(fd, buf, 1); printf("%s", buf); fflush(stdout); } // works
    
        sample(fd); // fails
        close(fd);
    } // main
    I have printed out both file descriptors before I do my read and they are the same value. So is there something else that is the reason behind my issue?

    Thanks,
    Chris

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I can't see any reason why it fails in the sample and works in main, except perhaps that you are not including stdlib.h, which combined with the fact that you are casting the result of malloc may mean that the result from malloc looses some data (in a 64-bit OS).

    It would certainly help if you explain to use what you mean by "fails", since it can mean anything from "doesn't work as you expected" (if so, it would help if you tell us what happens, and how that is different from what you expect) to "crashes" (in which case we'd like to see what the crash message is).
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    26
    OK, I already am including stdlib. By garbage I mean when it works I get two actual values, and by fails i get this instead of the two digits

    183, 254 // a working value

    U@dU@1U@dU@9U@dU@1U@dU@,U@dU@ U@dU@-U@dU@2U@dU@5U@dU@4U@dU@ // garbage

    The only crash message is from the atof() function which the garbage above cannot be converted to a float.

    Chris

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    26
    Also, this is being compiled on an arm-linux machine.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ah, I didn't read your code very carefully. You are printing a string, and reading a single character. Since you are not actually setting an end-marker for your string, it prints whatever happens to be in buf until it finds a zero-value. Which apparently is quite far into the string. Try clearing the buffer to zero first.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    26
    matsp,

    Thank you for your hawk eye, I was debugging for hours and didn't realize that. That solved the issue. Can you explain to me why in main the buffer doesn't need to be set to all zeroes? Why does the buffer throw in all of that junk in between readings even though it was malloc'ed?

    Thanks again,
    Chris

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by chris24300 View Post
    matsp,

    Thank you for your hawk eye, I was debugging for hours and didn't realize that. That solved the issue. Can you explain to me why in main the buffer doesn't need to be set to all zeroes? Why does the buffer throw in all of that junk in between readings even though it was malloc'ed?

    Thanks again,
    Chris
    Because the content of a buffer after malloc is undefined - it can be just about anything. So as luck would have it, it happens to contain a zero in the right place when you call malloc in main.

    If you want to KNOW what is in your malloc buffer, you need to fill it (or call calloc() which fills it with zero).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Serial Port Questions
    By valaris in forum Tech Board
    Replies: 2
    Last Post: 05-22-2009, 08:26 AM
  2. Serial port Communication
    By vin_pll in forum C++ Programming
    Replies: 23
    Last Post: 01-07-2009, 08:32 AM
  3. HELP with storing serial port data into txt file
    By inmaterichard in forum C Programming
    Replies: 2
    Last Post: 04-02-2008, 02:20 AM
  4. PC104 Serial Port Programming Problem
    By outerspace in forum C Programming
    Replies: 6
    Last Post: 11-09-2005, 06:07 PM
  5. serial port still wont work
    By alcoholic in forum C++ Programming
    Replies: 6
    Last Post: 10-31-2001, 11:51 AM

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