如何从 R 中的串口读取数据
How to Read Data from Serial Port in R
我想绘制来自串行端口的实时数据。我认为 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,
再次感谢大家的帮助!
Teraterm 和 Windows 使用不同的机制来配置串行设备。
与 teraterm 中配置的相比,您的系统连接设置是否正常?
Re-checkteraterm 中的配置参数,然后使用它们在 R 中设置您的 COM4: 配置。
系统("mode COM4: BAUD=115200 PARITY=N DATA=8 STOP=1")
查看模式/?在您的命令提示符下输入更多参数
使用 readChar()
逐个字符地读取数据可能也会有所帮助
teraterm 有时无法正确关闭 RS232 连接。
我正在使用 CRAN 上可用的 serial
软件包 (here)。这是为了完全满足您的需求而开发的。读取和发送数据表和RS232等连接。
我确实推荐这个,因为“mode.exe
”似乎不适用于虚拟 COM 端口。参见 NPort-Server 等
我知道这是五年前的事了,但我发现在你的代码中你没有调用握手。
我正在使用 PUTTY 而不是 teraterm 进行类似的工作,我可以在其中看到我的 COM 设备的以下所有输入。
我的命令如下:
con <-serialConnection(name="Prolific USB-to-Serial Comm Port(Com3)",
port="COM3",
mode="9600,n,8,1",
newline=0,
translation="lf",
handshake = 'xonxoff'
)
我想绘制来自串行端口的实时数据。我认为 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,
再次感谢大家的帮助!
Teraterm 和 Windows 使用不同的机制来配置串行设备。 与 teraterm 中配置的相比,您的系统连接设置是否正常? Re-checkteraterm 中的配置参数,然后使用它们在 R 中设置您的 COM4: 配置。
系统("mode COM4: BAUD=115200 PARITY=N DATA=8 STOP=1")
查看模式/?在您的命令提示符下输入更多参数
使用 readChar()
逐个字符地读取数据可能也会有所帮助teraterm 有时无法正确关闭 RS232 连接。
我正在使用 CRAN 上可用的 serial
软件包 (here)。这是为了完全满足您的需求而开发的。读取和发送数据表和RS232等连接。
我确实推荐这个,因为“mode.exe
”似乎不适用于虚拟 COM 端口。参见 NPort-Server 等
我知道这是五年前的事了,但我发现在你的代码中你没有调用握手。 我正在使用 PUTTY 而不是 teraterm 进行类似的工作,我可以在其中看到我的 COM 设备的以下所有输入。
我的命令如下:
con <-serialConnection(name="Prolific USB-to-Serial Comm Port(Com3)",
port="COM3",
mode="9600,n,8,1",
newline=0,
translation="lf",
handshake = 'xonxoff'
)