From c565451c1c0c259ace73fabb4fd403f190086720 Mon Sep 17 00:00:00 2001 From: FEIJINTI <83849113+FEIJINTI@users.noreply.github.com> Date: Mon, 19 Sep 2022 14:45:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E6=8E=89=E4=BA=86=E5=BE=88=E5=A4=9A?= =?UTF-8?q?=E5=A5=87=E6=80=AAbug=EF=BC=88zhenye.li=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- socket_detector.py | 15 +++++++++++++-- utils.py | 38 +++++++++++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/socket_detector.py b/socket_detector.py index bc61985..51ba778 100644 --- a/socket_detector.py +++ b/socket_detector.py @@ -1,6 +1,10 @@ import socket +import sys + import numpy as np import cv2 + +import root_dir from classifer import WoodClass import time import os @@ -60,7 +64,14 @@ def process_cmd(cmd: str, data: any, connected_sock: PreSocket, detector: WoodCl return response -def main(): +def main(is_debug=False): + file_handler = logging.FileHandler(os.path.join(ROOT_DIR, 'report.log')) + file_handler.setLevel(logging.DEBUG if is_debug else logging.WARNING) + console_handler = logging.StreamHandler(sys.stdout) + console_handler.setLevel(logging.DEBUG if is_debug else logging.WARNING) + logging.basicConfig(format='%(asctime)s %(filename)s[line:%(lineno)d] - %(levelname)s - %(message)s', + handlers=[file_handler, console_handler], level=logging.DEBUG) + status, connected_sock = False, None while not status: status, connected_sock = try_connect() @@ -89,7 +100,7 @@ if __name__ == '__main__': # 发送端口21123 # 接收到图片 n_rows * n_bands * n_cols, float32 # 发送图片 n_rows * n_cols, uint8 - main() + main(is_debug=False) # test(r"D:\build-tobacco-Desktop_Qt_5_9_0_MSVC2015_64bit-Release\calibrated15.raw") # main() # debug_main() diff --git a/utils.py b/utils.py index d1e5ec8..69cf577 100644 --- a/utils.py +++ b/utils.py @@ -13,7 +13,6 @@ import socket import numpy as np - def mkdir_if_not_exist(dir_name, is_delete=False): """ 创建文件夹 @@ -73,6 +72,7 @@ class PreSocket(socket.socket): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.pre_pack = b'' + self.settimeout(5) def receive(self, *args, **kwargs): if self.pre_pack == b'': @@ -88,34 +88,47 @@ class PreSocket(socket.socket): self.pre_pack = temp + pre_pack -def receive_sock(recv_sock: PreSocket, pre_pack: bytes = b'') -> (bytes, bytes): +def receive_sock(recv_sock: PreSocket, pre_pack: bytes = b'', time_out: float = 120, time_out_single=0.5) -> (bytes, bytes): """ 从指定的socket中读取数据. :param recv_sock: 指定sock :param pre_pack: 上一包的粘包内容 + :param time_out: 每隔time_out至少要发来一次指令,否则认为出现问题进行重连 + :param time_out_single: 单次质指令超时时间,单位是秒 :return: data, next_pack """ recv_sock.set_prepack(pre_pack) # 开头校验 + time_start_recv = time.time() while True: + if (time.time() - time_start_recv) > time_out: + logging.error(f'指令接收超时') + return b'', b'' try: temp = recv_sock.receive(1) except ConnectionError as e: logging.error(f'连接出错, 错误代码:\n{e}') return b'', b'' except TimeoutError as e: - logging.error(f'超时了,错误代码: \n{e}') - return b'', b'' + # logging.error(f'超时了,错误代码: \n{e}') + logging.info('运行中,等待指令..') + continue except Exception as e: logging.error(f'遇见未知错误,错误代码: \n{e}') return b'', b'' if temp == b'\xaa': break + # 接收开头后,开始进行时间记录 + time_start_recv = time.time() + # 获取报文长度 temp = b'' while len(temp) < 4: + if (time.time() - time_start_recv) > time_out_single: + logging.error(f'单次指令接收超时') + return b'', b'' try: temp += recv_sock.receive(1) except Exception as e: @@ -130,20 +143,28 @@ def receive_sock(recv_sock: PreSocket, pre_pack: bytes = b'') -> (bytes, bytes): # 读取报文内容 temp = b'' while len(temp) < data_len: + if (time.time() - time_start_recv) > time_out_single: + logging.error(f'单次指令接收超时') + return b'', b'' try: temp += recv_sock.receive(data_len) except Exception as e: logging.error(f'接收报文内容失败, 错误代码: \n{e},\n报文内容\n{temp}') return b'', b'' data, next_pack = temp[:data_len], temp[data_len:] + recv_sock.set_prepack(next_pack) + next_pack = b'' # 进行数据校验 temp = b'' while len(temp) < 3: + if (time.time() - time_start_recv) > time_out_single: + logging.error(f'单次指令接收超时') + return b'', b'' try: temp += recv_sock.receive(1) except Exception as e: - logging.error(f'接收报文校验失败, 错误代码: \n{e}') + logging.error(f'接收报文校验失败, 错误代码: \n{e}, 报文如下: \n{temp}') return b'', b'' if temp == b'\xff\xff\xbb': return data, next_pack @@ -152,13 +173,13 @@ def receive_sock(recv_sock: PreSocket, pre_pack: bytes = b'') -> (bytes, bytes): return b'', b'' -def parse_protocol(data: bytes) -> (str, any): - ''' +def parse_protocol(data: bytes) -> (str, any): + """ 指令转换. :param data:接收到的报文 :return: 指令类型和内容 - ''' + """ try: assert len(data) > 4 except AssertionError: @@ -219,7 +240,6 @@ def done_sock(send_sock: PreSocket, cmd_type: str, result: int = '') -> bool: result = b'\xff' elif cmd_type == 'IM': result = result.to_bytes(1, "big") - print(result) msg = b'\xaa\x00\x00\x00\x05'+(' D'+cmd_type).upper().encode('ascii') + result + b'\xff\xff\xbb' try: send_sock.send(msg)