与pyserial二进制数据(蟒蛇串口) [英] Binary data with pyserial(python serial port)

查看:445
本文介绍了与pyserial二进制数据(蟒蛇串口)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

serial.write()方法似乎只发送字符串数据。我有一个像[0xc0,0x04,0x00]数组,并希望能够通过串口发送/接收呢?是否有原始I任何单独的方法/ O?

serial.write() method in pyserial seems to only send string data. I have arrays like [0xc0,0x04,0x00] and want to be able to send/receive them via the serial port? Are there any separate methods for raw I/O?

我想我可能需要将阵列更改为['\\ XC0','\\ X04','\\ x00的'],仍在,空字符可能会带来问题。

I think I might need to change the arrays to ['\xc0','\x04','\x00'], still, null character might pose a problem.

推荐答案

您需要将您的数据转换为字符串

You need to convert your data to a string

"\xc0\x04\x00"

空字符不在Python中的一个问题 - 字符串不是空终止的零字节的行为就像一个字节\\ X00

这样做的一种方式:

>>> import array
>>> array.array('B', [0xc0, 0x04, 0x00]).tostring()
'\xc0\x04\x00'

这篇关于与pyserial二进制数据(蟒蛇串口)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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