LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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-12-2007, 04:27 AM   #1
levka
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
wjevans_7d1@yahoo.co
Member
 
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():

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

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

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
For more details on what wjevans says, see http://tldp.org/HOWTO/Serial-Program...WTO/index.html and http://tldp.org/HOWTO/Serial-HOWTO.html

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
levka
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!!!
 
  


Reply

Tags
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


All times are GMT -5. The time now is 05:07 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration