Binary data with pyserial(python serial port)(带有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串口)的二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有pyserial(python串口)的二进制数据
				
        
 
            
        基础教程推荐
- 包装空间模型 2022-01-01
 - PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
 - 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
 - Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
 - 修改列表中的数据帧不起作用 2022-01-01
 - 求两个直方图的卷积 2022-01-01
 - 在Python中从Azure BLOB存储中读取文件 2022-01-01
 - PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
 - 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
 - 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				