[R] Reading data from a serial port

vittorio vdemart1 at tin.it
Tue Sep 13 23:29:36 CEST 2005


Alle 21:26, martedì 13 settembre 2005, Ted Harding ha scritto:
...................................>
> 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,

Thanks, it helps! But Ted,
how do you let R know the parameters of the serial connection  (e.g. "4800 
7S1") ?
Ciao
Vittorio




More information about the R-help mailing list