1、需要导入pyserial 的模块 Python2.7
脚本如下:
# ! /usr/bin/env python # -*- coding: utf-8 -*- import sys import time reload(sys); sys.setdefaultencoding("utf-8") import serial # 导入串口模块 import threading import time import serial ser = serial.Serial("COM3",115200) send = ser.write(" ") send = ser.write(" ") time.sleep(2) send = ser.write("root") send = ser.write(" ") print ser.portstr print send time.sleep(2) send = ser.write("nE7jA%5m") send = ser.write(" ") print send time.sleep(2) send = ser.write("export") send = ser.write(" ") print send time.sleep(2) data = "" while ser.in_waiting > 0 : data+=ser.read(1) if data != "": print data
2、查看端扣列表 py2.7
#-*- coding: utf-8 -*-
import sys;
reload(sys);
sys.setdefaultencoding("utf-8")
import serial #导入模块
import serial.tools.list_ports
port_list = list(serial.tools.list_ports.comports())
print(port_list)
if len(port_list) == 0:
print('无可用串口')
else:
for i in range(0,len(port_list)):
print(port_list[i])