标题: Python使用pyserial进行串口通信 [打印本页]
作者: xiexie 时间: 2018-6-22 13:07 标题: Python使用pyserial进行串口通信
安装pyserialpip install pyserial
import serial.tools.list_ports
plist = list(serial.tools.list_ports.comports())
if len(plist) <= 0:
print("没有发现端口!")
else:
plist_0 = list(plist[0])
serialName = plist_0[0]
serialFd = serial.Serial(serialName, 9600, timeout=60)
print("可用端口名>>>", serialFd.name)
ingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif, SimHei, SimSun; font-size: 16px; text-align: justify;">串口通信ingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif, SimHei, SimSun; font-size: 16px; text-align: justify;">
ingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif, SimHei, SimSun; font-size: 16px; text-align: justify;">ingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif, SimHei, SimSun; font-size: 16px; text-align: justify;">Windows下端口为COM*
ingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif, SimHei, SimSun; font-size: 16px; text-align: justify;">, Ubuntu下为/dev/ttyS0、
import serial
class Ser(object):
def __init__(self):
self.port = serial.Serial(port='COM4', baudrate=9600, bytesize=8, parity='E', stopbits=1, timeout=2)
def send_cmd(self, cmd):
self.port.write(cmd)
response = self.port.readall()
response = self.convert_hex(response)
return response
def convert_hex(self, string):
res = []
result = []
for item in string:
res.append(item)
for i in res:
result.append(hex(i))
return result
[ 本帖最后由 xiexie 于 2018-6-22 13:11 编辑 ]
欢迎光临 PHP开发笔记 (http://phpvi.com/) |
Powered by Discuz! 6.1.0 |