Thread: Wirte and read serial port

  1. #31
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by ZuK View Post
    Then you should change
    Code:
       if (fd == -1) {                                              /* Could not open the port */
         fprintf(stderr, "open_port: Unable to open /dev/ttyUSB0 - %s\n",
                 strerror(errno));
       }
    to

    Code:
       if (fd < 0) {                                              /* Could not open the port */
         fprintf(stderr, "open_port: Unable to open /dev/ttyUSB0 - %s\n",
                 strerror(errno));
       }
    Kurt
    Still the same kurt. still have bad file Bad file descriptor error.
    Last edited by Marvin Gorres; 04-14-2013 at 10:52 AM.

  2. #32
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Did you miss seeing this post; because the question it raises needs answered!

    Tim S.

    Quote Originally Posted by jimblumberg View Post
    So what is fd in the following snippet?
    Code:
     mainfd = open_port();
     
    
     write(fd,"\xF5\x01\x00\x01\x03\x00\x03\xF5",8);
    The port file descriptor looks to be be mainfd, not fd.

    Jim
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #33
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by jimblumberg View Post
    So what is fd in the following snippet?
    Code:
     mainfd = open_port();
     
    
     write(fd,"\xF5\x01\x00\x01\x03\x00\x03\xF5",8);
    The port file descriptor looks to be be mainfd, not fd.

    Jim
    OMG! what a stupid mistakes i made! thanks for pointing it out jimblumberg! ^^.

    Another prob is, suppose i get this output from the device "\0xf5\0x01\0x00\0x00\0x00\0x00\0x01\0xf5" but unfortunately not. Here what i get: Got �.

  4. #34
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please repost your current code.

    Jim

  5. #35
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by stahta01 View Post
    Did you miss seeing this post; because the question it raises needs answered!

    Tim S.
    yea i was just replying them both at the same time.^^

  6. #36
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Quote Originally Posted by Marvin Gorres View Post
    OMG! what a stupid mistakes i made! thanks for pointing it out jimblumberg! ^^.
    Code:
    $ gcc -Wall foo.c
    foo.c:29:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
    foo.c: In function ‘main’:
    foo.c:57:7: warning: ‘fd’ is used uninitialized in this function [-Wuninitialized]
    Having learnt a lesson, turn on as many warnings as you can.

    > Another prob is, suppose i get this output from the device "\0xf5\0x01\0x00\0x00\0x00\0x00\0x01\0xf5" but unfortunately not. Here what i get: Got �.
    You need to do things like
    printf("%02x\n", ch );

    Printing binary data with %c will display random glyphs.

    You also need to read in multiple chars, not just one.
    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.

  7. #37
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by jimblumberg View Post
    Please repost your current code.

    Jim
    Here it is

    Code:
    
    
     #include <stdio.h>   /* Standard input/output definitions */
     #include <string.h>  /* String function definitions */
     #include <unistd.h>  /* UNIX standard function definitions */
     #include <fcntl.h>   /* File control definitions */
     #include <errno.h>   /* Error number definitions */
     #include <termios.h> /* POSIX terminal control definitions */
    
     /*
      * 'open_port()' - Open serial port 1.
      *
      * Returns the file descriptor on success or -1 on error.
      */
    
     int open_port(void)
     {
       int fd;                                   /* File descriptor for the port */
    
       fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
    
       if (fd < 0) {                                              /* Could not open the port */
         fprintf(stderr, "open_port: Unable to open /dev/ttyUSB0 - %s\n",
                 strerror(errno));
       }
    
       return (fd);
     }
    
    void main()
    {
     int mainfd=0,fd;                                            /* File descriptor */
     char chout, result, cnt;
     struct termios options;
       
     mainfd = open_port();
    
     fcntl(mainfd, F_SETFL, FNDELAY);                  /* Configure port reading */
                                         /* Get the current options for the port */
     tcgetattr(mainfd, &options);
     cfsetispeed(&options, B19200);                 /* Set the baud rates to 19200 */
     cfsetospeed(&options, B19200);
        
                                       /* 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);
           
                                            /* Set the new options for the port */
     tcsetattr(mainfd, TCSANOW, &options);
    
     result = tcsetattr(mainfd, TCSANOW, &options);
     if ( result == -1 ) { perror("error: tcsetattr"); }
     if ( result != 0 ) { fprintf(stderr,"error in tcsetattr, result = %d\n", result); }
    
     result = write(mainfd,"\xF5\x01\x00\x01\x03\x00\x03\xF5",8);
     if ( result == -1 ) { perror("error: write"); }
     if ( result != 8 ) { fprintf(stderr,"error in write, result = %d\n", result); }
      
     usleep(5000000);      /* wait for 5 second*/
     
     result = read(mainfd, &chout, sizeof(chout));          /* Read character from ABU (Auto buffering Unit) */
     if ( result == -1 ) { perror("error: read"); }
     if ( result != 1 ) { fprintf(stderr,"error in read, result = %d\n", result); }
       
       if (chout != 0)
          printf("Got %c.\n", chout);
    
       chout=0;
       
     
                                                        /* Close the serial port */
      close(mainfd);
     }

  8. #38
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by Salem View Post
    Code:
    $ gcc -Wall foo.c
    foo.c:29:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
    foo.c: In function ‘main’:
    foo.c:57:7: warning: ‘fd’ is used uninitialized in this function [-Wuninitialized]
    Having learnt a lesson, turn on as many warnings as you can.

    > Another prob is, suppose i get this output from the device "\0xf5\0x01\0x00\0x00\0x00\0x00\0x01\0xf5" but unfortunately not. Here what i get: Got �.
    You need to do things like
    printf("%02x\n", ch );

    Printing binary data with %c will display random glyphs.

    You also need to read in multiple chars, not just one.
    I tried to use this "printf("%02x\n", ch );" instead of %c but no data display. i only loop it once first, just to try.

    How can i display the output in a new file?

  9. #39
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Another prob is, suppose i get this output from the device "\0xf5\0x01\0x00\0x00\0x00\0x00\0x01\0xf5" but unfortunately not. Here what i get: Got �.
    Your only reading a single signed character, not a string. Also 0xF5 is outside the size that can be held in a signed character, you should be using an unsigned char not a char.

    If you want to read more than this single character I recommend a loop.

    Jim

  10. #40
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by jimblumberg View Post
    Your only reading a single signed character, not a string. Also 0xF5 is outside the size that can be held in a signed character, you should be using an unsigned char not a char.

    If you want to read more than this single character I recommend a loop.

    Jim
    u mean like looping while storing them into an unsigned char array? hurm, my C prog not really good, mind giving me an example?

  11. #41
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    u mean like looping while storing them into an unsigned char array?
    Yep. But you can start by receiving and printing each character as you receive it.

    urm, my C prog not really good, mind giving me an example?
    You've already done harder, give it a try and if you have problems post your new code and ask specific questions.

    Jim

  12. #42
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by jimblumberg View Post
    Yep. But you can start by receiving and printing each character as you receive it.



    You've already done harder, give it a try and if you have problems post your new code and ask specific questions.

    Jim
    Hey Jim,i did the unsigned array and it does display the CORRECT data. ^^, Thnx to you bro. Any further question i'll update here. Thanks alot man and also all other member. peace.

    Code:
     #include <stdio.h>   /* Standard input/output definitions */
     #include <string.h>  /* String function definitions */
     #include <unistd.h>  /* UNIX standard function definitions */
     #include <fcntl.h>   /* File control definitions */
     #include <errno.h>   /* Error number definitions */
     #include <termios.h> /* POSIX terminal control definitions */
    
     /*
      * 'open_port()' - Open serial port 1.
      *
      * Returns the file descriptor on success or -1 on error.
      */
    
     int open_port(void)
     {
       int fd;                                   /* File descriptor for the port */
    
       fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
    
       if (fd < 0) {                                              /* Could not open the port */
         fprintf(stderr, "open_port: Unable to open /dev/ttyUSB0 - %s\n",
                 strerror(errno));
       }
    
       return (fd);
     }
    
    void main()
    {
     int mainfd=0;                                            /* File descriptor */
     char cnt, result2;
     unsigned char result[8],chout[8];
     int i;
     struct termios options;
       
     mainfd = open_port();
    
     fcntl(mainfd, F_SETFL, FNDELAY);                  /* Configure port reading */
                                         /* Get the current options for the port */
     tcgetattr(mainfd, &options);
     cfsetispeed(&options, B19200);                 /* Set the baud rates to 19200 */
     cfsetospeed(&options, B19200);
        
                                       /* 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);
           
                                            /* Set the new options for the port */
     tcsetattr(mainfd, TCSANOW, &options);
    
     result2 = tcsetattr(mainfd, TCSANOW, &options);
     if ( result2 == -1 ) { perror("error: tcsetattr"); }
     if ( result2 != 0 ) { fprintf(stderr,"error in tcsetattr, result = %d\n", result2); }
    
     result2 = write(mainfd,"\xF5\x01\x00\x01\x03\x00\x03\xF5",8);
     if ( result2 == -1 ) { perror("error: write"); }
     if ( result2 != 8 ) { fprintf(stderr,"error in write, result = %d\n", result2); }
      
     usleep(3000000);      /* wait for 5 second*/
     for(i=0;i<8;i++)
    {
     result[i] = read(mainfd, &chout[i], sizeof(chout));          /* Read character from ABU (Auto buffering Unit) */
     if ( result[i] == -1 ) { perror("error: read"); }
     if ( result[i] != 1 ) { fprintf(stderr,"error in read, result = %d\n", result[i]); }
       
     if (chout[i] != 0)
    
          printf("%02x\n",chout[i]);
    
    }
    
          
     
                                                        /* Close the serial port */
      close(mainfd);
     }
    Last edited by Marvin Gorres; 04-14-2013 at 12:14 PM.

  13. #43
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why are you using an array for the return values? A single value is all you need. And it should not be a char or unsigned char. You would be better off with an int.
    Code:
    result[i] = read(mainfd, &chout[i], sizeof(chout));          /* Read character from ABU (Auto buffering Unit) */
     if ( result[i] == -1 ) { perror("error: read"); }
    Jim

  14. #44
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by jimblumberg View Post
    Why are you using an array for the return values? A single value is all you need. And it should not be a char or unsigned char. You would be better off with an int.
    Code:
    result[i] = read(mainfd, &chout[i], sizeof(chout));          /* Read character from ABU (Auto buffering Unit) */
     if ( result[i] == -1 ) { perror("error: read"); }
    Jim
    Ok thanks for the advice! ^^,
    Last edited by Marvin Gorres; 04-14-2013 at 09:33 PM.

  15. #45
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by jimblumberg View Post
    Why are you using an array for the return values? A single value is all you need. And it should not be a char or unsigned char. You would be better off with an int.
    Code:
    result[i] = read(mainfd, &chout[i], sizeof(chout));          /* Read character from ABU (Auto buffering Unit) */
     if ( result[i] == -1 ) { perror("error: read"); }
    Jim
    Hello, i got a problem now, i need to receive data more than 8 byte which is 111 byte and i tried to loop it 111 times the same thing i did to receive the 111 byte data but i did not receive the correct data. I've uploaded a picture of the 111 byte data which i get by using cutecom software.

    Wirte and read serial port->8byte-jpg

    Really need help on this part.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read in from serial port; getchar()
    By s.dodd in forum C Programming
    Replies: 9
    Last Post: 10-23-2011, 10:50 AM
  2. Can't Read From Serial Port
    By HalNineThousand in forum Linux Programming
    Replies: 14
    Last Post: 03-20-2008, 05:56 PM
  3. serial port read
    By mackrackit in forum C++ Programming
    Replies: 6
    Last Post: 03-22-2006, 01:40 AM
  4. How to read from a serial port
    By WDT in forum C++ Programming
    Replies: 6
    Last Post: 05-10-2004, 06:31 AM
  5. Serial port read..can someone tell me..
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-27-2002, 08:21 AM

Tags for this Thread