Find the answer to your Linux question:
Results 1 to 5 of 5
Hi at all and very compliments for the great forum! I have a little (big) problem with a program, because open the comunication, read buffer, but returns strange characters. I ...
  1. #1
    Just Joined!
    Join Date
    Mar 2010
    Posts
    8

    Red face [SOLVED] C++ serial comunication problem

    Hi at all and very compliments for the great forum!

    I have a little (big) problem with a program, because
    open the comunication, read buffer, but returns
    strange characters.
    I think it's a ttyS0 configuration,
    for comunicate i want to use this parameters:

    Baud : 4800
    Parity : Event
    StopBit : 1
    No flow control data bits 7

    This is the code :

    Code:
    // simple program to open the comm port, read data, save the raw data to a file.
    //
    // accumulates statistics for each group of 1,000 reads
    //
    // for each 1,000 reads, displays the number of bytes received
    // and the largest number of bytes in a single read
     
    #include <cerrno>
    #include <cstdio>
    #include <cstdlib>
    #include <fcntl.h>
    #include <iostream>
    #include <memory.h>
    #include <termios.h>
    #include <sys/ioctl.h>
     
    int main(int argc, char **argv)
    {
     int fd_port, fd_file;
     int max_read;    // maximum read line (in 1000 reads)
     int read_count;  // number of port reads
     int total_read;  // total bytes received from the port
     int bytes_read;  // total bytes received (in 1000 reads)
     
     // allow setting vmin and vtime from the command line
     int vmin  = (argc < 3) ? 1 : atoi(argv[1]);
     int vtime = (argc < 3) ? 0 : atoi(argv[2]);
     
     struct termios options;
     
     // open output file
     fd_file = open("comm2.bin", O_WRONLY|O_CREAT);
     fprintf(stderr, "fd_file %d\n", fd_file);
     
     // open comm 2
     fd_port = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NONBLOCK);
     fprintf(stderr, "fd_port %d\n", fd_port);
     
     // Configure port settings
     fcntl(fd_port, F_SETFL, O_NONBLOCK * 0 );
     
     // Save current port settings so we don't corrupt anything on exit
     
     memset(&options,0,sizeof(options));
     tcgetattr(fd_port, &options);
     options.c_cc[VMIN] = vmin;
     options.c_cc[VTIME] = vtime; // t * 0.1s
     fprintf(stderr, "vmin %d, vtime %d\n", options.c_cc[VMIN], options.c_cc[VTIME]);
     
     options.c_iflag |= PARMRK; // 0000010
     
    // set baud rate
     cfsetispeed(&options, B4800);
     cfsetospeed(&options, B4800);
     
     options.c_cflag |= (CLOCAL | CREAD);
     
     // clear mark/space parity
     options.c_cflag &= ~CMSPAR;
     
     // clear character size, stop bit count, and parity setting
     options.c_cflag &= ~(CSIZE|CSTOPB|PARENB|PARODD);
     
     // No parity, 1 stop bit (8N1), 8 data bits.
     options.c_cflag |= CS7;
     
     // Turn off hardware flow control
    // options.c_cflag &= ~CRTSCTS;
     
     // Turn on hardware flow control
     options.c_cflag |= CRTSCTS;
     
     options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
     
     tcflush(fd_port, TCIFLUSH);
     
     // Write changes to the port configuration
     tcsetattr(fd_port, TCSANOW, &options);
     
     /* Drop RTS to enable receiver */
     int flags = TIOCM_RTS;
     ioctl( fd_port, TIOCMBIC, &flags );
     
     // initialize counters
     max_read = read_count = total_read = bytes_read = 0;
     
     while (total_read < 200000)
     {
      unsigned char buff[256];
     
      // read bytes
      int cnt = read(fd_port, &buff, sizeof(buff));
     
      // save bytes
      write(fd_file, &buff, cnt);
     
      // accumulate statistics
      if (cnt > 0)
      {
       read_count += 1;
       bytes_read += cnt;
       total_read += cnt;
       if (cnt > max_read)
        max_read = cnt;
       // display statistics every 1000 reads
       if (read_count == 1000)
       {
        printf( "max: %3d, tot: %5d\n", max_read, bytes_read );
        max_read = read_count = bytes_read = 0;
       }
      }
     }
     
     // close comm port
     close(fd_port);
     
     // close output file
     close(fd_file);
     
     return 0;
    }
    Can anyone help me to set this parameter in this code?

    Thanks at all

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Tell us a bit more about what you are communicating with, and why you think the received data contains "strange" characters.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Mar 2010
    Posts
    8
    I want to comunicate via serial port with a laser,
    this return this type of string:

    DA12543789

    Received data contains strange character because
    the received typical string is (witch CS8 setting) :

    DA30...t.F.

    With CS7 setting :

    AwIudf89 u73jsa. q927qw30rhasvp

    But i don't understand why, if i open the comport with
    a terminal i can see the correct string

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    What settings does the terminal use? IE, what are the default settings on /dev/ttyS1? You can see with the terminal command stty </dev/ttyS1.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    Mar 2010
    Posts
    8

    Lightbulb

    Thanks for all, i have solve my problem.

    I miss to insert the correct parity configuration

    Code:
    options.c_cflag |= PARENB;
    and i miss to clear the buffer after reading,
    for this the array contains strange character.

    Code:
    //clear buffer
      for(int index = 0; index < 256 ; index++)
      {
       buff[index] = NULL;
      }
    See you

Posting Permissions

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