从串行端口二进制数据中构建十六进制阵列



我有一个外部设备,串行连接到PC。数据是二进制的,而不是字符,这意味着我不应该将数据解释为ASCII字符。

在PC中,我有python 3.7,它使用培训读取串行设备。我想用传入的数据填充INT8数组。

我正在使用线程,这是我到目前为止的地方,但这不是我的第一件代码,我尝试了几件事,它们都没有用。

def get_data(sent, m_serport)
    constr_resp = np.int8([0])
    resp = np.int8([0])
    resp_index = 0
        while (1):
            if (m_serport.in_waiting > 0):
                resp = master_ser.read(1)
                constr_resp = np.concatenate(constr_resp, resp)
                resp_index = resp_index + 1
                parse(constr_resp, resp_index)

这个生成以下错误:TypeError:"字节"对象不能解释为整数

我有一个强大的C背景,而Python在数据类型方面对我很困惑。

我希望我的问题很容易理解。

谢谢。

我认为获取数据应该从构建字节列表开始:

def get_data(sent, m_serport)
    alist = []
    resp_index = 0
        while (1):
            if (m_serport.in_waiting > 0):
                resp = master_ser.read(1)
                alist.append(resp)
                resp_index += resp_index
                # parse(alist, resp_index)
    return alist

您可能不需要resp_index,因为它应该是len(alist)。另外,我认为您不希望在此循环中使用parse,但我不知道应该做什么。

kostbil,使用python访问外部数据可能有些棘手,但请相信我,这没什么大不了的。在这种情况下,如果您知道错误是什么行,调试可能会更容易...您也可以使用print((进行调试。

我认为,在执行任何整数操作之前,您可以在您在外部获得的数据上调用Decode方法,即.decode((。说,externaldata.decode((

希望这有效。

最新更新