Diablo 2 Mp3 - Mortgage Calculator - Team Building in London - Refinance - Books
Simple serial question... [Archive] - DunLUG - Dunedin Linux Users Group

View Full Version : Simple serial question...


twgray
08-11-2005, 10:13 AM
How can I send and receive binary packets of data using the standard
Linux file i/o functions: open, write, read, close? It appears that
write and read are ascii only functions.

Help!

Grant Edwards
08-11-2005, 10:39 AM
On 2005-11-07, twgray <terry.gray@turbochef.com> wrote:

> How can I send and receive binary packets of data using the
> standard Linux file i/o functions: open, write, read, close?

You just call them.

> It appears that write and read are ascii only functions.

Nope. Where did you get that idea?

You'll probably want to put the tty driver into "raw" mode:

http://www.easysw.com/~mike/serial/

NB: Don't use the Linux serial programming HOWTO.

--
Grant Edwards grante Yow! .. the HIGHWAY is
at made out of LIME JELLO and
visi.com my HONDA is a barbequed
OYSTER! Yum!

Alain Mosnier
09-11-2005, 08:19 AM
Grant Edwards wrote:
> On 2005-11-07, twgray <terry.gray@turbochef.com> wrote:
>
>
>>How can I send and receive binary packets of data using the
>>standard Linux file i/o functions: open, write, read, close?
>
>
> You just call them.
>
>
>>It appears that write and read are ascii only functions.
>
>
> Nope. Where did you get that idea?
>
> You'll probably want to put the tty driver into "raw" mode:
>
> http://www.easysw.com/~mike/serial/
You can also have a look at the source code I included in my answer to
another thread in the same group called "need a sample code". In it, I
send an receive a character "A". But you can replace it with a pointer
to whatever variable of binary type that you want, and use the sizeof()
operator as the third argument of read() or write(). Be aware of the
byte ordering issue though: your source and destination might order the
bytes differently. This is not a problem if both source and destination
are Linux, which is always little-endian, as far as I know (less
significant byte first). Otherwise, you need to use the functions
htons(), htonl(), ntohs() and ntohl() to convert values back and forth
before and after transmitting them.
>
> NB: Don't use the Linux serial programming HOWTO.
>
Just out of curiosity, why is that? Anything specific that would be
wrong in that HOWTO? I'm rather using the "Serial Programming Guide
for POSIX Operating Systems" which is more recent myself, but I would
still be interested to hear what's wrong in that HOWTO.

Regards,

Alain

Grant Edwards
09-11-2005, 08:41 AM
On 2005-11-08, Alain Mosnier <alain.mosnier@chello.se> wrote:

> Be aware of the byte ordering issue though: your source and
> destination might order the bytes differently. This is not a
> problem if both source and destination are Linux, which is
> always little-endian, as far as I know

Wha?

There are plenty of big-endian architectures running Linux:

SPARC
PPC
68000
IBM-370 (and I presume z-series),
H8/300
ARM
etc.

I suspect that the majority of Linux-supporting architectures
are big-endian, even though most Linux machines are
little-endian.

>> NB: Don't use the Linux serial programming HOWTO.
>
> Just out of curiosity, why is that? Anything specific that
> would be wrong in that HOWTO?

Yes. Mainly the code used to open devices. I have vague
recollections of problems with the way some of the termios
stuff is done too, but I can't remember the details. There are
also large amounts of stuff that's just not there (e.g.
accessing modem control/status lines).

> I'm rather using the "Serial Programming Guide for POSIX
> Operating Systems" which is more recent myself, but I would
> still be interested to hear what's wrong in that HOWTO.

You're the only one, then. Corrections and entirely new
chapters have been submitted but have been ignored.

--
Grant Edwards grante Yow! The PINK SOCKS were
at ORIGINALLY from 1952!! But
visi.com they went to MARS around
1953!!

Alain Mosnier
10-11-2005, 03:43 AM
Grant Edwards wrote:
> On 2005-11-08, Alain Mosnier <alain.mosnier@chello.se> wrote:
>
>
>>Be aware of the byte ordering issue though: your source and
>>destination might order the bytes differently. This is not a
>>problem if both source and destination are Linux, which is
>>always little-endian, as far as I know
>
>
> Wha?
>
> There are plenty of big-endian architectures running Linux:
>
> SPARC
> PPC
> 68000
> IBM-370 (and I presume z-series),
> H8/300
> ARM
> etc.
>
> I suspect that the majority of Linux-supporting architectures
> are big-endian, even though most Linux machines are
> little-endian.
All right, I guess I was just talking about x86 then.

