-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuart.py
More file actions
37 lines (32 loc) · 883 Bytes
/
uart.py
File metadata and controls
37 lines (32 loc) · 883 Bytes
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
import serial#导入串口通信库
from time import sleep
ser = serial.Serial()
def port_open_recv(com):#对串口的参数进行配置
ser.port=com
ser.baudrate=115200
ser.bytesize=8
ser.stopbits=1
ser.parity="N"#奇偶校验位
ser.open()
if(ser.isOpen()):
print("串口打开成功!")
else:
print("串口打开失败!")
#isOpen()函数来查看串口的开闭状态
def port_close():
ser.close()
if(ser.isOpen()):
print("串口关闭失败!")
else:
print("串口关闭成功!")
def send(send_data):
if(ser.isOpen()):
ser.write(send_data.encode('utf-8'))#编码
print("发送成功",send_data)
port_close()
else:
print("发送失败!")
def go(order,com):
port_open_recv(com)
print(order)
send(order)