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; Hello guys,i've a code which objective is to send Hex command to the serial port and then wait for 5 ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    66

    Wirte and read serial port

    Hello guys,i've a code which objective is to send Hex command to the serial port and then wait for 5 second. Then, read data output from the serial port. But my code doesn't works well. Please help me guys. Thanks in advanced.

    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 == -1)
       {                                              /* 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;
     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);
    
     write(fd,"\xF5\x01\x00\x01\x03\x00\x03\xF5",8);
     
     usleep(5000000);      /* wait for 5 second*/
    
     read(mainfd, &chout, sizeof(chout));          /* Read character from ABU (Auto buffering Unit) */
       
       if (chout != 0)
          printf("Got %c.\n", chout);
    
       chout=0;
       
     
                                                        /* Close the serial port */
      close(mainfd);
     }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,753
    But my code doesn't works well.
    So what exactly is wrong with your code?

    Jim
    stahta01 likes this.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by jimblumberg View Post
    So what exactly is wrong with your code?

    Jim

    when i send the command to the device (ttyUSB0), the device did not respond.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    termios(3) - Linux man page
    Note that tcsetattr() returns success if any of the requested changes could be successfully carried out. Therefore, when making multiple changes it may be necessary to follow this call with a further call to tcgetattr() to check that all changes have been performed successfully.
    > tcsetattr(mainfd, TCSANOW, &options);
    > write(fd,"\xF5\x01\x00\x01\x03\x00\x03\xF5",8);
    > read(mainfd, &chout, sizeof(chout)); /* Read character from ABU (Auto buffering Unit) */
    ALL of these return a result.
    It's worth at least printing this out (or seeing what it is with a debugger), to find out where it might be going wrong.
    stahta01 likes this.
    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.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by Salem View Post
    termios(3) - Linux man page


    > tcsetattr(mainfd, TCSANOW, &options);
    > write(fd,"\xF5\x01\x00\x01\x03\x00\x03\xF5",8);
    > read(mainfd, &chout, sizeof(chout)); /* Read character from ABU (Auto buffering Unit) */
    ALL of these return a result.
    It's worth at least printing this out (or seeing what it is with a debugger), to find out where it might be going wrong.

    Actually when i send the command to the port,the device will light up but when i tried it didn't light up.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    Quote Originally Posted by Marvin Gorres View Post
    Actually when i send the command to the port,the device will light up but when i tried it didn't light up.
    And how does that tell you any more than checking all your functions for possible errors?
    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. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by Salem View Post
    And how does that tell you any more than checking all your functions for possible errors?
    I compile the code and does not have any error so i think something might wrong with the command i send, maybe there's any other format to send the hex command to the port? I used cutecom to send the command and it works really well but i'm thinking of using C.

  8. #8
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    138
    Because your open statement uses "/dev/ttyUSB0", I assume that you are using a *nix operating system with a USB simulated serial port. It is not clear from the information you provided if the basics have been tried. So here are some questions that you may have already answered for yourself.

    Do you know that your wiring between the USB serial port on your *nix box and your external device is correct? I would test it using a "dumb terminal" program on your *nix box. Can you use a "dumb terminal" to send the bytes and see a result?

    If this is more than a one time event and your wiring is not right, consider getting a "break out" box to debug any wiring issues. A "break out" box is a hardware device that allows one to test and debug the wiring design. It has pins and jumper wires to allow one to connect any pin to any pin.

    Do you have a driver for the simulated USB serial port that supports the primitives you are trying to use (and/or the termios for which Salem pointed you at the documentation)?

    Do you know that you have your software (or the dumb terminal) configured to match the serial configuration on your external device? It appears that you are trying to configure for 19200 bits per second, 8 data bits, no parity, and probably one stop bit. Does this match the configuration set (or preset) in your external device?

    When you know the wiring and device driver work, you can start to test and debug your software. While your are debugging, you may want to substitute the external device with a computer running a dumb terminal.

    I hope this helps.
    Last edited by pheininger; 04-13-2013 at 12:07 PM. Reason: missing verb
    ZuK and stahta01 like this.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    2,753
    Another thing to check, can you using a terminal program such as mini-com or gtkterm to transmit these bytes to your embedded system?


    Jim

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by pheininger View Post
    Because your open statement uses "/dev/ttyUSB0", I assume that you are using a *nix operating system with a USB simulated serial port. It is not clear from the information you provided if the basics have been tried. So here are some questions that you may have already answered for yourself.

    Do you know that your wiring between the USB serial port on your *nix box and your external device is correct? I would test it using a "dumb terminal" program on your *nix box. Can you use a "dumb terminal" to send the bytes and see a result?

    If this is more than a one time event and your wiring is not right, consider getting a "break out" box to debug any wiring issues. A "break out" box is a hardware device that allows one to test and debug the wiring design. It has pins and jumper wires to allow one to connect any pin to any pin.

    Do you have a driver for the simulated USB serial port that supports the primitives you are trying to use (and/or the termios for which Salem pointed you at the documentation)?

    Do you know that you have your software (or the dumb terminal) configured to match the serial configuration on your external device? It appears that you are trying to configure for 19200 bits per second, 8 data bits, no parity, and probably one stop bit. Does this match the configuration set (or preset) in your external device?

    When you know the wiring and device driver work, you can start to test and debug your software. While your are debugging, you may want to substitute the external device with a computer running a dumb terminal.

    I hope this helps.
    First of all,im using linux. Like i said previously, i used a software which is Cutecom to communicate with this device through serial port and it does works well. When the hex command sent to the device, it operates really well, so i dont think it's about wiring problem or serial port configuration problem at all. Problem is when i write in C using the same configuration in Cutecom and tried to send the HEX command, the device does not works well. The C code does not have any error though. That's the main problem now.

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by jimblumberg View Post
    Another thing to check, can you using a terminal program such as mini-com or gtkterm to transmit these bytes to your embedded system?


    Jim
    Yes i checked it already using Cutecom as i'm using linux. It works. Only in C code when i tried to send the HEX command to the device,not working.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    > The C code does not have any error though. That's the main problem now.
    So you are doing this now?

    Code:
     result = tcsetattr(mainfd, TCSANOW, &options);
     if ( result != 0 ) { fprintf(stderr,"error in tcsetattr\n");
     
     result write(fd,"\xF5\x01\x00\x01\x03\x00\x03\xF5",8);
     if ( result != 8 ) { fprintf(stderr,"error in write\n");
      
     usleep(5000000);      /* wait for 5 second*/
     
     result = read(mainfd, &chout, sizeof(chout));          /* Read character from ABU (Auto buffering Unit) */
     if ( result != 1 ) { fprintf(stderr,"error in read\n");
    The lack of compilation errors is just the first small step, getting it to work is where the real challenges start (especially when devices are involved).
    The number of possible programs that compile is a near infinite set, whereas the number of programs which work as intended is exceedingly finite.

    > Yes i checked it already using Cutecom
    Is there any way you can show us your Cutecom serial line settings, and the data you send?
    stahta01 likes this.
    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.

  13. #13
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by Salem View Post
    > The C code does not have any error though. That's the main problem now.
    So you are doing this now?

    Code:
     result = tcsetattr(mainfd, TCSANOW, &options);
     if ( result != 0 ) { fprintf(stderr,"error in tcsetattr\n");
     
     result write(fd,"\xF5\x01\x00\x01\x03\x00\x03\xF5",8);
     if ( result != 8 ) { fprintf(stderr,"error in write\n");
      
     usleep(5000000);      /* wait for 5 second*/
     
     result = read(mainfd, &chout, sizeof(chout));          /* Read character from ABU (Auto buffering Unit) */
     if ( result != 1 ) { fprintf(stderr,"error in read\n");
    The lack of compilation errors is just the first small step, getting it to work is where the real challenges start (especially when devices are involved).
    The number of possible programs that compile is a near infinite set, whereas the number of programs which work as intended is exceedingly finite.

    > Yes i checked it already using Cutecom
    Is there any way you can show us your Cutecom serial line settings, and the data you send?
    Name:  Screenshot from 2013-04-14 14:43:52.jpg
Views: 738
Size:  50.0 KB

    Above is the cutecom setting and in the middle is the response from the device while at the bottom is the command sent. Below is a wrong picture ignore it.
    Attached Images Attached Images  
    Last edited by Marvin Gorres; 04-14-2013 at 12:51 AM.

  14. #14
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by Salem View Post
    > The C code does not have any error though. That's the main problem now.
    So you are doing this now?

    Code:
     result = tcsetattr(mainfd, TCSANOW, &options);
     if ( result != 0 ) { fprintf(stderr,"error in tcsetattr\n");
     
     result write(fd,"\xF5\x01\x00\x01\x03\x00\x03\xF5",8);
     if ( result != 8 ) { fprintf(stderr,"error in write\n");
      
     usleep(5000000);      /* wait for 5 second*/
     
     result = read(mainfd, &chout, sizeof(chout));          /* Read character from ABU (Auto buffering Unit) */
     if ( result != 1 ) { fprintf(stderr,"error in read\n");
    The lack of compilation errors is just the first small step, getting it to work is where the real challenges start (especially when devices are involved).
    The number of possible programs that compile is a near infinite set, whereas the number of programs which work as intended is exceedingly finite.

    > Yes i checked it already using Cutecom
    Is there any way you can show us your Cutecom serial line settings, and the data you send?
    I tried the code to check error you gave me and it appears that there's an error in writing and reading,but sadly i really don;t know how to solve this.

  15. #15
    Registered User
    Join Date
    Apr 2013
    Posts
    1,284
    Not likely, but is there any chance you have the C compiler setup to use unicode (16 bit characaters) instead of Ascii?

Page 1 of 7 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, 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, 12: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


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