gpt4 book ai didi

r - 如何从 R 中的串口读取数据

转载 作者:可可西里 更新时间:2023-11-01 13:33:52 26 4
gpt4 key购买 nike

我想绘制来自串行端口的实时数据。我认为 R 将是完成这项工作的好工具。我在尝试从串行端口 (COM4) 读取数据时遇到了麻烦。我已经验证数据是通过 terra term 传入的(并在尝试 R 之前关闭 session ),但我似乎无法在 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,

enter image description here

再次感谢所有的帮助!

最佳答案

我正在使用 CRAN 上提供的 serial-package ( here)。这是为了完全满足您的需求而开发的。读取和发送数据表和RS232等连接。我确实推荐这个,因为“mode.exe”似乎不适用于虚拟 COM 端口。参见 NPort-Server 等。

关于r - 如何从 R 中的串口读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34522535/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com