From 0b3e23b1ab424bbf65f006f8cb8a09b9170debf6 Mon Sep 17 00:00:00 2001 From: esea_info Date: Fri, 14 Apr 2023 09:56:46 +0800 Subject: [PATCH] tcp rcv deal ok --- data/2023/3/20/37/info.bin | Bin 0 -> 79 bytes data/2023/3/20/37/sensor.bin | Bin 0 -> 1754 bytes handheld.py | 15 ++-- receive.py | 10 +++ tcp_handheld.py | 129 +++++++++++++++++++++++++++++++---- 5 files changed, 134 insertions(+), 20 deletions(-) create mode 100644 data/2023/3/20/37/info.bin create mode 100644 data/2023/3/20/37/sensor.bin diff --git a/data/2023/3/20/37/info.bin b/data/2023/3/20/37/info.bin new file mode 100644 index 0000000000000000000000000000000000000000..aff7373e46aeb93ebc5c7784022dd98764731c40 GIT binary patch literal 79 ycmWe<7U45Al?aQK3%nY}dgg@?ThHCY?iZA&iZUQT3WABm`Vs(wYzz#{2tfdh5Dg*# literal 0 HcmV?d00001 diff --git a/data/2023/3/20/37/sensor.bin b/data/2023/3/20/37/sensor.bin new file mode 100644 index 0000000000000000000000000000000000000000..84c1b09791a283134f088120039045ec365c8813 GIT binary patch literal 1754 zcmWd@R8C+}U|?Vf@^A?)oA{jJG^-D*7wbX>M&$(z2J8$BTl$ z#eUviq3$MbW?n6U4iQ>Wb3&K;PIL=#FtCxfzZc$I*jE5HhlPRRPFR53L5FX)ik5mt z7qz1`7O8z!HC0VhPE(wxa94h#+;rKaGWs$Tq$f$GNFI`Cl8BIaA|WB|Ap1r}R?1TR zyC~QkW(J0LqQ+uh#ZHOmNo#A#6)fjedo94d&+CX2R4U^f#EN&4ew+gcOG7z9o&W7a@=RRCUJ#v$#XGq ziEz1b*>PRqtm4e!jOSG3+{RJB5zk@5q0J!!3~g?XyX-sJr?P|1VPs$^U{7KXW%put z0LGOY5PAUF0qha%vFzdO{_M`|@$6wh906q8v#YSX0#%m-MeNzN*g4ssv0Y;Wn*&S_ zpV|1?RoRu;_1G2Inc2D7-?05>lVrC8#=Z%=1iK^b4Jt8X!;pVKcnT(K$JfK{lV6B literal 0 HcmV?d00001 diff --git a/handheld.py b/handheld.py index b3789cd..b48ff69 100644 --- a/handheld.py +++ b/handheld.py @@ -467,7 +467,7 @@ class Handheld(object): return True,self.error_result pass - def dealOneMeasurement_Handheld(self, fpath:Path): + def dealOneMeasurement_Handheld(self, ): '''handheld一次测量包含多组数据''' # 调用handheldbuf 处理,将一组数据提交出来 log.info(f" 手持测量数据", __name__, "dealOneMeasurement_Handheld") @@ -486,8 +486,8 @@ class Handheld(object): raise MyException( "处理文件"+ self.info_path_fname + "出现错误") pass - # 当前文件名 - self.output_path = self.new_folder() + # 当前文件名 ? + self.hhp.setMode(1) self.current_measure_time = self.getCurrentMeasureTimeFromInfoDict() ymdhms = "20"+ str(self.info_dict['year']) + '_' \ @@ -496,6 +496,9 @@ class Handheld(object): + str(self.info_dict['hour']) + '_' \ + str(self.info_dict['minute']) + '_' \ + str(self.info_dict['second']) + + # self.output_path 定位到 /data/output 目录? + self.output_path = OUTPUT_DIR.joinpath( ymdhms) log.debug(f"current_measure_time: {self.current_measure_time}", __name__, "dealOneMeasurement_Handheld") @@ -512,9 +515,9 @@ class Handheld(object): log.debug(f"group_num...: {group_num}", __name__, "dealOneMeasurement_Handheld") self.real_wavelength = self.getWavelenthDict() - if group_num == 1: - self.dealOneGroup() # self.intensity_before_avg - return + # if group_num == 1: + # self.dealOneGroup() # self.intensity_before_avg + # return self.dealMultiGroup(group_num) diff --git a/receive.py b/receive.py index 7b11f98..2ee5444 100644 --- a/receive.py +++ b/receive.py @@ -157,6 +157,15 @@ class illumination_sensor: break if self.__head != {}: + # 判断是否是测量结束标志 + if self.__head['type'] == 15: + log.info(f" end frame recieved! ") + self.__head['payload'] = b'\xff\xff' + data = self.__head.copy() + self.__head = {} + self.id = data['id'] + return data + pass payload = self.read_buf(self.__head['size']) if payload != b'': self.__head['payload'] = payload @@ -329,6 +338,7 @@ class DealData: log.info(" retrieve_data.... ") pass + # 收到的全部扔到 sensor.bin, 追加保存 def save(self,data: dict) -> None: log.info(f"save .....to first dir {str(data['con'])} - type:{data['type']} - num {data['num']}") # 路径 传感器id/测量序号(唯一) -- 处理数据时候改时间 diff --git a/tcp_handheld.py b/tcp_handheld.py index 9f9d71b..1ca112e 100644 --- a/tcp_handheld.py +++ b/tcp_handheld.py @@ -1,4 +1,5 @@ -# tcp 客户端 模拟 发送 信息帧 数据帧 图片帧 +# tcp Handheld 客户端 模拟 ,模拟数据打包 +# 直接平一个小包测试数据 直接写tou import socket import time @@ -7,15 +8,20 @@ import time IP = "127.0.0.1" PORT = 7887 ADDRESS = ( IP, PORT ) # 绑定地址 +INFO_HEADER = "111355AA020000250000004F000107" +DATA_HEADER = "11 13 55 AA 02 00 10 25 00 00 00 DA 06 02 07" +END_FRAME = "11 13 55 AA 02 00 FF 25 00 00 00 00 00 00 00" + # 改数据 class DataContent: - # 11 13 55 AA 02 00 00 11 00 00 00 4F 00 01 07 - info_frame = "11 13 55 AA 02 00 00 11 00 00 00 4F 00 01 07 1703140E313518565D1E51D55605CCE812068CDDC347D0239515000000000000000000000064000000000000000000000000000000000000000000F450F450F4500600000300000000000000000000" + # 11 13 55 AA 0200 00 11 00 00 00 4F00 01 07 + # id type-num 序号 17 字节数 包号 总 + info_frame = "1703140E313518565D1E51D55605CCE812068CDDC347D0239515000000000000000000000064000000000000000000000000000000000000000000F450F450F4500600000300000000000000000000" sensor_frame =[ # 11 13 55 AA 02 00 10 11 00 00 00 DA 06 02 07 - ' 11 13 55 AA 02 00 10 11 00 00 00 DA 06 02 07 131123600020000000524844557691E700CB054C054A05A10001\ + '131123600020000000524844557691E700CB054C054A05A10001\ 23A000300700000A07FD062A073707440750074D074C07530759075A07610765076A07720790079107A407C3070709BE0AAC0ABF080F080808270849089E0880094F0BB70F2A1901\ 23A000300600005223CD28242B3830E235813BE9401B46675A3181C68D5C7916715D74C676617658742E71B26C9C674363DA5F1C5E0160F164DF66A262F85AD153764E4F4C014D01\ 23A00030050000EA4D8C4D314DF44D3C4FC250E453BA5E4486B8C5BCECC4D29A96436BFA56734E4B4A55473446364A845140582A5A9C55A54D91465440303C1B3FDE5783718E7001\ @@ -42,7 +48,95 @@ class DataContent: 23A00030000000F3060F07250722072E07200703070B07EC06FF0619073E07590734071807190719070A07E106DD06F006EC061B072F073E073E07220725070B07F806C806DE0601', ] - end_frame = "11 13 55 AA 02 00 FF 11 00 00 00 00 00 00 00" #0xFF + end_frame = "11 13 55 AA 02 00 FF 25 00 00 00 00 00 00 00" #0xFF +class MyBuf: + '''一次测量三个传感器数据打一个包''' + def __init__(self,) -> None: + self.__buf = b'' + self.__head = {} + self.__begin_sign = b'\x23' + self.__end_sign = b'\x0D' + self.send_head = b'\x11\x13\x55\xAA' + self.send_id = 2 # 2byte 地位在前 + self.send_type = 0 # info 0 data 1 pic 2 + self.send_num = 0 # 一次测量最多十六次平均 + self.send_con = 0 # 总的测量序号 4byte ,16次平均算几次?每次算一个 + self.send_size = 0 #字节数 2byte + self.send_packet_con = 0 + self.send_packet_all = 0 + + self.repeat = 0 # 依据这个拆包 + self.groups = 0 + + self.state = 0 + + def readFile2Buf(self, fpath) -> None: + with open(fpath,"rb") as f: + self.__buf = f.read() + pass + pass + + def read_buf(self, size: int) -> bytes: + if size > self.__buf.__len__(): + return b'' + ret = self.__buf[0:size] + self.__buf = self.__buf[size:] + return ret + + def write_buf(self, buf: bytes) -> None: + len = buf.__len__() + # logging.info(f'Received ID:{id} Size:{len}') + self.__buf = self.__buf+buf + + def get_buf_size(self) -> int: + return self.__buf.__len__() + + def back_bytes(self, buf: bytes) -> None: + self.__buf = buf+self.__buf + + def reset_head(self) -> None: + self.__head = {} + + def reset_buf(self) -> None: + self.__buf = b'' + + def pack_info_send(self,): + length = 26 + ret = self.read_buf(length) + id = self.send_id.to_bytes(2, byteorder='little') + self.send_type = 0 + type_num = (self.send_type *16 + self.send_num).to_bytes(1,byteorder='little' ) # ?? + con = self.send_con.to_bytes(4, byteorder='little') + size = length.to_bytes(2, byteorder='little') + packet_con = self.send_packet_con.to_bytes(1,byteorder='little' ) + packet_all = self.send_packet_all.to_bytes(1,byteorder='little' ) + ret = self.send_head + id + type_num + con + size + packet_con + packet_all +ret + self.send_packet_con += 1 + + # 取重复次数和测量间隔 + return ret + + def pack_handheld_send(self,): + length = 26+576+576+576 + ret = self.read_buf(length) + id = self.send_id.to_bytes(2, byteorder='little') + self.send_type = 1 + type_num = self.send_type << 4 + self.send_num # ?? + con = self.send_con.to_bytes(4, byteorder='little') + size = length.to_bytes(2, byteorder='little') + packet_con = self.send_packet_con.to_bytes(1,byteorder='little' ) + packet_all = self.send_packet_all.to_bytes(1,byteorder='little' ) + ret = self.send_head + id + type_num + con + size + packet_con + packet_all +ret + self.send_con+=1 # 总测量序号+1 + self.send_num += 1 # 是平均中的第几次 + self.send_packet_con += 1 + return ret + pass + + def pack_end_send(self,): + ret = b'\x11\x13\x55\xAA\x02\x00\xFF\x11\x00\x00\x00\x00\x00\x00\x00' + return ret + pass class TcpHandheld(object): def __init__(self ): @@ -89,7 +183,7 @@ class TcpHandheld(object): def send_info(self,): - buf = bytes.fromhex (DataContent.info_frame) + buf = bytes.fromhex (INFO_HEADER+ DataContent.info_frame) self.flag = False self.head = buf[:15] print(f"head ....{self.head.hex()}") @@ -98,13 +192,18 @@ class TcpHandheld(object): def send_data(self,): buf = b'' - for i in range( len( DataContent.sensor_frame) ): - print( "sensorframe..." ) - buf = bytes.fromhex ( DataContent.sensor_frame[i] ) - self.flag = False - self.head = buf[:15] - print(f"head ....{self.head.hex()} ") - self.send_rcv(buf) + buf = bytes.fromhex (DATA_HEADER+ DataContent.sensor_frame[0]) + self.flag = False + self.head = buf[:15] + print(f"head ....{self.head.hex()}") + self.send_rcv(buf) + # for i in range( len( DataContent.sensor_frame) ): + # print( "sensorframe..." ) + # buf = bytes.fromhex ( DataContent.sensor_frame[i] ) + # self.flag = False + # self.head = buf[:15] + # print(f"head ....{self.head.hex()} ") + # self.send_rcv(buf) def send_end_frame(self,): buf = bytes.fromhex (DataContent.end_frame) @@ -124,7 +223,9 @@ if __name__ == '__main__': th.send_data() - time.sleep( 2) + th.send_end_frame() + + time.sleep( 10) except KeyboardInterrupt as e: print(e)