help with serial i/o from a device?

Robert Heller heller at deepsoft.com
Thu Sep 6 03:30:25 UTC 2018


At Wed, 5 Sep 2018 14:34:22 -0700 "Ubuntu user technical support,  not for general discussions" <ubuntu-users at lists.ubuntu.com> wrote:

> 
> I've got a Plantower PMS5003 particle counter that reports
> over a serial (RS-232) cable. Inline to my USB port is a uart, acting
> as a level shifter. Node.js has a module that allows me to read
> successfully from the device (or, strictly speaking, from the USB port).
> It is, however, a bit large and clumsy, a lot of overhead to just copy
> serial data (at a low rate, about 200 bits per second, into a file. I'd
> like to use as small a computer as I can and am currently looking at a
> Pi Zero W.
> 
> At first it seemed to me that I should just be able to copy incoming
> data into the file like 
> 
> cp /dev/ttyUSB0 filename.
> 
> But that doesn't work, I get sporadic ascii gibberish on screen and no
> output data in the file.
> 
> Then I wrote a little Pascal program called copy that exactly copied
> characters from input to output, like
> 
> sudo cat /dev/ttyUSB0 > copy > outputfile.bin
> 
> and that works fine.
> 
> Does anyone see why the original doesn't work? If I understood that I
> might be able to use fewer resources and use a simpler device.

cp is designed to copy *files*.  A serial I/O stream is not *exactly* a file 
on some level.

Normally, a serial I/O stream has no File EOF (but might send some kind of
termination message), you can't stat(2) it get its size and it is not in
blocks. 

What you need is a simple "program" that "understands" these issues.

I'm guessing that your particle counter outputs ASCII text lines (eg some sort 
of structured "record" that contains printable ASCII characters with a CRLF at 
the end.

It is possible you don't actually need the Pascal program.  What happens when 
you do this:

cat /dev/ttyUSB0 > outfile.bin

(note: you probably *should not* use sudo, and instead add yourself to the 
dialout group, which is the group the /dev/ttyUSB0 file should be owned by.)

Otherwise you might want to write a special program that does tty I/O.

Further reading:

man 2 read
man termios
man tty_ioctl

        
> 
> Dave
> 

-- 
Robert Heller             -- 978-544-6933
Deepwoods Software        -- Custom Software Services
http://www.deepsoft.com/  -- Linux Administration Services
heller at deepsoft.com       -- Webhosting Services
                                                                                    




More information about the ubuntu-users mailing list