Like Tree30Likes

Wirte and read serial port

This is a discussion on Wirte and read serial port within the C Programming forums, part of the General Programming Boards category; Originally Posted by ZuK Then you should change Code: if (fd == -1) { /* Could not open the 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 11:52 AM.

  2. #32
    Registered User
    Join Date
    May 2009
    Posts
    2,745
    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
    Marvin Gorres likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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
    2,886
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    2,886
    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
    Marvin Gorres likes this.

  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
    2,886
    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
    Marvin Gorres likes this.

  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 01:14 PM.

  13. #43
    Registered User
    Join Date
    May 2010
    Posts
    2,886
    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 10: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.

    Name:  >8byte.jpg
Views: 144
Size:  57.2 KB

    Really need help on this part.

Page 3 of 7 FirstFirst 1234567 LastLast
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, 11:50 AM
  2. Can't Read From Serial Port
    By HalNineThousand in forum Linux Programming
    Replies: 14
    Last Post: 03-20-2008, 06: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, 07:31 AM
  5. Serial port read..can someone tell me..
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-27-2002, 09:21 AM

Tags for this Thread


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