Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 04-12-2007, 04:27 AM   #1
LQ Newbie
Registered: Apr 2007
Location: Ukraine
Distribution: Red Hat, Debian
Posts: 4

Rep: Reputation: 0
Smile Serial port return wrong data

Hello everybody!

I hope someone had the same problem as mine and can help me to resolve it.

I'm developing console program for Red Hat 9.0, that uses serial port to read and write data to device.

I open serial port using function open(), write data to serial port using function write(), read data from serial port using function read().
The settings are: baud rate 9600, 8 data bits, no parity, one stop bit, disable flow control.

In some cases, read() return data, which isn't correct. I know for sure, that device send data like this:
ffffff 010101 07 1d 0d00.
And read() function read that:
ffffff 010101 07 1d 0a00.
Another words saying, in last 2 bytes, there are 0a00 instead of 0d00.

Can anybody say what can be happened with data? Maybe i have to set or unset some configure flags for serial port?

I would be grateful for your help.
Old 04-12-2007, 08:22 AM   #2
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
After you've open()ed the device, do a tcgetattr() on it to get the current attribute settings, modify what you want, and set the modified attributes using tcsetattr().

You'll for sure want to turn off ICRNL, but consider changing other bits too.

Don't forget to save the original attributes, and restore them just before you close() the device.

Read this and (to save time in the long run) make sure you understand it completely, or at least everything concerned with tcgetattr() and tcsetattr():

man tcsetattr
If that doesn't make things clear, consider googling for this:

tcsetattr tutorial
Hope this helps.
Old 04-12-2007, 01:35 PM   #3
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
For more details on what wjevans says, see and

To answer you specific question about the changing of byte '0D' to '0A', this is the result of using 'canonical' input mode, which has changed your Carriage-Returns to Linefeeds.

--- rod.
Old 04-13-2007, 01:54 AM   #4
LQ Newbie
Registered: Apr 2007
Location: Ukraine
Distribution: Red Hat, Debian
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks your both for your help!

I've turned off the flag ICRNL and data returned from read() is ok!

I am grateful for your help very much!
It works!!!


programming, serial port

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
help reading data from a serial port skydemon Linux - General 1 08-10-2006 08:48 AM
reading data from a serial port mchitrakar Linux - Networking 3 12-04-2004 01:24 PM
accessing data from serial port PrerakParikh Linux - Hardware 2 08-24-2004 10:17 PM
exchanging data between 2 computer using serial port blackzone Linux - General 1 07-24-2004 02:54 AM
anything wrong with my serial port? rgyan Linux - Hardware 3 11-19-2003 02:51 AM > Forums > Non-*NIX Forums > Programming

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

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