LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-04-2007, 07:03 AM   #1
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Rep: Reputation: 32
Serial Communication


Hi,

I have a bit of a problem reading a device from the usb port that sends data every second. I do receive data but i'm confused with one little thing:

The data length is usually 25 bytes (the nr between ::XX: but SOMETIMES it gets broken into parts like this:
Code:
::25::7E 42 08 00 00 00 00 00 7D 5E 00 01 7D 5D 01 00 92 0B 00 00 00 00 47 7C 7E
::25::7E 42 08 00 00 00 00 00 7D 5E 00 01 7D 5D 01 00 92 0B 00 00 00 00 47 7C 7E
::25::7E 42 08 00 00 00 00 00 7D 5E 00 01 7D 5D 01 00 8F 0B 00 00 00 00 80 28 7E
::25::7E 42 08 00 00 00 00 00 7D 5E 00 01 7D 5D 01 00 92 0B 00 00 00 00 47 7C 7E
::25::7E 42 08 00 00 00 00 00 7D 5E 00 01 7D 5D 01 00 92 0B 00 00 00 00 47 7C 7E
::25::7E 42 08 00 00 00 00 00 7D 5E 00 01 7D 5D 01 00 93 0B 00 00 00 00 E7 39 7E
::4::7E 42 08 00
::21::00 00 00 00 7D 5E 00 01 7D 5D 01 00 92 0B 00 00 00 00 47 7C 7E
::25::7E 42 08 00 00 00 00 00 7D 5E 00 01 7D 5D 01 00 92 0B 00 00 00 00 47 7C 7E
::7::7E 42 08 00 00 00 00
::18::00 7D 5E 00 01 7D 5D 01 00 92 0B 00 00 00 00 47 7C 7E
::25::7E 42 08 00 00 00 00 00 7D 5E 00 01 7D 5D 01 00 92 0B 00 00 00 00 47 7C 7E
::25::7E 42 08 00 00 00 00 00 7D 5E 00 01 7D 5D 01 00 8F 0B 00 00 00 00 80 28 7E
::25::7E 42 08 00 00 00 00 00 7D 5E 00 01 7D 5D 01 00 92 0B 00 00 00 00 47 7C 7E
I'm not very good in serial communication. I've been reading and trying some example codes resulting to this. The settings are as following
Code:
//Get the current options for the port...
	tcgetattr(fd, &options);

	//Set the baud rates for input and output (same))
	cfsetispeed(&options, type);
	cfsetospeed(&options, type);

	//Reset control flags
	;

	//Enable the receiver and set local mode.
	options.c_cflag |= (CLOCAL | CREAD);	

	//8 bit char mode, No parity, 1 stop bit
	options.c_cflag |= (options.c_cflag & ~CSIZE) | CS8;
	options.c_cflag &= ~PARENB;
	options.c_cflag &= ~CSTOPB;
	
	//Minimum caracters to read
	options.c_cc[VMIN]  = MIN_CHAR_READ; // MIN_CHAR_READ = 0

	//Time to wait for data
	options.c_cc[VTIME] = WAIT_DATA;     //WAIT_DATA = 20 (2 s)

	//Set hardware handshake
	options.c_cflag &= ~CRTSCTS;

	//No software flow control
	options.c_iflag &= ~(IXON | IXOFF | IXANY);

	//Input character management (raw or canonical)
	options.c_lflag = IRAW_MODE;
	//options.c_lflag = ICANON_MODE;

	//Output filtering options	(raw or processed)
	options.c_oflag = ORAW_MODE;
	//options.c_oflag = OPROCESS_MODE;
	//Set the new options for the port...
	tcsetattr(fd, TCSANOW, &options);
and then I just read the buffer like this:
Code:
//Empty the recieved buffer
 	tcflush(fd, TCIFLUSH);
	
	nBytes = read(fd, &data[0], MAX_DATA_SIZE) //(MAX_DATA_SIZE = 255)
Is it because the serial buffer is full for some reason?? If so, is there anyway I can determine the the tranmitted data is only a part of it and not complete so that I can read data again till it's complete?

Thanks a million

Indy
 
Old 04-04-2007, 08:47 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
Serial communications do not have inherent record boundaries. What constitutes a record is purely artificial.

The read() call gathers whatever bytes have been received since the prior read(). If your device sends data in spurts of 25 bytes, your read() call will usually read 25 bytes. But sometimes you can catch your device in mid-spurt, in which case you'll get whatever's available at the time.

Since you know that your data is best examined 25 bytes at a time, that's how you should look at it; just loop until you get all 25 bytes. But it can't hurt to put in a sanity check, in case a byte gets dropped or something. That may be very unlikely, but sanity checks are cheap, and if you need a sanity check and don't have one, it could take a long time to isolate the problem.

Are the first and final bytes of your 25-byte records guaranteed to be 0x7E? If so, that would be a good sanity check to use. If this device is provided by a third party, it would seem they included the 0x7E at the beginning and end of each record for precisely this purpose: so you could write a sanity check.

Hope this helps.
 
Old 04-04-2007, 09:24 AM   #3
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Original Poster
Rep: Reputation: 32
Well. No sanity check yet. I was afraid of that. there is no guarantee for the byte 7E on both side. And data can vary. I guess I have to put something in the data. I wanted to avoid it but I guess I have to live with it.

Thanks anyway

Indy
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Serial port communication.. brianbek Linux - Software 2 01-23-2006 01:43 PM
serial port communication prems Linux - Newbie 1 04-17-2005 03:31 AM
serial port communication vidyaraj Linux - Software 2 03-15-2004 12:32 AM
communication via serial port perdesiz Linux - Software 0 11-13-2003 07:23 AM
Serial communication - minicom sg3 Linux - Networking 0 07-17-2003 01:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration