Thread: Wirte and read serial port

  1. #16
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by rcgldr View Post
    Not likely, but is there any chance you have the C compiler setup to use unicode (16 bit characaters) instead of Ascii?
    I'm using linux and i compile using terminal.

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.
    Well post the error message(s) that do get printed.

    In fact, use this
    Code:
     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(fd,"\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); }
    What happens in Cutecom if you set the char delay (bottom right) to zero?
    At 19200 baud, it takes ~500uS to transmit each char, so a 1mS delay is quite significant (something your C code isn't doing).
    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.

  3. #18
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by Salem View Post
    > 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.
    Well post the error message(s) that do get printed.

    In fact, use this
    Code:
     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(fd,"\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); }
    What happens in Cutecom if you set the char delay (bottom right) to zero?
    At 19200 baud, it takes ~500uS to transmit each char, so a 1mS delay is quite significant (something your C code isn't doing).
    I changed the ms delay to 0 or any number and it still works. Anyway, how to make the char delay in C?

    I also got this when running the code with additional of your code:
    error: write: Bad file descriptor
    error in write, result = -1
    error: read: Resource temporarily unavailable
    error in read, result = -1

    p/s: When the device respond, it will send back this "\0xf5\0x01\0x00\0x00\0x00\0x00\0x01\0xf5" to me then only i read.
    Last edited by Marvin Gorres; 04-14-2013 at 08:22 AM.

  4. #19
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Marvin Gorres View Post
    I also got this when running the code with additional of your code:
    error: write: Bad file descriptor
    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

  5. #20
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Looking at the picture of your Cutecom window it appears you may be running this program as root, looking at the log location. Do you need "root" access to access your USB port?

    Jim

  6. #21
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by jimblumberg View Post
    Looking at the picture of your Cutecom window it appears you may be running this program as root, looking at the log location. Do you need "root" access to access your USB port?

    Jim
    Yes Jim,i need to be root to access my usb port.

  7. #22
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Are you running your program as root?


    You may want to consider changing you user settings to allow using the ttyUSB port as non-root. Try adding the user to the dialout group.


    Jim

  8. #23
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Marvin Gorres View Post
    Yes Jim,i need to be root to access my usb port.
    Are you running your C program as root?

    Tim S.
    "...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

  9. #24
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by stahta01 View Post
    Are you running your C program as root?

    Tim S.
    yes i compile and run it as root.

  10. #25
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by jimblumberg View Post
    Are you running your program as root?


    You may want to consider changing you user settings to allow using the ttyUSB port as non-root. Try adding the user to the dialout group.


    Jim
    Well,i compile and run my code in root.

  11. #26
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Running your code as root is a dangerous thing to do. You really should change your settings to allow non-root access to the port.

    Jim

  12. #27
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by jimblumberg View Post
    Running your code as root is a dangerous thing to do. You really should change your settings to allow non-root access to the port.

    Jim
    how do i do that? sorry i dont know.

  13. #28
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    I found this link when searching Google for "Ubuntu USB access non-root", Since I don't run Ubuntu I don't really know what one of the answers will solve the problem. But on my system (Mint) there is a GUI program (system settings) that allows me to edit user profiles to add the dialup group to the user.

    Jim

  14. #29
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by jimblumberg View Post
    I found this link when searching Google for "Ubuntu USB access non-root", Since I don't run Ubuntu I don't really know what one of the answers will solve the problem. But on my system (Mint) there is a GUI program (system settings) that allows me to edit user profiles to add the dialup group to the user.

    Jim
    well thanks for the link but i'm still focusing on the main problem now which is to send the hex command properly to the port.

  15. #30
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    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

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