0
点赞
收藏
分享

微信扫一扫

python的serial读取异常编码报错

AbrahamW 2022-02-07 阅读 66

话不多说,直接上代码:

c4 = ser.read(ser.in_waiting).decode(encoding='gbk',errors='ignore')
#errors="ignore") 忽略其中有异常的编码,仅显示有效的编码,errors="replace") 替换其中异常的编码,这个相对来可能一眼就知道那些字符编码出问题了。

添加个这玩意即可解决,

当时试过,抛出异常啊,跳过啊,都有点问题, 一行代码搞定,

以下附我最近项目的一个用例,自行删减啊,我可不给你们删:

    def Serial_com4_Ping_lj(self):
        try:
            # print('连接串口成功')
            portx = STA_Serial
            bps = 115200
            timex = 5
            ser = serial.Serial(portx, bps, timeout=timex)
            # print("衔接串口成功:详情参数:", ser)
            ser.write(chr(0x03).encode())
            ser.write('\r'.encode("utf-8"))
            ser.write('tail -f /var/log/iscm.log &\r'.encode("utf-8"))
            ser.write('\r'.encode("utf-8"))
            ser.write('tail -f /var/log/iscm_iot.log &\r'.encode("utf-8"))
            ser.write('\r'.encode("utf-8"))
            ser.write('tail -f /var/log/libqtrans.log &\r'.encode("utf-8"))
            while True:
                if ser.in_waiting:
                    c4 = ser.read(ser.in_waiting).decode(encoding='gbk',errors='ignore')
                    if(c4=="exit"):#退出标志
                        break
                    else:
                        with open('LuYou_power.txt', 'a') as log:
                            log.writelines(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+' '+c4)
                        time.sleep(5)
                        if ' [wifi.router.connect]mac:5C:27:D4:AE:DE:58 ' in c4:
                            print('路由与基站连接成功,[wifi.router.connect]')
                            wifi_router=parse(time.strftime("%Y-%m-%d %H:%M:%S"))
                            Open_Power =parse(off_Power_l[-1])
                            aa = (wifi_router - Open_Power).total_seconds()
                            print('耗链接时:%sS'%aa)
                            break
                        if 'On line'  in c4:
                            print('wifi与基站连接成功,On line')
                            On_line = parse(time.strftime("%Y-%m-%d %H:%M:%S"))
                            Open_Power =parse(off_Power_l[-1])
                            aa = (On_line - Open_Power).total_seconds()
                            print('链接耗时:%sS' % aa)
                            break
                        if 'LCONN: ReConnect to cloud. ' in c4:
                            print('路由与基站连接成功,ReConnect to cloud')
                            ReConnect_to_cloud = parse(time.strftime("%Y-%m-%d %H:%M:%S"))
                            Open_Power =parse(off_Power_l[-1])
                            aa = (ReConnect_to_cloud - Open_Power).total_seconds()
                            print('链接耗时:%sS' % aa)
                            break
                        if 'LED_STATUS_ON_LINE' in c4:
                            print('路由与基站连接成功,LED_STATUS_ON_LINE')
                            LED_STATUS_ON_LINE = parse(time.strftime("%Y-%m-%d %H:%M:%S"))
                            Open_Power =parse(off_Power_l[-1])
                            aa = (LED_STATUS_ON_LINE - Open_Power).total_seconds()
                            print('链接耗时:%sS' % aa)
                            break
            ser.close()  # 关闭串口
        except Exception as e:
            print("---异常---:", e)
            self.Serial_com4_Ping_lj()
举报

相关推荐

0 条评论