mirror of
https://github.com/NanjingForestryUniversity/supermachine-wood.git
synced 2025-11-08 18:23:54 +00:00
改掉了很多奇怪bug(zhenye.li修改)
This commit is contained in:
parent
03346292b9
commit
c565451c1c
@ -1,6 +1,10 @@
|
|||||||
import socket
|
import socket
|
||||||
|
import sys
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import cv2
|
import cv2
|
||||||
|
|
||||||
|
import root_dir
|
||||||
from classifer import WoodClass
|
from classifer import WoodClass
|
||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
@ -60,7 +64,14 @@ def process_cmd(cmd: str, data: any, connected_sock: PreSocket, detector: WoodCl
|
|||||||
return response
|
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
|
status, connected_sock = False, None
|
||||||
while not status:
|
while not status:
|
||||||
status, connected_sock = try_connect()
|
status, connected_sock = try_connect()
|
||||||
@ -89,7 +100,7 @@ if __name__ == '__main__':
|
|||||||
# 发送端口21123
|
# 发送端口21123
|
||||||
# 接收到图片 n_rows * n_bands * n_cols, float32
|
# 接收到图片 n_rows * n_bands * n_cols, float32
|
||||||
# 发送图片 n_rows * n_cols, uint8
|
# 发送图片 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")
|
# test(r"D:\build-tobacco-Desktop_Qt_5_9_0_MSVC2015_64bit-Release\calibrated15.raw")
|
||||||
# main()
|
# main()
|
||||||
# debug_main()
|
# debug_main()
|
||||||
|
|||||||
38
utils.py
38
utils.py
@ -13,7 +13,6 @@ import socket
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def mkdir_if_not_exist(dir_name, is_delete=False):
|
def mkdir_if_not_exist(dir_name, is_delete=False):
|
||||||
"""
|
"""
|
||||||
创建文件夹
|
创建文件夹
|
||||||
@ -73,6 +72,7 @@ class PreSocket(socket.socket):
|
|||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.pre_pack = b''
|
self.pre_pack = b''
|
||||||
|
self.settimeout(5)
|
||||||
|
|
||||||
def receive(self, *args, **kwargs):
|
def receive(self, *args, **kwargs):
|
||||||
if self.pre_pack == b'':
|
if self.pre_pack == b'':
|
||||||
@ -88,34 +88,47 @@ class PreSocket(socket.socket):
|
|||||||
self.pre_pack = temp + pre_pack
|
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中读取数据.
|
从指定的socket中读取数据.
|
||||||
|
|
||||||
:param recv_sock: 指定sock
|
:param recv_sock: 指定sock
|
||||||
:param pre_pack: 上一包的粘包内容
|
:param pre_pack: 上一包的粘包内容
|
||||||
|
:param time_out: 每隔time_out至少要发来一次指令,否则认为出现问题进行重连
|
||||||
|
:param time_out_single: 单次质指令超时时间,单位是秒
|
||||||
:return: data, next_pack
|
:return: data, next_pack
|
||||||
"""
|
"""
|
||||||
recv_sock.set_prepack(pre_pack)
|
recv_sock.set_prepack(pre_pack)
|
||||||
# 开头校验
|
# 开头校验
|
||||||
|
time_start_recv = time.time()
|
||||||
while True:
|
while True:
|
||||||
|
if (time.time() - time_start_recv) > time_out:
|
||||||
|
logging.error(f'指令接收超时')
|
||||||
|
return b'', b''
|
||||||
try:
|
try:
|
||||||
temp = recv_sock.receive(1)
|
temp = recv_sock.receive(1)
|
||||||
except ConnectionError as e:
|
except ConnectionError as e:
|
||||||
logging.error(f'连接出错, 错误代码:\n{e}')
|
logging.error(f'连接出错, 错误代码:\n{e}')
|
||||||
return b'', b''
|
return b'', b''
|
||||||
except TimeoutError as e:
|
except TimeoutError as e:
|
||||||
logging.error(f'超时了,错误代码: \n{e}')
|
# logging.error(f'超时了,错误代码: \n{e}')
|
||||||
return b'', b''
|
logging.info('运行中,等待指令..')
|
||||||
|
continue
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f'遇见未知错误,错误代码: \n{e}')
|
logging.error(f'遇见未知错误,错误代码: \n{e}')
|
||||||
return b'', b''
|
return b'', b''
|
||||||
if temp == b'\xaa':
|
if temp == b'\xaa':
|
||||||
break
|
break
|
||||||
|
|
||||||
|
# 接收开头后,开始进行时间记录
|
||||||
|
time_start_recv = time.time()
|
||||||
|
|
||||||
# 获取报文长度
|
# 获取报文长度
|
||||||
temp = b''
|
temp = b''
|
||||||
while len(temp) < 4:
|
while len(temp) < 4:
|
||||||
|
if (time.time() - time_start_recv) > time_out_single:
|
||||||
|
logging.error(f'单次指令接收超时')
|
||||||
|
return b'', b''
|
||||||
try:
|
try:
|
||||||
temp += recv_sock.receive(1)
|
temp += recv_sock.receive(1)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -130,20 +143,28 @@ def receive_sock(recv_sock: PreSocket, pre_pack: bytes = b'') -> (bytes, bytes):
|
|||||||
# 读取报文内容
|
# 读取报文内容
|
||||||
temp = b''
|
temp = b''
|
||||||
while len(temp) < data_len:
|
while len(temp) < data_len:
|
||||||
|
if (time.time() - time_start_recv) > time_out_single:
|
||||||
|
logging.error(f'单次指令接收超时')
|
||||||
|
return b'', b''
|
||||||
try:
|
try:
|
||||||
temp += recv_sock.receive(data_len)
|
temp += recv_sock.receive(data_len)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f'接收报文内容失败, 错误代码: \n{e},\n报文内容\n{temp}')
|
logging.error(f'接收报文内容失败, 错误代码: \n{e},\n报文内容\n{temp}')
|
||||||
return b'', b''
|
return b'', b''
|
||||||
data, next_pack = temp[:data_len], temp[data_len:]
|
data, next_pack = temp[:data_len], temp[data_len:]
|
||||||
|
recv_sock.set_prepack(next_pack)
|
||||||
|
next_pack = b''
|
||||||
|
|
||||||
# 进行数据校验
|
# 进行数据校验
|
||||||
temp = b''
|
temp = b''
|
||||||
while len(temp) < 3:
|
while len(temp) < 3:
|
||||||
|
if (time.time() - time_start_recv) > time_out_single:
|
||||||
|
logging.error(f'单次指令接收超时')
|
||||||
|
return b'', b''
|
||||||
try:
|
try:
|
||||||
temp += recv_sock.receive(1)
|
temp += recv_sock.receive(1)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f'接收报文校验失败, 错误代码: \n{e}')
|
logging.error(f'接收报文校验失败, 错误代码: \n{e}, 报文如下: \n{temp}')
|
||||||
return b'', b''
|
return b'', b''
|
||||||
if temp == b'\xff\xff\xbb':
|
if temp == b'\xff\xff\xbb':
|
||||||
return data, next_pack
|
return data, next_pack
|
||||||
@ -152,13 +173,13 @@ def receive_sock(recv_sock: PreSocket, pre_pack: bytes = b'') -> (bytes, bytes):
|
|||||||
return b'', b''
|
return b'', b''
|
||||||
|
|
||||||
|
|
||||||
def parse_protocol(data: bytes) -> (str, any):
|
def parse_protocol(data: bytes) -> (str, any):
|
||||||
'''
|
"""
|
||||||
指令转换.
|
指令转换.
|
||||||
|
|
||||||
:param data:接收到的报文
|
:param data:接收到的报文
|
||||||
:return: 指令类型和内容
|
:return: 指令类型和内容
|
||||||
'''
|
"""
|
||||||
try:
|
try:
|
||||||
assert len(data) > 4
|
assert len(data) > 4
|
||||||
except AssertionError:
|
except AssertionError:
|
||||||
@ -219,7 +240,6 @@ def done_sock(send_sock: PreSocket, cmd_type: str, result: int = '') -> bool:
|
|||||||
result = b'\xff'
|
result = b'\xff'
|
||||||
elif cmd_type == 'IM':
|
elif cmd_type == 'IM':
|
||||||
result = result.to_bytes(1, "big")
|
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'
|
msg = b'\xaa\x00\x00\x00\x05'+(' D'+cmd_type).upper().encode('ascii') + result + b'\xff\xff\xbb'
|
||||||
try:
|
try:
|
||||||
send_sock.send(msg)
|
send_sock.send(msg)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user