ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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.
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.