如何在python中从pyserialtransfer的数字列表中重建结构。

我试图通过来回传递一个通用结构在我的笔记本电脑& 我的arduino之间进行双向通信,但在解包Arduino返回的字节时遇到了麻烦。使用pySerialTransfer,似乎rxBuff返回了一个数字列表,我不知道如何将其转换为一个字节串,让struct.unpack渲染回我开始的数字。

目前,Arduino代码只是简单地接收数据并将其吐回,Arduino方面没有进行转换或操作。这是我的Arduino代码。

#include "SerialTransfer.h"

SerialTransfer myTransfer;

struct POSITION {
  long id;
  float azimuth;
  float altitude;
} position;


void setup()
{
  Serial.begin(9600);
  myTransfer.begin(Serial);
}

void loop()
{
  if(myTransfer.available())
  {

    myTransfer.rxObj(position, sizeof(position));

    myTransfer.txObj(position, sizeof(position));
    myTransfer.sendData(sizeof(position));

  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");

    if(myTransfer.status == -1)
      Serial.println(F("CRC_ERROR"));
    else if(myTransfer.status == -2)
      Serial.println(F("PAYLOAD_ERROR"));
    else if(myTransfer.status == -3)
      Serial.println(F("STOP_BYTE_ERROR"));
  }
}

还有我的python代码

import time
import struct
from pySerialTransfer import pySerialTransfer


def StuffObject(txfer_obj, val, format_string, object_byte_size, start_pos=0):
  """Insert an object into pySerialtxfer TX buffer starting at the specified index.

  Args:
    txfer_obj: txfer - Transfer class instance to communicate over serial
    val: value to be inserted into TX buffer
    format_string: string used with struct.pack to pack the val
    object_byte_size: integer number of bytes of the object to pack
    start_pos: index of the last byte of the float in the TX buffer + 1

  Returns:
    start_pos for next object
  """
  val_bytes = struct.pack(format_string, *val)
  for index in range(object_byte_size):
    txfer_obj.txBuff[index + start_pos] = val_bytes[index]
  return object_byte_size + start_pos


if __name__ == '__main__':
  try:
    link = pySerialTransfer.SerialTransfer('/dev/cu.usbmodem14201', baud=9600)

    link.open()
    time.sleep(2) # allow some time for the Arduino to completely reset
    base = time.time()

    while True:
      time.sleep(0.2)

      sent = (4, 1.2, 2.5)
      format_string = 'iff'
      format_size = 4+4+4
      StuffObject(link, sent, format_string, format_size, start_pos=0)
      link.send(format_size)

      start_time = time.time()
      elapsed_time = 0
      while not link.available() and elapsed_time < 2:
        if link.status < 0:
          print('ERROR: {}'.format(link.status))
        else:
          print('.', end='')
        elapsed_time = time.time()-start_time
      print()

      response =  link.rxBuff[:link.bytesRead]
      print(response)
      response = struct.unpack(format_string, response)

      print('SENT: %s' % str(sent))
      print('RCVD: %s' % str(response))
      print(' ')

  except KeyboardInterrupt:
    link.close()

当我运行python代码时,我得到了以下的错误。

Traceback (most recent call last):
  File "double_float.py", line 54, in <module>
    response = struct.unpack(format_string, response)
TypeError: a bytes-like object is required, not 'list'

但就在错误之前,我打印了响应: [4, 0, 0, 0, 154, 153, 153, 63, 0, 0, 32, 64].

我如何将这个列表转换成 struct.unpack 可以使用的东西?或者我如何直接解包那个数字列表?非常感谢

解决方案:

这是你要找的吗。

import struct
data = [4, 0, 0, 0, 154, 153, 153, 63, 0, 0, 32, 64]
binary_str = bytearray(data)
result = struct.unpack("<lff", binary_str)
print(result)

輸出

(4, 1.2000000476837158, 2.5)

给TA打赏
共{{data.count}}人
人已打赏
解决方案

Next.jsReact在组件中生成随机值时发出警告

2022-4-22 12:08:53

解决方案

将熊猫移动窗口与列表进行比较,找出错误最少的窗口。

2022-4-22 12:08:55

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索