Find the answer to your Linux question:
Results 1 to 2 of 2
Hi, I need to interface a device (send a string and receive and validate replies) with follwing rs232 properties. 8 data bits, 1 start bit, 1 stop bit, and no ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2010
    Location
    Planet Earth
    Posts
    18

    Question interface rs232


    Hi,

    I need to interface a device (send a string and receive and validate replies) with follwing rs232 properties.
    8 data bits, 1 start bit, 1 stop bit, and no parity.
    Default baud rate is 19200.

    I've so far tried this with no success. Data sent doesn't seem to be correct and the select() never fires...
    Any hints, tips and suggestions are appreciated!!!

    Code:
    #include <stdio.h> // standard input / output functions
    #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 definitionss
    #include <time.h>   // time calls
    
    
    int open_port(void)
    {
    int fd; // file description for the serial port
    
    fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
    
    if(fd == -1) // if open is unsucessful
    {
     perror("open_port: Unable to open /dev/ttyUSB0");
    }
    else
    {
     fcntl(fd, F_SETFL, 0);
    }
    
    return(fd);
    }
    
    int configure_port(int fd)      // configure the port
    {
    struct termios port_settings;      // structure to store the port settings in
    
    cfsetispeed(&port_settings, B19200);    // set baud rates
    cfsetospeed(&port_settings, B19200);
    
    port_settings.c_cflag &= ~PARENB;    // set no parity, stop bits, data bits
    port_settings.c_cflag &= ~CSTOPB;
    port_settings.c_cflag &= ~CSIZE;
    port_settings.c_cflag |= CS8;
    
    cfmakeraw(&port_settings);
    tcsetattr(fd, TCSANOW, &port_settings);    // apply the settings to the port
    return(fd);
    
    }
    
    int query_modem(int fd)   // query modem with an AT command
    {
    int n;
    fd_set rdfs;
    struct timeval timeout;
    
    // initialise the timeout structure
    timeout.tv_sec = 10; // ten second timeout
    timeout.tv_usec = 0;
    
    if (FD_ISSET(fd, &rdfs)){
      FD_ZERO(&rdfs);
      FD_CLR(fd,&rdfs);
    }
    
    write(fd, "LAMPS 1\r", 8);  // send an AT command followed by a CR
    
    // do the select
    n = select(fd + 1, &rdfs, NULL, NULL, &timeout);
    
    // check if an error has occured
    if(n < 0)
    {
     perror("select failed\n");
    }
    else if (n == 0)
    {
     puts("Timeout!");
    }
    else
    {
     printf("\nBytes detected on the port!\n");
    }
    
    }
    
    int main(void)
    {
    int fd = open_port();
    configure_port(fd);
    query_modem(fd);
    return(0);
    }

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,397
    You aren't setting up the fd_set rdfs properly. Instead of this:
    Code:
    if (FD_ISSET(fd, &rdfs)){
      FD_ZERO(&rdfs);
      FD_CLR(fd,&rdfs);
    }
    do this instead:
    Code:
    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);
    The way you did it, you aren't asking to be informed when there is input to be read.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •