首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将数据从LabVIEW连续发送到R?(代码帮助)

如何将数据从LabVIEW连续发送到R?(代码帮助)
EN

Stack Overflow用户
提问于 2019-07-13 02:52:23
回答 1查看 207关注 0票数 1

我正在尝试将来自LabVIEW (轴承振动和温度)的实时数据导入到一个用R编写的应用程序中,以创建控制图。它工作了一段时间,但最终崩溃,并显示以下错误消息:

代码语言:javascript
运行
复制
Error in aggregate.data.frame(B, list(rep(1:(nrow(B)%/%n + 1), each = n,  :
    no rows to aggregate

该过程的工作方式是LabVIEW获取数据并将其投影到两个Excel文件中。这些文件在R代码中读取,并用于在R中投影控制图。该过程成功了一段时间,失败时刻并不总是相同的时间量。有时控制图会运行6-7分钟,有时会在2分钟内崩溃。

我怀疑是Excel文件更新得不够快,所以R代码会在Excel文件为空时尝试读取它。

任何建议都是很棒的!谢谢!

我已经尝试降低每秒采集的样本大小。但这并不管用。

代码语言:javascript
运行
复制
getwd()
setwd("C:/Users/johnd/Desktop/R Data")

while(1) {

  A = fread("C:/Users/johnd/Desktop/R Data/a1.csv" , skip = 4  , header = FALSE , col.names = c("t1","B2","t2","AM","t3","M","t4","B1"))
  t1 = A$t1
  B2 = A$B2
  t2 = A$t2
  AM = A$AM
  t3 = A$t3
  M = A$M
  t4 = A$t4
  B1 = A$B1

  B = fread("C:/Users/johnd/Desktop/R Data/b1.csv" , skip = 4  , header = FALSE , col.names = c("T1","small","T2","big"))
  T1 = B$T1
  small = B$small
  T2 = B$T2
  big = B$big

  DJ1 = A[seq(1,nrow(A),1),c('t1','B2','AM','M','B1')]
  DJ1

  n = 16
  DJ2 = aggregate(B,list(rep(1:(nrow(B)%/%n+1),each=n,len=nrow(B))),mean)[-1]
  DJ2

  #------------------------------------------------------------------------
  DJ6 = cbind(DJ1[,'B1'],DJ2[,c('small','big')]) # creates matrix for these three indicators
  DJ6


  #--------------T2 Hand made---------------------------------------------------------------------

  new_B1 = DJ6[,'B1']
  new_small = DJ6[,'small']   ### decompose the DJ6 matrix into vectors for each indicator(temperature, big & small accelerometers)
  new_big = DJ6[,'big']

  new_B1
  new_small
  new_big

  mean_B1 = as.numeric(colMeans(DJ6[,'B1']))
  mean_small = as.numeric(colMeans(DJ6[,'small']))    ##decomposes into vectors of type numeric 
  mean_big = as.numeric(colMeans(DJ6[,'big']))

  cov_inv = data.matrix(solve(cov(DJ6)))   # obtain inverse covariance matrix 
  cov_inv

  p = ncol(DJ6) #changed to pull number of parameters by taking the number of coumns in OG matrix   #p=3   # #ofQuality Characteristics 
  m=64 # #of samples (10 seconds of data)
  a_alpha = 0.99
  f= qf(a_alpha , df1 = p,df2 = (m-p))  ### calculates the F-Statistic for our data    
  f
  UCL = (p*(m+1)*(m-1)*(f))/(m*(m-p))   ###produces upper control limit 
  UCL

  diff_B1 = new_B1-mean_B1
  diff_small = new_small-mean_small
  diff_big = new_big-mean_big

  DJ7 = cbind(diff_B1, diff_small , diff_big) #produces matrix of difference between average and observations (x-(x-bar))
  DJ7
  # DJ8 = data.matrix(DJ7[1,])
  # DJ8
  DJ9 = data.matrix(DJ7)     ### turns matrix into appropriate numeric form   
  DJ9

  # T2.1.1 = DJ8 %*% cov_inv %*% t(DJ8)
  # T2.1.1

  # T2.1 = t(as.matrix(DJ9[1,])) %*% cov_inv %*% as.matrix(DJ9[1,])
  # T2.1

  #T2 <- NULL
  for(i in 1:64){   #### creates vector of T^2 statistic 

    T2<- t(as.matrix(DJ9[i,])) %*% cov_inv %*% as.matrix(DJ9[i,])   # calculation of T^2 test statistic   ## there is no calculation of x-double bar

    write.table(T2,"C:/Users/johnd/Desktop/R Data/c1.csv",append=T,sep="," , col.names = FALSE)#
     #
    DJ12 <-fread("C:/Users/johnd/Desktop/R Data/c1.csv" , header = FALSE ) #
  }
  # DJ12

  DJ12$V1 = 1:nrow(DJ12)  
  # plot(DJ12 , type='l')

  p1 = nrow(DJ12)-m
  p2 = nrow(DJ12)

  plot(DJ12[p1:p2,], type ='o', ylim =c(0,15), ylab="T2 Chart" , xlab="Data points")  ### plots last 640 points     
  # plot(DJ12[p1:p2,], type ='o' , ylim =c(0,15) , ylab="T2 Chart" , xlab="Data points")
  abline(h=UCL , col="red") ## displays upper control limit 


  Sys.sleep(1)
}
EN

回答 1

Stack Overflow用户

发布于 2019-07-17 13:42:07

过程成功了一段时间,失败时刻并不总是相同的时间量。有时控制图会运行6-7分钟,有时会在2分钟内崩溃。

我怀疑是Excel文件更新得不够快,所以R代码会在Excel文件为空时尝试读取它。

你的怀疑是对的。

在当前的设计中,R应用程序可能会崩溃,这取决于它相对于LabVIEW应用程序的运行速度。这称为争用条件;您必须从代码中消除争用条件。

又快又脏的解决方案

避免崩溃的一个简单解决方案是调用NROW来检查是否存在任何数据。如果没有可用的数据,就不要调用aggregate。这在这里描述:error message in r: no rows to aggregate

更强大的解决方案

更好的解决方案是使用像TCP这样的通信协议将数据从LabVIEW流式传输到R,而不是使用CSV文件来传输实时数据。例如,您的R程序可以侦听TCP套接字上的数据。在运行数据处理代码之前,让它等待从LabVIEW发送数据。

  • 这里是在R中使用socketConnection的示例:是在LabVIEW中通过TCP发送/接收数据的示例:
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57012609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档