python通过serial模块去与串口通信
1、安装python
一般mac自带python
如果没有
通过brew安装python
brew install python 或brew install python3
2、安装pyserial
pip install pyserial 根据实际情况来,如果是python3则用pip3 install pyserial
3、python 代码
3.1
import serial
import time
ser=serial.Serial("/dev/tty.usbserial-1420",2000000,timeout=0.5)
ser.open
index=15
while(index>0):
s=ser.read(64)
print(s)
index = index-1
ser.write("reset\n".encode())
print("after reset")
index2=15
while(index2>0):
s2=ser.read(64)
print(s2)
index2 = index2-1
time.sleep(1)
ser.close
3.2 python 代码优化,接收入参
import serial
import time
import sys
def serialCtl(port,baute,cmd):
ser=serial.Serial(port,baute,timeout=0.5)
print(ser.name)
ser.open
index=15
while(index>0):
s=ser.read(64)
print(s)
index = index-1
ser.write((cmd+"\n").encode())
print("after reset")
index2=15
while(index2>0):
s2=ser.read(64)
print(s2)
index2 = index2-1
time.sleep(1)
ser.close
a=sys.argv[1]
b=sys.argv[2]
c=sys.argv[3]
print("get para:"+a+" "+str(b)+" "+c)
serialCtl(a,int(b),c)