硬件开发不可避免要与串口打交道,使用python下的pyserial可以使串口测试和设备调用的工作自动化。这里介绍pyserial的一些基础知识。
<strong> 1、安装pyserial</strong>
linux上直接安装:
#python2
sudo pip install pyserial
#或者python3
sudo pip3 install pyserial
<strong> 2、Bytes与string的转换</strong>
很多串口使用的数据是byte格式,需要进行转换。
使用str.decode()将ASCII转为String的Unicode。
使用bytes.encode(str,"ASCII")将Unicode转为ASCII的byte格式。
使用string.strip()去除后面的回车、换行等无效字符。
声明:s为字符串,rm为要删除的字符序列