How to read data from a serial port in R

I want to write data from a serial port. I figured R would be a good tool to work with. I am trying to read data from a serial port (COM4). I have verified that the data comes in via a ter-term (and closes the session before trying R), but I cannot get anything in R.

I checked several places, including these threads: How to call a script that uses scan () on Windows? How to enable interactive input in a script to run from the command line

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

They have driven me so far, but I just can't get any data in R from the serial port.

At this point, I can transfer data to excel using VBA, but I would like to do it in R for a more pleasant live construction and data filtering.

Edit: Thanks for the help so far. I just got it working when writing this edit, so here is 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 with a long line 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 the help!

+4
source share
2 answers

serial - (), CRAN. , , . RS232 .. , "mode.exe", , COM-. . NPort-Server ..

+3

Teraterm Windows . , teraterm? teraterm, COM4: R.

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

. /?

, readChar()

, teraterm RS232 .

0

Source: https://habr.com/ru/post/1622160/


All Articles