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

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.
Why am I not happy that they have found common ground?

Tim Wescott, Communications, Control, Circuits & Software
http://www.wescottdesign.com
Reply to
Tim Wescott
Loading thread data ...

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:

Reply to
David Brown

formatting link

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):

formatting link

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):

formatting link

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
                              gmail.com            this place evacuated in
                                                   20 seconds!
Reply to
Grant Edwards

It _is_ Monday, a bad time for a "head shift; "hardware abstraction" is a wonderful thing, and I know it helps portability, but (as you've noticed) it can be a real pain in the assets when one tries to shift up out of the raw hardware into AbstractLand.

Here are a couple of places to start:

Serial Programming Guide for POSIX Operating Systems

formatting link

TLDP Serial Programming HOWTO

formatting link

TLDP Serial HOWTO

formatting link

You can probably find more using Google with something like:

linux serial port programming howto

Hope this hepls.

Frank McKenney

--
  Perhaps the most valuable result of all education is the ability
  to make yourself do the thing you have to do, when it ought to be
  done, whether you like it or not; it is the first lesson that
  ought to be learned; and however early a man's training begins, it
  is probably the last lesson that he learns thoroughly.
                             -- Thomas H. Huxley
Reply to
Frnak McKenney

That's the definitive source.

That has been abandoned for 10+ years and is rife with errors and omissions. I've tried to no avail on several occasions to submit corrections and new material. Don't even bother looking at it.

--
Grant Edwards               grant.b.edwards        Yow! I'll eat ANYTHING
                                  at               that's BRIGHT BLUE!!
                              gmail.com
Reply to
Grant Edwards

d

on windows I always to the easy way out; non-overlapped, no messing with timeout etc.

just open port, fill struct with setup set it, one function to check how many bytes available, one function to read that number of bytes, one function write bytes

I'm sure something like it is doable in linux

-Lasse

Reply to
langwadt

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.
Why am I not happy that they have found common ground?

Tim Wescott, Communications, Control, Circuits & Software
http://www.wescottdesign.com
Reply to
Tim Wescott

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

Reply to
David Brown

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
www.wescottdesign.com
Reply to
Tim Wescott

??? 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.

Reply to
Mel Wilson

l,

he

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

Reply to
langwadt

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

Reply to
Arlet Ottens

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.
Why am I not happy that they have found common ground?

Tim Wescott, Communications, Control, Circuits & Software
http://www.wescottdesign.com
Reply to
Tim Wescott

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

Reply to
Arlet Ottens

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.

Reply to
Dombo

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.

Reply to
Arlet Ottens

We had a problem like that with FTDI USBserial 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.

Reply to
Mel Wilson

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!!
                              gmail.com
Reply to
Grant Edwards

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.
Why am I not happy that they have found common ground?

Tim Wescott, Communications, Control, Circuits & Software
http://www.wescottdesign.com
Reply to
Tim Wescott

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.

Reply to
Arlet Ottens

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.