How to Read Data from Serial Port in R

时光怂恿深爱的人放手 提交于 2019-12-03 10:08:30

问题


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,

Thanks again for all the help!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/34522535/how-to-read-data-from-serial-port-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!