help with serial i/o from a device?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

help with serial i/o from a device?

Dave Stevens
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.

Dave

--
In modern fantasy (literary or governmental), killing people is the
usual solution to the so-called war between good and evil. My books are
not conceived in terms of such a war, and offer no simple answers to
simplistic questions.

----- Ursula Le Guin

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: help with serial i/o from a device?

Robert Heller
At Wed, 5 Sep 2018 14:34:22 -0700 "Ubuntu user technical support,  not for general discussions" <[hidden email]> 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
[hidden email]       -- Webhosting Services
                                                                                   

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: help with serial i/o from a device?

Oliver Grawert
hi
Am Mittwoch, den 05.09.2018, 23:30 -0400 schrieb Robert Heller:
...
> cat /dev/ttyUSB0 > outfile.bin
...

beyond roberts exccellent explanation, you can also use dd for byte
streams, that gives you a bit more control than cat (allowing to set
blocksize, legth or offset of what you want to capture)

sudo dd if=/dev/ttyUSB0 of=outfile.bin

(there you can for example use bs=1MB count=1 to capture exactly 1MB of
data ... or skip=1kB to cut off the first kilobyte (i.e. a header, in
case you only want the payload data) of the stream)

"man dd" has all the dirty details :)

ciao
        oli
--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

signature.asc (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: help with serial i/o from a device?

Dave Stevens
On Thu, 06 Sep 2018 09:53:24 +0200
Oliver Grawert <[hidden email]> wrote:

> hi
> Am Mittwoch, den 05.09.2018, 23:30 -0400 schrieb Robert Heller:
> ...
> > cat /dev/ttyUSB0 > outfile.bin  
> ...
>
> beyond roberts exccellent explanation, you can also use dd for byte
> streams, that gives you a bit more control than cat (allowing to set
> blocksize, legth or offset of what you want to capture)
>
> sudo dd if=/dev/ttyUSB0 of=outfile.bin
>
> (there you can for example use bs=1MB count=1 to capture exactly 1MB
> of data ... or skip=1kB to cut off the first kilobyte (i.e. a header,
> in case you only want the payload data) of the stream)
>
> "man dd" has all the dirty details :)
>
> ciao
> oli

fabulous, thanks both for this. I'll post when I've got it sorted, dd
sound great.

d


--
In modern fantasy (literary or governmental), killing people is the
usual solution to the so-called war between good and evil. My books are
not conceived in terms of such a war, and offer no simple answers to
simplistic questions.

----- Ursula Le Guin

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users