在python3.5中使用serial模块进行串口通信,通过while 1循环进行持续性的设备通信,但是第一个设备完成之后对下一个设备进行通信会有问题:Attempting to use a port that is not open
一下是我的代码:
import serial
import time
import base64
import hmac
import pymysql
from esptool import ESP8266StubLoader
from esptool import erase_flash, read_mac, write_flash
from log import TNLog
values = ['0x01000', 'D:\esp8266\user1.4096.new.4(3).bin']
values = ['0x01000', '/home/pi/Interface/Anthink/esp8266/user1.4096.new.4(3).bin']
v1 = ['0x00000', 'D:\esp8266\boot_v1.7.bin']
v1 = ['0x00000', '/home/pi/Interface/Anthink/esp8266/boot_v1.7.bin']
v2 = ['0x3fb000', 'D:\esp8266\blank.bin']
v2 = ['0x3fb000', '/home/pi/Interface/Anthink/esp8266/blank.bin']
v3 = ['0x3fe000', 'D:\esp8266\blank.bin']
v3 = ['0x3fe000', '/home/pi/Interface/Anthink/esp8266/blank.bin']
v4 = ['0x3fc000', 'D:\esp8266\esp_init_data_default_v08.bin']
v4 = ['0x3fc000', '/home/pi/Interface/Anthink/esp8266/esp_init_data_default_v08.bin']
argfile = open(values[1], 'rb')
a1 = open(v1[1], 'rb')
a2 = open(v2[1], 'rb')
a3 = open(v3[1], 'rb')
a4 = open(v4[1], 'rb')
db = pymysql.connect("192.168.0.250", "dev_v7", "vvvvvv7", "db_iot_v7", charset='utf8')
cursor = db.cursor()
logger = TNLog()
prorts = serial.serial_for_url('COM3')
prorts = serial.serial_for_url('COM3')
prorts.baudrate = 115200
s = 1019300100006626 # 扫码得到的设备SN
自定义属性值
class WriteOpthons(object):
def init(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
设备初始化
class MyClass(object):
def init(self):
self._port = prorts
self._trace_enabled = False
logger.info('接口初始化已经完成:{}'.format(self._port))
设备调试
class SerialPort():
logger.info('现在开始进行调试硬件设备操作!')
message = ''
def connectport(self, com, baud):
try:
super(SerialPort, self).__init__()
self.port = serial.Serial(com, baud)
try:
if self.port.isOpen():
logger.info('已经连接OK:blfadd:connect port is ok')
print("blfadd:connect port is ok")
return 1
except Exception as e: # 捕获异常输出并终止运行
logger.error('硬件连接失败:{}'.format(e))
return 0
raise
except Exception as e:
logger.error('硬件调试连接操作出错:{}'.format(e))
def restart(self):
self.port.write()
def restart(self):
self.port.setDTR(True) # 0wei gao
self.port.setRTS(True)
time.sleep(1.5)
self.port.setDTR(True)
self.port.setRTS(False)
def setDTR(self, state):
self.port.setDTR(state)
def read_data(self):
global timeStamp
test1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
start_time = time.strptime(test1, "%Y-%m-%d %H:%M:%S")
# strftime是转换为特定格式输出,而strptime是将一个(时间)字符串解析为时间的一个类型对象。一个是按照想要的格式,去转换。重点是格式!另外一个不管什么格式,我只要把特定的时间字符串转成时间类型即可!
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
while True:
self.message = self.port.readline().decode('utf8', 'ignore')
test2 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
end_time = time.strptime(test2, "%Y-%m-%d %H:%M:%S")
timeStamp = int(time.mktime(end_time)) - int(time.mktime(start_time))
if (self.message[0:10] == "uart begin" and timeStamp <= 80):
return 1
elif (timeStamp > 90):
raise Exception("blfadd:Time out")
def getwifiinfo(self, query_command, catchnum): # catchnum增加参数为捕获的wifi数量
try:
global count # 定义变量couunt用来计数
global macI