LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 01-08-2009, 11:36 AM   #1
farofeiro
LQ Newbie
 
Registered: Jan 2009
Posts: 1

Rep: Reputation: 0
Porting Serial port program from windows to linux


I bought a set of 3 laser sensors and it came with a software and its C++ source code for windows to receive the lasers info through the serial port.

Since the entire project is running on Linux, I need to port the windows code to Linux. At the present moment, I already can receive the data from each of the 3 sensors, but the info is coming concatenated!

Tracking down the original windows code, I found that it is probably happening cause I didn't ported these lines to my Linux version:
Code:
COMMTIMEOUTS CommTimeouts;
[...]
CommTimeouts.ReadIntervalTimeout = 2;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 2;
CommTimeouts.WriteTotalTimeoutMultiplier = 5;
CommTimeouts.WriteTotalTimeoutConstant = 5;
I've searched already about how to specify the serial port timeouts on Linux, but didn't find a solution.

Does anyone have any idea about how to port these lines to Linux or maybe where to find more info about how to set serial port timeouts on Linux?
 
Old 01-08-2009, 12:47 PM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Frankly, I'm at a loss. Serial I/O is a byte stream; there is no externally imposed grouping of bytes into records, no record boundaries at all. All serial data is, ultimately, "concatenated".

If you're relying on some sort of timeout mechanism to determine where one chunk of data ends and another begins, I would invite you to consider the possibility of keeping track of when you receive each byte. Use gettimeofday() for this purpose. If the gap between the time of day of reception of one byte and the time of day of reception of the next one is over a certain threshold, then you know you have the beginning of a new chunk of data. By implication, any bytes you have been collecting but have not used so far, not including the new byte, are part of an old chunk.

Hope this helps.
 
Old 01-08-2009, 12:51 PM   #3
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
Have a look at this document on serial port programming in Windows and Linux.

Although I have no experience in what you are trying to do it would seem these stuct members are relevant - termio.c_cc[VMIN] being the minimum amount of characters to read and termio.c_cc[V_TIME] the wait time in tenths of a second. This structure is defined in /usr/include/asm/termios.h
 
Old 01-08-2009, 01:53 PM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
I hadn't mentioned tcgetattr() and tcsetattr() because I assumed that the only problem that farofeiro was having was with timeout; therefore, he had resolved all the byte size and parity and baud rate and noncanonical I/O issues; therefore, he was already familiar with tcgetattr() and tcsetattr(); therefore, he was already familiar with the timeout capability offered there.

Again, if you wish to set the per-character timeout as well as the overall "record" timeout, as offered by the Windows API, you'll have to do that yourself. If the purpose is to separate records, set the timeout to whatever you want, read one character at a time, and use gettimeofday().

While the document cited by bgeddy is somewhat useful, you'll get a far better discussion of the relationship between VTIME and VMIN in the tcgetattr() man page.
 
Old 01-09-2009, 11:40 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Perhaps farofeiro is simply being confused by Windows vs Linux end-of-line conventions.
As wje_lq has pointed out, there is little else within a serial byte stream to delimit distinct records. Perhaps the original poster could clarify what he means by 'concatenated' and show some code that he has already tried.

farofeiro, does your laser speak in human-readable text, or does it use non-ASCII binary data? If strictly ASCII, I suggest using a tool such as C-Kermit to try to observe the likes of CR/LF usage for end-of-lines. Writing a test/debug stub that dumps all data as human readable text in a format similar to the output of 'od -cx' can also be extremely illuminating.

--- rod.
 
  


Reply



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
Does Linux have a .bin serial port uploading program? Balarabay1 Linux - Software 3 04-25-2007 09:31 PM
Windows to Linux connection via Serial Port andrewm659 Linux - Networking 11 02-23-2006 03:57 PM
serial port program for Linksys WAP54G segfaulting (linux) hedpe Programming 3 06-06-2005 02:58 PM
Windows to Linux via serial port longnam Linux - Networking 1 05-01-2005 06:50 AM
Windows to linux via serial port longnam Linux - Hardware 2 04-30-2005 02:07 PM

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

All times are GMT -5. The time now is 06:41 AM.

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