7

I'm wanting to plot live data from the serial port. I figured R would be a good tool for the job. I'm stumbling on trying to read data from the serial port (COM4). I've verified the data is coming in through terra term (and close the session before trying R), but I can't seem to get anything in R.

I've checked a few places, including these threads: How to invoke script that uses scan() on Windows? How to include interactive input in script to be run from the command line

I've also found this old thread on the R forum: https://stat.ethz.ch/pipermail/r-help/2005-September/078929.html

These have gotten me this far, but I can't seem to actually get any data into R from the serial port.

At this point I can stream in the data in excel using VBA, but I'd like to do it in R for some nicer live plotting and filtering of the data.

Edit: Thanks for the help so far. I just got it working while writing up this edit, so here's the code:

#
# Reset environment
#
rm(list = ls())         # Remove environemnent variables
graphics.off()          # Close any open graphics

#
# Libraries
#
library(serial)

#
# Script
#

con <- serialConnection(name = "test_con",
                        port = "COM11",
                        mode = "115200,n,8,1",
                        buffering = "none",
                        newline = 1,
                        translation = "cr")

open(con)

stopTime <- Sys.time() + 2
foo <- ""
textSize <- 0
while(Sys.time() < stopTime)
{
    newText <- read.serialConnection(con)
    if(0 < nchar(newText))
    {
        foo <- paste(foo, newText)
    }
}

cat("\r\n", foo, "\r\n")

close(con)

foo ends up being a long string with new lines the way I want them:

3181, -53120, -15296, 2,  
3211, -53088, -15328, 2,  
3241, -53248, -15456, 1,  
3271, -53216, -15424, 2,  
3301, -53184, -15488, 2,  
3331, -53344, -15360, 1,  
3361, -53440, -15264, 1,

enter image description here

Thanks again for all the help!

Community
  • 1
  • 1
Ryan B
  • 457
  • 1
  • 4
  • 15
  • I found this, and it helps for the moment, but it would be nice to be able to pull up the data in R and have more control over things: https://hackaday.io/project/5334-serialplot/log/26735-serialplot-v06-commands-snapshots – Ryan B Dec 30 '15 at 04:07
  • I edited the question to reflect my progress so far. – Ryan B Jan 08 '16 at 01:56

2 Answers2

5

i am working with the serial-package (here) available on CRAN. This was developed to do exactly that what you need. Reading and sending data form and to RS232 etc. connections. I do really recommend this, because "mode.exe" seems not to work for virtual COM-ports. See NPort-Server etc.

Seily
  • 466
  • 3
  • 7
  • Thanks. I'll have to check that out when I get in today. – Ryan B Feb 24 '16 at 15:20
  • Sorry, I've been busy with some other things. I tried it out this afternoon. Writing worked fine, but reading still eludes me. I'll update the question. – Ryan B Mar 10 '16 at 23:16
  • And I got it working up while editing my question. Thanks for the help! – Ryan B Mar 10 '16 at 23:28
0

Teraterm and Windows use a different mechanism to configure serial devices. Are your system connection settings ok compared to what is configured in teraterm? Re-check the configuration parameter in teraterm and then use them to set your COM4: configuration in R.

system("mode COM4: BAUD=115200 PARITY=N DATA=8 STOP=1")

see mode /? on your command prompt for further parameters

it might also be helpful to read data character by character using readChar()

It sometimes happens that teraterm doesn't close RS232 connections properly.

Frank
  • 180
  • 1
  • 10
  • Yeah, I checked everything in Terraterm and in the command prompt. The connection is closed, or I get an error on opening the connection. The program runs to completion, so that tells me it opens the connection, reads (nothing), and then closes the connection successfully. I'll give readChar() a try. – Ryan B Dec 30 '15 at 20:40
  • Please let us know about your progress – Frank Jan 07 '16 at 08:18
  • I can get it working with VBA in excel, but R is giving me headaches. I can write to the serial port, but reading seems inconsistent. Sometimes I get a full line, sometimes partial, and sometimes none. What I would expect is 2 seconds worth of lines. I'll post my current code to the question. – Ryan B Jan 08 '16 at 01:51