Thread: problem reading from serial port
-
February 2nd, 2011, 09:54 AM #1Junior Member
- Join Date
- Jun 2009
- Posts
- 11
problem reading from serial port
Hi, I'm not sure if this should go in the Linux/Unix forum, so forgive me if it's in the wrong place, but here goes anyway.
I'm sending a character array over the serial port from one PC to another using c on linux. The following code is used to transmit the data. It's in an infinite loop so it will keep writing to the port every 2 seconds.
This data is then read by a program on the other PC.Code:char message[]={0x1,'C','L','A','1','0','0','2','6',0x2,'\r','\n','3','0','a','b','\0'}; int p=0; printf("transmitting data\n"); while(p==0) { error= write(fWSfd, message,17); error= tcsetattr(fWSfd, TCSAFLUSH, &newtio) <0; sleep(2); }
It loops continuosly, the inner loop being used to wait for input to arrive at the port.Code:int y=0; char data[50]; while(y==0) { error= read( fWSfd, data, 50*sizeof(char) ); while (error<1) { error= read( fWSfd, data, 50*sizeof(char) ); data[error]=0; } printf("Read the following data: %s\n", data); error = tcsetattr(fWSfd,TCSAFLUSH,&newtio)<0 ; }
My problem is with the output, which turns out like this:
Read the following data: #CLA10026# (# = 0x1 and 0x2 characters )
30
Read the following data: ab
It seems to only read part of the message and then stop, and then loop around again and grab the rest of the data. I can't figure out why it's doing this. I have it set to raw data, so the carraige returns and line feeds shouldn't affect it. If you're wondering, the reason they're in there in the first place is because those exact characters must be transmitted.
I can post all the serial port settings too if it would be helpful, I just thought maybe I overlooked something obvious.
I hope somebody can help me with this. Any ideas would be appreciated.
Thanks
-
February 2nd, 2011, 11:35 AM #2Junior Member
- Join Date
- Jun 2009
- Posts
- 11
Never mind, I got it working. I think it was reading before it received all the characters. A small delay before it reads sorts it out.
Thanks anyway.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
serial port
By Mahlon Gillard in forum Technical SupportReplies: 1Last Post: January 20th, 2010, 06:25 AM -
Serial port error
By videobruce in forum Technical SupportReplies: 0Last Post: August 21st, 2005, 10:58 AM -
Printer port to serial port ???
By pickel in forum Applications and Operating SystemsReplies: 5Last Post: June 9th, 2005, 09:43 PM -
Whats the serial 1 port for?
By mjolnir1134 in forum General Tech DiscussionReplies: 2Last Post: February 24th, 2005, 06:44 PM -
Serial port
By seanmark2 in forum General Tech DiscussionReplies: 9Last Post: June 24th, 2003, 10:56 PM



LinkBack URL
About LinkBacks



Reply With Quote

There is a vintage Diaclone Grimlock in the original box on eBay. :eek: Diaclone, as in before Transformers G1. Start is $429.
Thread About Anime