Rya*_*n B 5 windows serial-port r uart
我想从串行端口绘制实时数据。我认为R是完成这项工作的好工具。我在尝试从串行端口(COM4)读取数据时遇到了麻烦。我已经验证了数据是通过terra项传入的(并在尝试R之前关闭了会话),但是我似乎无法在R中得到任何东西。
我检查了一些地方,包括这些线程: 如何在Windows上调用使用scan()的脚本? 如何在脚本中包括交互式输入以从命令行运行
我还在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)
Run Code Online (Sandbox Code Playgroud)
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,
Run Code Online (Sandbox Code Playgroud)
再次感谢您提供的所有帮助!