***UPDATED: June 3, 2010 - revert name from "tty" to "serial", R version (2.11.1)***
I'm working on a patch for R (currently 2.11.1) that adds a serial connection feature for POSIX systems (i.e. Linux, Mac OS X, ...). The serial connection works like the other connections. For example, the following code opens, writes a single byte, and closes a serial device
> con <- serial("/dev/ttyUSB0", "r+b") > con description class mode text opened "/dev/ttyUSB0" "serial" "r+b" "binary" "opened" can read can write baudrate charsize stopbits "yes" "yes" "9600" "8" "1" parity readmin readtime startstop startchar "N" "0" "0" "FALSE" "11" stopchar "13" > writeBin(charToRaw("xff"), con) > flush(con) > close(con)
The serial connection supports modifying serial port parameters, including input/output baud rates, character size, number of stop bits, parity, blocking, and start/stop flow control. For instance, to open a serial connection with input and output baud rate set to 38400 use
... > con <- serial("/dev/ttyS0", "r+b", baudrate=38400L) ...
The rest of this post is a HOW TO for applying, configuring, and compiling the patch on a POSIX system so that you can use the serial connection! There are two ways to apply the patch, the second (Elegant) requires a little more work than the first (Hack) , but is the recommended route.
Hack Patch Job
- This method is a hack because it doesn't require you to use autoconf, or any of the other autotools. These tools are used to build a new configure script and config.h.in files. The new configure script and config.h.in files are simply bundled with the patch. However, you will need the patch and make programs, and a compiler suite (e.g. GNU tool-chain). If you use Debian or Ubuntu Linux you can get the GNU tool-chain using
$ aptitude install build-essential
- The next step is to download and unpackage the R 2.11.1 source files, download the patch, and apply the patch. From a shell (replacing lib.stat.cmu.edu with your favorite CRAN mirror)
$ cd /usr/src/ $ wget http://lib.stat.cmu.edu/CRAN/src/base/R-2/R-2.11.1.tar.gz $ tar -xvzf R-2.11.1.tar.gz $ cp -r R-2.11.1 R-2.11.1-serial $ cd R-2.11.1-serial/ $ wget http://biostatmatt.com/R/R-2.11.1-serial+conf.patch $ patch -p4 < R-2.11.1-serial+conf.patch
- The last step is to configure, build, and optionally install the patched version of R. If you don't want to install, you can still use the patched executables in the bin/ directory. From the shell
$ ./configure $ make $ make install
Elegant Patch Job
- Applying the patch requires that you have autoconf, automake, m4, and libtool to update the configuration script. R version 2.11.1 uses autoconf version 2.65, and earlier versions may not work properly. I recommend installing the most recent versions of each of these tools. You can download the sources for each of these tools from the GNU website. Installation of the autotools involves a series of ./configure, make, and make install commands. However detailed instructions are given in the source packages. In addition to the autotools, you will need the patch and make programs, and a compiler suite (e.g. GNU tool-chain). If you use Debian or Ubuntu Linux you can get the GNU tool-chain using
$ aptitude install build-essential
- The next step is to download and unpackage the R 2.11.1 source files, download the patch, and apply the patch. From a shell (replacing lib.stat.cmu.edu with your favorite CRAN mirror)
$ cd /usr/src/ $ wget http://lib.stat.cmu.edu/CRAN/src/base/R-2/R-2.11.1.tar.gz $ tar -xvzf R-2.11.1.tar.gz $ cp -r R-2.11.1 R-2.11.1-serial $ cd R-2.11.1-serial/ $ wget http://biostatmatt.com/R/R-2.11.1-serial.patch $ patch -p4 < R-2.11.1-serial.patch
- The next step is to rebuild the configure script using autoreconf. From the shell
$ autoreconf -I m4
- The last step is to configure, build, and optionally install the patched version of R. If you don't want to install, you can still use the patched executables in the bin/ directory. From the shell
$ ./configure $ make $ make install
Disclaimer
This patch is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This patch is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this patch. If not, see http://www.gnu.org/licenses/.
Very intriguing. I would love to have a patched R version to test this out.
Too bad it does not work with standard Mac OS X 10.6 Snow Leopard
because I can find no easy way to assemble the needed utilities without compromising OSX 10.6 itself.
Perhaps some advice would help. Perhaps OS X is not compatible period.
I would recommend (hope) that a more standard R package could be developed
to implement the tty connection. - AW
I had originally wanted to write a package to do this sort of thing. The trouble is, the R connections API is not accessible to user extension packages. Hence, any tty interface in a package could not provide an R "connection", and not use the associated tools, (i.e. readBin, writeBin). I have discussed this issue on the R-devel mailing list without any real conclusion.
Unfortunately, I don't have a Mac, so I'm not of much help with the build toolchain on OS X. But maybe this link would help
http://developer.apple.com/technologies/tools/xcode.html
Mac OS X is certainly compatible. I consulted the Mac OS X documentation while writing this patch, and it is essentially the same as the BSD and Linux documentation. The tty interface is essentially identical between Mac OS X and other POSIX compliant systems.