Anyway, I guess my point about byte ordering was even more relevant that
I thought. ;-)
>
>
>>>NB: Don't use the Linux serial programming HOWTO.
>>
>>Just out of curiosity, why is that? Anything specific that
>>would be wrong in that HOWTO?
>
>
> Yes. Mainly the code used to open devices. I have vague
> recollections of problems with the way some of the termios
> stuff is done too, but I can't remember the details. There are
> also large amounts of stuff that's just not there (e.g.
> accessing modem control/status lines).
>
Good to know. Thanks for the info.
>
>>I'm rather using the "Serial Programming Guide for POSIX
>>Operating Systems" which is more recent myself, but I would
>>still be interested to hear what's wrong in that HOWTO.
>
>
> You're the only one, then. Corrections and entirely new
> chapters have been submitted but have been ignored.
>
Do you mean that people don't know that document? I found it immediately
at my first Google search about Linux serial programming (actually, the
HOWTO points to it). I think that document fills a surprising hole in
books like "Advanced programming in the UNIX environment", "Advanced
UNIX programming" and "UNIX network programming", which are very good
books otherwise.

Regards,

Alain

Grant Edwards
10-11-2005, 04:14 AM
On 2005-11-09, Alain Mosnier <alain.mosnier@chello.se> wrote:

>> You're the only one, then. Corrections and entirely new
>> chapters have been submitted but have been ignored.
>
> Do you mean that people don't know that document?

No, I mean that people have sent in corrections for the HOWTO,
but those corrections are ignored. I wrote an entire new
chapter on how to read/write modem control/status lines and
submitted that. Nothing happened. Despite the fact that
people send in corrections and new material, the HOWTO hasn't
been updated in 7 years. It was out-of date 7 years ago, and
even more so now. It needs to be removed from the website.

It's incomplete, obsolete and misleading.

> I found it immediately at my first Google search about Linux
> serial programming (actually, the HOWTO points to it).

I know. It's quite unfortunate that it's so easy to find.

> I think that document fills a surprising hole in books like
> "Advanced programming in the UNIX environment", "Advanced UNIX
> programming" and "UNIX network programming", which are very
> good books otherwise.

I'm not saying there's no need for serial programming
documentation, I'm saying that the HOWTO does a bad job of
meeting that need. The Posix serial programming guide is much,
much better. The serial HOWTO _could_ be better, and those of
us who do serial stuff for a living have tried (and failed) to
make it better. Now we've just given up on it.

--
Grant Edwards grante Yow!
at FISH-NET-FISH-NET-FISH-NET-FISH-NET-FISH!!
visi.com

Grant Edwards
11-11-2005, 04:06 AM
On 2005-11-10, Alain Mosnier <alain.mosnier@chello.se> wrote:

> So as a summary, you're saying that the HOWTO is outdated, and
> that people should use the POSIX guide instead
> (http://www.easysw.com/~mike/serial/).

Exactly.

--
Grant Edwards grante Yow! Why am I in this ROOM
at in DOWNTOWN PHILADELPHIA?
visi.com

Jim Jackson
14-11-2005, 09:07 AM
Grant Edwards <grante@visi.com> wrote:

> I'm not saying there's no need for serial programming
> documentation, I'm saying that the HOWTO does a bad job of
> meeting that need. The Posix serial programming guide is much,
> much better. The serial HOWTO _could_ be better, and those of
> us who do serial stuff for a living have tried (and failed) to
> make it better. Now we've just given up on it.

Take it over. Correct it, add you addenda, and put it up onyour own
website and publicise it. Submit it back to to the LDP as a new document.

Sometime authors disappear, like software maintainers, but others can step
forward and keep things rolling.

Grant Edwards
14-11-2005, 12:18 PM
On 2005-11-13, Jim Jackson <jj@franjam.org.uk> wrote:
> Grant Edwards <grante@visi.com> wrote:
>
>> I'm not saying there's no need for serial programming
>> documentation, I'm saying that the HOWTO does a bad job of
>> meeting that need. The Posix serial programming guide is
>> much, much better. The serial HOWTO _could_ be better, and
>> those of us who do serial stuff for a living have tried (and
>> failed) to make it better. Now we've just given up on it.

> Take it over. Correct it, add you addenda, and put it up
> onyour own website and publicise it. Submit it back to to the
> LDP as a new document.

Firstly, I've submitted corrections and new sections. My
submissions were ignored. Secondly, the entire document is now
redundant and just needs to be deleted.

> Sometime authors disappear, like software maintainers, but
> others can step forward and keep things rolling.

Somebody has already written a far better document. I see no
need to try to compete with it. It's just too bad the serial
HOWTO won't "go away".

--
Grant Edwards grante Yow! Kids, don't gross me
at off... "Adventures with
visi.com MENTAL HYGIENE" can be
carried too FAR!