带有pyserial(python串口)的二进制数据

2023-07-23Python开发问题
52

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

问题描述

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

"xc0x04x00"

空字符在 Python 中不是问题——字符串不是以空字符结尾的,零字节的行为就像另一个字节 "x00".

Null characters are not a problem in Python -- strings are not null-terminated the zero byte behaves just like another byte "x00".

一种方法:

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

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

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10

按10分钟间隔对 pandas 数据帧进行分组
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)...
2024-08-22 Python开发问题
11