win或linux通过串口与嵌入式通讯

准备

  • 插入usb并打开权限
    1
    2
    ls /dev/ttyUSB*
    sudo chmod 777 /dev/ttyUSB*

python 上位机代码

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
import serial

# 串口打开函数
def open_ser():
port = 'com1' # 串口号
baudrate = 9600 # 波特率
try:
global ser
#ser = serial.Serial(port,baudrate,timeout=0.5)
ser=serial.Serial("/dev/ttyUSB1",9600,timeout=0.5) #使用USB连接串行口
if(ser.isOpen()==True):
print("串口打开成功")
except Exception as exc:
print("串口打开异常",exc)
# 数据发送
def send_msg(data):
try:
#send_datas = input("请输入要发送的数据\n")
send_datas = str(data)
ser.write(str(send_datas).encode("gbk"))
print("已发送数据:",send_datas)
except Exception as exc:
print("发送异常", exc)
# 接收数据
def read_msg():
try:
print("等待接收数据")
while True:
data = ser.read(ser.in_waiting).decode('gbk')
if data != '':
break
print("已接受到数据:",data)
except Exception as exc:
print("读取异常",exc)
# 关闭串口
def close_ser():
try:
ser.close()
if ser.isOpen():
print("串口未关闭")
else:
print("串口已关闭")
except Exception as exc:
print("串口关闭异常", exc)
#查找串口
def find_com():
plist = list(serial.tools.list_ports.comports())

if len(plist) <= 0:
print ("The Serial port can't find!")
else:
plist_0 =list(plist[0])
serialName = plist_0[0]
serialFd = serial.Serial(serialName,9600,timeout = 60)
print ("check which port was really used >",serialFd.name)

open_ser()
data = input("input\n")
send_msg(data)
read_msg()
close_ser()