Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Peripherals / Hardware > RS232 programming problem
 Peripherals / Hardware   Is your hardware supported? Having trouble getting some hardware working? Post here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 10-12-2008   #1 (permalink)
Just Joined!
 
Join Date: Oct 2008
Posts: 1
RS232 programming problem

Hi all

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

I have simple 2 processes:

1) process which WRITE characters to serial device /dev/ttyS0
For example write the characters "aa102222233333444445555566666777778888899999aaaaa bbbbbcccccddddd"

2) process which READ characters from serial device /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 "aa102222233333444445555566666777778888899999aaaaa bbbbbcccccddddd"

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


In my test I run WRITE process few times and check what READ process get

I want to emphasize that this problem happens some time (most case when I run process READ first time, but some time after few time that I write to the serial device)



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 = "aa102222233333444445555566666777778888899999aaaaa bbbbbcccccddddd";

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;
}


Best regards,
Dudi
dudi.forum is offline  

Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Free Network Mapping Tool for Microsoft® Office Visio® Professional 2007 Users
Don't map your network by hand – let LANsurveyor Express for Microsoft Visio Professional 2007 automatically create network diagrams for you.
subscribe
Free eBook:"Vulnerability Management for Dummies"
Get all the Facts and See How to Implement a Successful Vulnerability Management Program.
subscribe
Google vs The World: The Battle of the Message Security Vendors
With such a powerful name behind it, Google Message Security stands out in a sea of products that do exactly the same thing - or so they say.
subscribe

Safe, Secure Backup


All times are GMT. The time now is 02:49 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2