有没有哪位大咖,能帮忙看看以下两个线程,跑了几天或者一个星期之后,串口接收的‘\xFD’开头的数据不能往服务器送了?
两个线程分别如下:
#----------------串口接收处理线程--------------------------#
def uart_read():
global socket
global uart
global is_start,hb_lost_count,CSQZHI,hb_RevJH,hb_cnt,timer0,timer1
global LED_NET_STATUS,timer2,LEDGPIO
global YMDKdatabuf,YMPORTdatabuf,ZHZCBdatabuf,ZHXTdatabuf,XTdatabuf,zcbdatabuf
global uart_revbuf,uart_revFlag,CSQ_HQFlag
global Secure_lock,ATS_lock,network_lock
while True:
try:
if not uart:
break
msglen = uart.any() # 返回是否有可读取的数据长度
# 当有数据时进行读取
if msglen:
msg = uart.read(msglen)
# 初始数据是字节类型(bytes),将字节类型数据进行编码
utf8_msg = msg.decode()
baozhiling_head=bytearray(10)
baozhiling_jiexi=bytearray(60)
databuf = bytearray(60)
baozhiling_head = utf8_msg[0:10]
baozhiling_jiexi = utf8_msg[10:60]
#配置参数命令
if baozhiling_head == ‘usr.cn#AT+’: #包头获取正确
#取其中的部分字节进行判断
#usr.cn#AT+CSQ\r\n
if baozhiling_jiexi[:3] ==‘CSQ’:
CSQ_HQFlag = True
#每判断一次就清一次零
utf8_msg = 0
msg = 0
msglen = 0
#usr.cn#AT+REGDT=FD01104344323030323030FFFFFCC927\r\n
elif baozhiling_jiexi[:6] ==‘REGDT=’:
databuf =baozhiling_jiexi[6:38]
with Secure_lock:
SecureData.Store(9, databuf, len(databuf)) #注册包保存到9-12安全数据区
zcbdatabuf =databuf[0:len(databuf)]
strtohex_ZCB(zcbdatabuf)
uart.write(“usr.cn#\r\nOK\r\n”)
ylq_log.info(‘REG Data: {} \r\n’.format(databuf))
ylq_log.info(‘REG Data: {} \r\n’.format(zcbdatabuf))
ylq_log.info(ZHZCBdatabuf)
#每判断一次就清一次零
utf8_msg = 0
msg = 0
msglen = 0
#ylq_log.info(ZHZCBdatabuf)
#usr.cn#AT+SOCKA=TCP,jk49.chendong-jk.com,50201\r\n
elif baozhiling_jiexi[:6] ==‘SOCKA=’:
databuf = baozhiling_jiexi[10:60]
XRYUMINGlen = 0
#XRDatabuf = bytearray(50)
#databuf = “”
XRDatabuf = “”
for i in range(51):
if databuf[i-1]==‘,’:
XRYUMINGlen = i-1
break
XRDatabuf = databuf[0:XRYUMINGlen]
XRDatabuf += (databuf[XRYUMINGlen+1:XRYUMINGlen+6])#备注:提取端口号是数字,str转字符格式
with Secure_lock:
SecureData.Store(13, XRDatabuf, len(XRDatabuf)) #域名和端口保存到9-12安全数据区
YMDKdatabuf = databuf[0:XRYUMINGlen]
YMPORTdatabuf = databuf[XRYUMINGlen+1:XRYUMINGlen+6]
uart.write(“usr.cn#\r\nOK\r\n”)
ylq_log.info(‘SOCKA Data: {} \r\n’.format(XRDatabuf))
ylq_log.info(‘SOCKA Data: {} \r\n’.format(YMDKdatabuf))
ylq_log.info(‘SOCKA Data: {} \r\n’.format(YMPORTdatabuf))
#每判断一次就清一次零
utf8_msg = 0
msg = 0
msglen = 0
#usr.cn#AT+HEARTDT=AA\r\n
elif baozhiling_jiexi[:7] ==‘HEARTDT’:
databuf = baozhiling_jiexi[8:10] #提取心跳包数据
# 在index为1的存储区域中存储长度为2的数据XTdatabuf 心跳包和心跳间隔保存
with Secure_lock:
SecureData.Store(1, databuf, 2) #心跳数据长度为2存取在index1的区域
XTdatabuf = databuf[0:2]
strtohex_XT(XTdatabuf)
uart.write(“usr.cn#\r\nOK\r\n”) #收到有效指令后返回给控制板
ylq_log.info(‘HEART Data: {} \r\n’.format(databuf))
ylq_log.info(‘HEART Data: {} \r\n’.format(XTdatabuf))
ylq_log.info(ZHXTdatabuf)
#每判断一次就清一次零
utf8_msg = 0
msg = 0
msglen = 0
#usr.cn#AT+REGEN=ON\r\n–>注册包使能
elif baozhiling_jiexi[:5] ==‘REGEN’:
uart.write(“usr.cn#\r\nOK\r\n”) #收到有效指令后返回给控制板
#每判断一次就清一次零
utf8_msg = 0
msg = 0
msglen = 0
#usr.cn#AT+NATEN=OFF\r\n
elif baozhiling_jiexi[:5] ==‘NATEN’:
uart.write(“usr.cn#\r\nOK\r\n”) #收到有效指令后返回给控制板
#每判断一次就清一次零
utf8_msg = 0
msg = 0
msglen = 0
#usr.cn#AT+S\r\n
elif baozhiling_jiexi[:3] ==‘S\r\n’:
uart.write(“usr.cn#\r\nOK\r\n”)
ylq_log.info(“Save Data”)
#每判断一次就清一次零
utf8_msg = 0
msg = 0
msglen = 0
#更新成新的参数再执行联网操作
is_start = True
#模块软重启
#Power.powerRestart() #测试是无效的
#print(baozhiling_jiexi)
#如果是下行数据
elif baozhiling_head[:1] == ‘\xFD’:
print(‘shangxingshujulaile’)
LED_NET_STATUS = 2 #连上网后数据传输状态
timer2.stop()
#LEDGPIO.write(1) #先让灯亮一下
timer2.start(period=500, mode=timer2.PERIODIC, callback=NETLEDSTATUSCS)
ylq_log.info(‘----------------Uart Recv Data-----------------’)
ylq_log.info(‘Uart Recv Data: {} Len: {:03d}\r\n’.format(utf8_msg, len(utf8_msg)))
uart_revbuf = utf8_msg
uart_revFlag = True
#每判断一次就清一次零
utf8_msg = 0
msg = 0
msglen = 0
#若是无效数据时全部清零
else:
#每判断一次就清一次零
utf8_msg = 0
msg = 0
msglen = 0
utime.sleep_ms(1)
else:
utime.sleep_ms(1)
continue
utime.sleep_ms(1)
except Exception as e:
ylq_log.info(“串口接收数据故障”)
#需要添加异常处理代码
#with network_lock:
ylq_log.info(“因串口异常,关闭串口重新开启串口”)
#先关闭所有的连接
uart.close()
uart = None
uart = UART(UART.UART2, 115200, 8, 0, 1, 0) # 创建UART对象
utime.sleep_ms(1)
#每接收到一次上行数据就发送一次
if uart_revFlag:
uart_revFlag = False
ylq_log.info(‘uart_revbuf: {} Len: {:03d}\r\n’.format(uart_revbuf, len(uart_revbuf)))
if socket: #连网成功后发送
socket.send(“{}”.format(uart_revbuf)) # TCP发送数据
uart_revbuf = 0 #每次发送完记得清零
#每收到一次查询CSQ指令,进行一次获取CSQ值,并回复一次
if CSQ_HQFlag:
CSQ_HQFlag = False
CSQZHI = net.csqQueryPoll()
ylq_log.info(CSQZHI)
uart.write('usr.cn#\r\n+CSQ: ')
uart.write(str(CSQZHI))
uart.write(‘\x2C\x39\x39\r\n\r\nOK\r\n’)
def socket_read():
global socket
global uart
global is_connected
global hb_lost_count,hb_RevJH,hb_cnt,timer0,timer1
global is_start
global LED_NET_STATUS,timer2,LEDGPIO
global QDXTbuf, QDXTlength, XTdatabuf
global QDZCBbuf, QDZCBlength, zcbdatabuf
global QDYMDKdatabuf, QDYMDKdatalength, YMDKdatabuf, YMPORTdatabuf
global ATS_lock,network_lock
global socket_revbuf,socket_revFlag
while True:
if is_connected:
hb_RevJH = False
try:
with network_lock:
#判断Socket是否存在
if not socket:
break
ylq_log.info(‘接收前’)
data = socket.recv(1024)
u8_data =data.decode()
ylq_log.info(‘接收后’)
LED_NET_STATUS = 2 #连上网之后数据传输状态
timer2.stop()
#LEDGPIO.write(1) #先让灯亮一下
timer2.start(period=500, mode=timer2.PERIODIC, callback=NETLEDSTATUSCS)
if len(data) > 0 :
ylq_log.info(‘----------------TCPclient Recv FWQData-----------------’)
ylq_log.info(‘TCPclient Recv Data: {} Len: {:03d}\r\n’.format(data.decode(), len(data)))
socket_revbuf = u8_data
socket_revFlag = True
if len(data) > 1 and u8_data[0] ==‘\xFE’: #是联网下行有效数据,说明活网络
ylq_log.info(u8_data)
hb_lost_count = 0 #收到\x68记得计数清零
ylq_log.info(hb_lost_count)
elif len(data) == 1 and data.decode() ==‘\x68’:
ylq_log.info(‘----------------TCPclient Recv HeartData-----------------’)
ylq_log.info(‘TCPclient Recv Data: {} Len: {:03d}\r\n’.format(data.decode(), len(data)))
hb_lost_count = 0 #收到\x68记得计数清零
hb_RevJH = True
hb_cnt = 0
ylq_log.info(hb_lost_count)
#hb_lost_count = 0 #收到\x68记得计数清零
elif len(data) == 1 and data.decode() !=‘\x68’:
hb_lost_count+=1 #没收到\x68记得计数
ylq_log.info(hb_lost_count)
else:
#没收到’\x68’心跳包 → 计数+1
hb_lost_count += 1
ylq_log.info(hb_lost_count)
#将缓存区进行清零
u8_data = 0
data = 0
else:
#没收到’\x68’心跳包 → 计数+1
hb_lost_count += 1
ylq_log.info(hb_lost_count)
utime.sleep_ms(1)
except Exception as e:
ylq_log.info(“网络接收数据失败”)
LED_NET_STATUS = 0 #初始搜网过程
timer2.stop()
#LEDGPIO.write(1) #先让灯亮一下
timer2.start(period=125, mode=timer2.PERIODIC, callback=NETLEDSTATUSSW)
#需要添加异常处理代码
with network_lock:
ylq_log.info(“因网络异常,而触发重连”)
#先关闭所有的连接
if not is_start:
if socket:
socket.close()
ylq_log.info(“关闭socket连接”)
timer1.stop()
ylq_log.info(“关闭timer1”)
socket = None
is_connected = False
hb_lost_count = 0
ylq_log.info(‘Uart Init Config OK\r\n’)
ylq_log.info(“[重连] 尝试连接:{}:{}”.format(YMDKdatabuf,YMPORTdatabuf))
#发送重连网络指令
socket = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM) # 创建一个Socket实例
sockinfo = usocket.getaddrinfo(YMDKdatabuf, int(YMPORTdatabuf))[0][-1] # 解析域名
socket.connect(sockinfo) # 连接平台
#socket.send(‘\xFD\x01\x10\x43\x44\x36\x36\x38\x38\x38\x36\xFF\xFF\xFC\x13\xEE’) # 发送注册包信息连接设备
# 发送注册包
socket.sendall(‘’.join(ZHZCBdatabuf))
#ylq_log.info(“[网络] 连接成功 | 注册包,domain:{} port:{} reg:{}}”.format(YMDKdatabuf,YMPORTdatabuf,ZHZCBdatabuf))
is_connected = True
LED_NET_STATUS = 1 #连上网之后空闲
timer2.stop()
#LEDGPIO.write(1) #先让灯亮一下
timer2.start(period=3000, mode=timer2.PERIODIC, callback=NETLEDSTATUSXS)
hb_cnt = 0
hb_RevJH = False
timer0.stop()
timer0.start(period=180000, mode=timer0.PERIODIC, callback=WSDFWQ_hb)
#循环30s去发送心跳包给服务器
if is_connected==True:
timer1.stop()
timer1.start(period=30000, mode=timer1.PERIODIC, callback=sendXTBtoFWQ)
utime.sleep_ms(1)
#break
#每接收一次下行数据就发送一次
if socket_revFlag:
socket_revFlag = False
ylq_log.info(‘socket_revbuf: {} Len: {:03d}\r\n’.format(socket_revbuf, len(socket_revbuf)))
if uart:
uart.write(socket_revbuf)
socket_revbuf = 0
#出现网络的几种判断时,进行以下处理
with network_lock:
#usr.cn#AT+S\r\n引起的网络重连
if is_start:
ylq_log.info(“收到AT+S配置参数指令引起的重新连网”)
#先关闭所有的连接
if socket:
socket.close()
ylq_log.info(“关闭socket连接”)
timer1.stop()
ylq_log.info(“关闭timer1”)
socket = None
is_connected = False
hb_lost_count = 0
LED_NET_STATUS = 0 #初始搜网过程
timer2.stop()
#LEDGPIO.write(1) #先让灯亮一下
timer2.start(period=125, mode=timer2.PERIODIC, callback=NETLEDSTATUSSW)
ylq_log.info(“收到配置参数完成的指令,先关闭所有”)
ylq_log.info(“usr.cn#AT+S指令触发的网络重连:{}:{}”.format(YMDKdatabuf,YMPORTdatabuf))
ylq_log.info(‘Init Config OK\r\n’)
ylq_log.info(YMPORTdatabuf)
socket = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM) # 创建一个Socket实例
ylq_log.info(‘=====================has new Socket===================\r\n’)
ylq_log.info(YMPORTdatabuf)
sockinfo = usocket.getaddrinfo(YMDKdatabuf, int(YMPORTdatabuf))[0][-1] # 解析域名
socket.connect(sockinfo) # 连接平台
ylq_log.info(‘=====================Socket new domainport===================\r\n’)
socket.send(‘’.join(ZHZCBdatabuf)) # 发送注册包信息连接设备
is_connected = True
LED_NET_STATUS = 1 #连上网之后空闲时
timer2.stop()
#LEDGPIO.write(1) #先让灯亮一下
timer2.start(period=3000, mode=timer2.PERIODIC, callback=NETLEDSTATUSXS)
ylq_log.info(‘========================Socket connect========================\r\n’)
hb_cnt = 0
hb_RevJH = False
timer0.stop()
timer0.start(period=180000, mode=timer0.PERIODIC, callback=WSDFWQ_hb)
#循环30s去发送心跳包给服务器
if is_connected==True:
timer1.stop()
timer1.start(period=30000, mode=timer1.PERIODIC, callback=sendXTBtoFWQ)
is_start = False
# 连续3次未收到心跳引起的断网重连
if not is_start:
if hb_lost_count >= 3:
hb_lost_count = 0
ylq_log.info(hb_lost_count)
ylq_log.info(“[心跳] 连续多次未收,触发重连”)
#先关闭所有的连接
if socket:
socket.close()
ylq_log.info(“关闭socket连接”)
timer1.stop()
ylq_log.info(“关闭timer1”)
socket = None
is_connected = False
hb_lost_count = 0
LED_NET_STATUS = 0 #初始搜网过程
timer2.stop()
#LEDGPIO.write(1) #先让灯亮一下
timer2.start(period=125, mode=timer2.PERIODIC, callback=NETLEDSTATUSSW)
ylq_log.info(“[重连] 尝试连接:{}:{}”.format(YMDKdatabuf,YMPORTdatabuf))
#发送重连网络指令
socket = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM) # 创建一个Socket实例
sockinfo = usocket.getaddrinfo(YMDKdatabuf, int(YMPORTdatabuf))[0][-1] # 解析域名
socket.connect(sockinfo) # 连接平台
#socket.send(‘\xFD\x01\x10\x43\x44\x36\x36\x38\x38\x38\x36\xFF\xFF\xFC\x13\xEE’) # 发送注册包信息连接设备
# 发送注册包
socket.sendall(‘’.join(ZHZCBdatabuf))
#ylq_log.info(“[网络] 连接成功 | 注册包,domain:{} port:{} reg:{}}”.format(YMDKdatabuf,YMPORTdatabuf,ZHZCBdatabuf))
is_connected = True
LED_NET_STATUS = 1 #连上网空闲状态
timer2.stop()
#LEDGPIO.write(1) #先让灯亮一下
timer2.start(period=3000, mode=timer2.PERIODIC, callback=NETLEDSTATUSXS)
hb_cnt = 0
hb_RevJH = False
timer0.stop()
timer0.start(period=180000, mode=timer0.PERIODIC, callback=WSDFWQ_hb)
#循环30s去发送心跳包给服务器
if is_connected==True:
timer1.stop()
timer1.start(period=30000, mode=timer1.PERIODIC, callback=sendXTBtoFWQ)
#计时180S连续没收到过0x68
if not is_start:
if hb_cnt>=1:
hb_cnt = 0
hb_RevJH = False
ylq_log.info(“因连上网之后连续超过180s没收到0x68,而触发重连”)
#先关闭所有的连接
if socket:
socket.close()
ylq_log.info(“关闭socket连接”)
timer1.stop()
ylq_log.info(“关闭timer1”)
socket = None
is_connected = False
hb_lost_count = 0
ylq_log.info(‘Uart Init Config OK\r\n’)
ylq_log.info(“[重连] 尝试连接:{}:{}”.format(YMDKdatabuf,YMPORTdatabuf))
#发送重连网络指令
socket = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM) # 创建一个Socket实例
sockinfo = usocket.getaddrinfo(YMDKdatabuf, int(YMPORTdatabuf))[0][-1] # 解析域名
socket.connect(sockinfo) # 连接平台
#socket.send(‘\xFD\x01\x10\x43\x44\x36\x36\x38\x38\x38\x36\xFF\xFF\xFC\x13\xEE’) # 发送注册包信息连接设备
# 发送注册包
socket.sendall(‘’.join(ZHZCBdatabuf))
#ylq_log.info(“[网络] 连接成功 | 注册包,domain:{} port:{} reg:{}}”.format(YMDKdatabuf,YMPORTdatabuf,ZHZCBdatabuf))
is_connected = True
LED_NET_STATUS = 1 #连上网之后空闲
timer2.stop()
#LEDGPIO.write(1) #先让灯亮一下
timer2.start(period=3000, mode=timer2.PERIODIC, callback=NETLEDSTATUSXS)
hb_cnt = 0
hb_RevJH = False
timer0.stop()
timer0.start(period=180000, mode=timer0.PERIODIC, callback=WSDFWQ_hb)
#循环30s去发送心跳包给服务器
if is_connected==True:
timer1.stop()
timer1.start(period=30000, mode=timer1.PERIODIC, callback=sendXTBtoFWQ)
else:
if socket:
socket.close()
socket = None
hb_RevJH = False
hb_cnt = 0
hb_lost_count = 0
LED_NET_STATUS = 0 #初始搜网过程
ylq_log.info(“初次联网中,尝试来连接:{}:{}”.format(YMDKdatabuf,YMPORTdatabuf))
socket = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM) # 创建一个Socket实例
ylq_log.info(YMPORTdatabuf)
sockinfo = usocket.getaddrinfo(YMDKdatabuf, int(YMPORTdatabuf))[0][-1] # 解析域名
socket.connect(sockinfo) # 连接平台
#socket.send(‘\xFD\x01\x10\x43\x44\x36\x36\x38\x38\x38\x36\xFF\xFF\xFC\x13\xEE’) # 发送注册包信息连接设备
socket.send(‘’.join(ZHZCBdatabuf)) # 发送注册包信息连接设备
is_connected = True
LED_NET_STATUS = 1 #连上网后空闲状态
timer2.stop()
#LEDGPIO.write(1) #先让灯亮一下
timer2.start(period=3000, mode=timer2.PERIODIC, callback=NETLEDSTATUSXS) #主要是网络状态灯的变化
ylq_log.info(‘========================Socket connect========================\r\n’)
#循环30s去发送心跳包给服务器
if is_connected==True:
timer1.stop()
timer1.start(period=30000, mode=timer1.PERIODIC, callback=sendXTBtoFWQ)
timer0.stop()
timer0.start(period=180000, mode=timer0.PERIODIC, callback=WSDFWQ_hb)