0%

python串口发送数据示例

python3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import time
import serial

#RS232指令,使用一个字典,把需要被操作的RS232命令封装到一起
RS232_Command = {
'command_hex1': 'A5 5A 03 00 03',
'command_hex2': '6B 30 31 73 50 32 30 30 0D',
}

#把16进制的数据转换为bytes数据流进行发送,RS232命令发送函数
def serial_sent_hex(command):
#bytes.fromhex(),使用这个函数进行数据转换,可以把16进制的数值转换字节数据(即比特流,字符串与比特流间还可以用encode()和decode()进行编解码)
var = bytes.fromhex(RS232_Command["%s" % command])
ser.write(var)
data = ser.read(10)
# 获取指令的返回值,并且进行类型转换,转换为字符串后便可以进行字符串对比,因而便可以根据返回值进行判断是否执行特定功能
data = str(data, encoding="utf-8")
return data

if __name__ == '__main__':
#实现串口的连接
ser = serial.Serial('COM29', 9600, timeout=3)
command_hex1 = serial_sent_hex('command_hex1')
#command_hex2 = serial_sent_hex('command_hex2')

python2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import time
import serial

#RS232指令,使用一个字典,把需要被操作的RS232命令封装到一起
RS232_Command = {
'command_hex1': 'A5 5A 04 00 04',
'command_hex2': '6B 30 31 73 50 32 30 30 0D',
}

#把16进制的数据转换为bytes数据流进行发送,RS232命令发送函数
def serial_sent_hex(command):
#bytes.fromhex(),使用这个函数进行数据转换,可以把16进制的数值转换字节数据(即比特流,字符串与比特流间还可以用encode()和decode()进行编解码)
var = bytearray.fromhex(RS232_Command["%s" % command])
ser.write(var)
data = ser.read(10)
# 获取指令的返回值,并且进行类型转换,转换为字符串后便可以进行字符串对比,因而便可以根据返回值进行判断是否执行特定功能
data = str(data)
return data

if __name__ == '__main__':
#实现串口的连接
ser = serial.Serial('COM29', 9600, timeout=3)
command_hex1 = serial_sent_hex('command_hex1')
#command_hex2 = serial_sent_hex('command_hex2')

python基于pythonQT5和pyserial编写串口助手

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from PyQt5 import QtWidgets, QtCore, QtSerialPort

class SerialAssistant(QtWidgets.QWidget):
def __init__(self):
super().__init__()

# 创建串口对象
self.serial = QtSerialPort.QSerialPort()

# 创建界面控件
self.port_combo = QtWidgets.QComboBox()
self.baudrate_combo = QtWidgets.QComboBox()
self.open_button = QtWidgets.QPushButton('打开串口')
self.close_button = QtWidgets.QPushButton('关闭串口')
self.send_button = QtWidgets.QPushButton('发送')
self.receive_text = QtWidgets.QTextEdit()

# 初始化界面
self.init_ui()

# 绑定信号和槽函数
self.open_button.clicked.connect(self.open_serial)
self.close_button.clicked.connect(self.close_serial)
self.send_button.clicked.connect(self.send_data)
self.serial.readyRead.connect(self.receive_data)

def init_ui(self):
layout = QtWidgets.QVBoxLayout()
form_layout = QtWidgets.QFormLayout()

# 添加串口选项
ports = QtSerialPort.QSerialPortInfo.availablePorts()
for port in ports:
self.port_combo.addItem(port.portName())

# 添加波特率选项
baudrates = ['1200', '2400', '4800', '9600', '19200', '38400', '57600', '115200']
for baudrate in baudrates:
self.baudrate_combo.addItem(baudrate)

form_layout.addRow('串口:', self.port_combo)
form_layout.addRow('波特率:', self.baudrate_combo)

layout.addLayout(form_layout)
layout.addWidget(self.open_button)
layout.addWidget(self.close_button)
layout.addWidget(self.send_button)
layout.addWidget(QtWidgets.QLabel('接收数据:'))
layout.addWidget(self.receive_text)

self.setLayout(layout)
self.setWindowTitle('串口调试助手')

def open_serial(self):
port_name = self.port_combo.currentText()
baudrate = int(self.baudrate_combo.currentText())

# 设置串口参数
self.serial.setPortName(port_name)
self.serial.setBaudRate(baudrate)
self.serial.setDataBits(QtSerialPort.QSerialPort.Data8)
self.serial.setParity(QtSerialPort.QSerialPort.NoParity)
self.serial.setStopBits(QtSerialPort.QSerialPort.OneStop)
self.serial.setFlowControl(QtSerialPort.QSerialPort.NoFlowControl)

if self.serial.open(QtCore.QIODevice.ReadWrite):
QtWidgets.QMessageBox.information(self, '提示', '串口打开成功')
else:
QtWidgets.QMessageBox.warning(self, '错误', '串口打开失败')

def close_serial(self):
self.serial.close()
QtWidgets.QMessageBox.information(self, '提示', '串口已关闭')

def send_data(self):
data = self.receive_text.toPlainText().encode()
self.serial.write(data)

def receive_data(self):
data = self.serial.readAll()
self.receive_text.append(data.data().decode())

if __name__ == '__main__':
app = QtWidgets.QApplication([])
window = SerialAssistant()
window.show()
app.exec_()