从Python到MATLAB的实时数据传输 [英] Real-time data transfer from Python to MATLAB

查看:138
本文介绍了从Python到MATLAB的实时数据传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python从USB输入设备读取数据.我想知道是否可以通过MATLAB实时与模型交换这种方法.我现在要做的是将读取的数据保存到.mat文件中,然后让模型从那里读取数据,这不是很直观.我为此使用的代码如下:

I am using python to read data from a USB input device. I would like to know if there is a way this could be exchanged with the model in MATLAB real-time. How I do it now is to save the data read in a .mat file and then let the model read it from there, which is not very intuitive.The code I use for this is as below:

    #Import the needed libraries
    import usb.core,usb.util,sys,time
    import sys
    from array import *
    import scipy.io

    #find our device
    dev = usb.core.find(idVendor=0x046d, idProduct=0xc29a)

    # was it found?
    if dev is None:
       raise ValueError('Device not found')

   # set the active configuration. With no arguments, the first
   # configuration will be the active one
   try:
     dev.set_configuration()
     #In the event of an error
   except usb.core.USBError as e:
     print('Cannot set configuration the device: %s' %str(e))
     sys.exit()

   # get an endpoint instance
   cfg = dev.get_active_configuration()
   intf = cfg[(0,0)]
   ep = usb.util.find_descriptor(
               intf,
     # match the first IN endpoint
     custom_match = \
     lambda e: \
     usb.util.endpoint_direction(e.bEndpointAddress) == \
     usb.util.ENDPOINT_IN)

  #Initialising variables
  #Databases for access in MATLAB
  gas_pedal_data={}
  brake_pedal_data={}
  steering_wheel_bit5_data={}
  steering_wheel_bit6_data={}
  gas_pedal=[]
  brake_pedal=[]
  steering_wheel_bit5=[]
  steering_wheel_bit6=[]
  i=0
  #Read data from the device as long as it is connected
  while(dev!= None):
       try:
          #Read data
          data = dev.read(ep.bEndpointAddress, ep.wMaxPacketSize,
                          timeout=1000)
          gas_pedal.append(data[6])
          gas_pedal_data['gas_pedal']=gas_pedal
          scipy.io.savemat('test.mat',gas_pedal_data)
          brake_pedal.append(data[7])
          brake_pedal_data['brake_pedal']=brake_pedal
          scipy.io.savemat('test.mat',brake_pedal_data)
          steering_wheel_bit5.append(data[4])
          steering_wheel_bit5_data['steering_wheel_bit5']=steering_wheel_bit5
          scipy.io.savemat('test.mat',steering_wheel_bit5_data)
          steering_wheel_bit6.append(data[5])
          steering_wheel_bit6_data['steering_wheel_bit6']=steering_wheel_bit6
          scipy.io.savemat('test.mat',steering_wheel_bit6_data)
       except usb.core.USBError as e:
          print("Error readin data: %s" %str(e))
          time.sleep(5)
          continue

推荐答案

您有一些选择.

  • 您可以在Matlab中轮询文件是否存在,然后在可用时读入新数据
  • 您可以打开管道以在python和matlab之间执行进程间通信(也需要从matlab端进行轮询).参见此处以获取代码.
  • 您可以使用本地UDP或TCP套接字进行通信.通过使用 PNET (仍然需要轮询)或matlab仪器控制工具箱(可让您配置回调函数).
  • You can poll from within Matlab for the presence of a file, then read in the new data when available
  • You can open a pipe to perform inter-process communication between python and matlab (also requires polling from the matlab side). See here for code.
  • You can use a local UDP or TCP socket for communication. Either by using PNET (which will still require polling), or the matlab Instrument Control Toolbox (which allows you to configure a callback function).

由于matlab是单线程的,因此在设计模型时必须考虑提供新数据.当提供新数据时,您将需要明确触发模型以重新评估.

Since matlab is single-threaded, your model will have to be designed with the provision of new data in mind. You will need to explicitly trigger the model to re-evaluate when new data is provided.

这篇关于从Python到MATLAB的实时数据传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