【发布时间】:2016-04-04 00:22:16
【问题描述】:
我想从串口绘制实时数据。我认为 R 将是完成这项工作的好工具。我在尝试从串行端口(COM4)读取数据时磕磕绊绊。我已经验证数据是通过 terra term 进入的(并在尝试 R 之前关闭会话),但我似乎无法在 R 中得到任何东西。
我检查了几个地方,包括这些线程: How to invoke script that uses scan() on Windows? How to include interactive input in script to be run from the command line
我还在 R 论坛上找到了这个旧帖子: https://stat.ethz.ch/pipermail/r-help/2005-September/078929.html
这些已经让我走到了这一步,但我似乎无法真正从串行端口将任何数据输入 R。
此时我可以使用 VBA 在 excel 中流式传输数据,但我想在 R 中进行,以便更好地实时绘制和过滤数据。
编辑:感谢到目前为止的帮助。我只是在编写此编辑时让它工作,所以这里是代码:
#
# 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 最终成为一个长字符串,其中包含我想要的新行:
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,
再次感谢大家的帮助!
【问题讨论】:
-
我发现了这一点,它暂时有所帮助,但如果能够在 R 中提取数据并更好地控制事物,那就太好了:hackaday.io/project/5334-serialplot/log/…
-
我编辑了问题以反映我到目前为止的进度。
标签: r windows serial-port uart