[R] Reading data from a serial port

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Tue Sep 13 21:26:16 CEST 2005


On 13-Sep-05 vittorio wrote:
> I need to read data from from a medical appliance via the serial port.
> This medical appliance produces streams of set data at regular
> intervals.
> What commands,  packages are available for this purpose?
> 
> Vittorio

Have a look at "?scan".

You will have to do several things which depend on your hardware
setup, your operating system, the behaviour of your medical applicance,
and how you want to handle the data as it comes through. Some of this
can be worked out from "?scan"; the rest is up to you!

Example: On my Linux machine here, the serial port at the back is
/dev/ttyS0. I have used 'minicom' to set its data parameters to
4800 baud, 7-bit data, space parity, 1 stop bit ("4800 7S1").

Next, I have connected my GPS gadget (which has a serial output
in ASCII text format at the above characteristics) to the serial
port.

Then, in R (with permissions on /dev/ttyS0 set to allow user read/write,
namely "rw-rw-rw-"), in R I have executed, for example,

scan(file="/dev/ttyS0",n=1,what="character")
Read 1 items
[1] "@050913192752N5228545E00023023G007-00004E0000N0000D0000"

which tells me that it is 2005/09/13 at 19:27:52 UTC, that I
am at 52deg 28.545minN and 000deg 23.023min E, that I have a
Good stellite fix, have a potential horizontal position error
of 007 metres, am at 0000.4 metres below sea level, and am
moving at 000.0m/s Eastwards, 000.0m/s Northwards, and 00.00m/s
Downwards. (Illustrating that such data is parsed by position;
'scan' does not seem to have a mechanism for splitting a line
into fields by position, but it can be done after reading by
using 'substr').

That command read just one line, so by repeating the command
I can read a line at a time, do something with it, read the next ...

Again,

scan(file="/dev/ttyS0",n=5,what="character")
Read 5 items
[1] "@050913191942N5228544E00023023G010-00001E0000N0000U0001"
[2] "@050913191943N5228544E00023023G010-00001E0000N0000U0001"
[3] "@050913191944N5228544E00023023G010-00001E0000N0000U0001"
[4] "@050913191945N5228544E00023023G010-00001E0000N0000U0001"
[5] "@050913191946N5228544E00023023G010-00001E0000N0000U0001"

reads a batch of lines, which can be assigned to a vector.

Variants on this depend on what data format your apparatus
puts out, and on what you want to do. In particular, if you
want to process the output in real time, then probably you
are best off reading a line at a time. But if you simply
want to store a batch of lines for later processing, then
set a (possibly large) number of lines to be read at a time.

An so on.

Hoping this helps,
Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 13-Sep-05                                       Time: 20:26:09
------------------------------ XFMail ------------------------------




More information about the R-help mailing list