Download your favorite Linux distribution at LQ ISO.
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-06-2008, 03:44 PM   #1
LQ Newbie
Registered: Mar 2008
Posts: 19

Rep: Reputation: 0
Serial programming - binary data using open() and read()

Hello everyone!
I am using the old "open" function to access the serial port to try and read some incoming data. The problem is when I receive beyond the standard ascii characters, 0x80 and above, when I do a read I get 0xffff(msb)(lsb). I'm assuming that these f's are getting tacked on because I'm doing a read - and it's expecting ascii chars. How do I get around this? It goes something like this...

int fd;

fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);

...setup async handler and serial settings..
Now.. in my signal handler
int raw_rx_data;
char in_bufer[128];

//read two bytes
read(fd, in_buffer, 2);

//convert to 16 bit integer
raw_rx_data = (in_buffer[0] << 8) | in_buffer[1];
1. Now do you think the issue is the way I'm converting the string to integer? (Note: I don't want to just mask the bits for other reasons).
2. Is there a way to use the open function with a binary flag (searched and couldn't find one)?
3. Is there a way to use the fopen function with the serial port? I don't think fopen supports the noctty / ndelay flags.

Old 04-06-2008, 04:56 PM   #2
Registered: Oct 2006
Distribution: debian
Posts: 124

Rep: Reputation: 15
Yes, it's sign extension. int8 0xff == int32 0xffffff == -1 ; uint8 0xff == (u)int32 0x000000ff == 127.

Ha det. Beeneken.
Old 04-06-2008, 05:28 PM   #3
LQ Newbie
Registered: Mar 2008
Posts: 19

Original Poster
Rep: Reputation: 0
Hmmm... I don't quite understand.
If my shifting operation works for ascii values, why does it not work for non-ascii values?
Could you please explain the correct way to perform this operation? Sorry, I'm still new to this bit stuff.
Is simple bit shifting not enough?
Old 04-07-2008, 08:01 AM   #4
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Why not raw_rx_data = *(uint16_t*) in_buffer;? Bit operators automatically promote to int when using C. Also, you might have your bytes backwards: maybe byte 1 should have been shifted? I guess it depends on the system and how you wrote the data in the first place.
Old 04-07-2008, 09:02 AM   #5
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,575

Rep: Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885Reputation: 1885
Non-ascii values have the high bit set, so they are negative numbers. I think declaring in_buffer as unsigned char would work.
Old 04-07-2008, 04:04 PM   #6
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,398
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Note, also, that the serial driver may trap certain character sequences (eg XON/XOFF), depending upon how you have configured the serial port. The Serial Programming Guide forPOSIX Operating Systems, and the Serial Programming Howto both provide a good bit of information on the subject.
--- rod.


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
Problems with reading back serial binary data esi-eric Linux - Software 15 10-03-2018 10:13 AM
How to read data coming from serial connected procurve 2524 with python Tanc Programming 3 11-07-2012 07:44 AM
Using open(... to process binary data over ttyS0 neutron001 Linux - Software 1 04-08-2008 12:45 AM
is there any shell command to read and write data from parallel and serial port? baosheng Linux - Hardware 2 01-13-2007 09:35 PM
Why can't I read in data from the serial port using a bash script? tjt Linux - Newbie 1 06-17-2004 01:21 AM > Forums > Non-*NIX Forums > Programming

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

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
Open Source Consulting | Domain Registration