Linux Serial How-To (For a guy with an Embedded mindset)

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
I hate writing code for desktops, at least anything that's easy on an
embedded system but requires one to wade through all sorts of OS glop to
get it to work on the thingie with the screen and typewriter.

Anyone got a suggestion for a comprehensive "how-to" on Linux serial
communications programming?  Something that explains when, when not, and
how to use ioctl, fcntl, termios, and all that other mystic crap that
gets interposed between me and the UART's registers?

TIA.

--
My liberal friends think I'm a conservative kook.
My conservative friends think I'm a liberal kook.
We've slightly trimmed the long signature. Click to see the full one.
Re: Linux Serial How-To (For a guy with an Embedded mindset)
Quoted text here. Click to load it

If you use Python (which is an excellent language for "quick and easy"
programming on Linux, Windows, and other bigger systems), then you can
install "pyserial" and have a very simple and obvious connection to a
serial port.

Even if you don't want to use Python, you might find the source code for
the Linux version useful to show you where the various calls go:

<http://pyserial.svn.sourceforge.net/viewvc/pyserial/trunk/pyserial/serial/serialposix.py?revision45%0&view=markup>

Re: Linux Serial How-To (For a guy with an Embedded mindset)

Quoted text here. Click to load it

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

That's assuming you want to write C code.

I generally do all my Linux serial programming in Python using either
pySerial (which allows you to write apps that also run on Windows):

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

or PosixSerial (which is what pySerial is based on, but it's a thinner
layer that's pretty much a 1:1 mapping to the Posix API):

  http://www.panix.com/~grante/files/python/PosixSerial.py

At least for me, putting together a small serial-comm utility in
Python probably takes 1/10 the time as does doing it in C.
  
--
Grant Edwards               grant.b.edwards        Yow! All right, you
                                  at               degenerates!  I want
We've slightly trimmed the long signature. Click to see the full one.
Re: Linux Serial How-To (For a guy with an Embedded mindset)

Quoted text here. Click to load it

Thanks Grant, David.

Alas, part of what I'm doing is testing out soon-to-be-embedded code on a
Linux box, which is not only forcing me into C++, but forcing me to write
a class that warps all the Linux OS coolness down to a standard interface
that I use for most embedded serial comms.

Should I ever actually need to do real serial on Linux for its own sake,
though, I'll remember.

--
My liberal friends think I'm a conservative kook.
My conservative friends think I'm a liberal kook.
We've slightly trimmed the long signature. Click to see the full one.
Re: Linux Serial How-To (For a guy with an Embedded mindset)
Quoted text here. Click to load it

If the system has plenty of memory and the program is not time-critical,
then there is no reason not to run Python in an embedded Linux system.
It will not be close to optimal in either space efficiency or speed -
but if it is good enough to do the job, then it is good enough to do the
job.

mvh.,

David


Re: Linux Serial How-To (For a guy with an Embedded mindset)

Quoted text here. Click to load it

Nah, it's a Cortex M3 that's replacing a PIC.  No room for Linux, it
needs to be real-time, and it'd take me more time just to get a command
line than I have for the whole project.

--
Tim Wescott
Control system and signal processing consulting
We've slightly trimmed the long signature. Click to see the full one.
Re: Linux Serial How-To (For a guy with an Embedded mindset)

Quoted text here. Click to load it

???  I hope I understand this as:
1) If you can run Linux, then you can probably run Python and PySerial
2) If you can't run Linux, then hardware serial is easy and the problem
never existed.

I'm just getting dug into Cortex-M4 and I'm hoping that the effort for
serial I/O is of the same order of magnitude that it was on AVRs.

    Mel.

Re: Linux Serial How-To (For a guy with an Embedded mindset)
Quoted text here. Click to load it

I'm sure what Tim wants to do is start out developing and debuggging
his
embedded code on linux as if it was his embedded platform, he just
wants
to replace the serialport functions for cortex with something
functionally
equivalents for linux

once its debugged, replace serialport functions and recompile for
cortex

-Lasse

Re: Linux Serial How-To (For a guy with an Embedded mindset)
Quoted text here. Click to load it

In that case, just open /dev/ttyS0 and read/write to it. To set all the
serial flags and options, use system( "stty <args>" );


Re: Linux Serial How-To (For a guy with an Embedded mindset)

Quoted text here. Click to load it

If you really want to write embedded code on Linux, you can mmap() the
UART into userspace and diddle the registers directly.  Handling
interrupts in userspace is a bit more difficult, but can be done.

