RS232 programming problem

[LEFT]Hi all

I encountered a strange phenomenon when reading / writing to RS232 serial device (on my machine /dev/ttyS0)

I have simple 2 processes:

1) process which WRITE characters from /dev/ttyS0
For example write the characters "aa102222233333444445555566666777778888899999aaaaabbbbbcccccddddd"

2) Program which READ characters from /dev/ttyS0 ( the process is blocked when nothing to be read)

I short the TX and the RX pins in my RS232 PC port (the 9 pins serial connector) so I 'll be able to read what I write.

The problem is:

when WRITE process write something like "aa102222233333444445555566666777778888899999aaaaabbbbbcccccddddd"

The READ process reads some time read ONLY part of the above characters like: "8888899999aaaaabbbbbcccccdddddaa102222233333"

Can you please help me to understand what is wrong?

code:

Process write:
----------------

#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 */

int main()
{
      int fd; /* File descriptor for the port */

      // num of read characters
      int n = 0;
    
      const char* serial_device = "/dev/ttyS0";
    
     fd = open( serial_device , O_WRONLY | O_NOCTTY | O_NDELAY);


      if (fd == -1)
      {
               /*
        * Could not open the port.
        */
          printf("open_port: Unable to open %s \n" , serial_device);
      }
   


       const char* sentance = "aa102222233333444445555566666777778888899999aaaaabbbbbcccccddddd";

    printf("About to write %s \n to UART on %s \n" , sentance ,  serial_device);

        n = write(fd, sentance , strlen(sentance));
    
        if (n < 0)
    {
          fputs("write() byte failed!\n", stderr);
    }
    else
    {
        
        printf("writting %d bytes to UART on %s \n" , n , serial_device);
    }


    close(fd);
    return 0;
}




Process read:
---------------

#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 */
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include "signal.h"
#include <assert.h>

#define N 1000

int main()
{
      int fd; /* File descriptor for the port */
    
        
      char array[N] = {'\n'};

      // num of read characters
      int n = 0;
    
    
      const char* serial_device = "/dev/ttyS0";
    

      
      fd = open(serial_device, O_RDONLY | O_NOCTTY );

      if (fd == -1)   
      {
           /*
          * Could not open the port.
         */
          printf("open_port: Unable to open %s \n" , serial_device);
          
      }
      else
      {
          fcntl(fd, F_SETFL, 0);
      }

    
      printf("Reading process form UART on %s \n" , serial_device);

    
    while (1)
    {
        printf("Begin to read\n");
        fflush(stdout);


        n = read(fd, array , 500 );

        if (n >= 0)
        {
            array[n]='\n';
        }
        
        if (n < 0)
        {
              fputs("read() failed!\n", stderr);
        }
        else
        {
        
            printf("The content of the Serial port is: %s len is %d \n" , array , n);
            fflush(stdout);

            n = 0;
    
    
        }

    }


    close(fd);
    return 0;
}

[/LEFT]

  1. You should start the READ process first.

  2. When you do the read, you're trying to read 500 bytes. It never gets to 500. So I'm not sure how your program outputs what it does. If you want, you can read 1 byte at a time and collect the input into a string which you output after it's 20 bytes long or so. Otherwise, use poll().

Thanks for you replay

I think I was not describing my problem properly,

I Run the READ process first, and then the WRITE process

I write the code following the Serial Programming guide:
http://www.faqs.org/docs/Linux-HOWTO/Serial-Programming-HOWTO.html

Because I open the device file /dev/ttyS0 with the flags then the read system call is blocked. mean that the read is blocked until there is data in the file descriptor (no need to poll, unless I need to poll between 2 files descriptors)

The problem might be partially solved (for the first time running the read process) by flushing the rx buffers by the code:

tcflush(fd , TCIOFLUSH ); //this code is supposed to flush out the content of write buffer of the file descriptor fd.

However, process READ still get (sometimes) only part of chars what process WRITE writing to the fd.

:frowning: