[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]