--
Grant Edwards               grant.b.edwards        Yow! One FISHWICH coming
                                  at               up!!
We've slightly trimmed the long signature. Click to see the full one.
Re: Linux Serial How-To (For a guy with an Embedded mindset)

Quoted text here. Click to load it

Exactly.  In fact, the whole thing is working in a project directory
right now, with the embedded stuff in the main directory, and a directory
named "PC_test" that reaches down into the embedded directory for the
middleware.

I'm dropping bytes, but I'm almost 100% sure that's happening in the
Linux part -- which is a b**ch, because I don't know if I should be
ignoring it or chasing it down.

Chasing it down, probably, even if it'll turn out to be on the Linux
side, and therefore a "waste of time".  Sigh.

--
My liberal friends think I'm a conservative kook.
My conservative friends think I'm a liberal kook.
We've slightly trimmed the long signature. Click to see the full one.
Re: Linux Serial How-To (For a guy with an Embedded mindset)

Quoted text here. Click to load it

If the serial connection is dropping bytes, try using a USB/serial cable
instead of a regular serial port.

Re: Linux Serial How-To (For a guy with an Embedded mindset)
Op 21-Mar-12 19:33, Arlet Ottens schreef:
Quoted text here. Click to load it

My experience with USB/serial cables as far as reliability goes has not
been that great; the old fashioned 'real' serial ports seem to be more
reliable. I have had cases with certain USB/serial cables where the only
way to get the data flowing again was to physically unplug it from the
USB port and reinsert it; a bit disconcerting for unattended systems
that have to run 24/7.

Re: Linux Serial How-To (For a guy with an Embedded mindset)
Quoted text here. Click to load it

I got the impression it was just a temporary system, so the long term
stability of such USB/serial cables shouldn't be a issue.

At least it's a good step towards finding the cause of the problem. If
it's still dropping characters, it's probably not a Linux problem.

Re: Linux Serial How-To (For a guy with an Embedded mindset)

Quoted text here. Click to load it

It is using a USB serial cable.

And when I said "Linux problem" I meant "Tim's use of Linux problem".

--
My liberal friends think I'm a conservative kook.
My conservative friends think I'm a liberal kook.
We've slightly trimmed the long signature. Click to see the full one.
Re: Linux Serial How-To (For a guy with an Embedded mindset)
Quoted text here. Click to load it

In that case, try it with an original serial port instead. Or try it
with a different brand of USB/serial chip that uses a different driver.


Re: Linux Serial How-To (For a guy with an Embedded mindset)

Quoted text here. Click to load it

Actually, I think my shortest path to joy at this point is to just live
with the errors, and only pick up this particular baton again if the
embedded system shows the same behavior (in which case I'll know it's not
Linux, or me+Linux, but just me).

--
My liberal friends think I'm a conservative kook.
My conservative friends think I'm a liberal kook.
We've slightly trimmed the long signature. Click to see the full one.
Re: Linux Serial How-To (For a guy with an Embedded mindset)

Quoted text here. Click to load it

We had a problem like that with FTDI USB<->serial on Linux.  We never
tracked it down. (I've probably mentioned it here before.)  Didn't try past
a couple of days.  We never saw bytes other than 0x00 being dropped; we were
running a home-grown binary packet format with dedicated end-of-packet
markers and an escape convention; it was easy to extend the escape
convention so that we never transmitted 0x00 bytes in a packet.  Easier than
hassling FTDI and picking apart the serial driver routines.

    Mel.


Re: Linux Serial How-To (For a guy with an Embedded mindset)
Quoted text here. Click to load it

Personally, I never, ever, ignore the dropping of bytes. In a robustly
designed system that simply should not happen (unless you are experiencing
interference or other hardware problems).

I'm surprised no one has asked you what your flow control settings are
and if you are sure they are working as expected.

What flow control are you using and are you using the same flow control
method in the final product ?

Simon.

--
Simon Clubley, clubley@remove_me.eisner.decus.org-Earth.UFP
Microsoft: Bringing you 1980s technology to a 21st century world

Re: Linux Serial How-To (For a guy with an Embedded mindset)

Quoted text here. Click to load it

None, and yes.

And normally I'd chase down the dropped bytes, too -- except the Linux
part of this is most definitely throw-away, and I'm starting to feel like
I'm wasting time that my customer is paying for.

--
My liberal friends think I'm a conservative kook.
My conservative friends think I'm a liberal kook.
We've slightly trimmed the long signature. Click to see the full one.

Site Timeline