A serial Connection for R

[This article was first published on BioStatMatt » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

***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

  1. 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
  2. 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
  3. 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

  1. 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
  2. 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
    
  3. The next step is to rebuild the configure script using autoreconf. From the shell
    $ autoreconf -I m4
    
  4. 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/.

To leave a comment for the author, please follow the link and comment on their blog: BioStatMatt » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)