当前位置: 移动技术网 > IT编程>脚本编程>Python > 树莓派 serial 串口发送16进制hex python2.7/3.0+

树莓派 serial 串口发送16进制hex python2.7/3.0+

2020年11月12日  | 移动技术网IT编程  | 我要评论
树莓派 serial 串口发送16进制hex python2.7/3.0+ 适用首先需要找到树莓派的串口是哪个,并且确保树莓派的串口设置已经打开。raspi-config 里面的。注意寻找对应的串口设备。python -m serial.tools.list_ports绑定串口,设置连接波特率等参数 python代码如下,其中py2 和 py3 在发送hex数据的方法有所不同。

树莓派 serial 串口发送16进制hex python2.7/3.0+ 适用

  1. 首先需要找到树莓派的串口是哪个,并且确保树莓派的串口设置已经打开。raspi-config 里面的。注意寻找对应的串口设备。
python -m serial.tools.list_ports
  1. 绑定串口,设置连接波特率等参数 python代码如下,其中py2 和 py3 在发送hex数据的方法有所不同。
    代码如下:
import serial
import time
red_on = '01050000ff008C3A'
red_off = '010500000000CDCA'
s = serial.Serial('com11', 9600, bytesize=8, stopbits=1, parity='N', timeout=0.5)
# d = bytes.fromhex(red_on)  # off
d = bytes.fromhex(red_off)  # off # python3

# d = red_on.decode('hex') # a.decode('hex') # off # python2
# d = red_off.decode('hex')


a = s.write(d)

time.sleep(2)  # must stop
s.close()

总结

以上便是 树莓派 serial 串口发送16进制hex python2.7/3.0+ 适用的解决方案。

本文地址:https://blog.csdn.net/qq_44880154/article/details/109636345

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网