def setUp(self): self.hex_string = '452F68' self.bytesStream_b_endian = BytesStream(self.hex_string, 0) self.hex_string = '12345678' self.hex_string2 = '039A120D' self.bytesStream_l_endian = BytesStream(self.hex_string, 1) self.bytesStream_l_endian2 = BytesStream(self.hex_string2, 1) self.hex_string = '5a' self.bytesStream_l_set = BytesStream(self.hex_string, 1) self.hex_string = '005a' self.bytesStream_l_set2 = BytesStream(self.hex_string, 1)
def test_setSegmentByIndex(self): self.assertEqual( '5a6d', self.bytesStream_l_set.set_segment_by_index(109, 8, 8)) self.assertEqual('5a80', self.bytesStream_l_set.set_segment_by_index(4, 8, 3)) self.assertEqual('5f', self.bytesStream_l_set.set_segment_by_index(7, 5, 3)) self.assertEqual('56', self.bytesStream_l_set.set_segment_by_index(3, 4, 3)) self.assertEqual('565a', self.bytesStream_l_set.set_segment_by_index(90, 8, 8)) self.assertEqual( '7e', self.bytesStream_l_set.set_segment_by_index(126, 0, 8)) self.assertEqual( '7ec9', self.bytesStream_l_set.set_segment_by_index( 201, self.bytesStream_l_set.curBitsIndex, 8)) hex_string = '005a' bytesStream_l_set2 = BytesStream(hex_string) self.assertEqual('005a0000fe', bytesStream_l_set2.set_segment_by_index(254, 32, 8))
def mvb_process(raw_record, fw): """ 对于每一条原始记录板信息进行处理 :param raw_record: 原始记录信息 :return: -1=错误,0=正常, 1=空转, 2=打滑, 3=空转+打滑 """ ret = 0 # 返回值 spin = 0 # 空转指示 slip = 0 # 打滑指示 dl = DataLabel() streamReverseEsc = iner_escape_reverse(raw_record) # 反转义每一条记录 if streamReverseEsc == '': ret = -1 return ret, dl item = BytesStream(streamReverseEsc) # 创建解析对象 # 解析消息头 #print('*' * 30 + 'MSG_HEAD ' + '*' * 30) for idx, content in enumerate(msg_head_width): #print(str_head_name[idx] + ':' + str(item.get_segment_by_index(item.curBitsIndex, msg_head_width[idx]))) dic_msg[str_head_name[idx]] = str( item.get_segment_by_index(item.curBitsIndex, msg_head_width[idx], sign=1)) #print('*' * 30 + 'MSG_CONTENT ' + '*' * 30) dl.t_atoutc = dic_msg['t_atoutc'] dl.t_ato = dic_msg['t_ato'] dl.m_atomode = dic_msg['m_atomode'] dl.v_speed = dic_msg['v_speed'] # ATO控制信息 tb_info = 0 tb_cmd = '' t_num = 0 b_num = 0 # 解析内容 while item.curBitsIndex < len(item.get_stream_in_bytes()) * 8 - 1: nid = item.get_segment_by_index(item.curBitsIndex, 8) #print('nid:' + str(nid)) l_pkt = item.get_segment_by_index(item.curBitsIndex, 13) #print('l_pkt:' + str(l_pkt)) if nid == 1: item.get_segment_by_index(item.curBitsIndex, 8) # 心跳 item.get_segment_by_index(item.curBitsIndex, 8) # 有效 tb_info = item.get_segment_by_index(item.curBitsIndex, 8) # 牵引制动 if tb_info == 170: tb_cmd = '牵引' elif tb_info == 165: tb_cmd = '惰行' elif tb_info == 85: tb_cmd = '制动' else: tb_cmd = '无命令' t_num = item.get_segment_by_index(item.curBitsIndex, 16) # 牵引控制量 b_num = item.get_segment_by_index(item.curBitsIndex, 16) # 牵引控制量 item.get_segment_by_index(item.curBitsIndex, 9 * 8) elif nid == 0: #print('hear_beat:' + str(item.get_segment_by_index(item.curBitsIndex, 8))) item.get_segment_by_index(item.curBitsIndex, 8) item.get_segment_by_index(item.curBitsIndex, 8) # MOMC/AOMC item.get_segment_by_index(item.curBitsIndex, 8) # ATO有效反馈 item.get_segment_by_index(item.curBitsIndex, 8) # 牵引制动反馈 item.get_segment_by_index(item.curBitsIndex, 16) # 牵引控制量反馈 item.get_segment_by_index(item.curBitsIndex, 16) # 制动控制量反馈 item.get_segment_by_index(item.curBitsIndex, 8) # 保持制动反馈 item.get_segment_by_index(item.curBitsIndex, 8) # 开左门开右门 item.get_segment_by_index(item.curBitsIndex, 8) # 恒速反馈 item.get_segment_by_index(item.curBitsIndex, 8) # 车门状态 if item.get_segment_by_index(item.curBitsIndex, 4) != 0: spin = 1 dl.spin_event = 1 ltime = time.localtime(int(dic_msg['t_atoutc'])) timeStr = time.strftime(",%H:%M:%S", ltime) fw.write(dic_msg['m_atomode'] + ',') fw.write(tb_cmd + ',' + str(t_num) + ',' + str(b_num)) fw.write(timeStr + ',' + dic_msg['v_speed'] + ',空转!\n') # fw.write('记录板记录: '+ raw_record + '\n') if item.get_segment_by_index(item.curBitsIndex, 4) != 0: slip = 1 dl.slope_event = 1 ltime = time.localtime(int(dic_msg['t_atoutc'])) timeStr = time.strftime(",%H:%M:%S", ltime) fw.write(dic_msg['m_atomode'] + ',') fw.write(tb_cmd + ',' + str(t_num) + ',' + str(b_num)) fw.write(timeStr + ',' + dic_msg['v_speed'] + ',打滑!\n') # fw.write('记录板记录: ' + raw_record + '\n') item.get_segment_by_index(item.curBitsIndex, 19 * 8) else: item.get_segment_by_index(item.curBitsIndex, l_pkt - 21) #print('content:' + hex(item.get_segment_by_index(item.curBitsIndex, l_pkt - 21))) # 判断空转和打滑 if spin == 0 and slip == 0: pass else: if spin == 1 and slip == 1: ret = 3 elif slip == 1: # 打滑2 ret = 2 elif spin == 1: # 空转1 ret = 1 # 在下一次监测前,判断退出 if item.curBitsIndex >= len( item.get_stream_in_bytes()) * 8 - 1 - 8 - 8: break # 解析完成 return ret, dl
from CommonParse import BytesStream streamReverseEsc = ('2d1efa03e0496405f0fc048bd4800bd46000000000480002945c8a6180bf') item = BytesStream(streamReverseEsc) content = ['m_waysidetime', 'nid_depart_track', 'm_depart_time', 'nid_arrival_track', 'm_arrival_time', 'm_task', 'm_skip', 'm_tbplan'] width = [24, 24, 24, 24, 24, 2, 2, 2] content_it = ['nid_depart_track', 'm_depart_time', 'nid_arrival_track', 'm_arrival_time', 'm_task', 'm_skip', 'm_tbplan'] width_it = [24, 24, 24, 24, 2, 2, 2] # 解析内容 while item.curBitsIndex < len(item.get_stream_in_bytes()) * 8 - 1: nid_stm = item.get_segment_by_index(item.curBitsIndex, 8) l_msg = item.get_segment_by_index(item.curBitsIndex, 8) nid_packet = item.get_segment_by_index(item.curBitsIndex, 8) l_packet = item.get_segment_by_index(item.curBitsIndex, 13) nid_sub_packet = item.get_segment_by_index(item.curBitsIndex, 8) print('nid:' + str(nid_sub_packet)) # E44 用户消息 packet = item.get_segment_by_index(item.curBitsIndex, 8) print('nid:' + str(packet)) dir = item.get_segment_by_index(item.curBitsIndex, 2) print('dir:' + str(dir)) l_pkt = item.get_segment_by_index(item.curBitsIndex, 13) print('l_packet:' + str(l_pkt)) # 计划包 wl_packet = item.get_segment_by_index(item.curBitsIndex, 9) print('wl_packet:' + str(wl_packet))
from CommonParse import BytesStream from app.BaseMethod import iner_escape_reverse streamReverseEsc = iner_escape_reverse( '7EC9355800220D980D4D94F2F1534291DADAFC51081209022908303827866028194600A002A91794C5280FC0412BF000000000000000000000000000000004095F8AA0005D21DFFFE02B10BE2000000003405B7D5FFFFFFFFFFFFFFFFFFFFFFF9DBDAAFB1DBDAAFB00022A0660000000000000000001540002257D5D555460000000000000000000000000000625D2D49FA0EA0180D477DF0210817D5FE183D0013540000001007D5EB303B5B655821A35214BFFFCAC20000001D998000175A48001EA0C8003E93010005D21DA000043F2C01A8EFBEFD324CE207F' ) if streamReverseEsc == '': print("err stream, can not analysis!") item = BytesStream(streamReverseEsc) msg_head_width = [8, 8, 10, 2, 1, 32, 32, 32, 32, 16, 3] str_head_name = [ 'escap_head', 'nid_msg', 'l_msg', 'nid_modle', 'q_standby', 'n_cycle', 't_ato', 't_atoutc', 'm_pos', 'v_speed', 'm_atomode' ] print('*' * 30 + 'MSG_HEAD ' + '*' * 30) for idx, content in enumerate(msg_head_width): print(str_head_name[idx] + ':' + str( item.fast_get_segment_by_index(item.curBitsIndex, msg_head_width[idx])) ) #print('bitoffset ' + str(item.curBitsIndex)) print('*' * 30 + 'MSG_CONTENT ' + '*' * 30) # 解析内容 while item.curBitsIndex < len(item.get_stream_in_bytes()) * 8 - 1: nid = item.get_segment_by_index(item.curBitsIndex, 8) print('nid:' + str(nid)) l_pkt = item.get_segment_by_index(item.curBitsIndex, 13) print('l_pkt:' + str(l_pkt)) if nid < 9: print('content:' +
def atp2ato_process(raw_record): """ 对于每一条原始记录板信息进行处理 :param raw_record: 原始记录信息 :return: -1=错误,0=正常, 1=空转, 2=打滑, 3=空转+打滑 """ ret = 0 # 返回值 spin = 0 # 空转指示 slip = 0 # 打滑指示 streamReverseEsc = iner_escape_reverse(raw_record) # 反转义每一条记录 if streamReverseEsc == '': return -1 item = BytesStream(streamReverseEsc) # 创建解析对象 # 解析消息头 #print('*' * 30 + 'MSG_HEAD ' + '*' * 30) for idx, content in enumerate(msg_head_width): #print(str_head_name[idx] + ':' + str(item.get_segment_by_index(item.curBitsIndex, msg_head_width[idx]))) dic_msg[str_head_name[idx]] = str( item.get_segment_by_index(item.curBitsIndex, msg_head_width[idx], sign=1)) #print('*' * 30 + 'MSG_CONTENT ' + '*' * 30) # 解析内容 while item.curBitsIndex < len(item.get_stream_in_bytes()) * 8 - 1: nid = item.get_segment_by_index(item.curBitsIndex, 8) #print('nid:' + str(nid)) l_pkt = item.get_segment_by_index(item.curBitsIndex, 13) #print('l_pkt:' + str(l_pkt)) if nid == 4: item.get_segment_by_index(item.curBitsIndex, 8) l_msg = item.get_segment_by_index(item.curBitsIndex, 8) # 记录bit数 bit_idx = item.curBitsIndex byte_idx = item.curBytesIndex # 以下为lpacket描述范围 item.get_segment_by_index(item.curBitsIndex, 8) all_len = item.get_segment_by_index(item.curBitsIndex, 13) # 监测ATO2ATP数据包 while all_len != (item.curBitsIndex - bit_idx): nid = item.get_segment_by_index(item.curBitsIndex, 8) print('nid:%d' % nid, end=',') if nid == 132: print('-->' + dic_msg['t_ato'], end=',') print('e44:' + str(item.get_segment_by_index(item.curBitsIndex, 8)), end=',') print( 'e44_l_p:' + str(item.get_segment_by_index(item.curBitsIndex, 13)), end=',') print('wl_id:' + str(item.get_segment_by_index(item.curBitsIndex, 9)), end=',') print( 'wl_id_len:' + str(item.get_segment_by_index(item.curBitsIndex, 13)), end=',') print( 'track:' + str(item.get_segment_by_index(item.curBitsIndex, 24)), end=',') print( 'time:' + str(item.get_segment_by_index(item.curBitsIndex, 24)), end=',') print( 'track:' + str(item.get_segment_by_index(item.curBitsIndex, 24)), end=',') print( 'time:' + str(item.get_segment_by_index(item.curBitsIndex, 24)), end=',') print('task:' + str(item.get_segment_by_index(item.curBitsIndex, 2)), end=',') print('skip:' + str(item.get_segment_by_index(item.curBitsIndex, 2)), end=',') print('mtbplan:' + str(item.get_segment_by_index(item.curBitsIndex, 2))) elif nid == 129: print('ato_mode:' + str(item.get_segment_by_index(item.curBitsIndex, 4)), end=',') print('door_mode:' + str(item.get_segment_by_index(item.curBitsIndex, 2)), end=',') print('tb_state:' + str(item.get_segment_by_index(item.curBitsIndex, 2)), end=',') print('door_state:' + str(item.get_segment_by_index(item.curBitsIndex, 2)), end=',') print( 'ato_err:' + str(item.get_segment_by_index(item.curBitsIndex, 16)), end=',') print( 'ato_stop_err:' + str(item.get_segment_by_index(item.curBitsIndex, 16))) elif nid == 130: print('l_cmd:' + str(item.get_segment_by_index(item.curBitsIndex, 2)), end=',') print('r_cmd:' + str(item.get_segment_by_index(item.curBitsIndex, 2))) elif nid == 131: print('tbs:' + str(item.get_segment_by_index(item.curBitsIndex, 2)), end=',') print('skip:' + str(item.get_segment_by_index(item.curBitsIndex, 2)), end=',') print('plan_valid:' + str(item.get_segment_by_index(item.curBitsIndex, 2)), end=',') print('tb:' + str(item.get_segment_by_index(item.curBitsIndex, 2)), end=',') print( 'ato_count_time:' + str(item.get_segment_by_index(item.curBitsIndex, 16)), end=',') print('tcms_state:' + str(item.get_segment_by_index(item.curBitsIndex, 2)), end=',') print( 'padding:' + str(item.get_segment_by_index(item.curBitsIndex, 22))) elif nid == 133: print() else: print('err!!!!!') #结果监测 if all_len == (item.curBitsIndex - bit_idx): if (all_len + 16) % 8 == 0: # 刚好除整数 print( 'seq:' + str(item.get_segment_by_index(item.curBitsIndex, 32)), end=',') print( 't_atp:' + str(item.get_segment_by_index(item.curBitsIndex, 32)), end=',') print( 'crc:' + hex(item.get_segment_by_index(item.curBitsIndex, 32)), end=',') if l_msg == item.curBytesIndex - byte_idx + 2: # 2B即为消息头两个8位 print('msg ok') else: # 重新校正bit索引 tump = int(((all_len + 16 + 7) // 8) * 8) - all_len - 16 # 字节下跳的bit数,减去消息头16bit和内容bit后 item.get_segment_by_index(item.curBitsIndex, tump) print( 'seq:' + str(item.get_segment_by_index(item.curBitsIndex, 32)), end=',') print( 't_atp:' + str(item.get_segment_by_index(item.curBitsIndex, 32)), end=',') print( 'crc:' + hex(item.get_segment_by_index(item.curBitsIndex, 32)), end=',') if l_msg == item.curBytesIndex - byte_idx + 2: print('msg ok') else: print('fatal err! l_msg %d, real %d' % (l_msg, item.curBytesIndex - byte_idx + 2)) elif nid == 3: print('---->atp2ato_content:' + hex(item.get_segment_by_index(item.curBitsIndex, l_pkt - 21))) else: item.get_segment_by_index(item.curBitsIndex, l_pkt - 21) #print('content:' + hex(item.get_segment_by_index(item.curBitsIndex, l_pkt - 21))) # 在下一次监测前,判断退出 if item.curBitsIndex >= len( item.get_stream_in_bytes()) * 8 - 1 - 8 - 8: break # 解析完成 return ret
def ctrl_process_transfer(raw_record): """ 对于每一条原始记录板信息进行处理 :param raw_record: 原始记录信息 :return: 函数执行返回值和数据解析结果 ret_sig:0=政常,-1=错误消息,-2=存在启机过程 """ global dic_msg global dic_mvb global dic_sdu global dic_atp2ato_pkt global dic_ato2atp_pkt # 清空数据包字典 dic_atp2ato_pkt = {} dic_ato2atp_pkt = {} ret_sig = 0 # 返回值指示 streamReverseEsc = iner_escape_reverse(raw_record) # 反转义每一条记录 if streamReverseEsc == '': ret_sig = -1 return ret_sig item = BytesStream(streamReverseEsc) # 创建解析对象 # 解析消息头 #print('*' * 30 + 'MSG_HEAD ' + '*' * 30) for idx, content in enumerate(msg_head_width): #print(str_head_name[idx] + ':' + str(item.get_segment_by_index(item.curBitsIndex, msg_head_width[idx]))) dic_msg[str_head_name[idx]] = str( item.get_segment_by_index(item.curBitsIndex, msg_head_width[idx])) #print('*' * 30 + 'MSG_CONTENT ' + '*' * 30) # 解析内容 while item.curBitsIndex < len(item.get_stream_in_bytes()) * 8 - 1: nid = item.get_segment_by_index(item.curBitsIndex, 8) #print('nid:' + str(nid)) l_pkt = item.get_segment_by_index(item.curBitsIndex, 13) #print('l_pkt:' + str(l_pkt)) if nid == 0: mvb_recv = hex( item.get_segment_by_index(item.curBitsIndex, l_pkt - 21)).upper()[2:] if len(mvb_recv) % 2 == 0: dic_mvb['tcms2ato'] = 'MVB[3346]:039A120D' + mvb_recv else: dic_mvb['tcms2ato'] = 'MVB[3346]:039A120D' + '0' + mvb_recv elif nid == 1: ato_ctrl = hex( item.get_segment_by_index(item.curBitsIndex, l_pkt - 21)).upper()[2:] if len(ato_ctrl) % 2 == 0: dic_mvb['ato2tcms_ctrl'] = 'MVB[3344]:01CF100D' + ato_ctrl else: dic_mvb[ 'ato2tcms_ctrl'] = 'MVB[3344]:01CF100D' + '0' + ato_ctrl elif nid == 2: ato_state = hex( item.get_segment_by_index(item.curBitsIndex, l_pkt - 21)).upper()[2:] if len(ato_ctrl) % 2 == 0: dic_mvb['ato2tcms_state'] = 'MVB[3345]:01CF110D' + ato_state else: dic_mvb[ 'ato2tcms_state'] = 'MVB[3345]:01CF110D' + '0' + ato_state elif nid == 3: item.get_segment_by_index(item.curBitsIndex, 8) l_msg = item.get_segment_by_index(item.curBitsIndex, 8) # 记录bit数 bit_idx = item.curBitsIndex byte_idx = item.curBytesIndex # 以下为lpacket描述范围 item.get_segment_by_index(item.curBitsIndex, 8) all_len = item.get_segment_by_index(item.curBitsIndex, 13) # 监测ATP2ATO数据包(L_PACKET) while all_len != (item.curBitsIndex - bit_idx): nid = item.get_segment_by_index(item.curBitsIndex, 8) if nid == 0: item.get_segment_by_index(item.curBitsIndex, 13) item.get_segment_by_index(item.curBitsIndex, 2) item.get_segment_by_index(item.curBitsIndex, 24) # NID_LRBG item.get_segment_by_index(item.curBitsIndex, 15) item.get_segment_by_index(item.curBitsIndex, 2) item.get_segment_by_index(item.curBitsIndex, 2) item.get_segment_by_index(item.curBitsIndex, 15) item.get_segment_by_index(item.curBitsIndex, 15) Q_LENGTH = item.get_segment_by_index(item.curBitsIndex, 2) # 列车完整性确认 if Q_LENGTH == 1 or Q_LENGTH == 2: L_TRAINT = item.get_segment_by_index( item.curBitsIndex, 15) item.get_segment_by_index(item.curBitsIndex, 7) item.get_segment_by_index(item.curBitsIndex, 2) item.get_segment_by_index(item.curBitsIndex, 4) M_LEVEL = item.get_segment_by_index( item.curBitsIndex, 3) if M_LEVEL == 1: NID_STM = item.get_segment_by_index( item.curBitsIndex, 8) else: item.get_segment_by_index(item.curBitsIndex, 7) item.get_segment_by_index(item.curBitsIndex, 2) item.get_segment_by_index(item.curBitsIndex, 4) M_LEVEL = item.get_segment_by_index( item.curBitsIndex, 3) if M_LEVEL == 1: NID_STM = item.get_segment_by_index( item.curBitsIndex, 8) elif nid == 1: item.get_segment_by_index(item.curBitsIndex, 13) item.get_segment_by_index(item.curBitsIndex, 2) item.get_segment_by_index(item.curBitsIndex, 24) # NID_LRBG item.get_segment_by_index(item.curBitsIndex, 24) # NID_PRVBG item.get_segment_by_index(item.curBitsIndex, 15) item.get_segment_by_index(item.curBitsIndex, 2) item.get_segment_by_index(item.curBitsIndex, 2) item.get_segment_by_index(item.curBitsIndex, 15) item.get_segment_by_index(item.curBitsIndex, 15) Q_LENGTH = item.get_segment_by_index(item.curBitsIndex, 2) # 列车完整性确认 if Q_LENGTH == 1 or Q_LENGTH == 2: L_TRAINT = item.get_segment_by_index( item.curBitsIndex, 15) item.get_segment_by_index(item.curBitsIndex, 7) item.get_segment_by_index(item.curBitsIndex, 2) item.get_segment_by_index(item.curBitsIndex, 4) M_LEVEL = item.get_segment_by_index( item.curBitsIndex, 3) if M_LEVEL == 1: NID_STM = item.get_segment_by_index( item.curBitsIndex, 8) else: item.get_segment_by_index(item.curBitsIndex, 7) item.get_segment_by_index(item.curBitsIndex, 2) item.get_segment_by_index(item.curBitsIndex, 4) M_LEVEL = item.get_segment_by_index( item.curBitsIndex, 3) if M_LEVEL == 1: NID_STM = item.get_segment_by_index( item.curBitsIndex, 8) elif nid == 2: q_atopermit = item.get_segment_by_index( item.curBitsIndex, 2) q_ato_hardpermit = item.get_segment_by_index( item.curBitsIndex, 2) q_leftdoorpermit = item.get_segment_by_index( item.curBitsIndex, 2) q_rightdoorpermit = item.get_segment_by_index( item.curBitsIndex, 2) q_door_cmd_dir = item.get_segment_by_index( item.curBitsIndex, 2) q_tb = item.get_segment_by_index(item.curBitsIndex, 2) v_target = item.get_segment_by_index(item.curBitsIndex, 16) d_target = item.get_segment_by_index(item.curBitsIndex, 32) m_level = item.get_segment_by_index(item.curBitsIndex, 3) # M_LEVEL m_mode = item.get_segment_by_index(item.curBitsIndex, 4) # M_MODE o_train_pos = item.get_segment_by_index( item.curBitsIndex, 32) v_permitted = item.get_segment_by_index( item.curBitsIndex, 16) d_ma = item.get_segment_by_index(item.curBitsIndex, 16) m_ms_cmd = item.get_segment_by_index(item.curBitsIndex, 2) # M_MS_CMD d_neu_sec = item.get_segment_by_index( item.curBitsIndex, 16) # D_DEU_SEC m_low_frequency = item.get_segment_by_index( item.curBitsIndex, 8) q_stopstatus = item.get_segment_by_index( item.curBitsIndex, 4) m_atp_stop_err = item.get_segment_by_index( item.curBitsIndex, 16) d_station_mid_pos = item.get_segment_by_index( item.curBitsIndex, 32) d_jz_sig_pos = item.get_segment_by_index( item.curBitsIndex, 32) d_cz_sig_pos = item.get_segment_by_index( item.curBitsIndex, 32) d_tsm = item.get_segment_by_index(item.curBitsIndex, 32) m_cab_state = item.get_segment_by_index( item.curBitsIndex, 2) # M_CAB_STATE m_position = item.get_segment_by_index( item.curBitsIndex, 32) m_tco_state = item.get_segment_by_index( item.curBitsIndex, 2) reserve = item.get_segment_by_index(item.curBitsIndex, 2) # 解析到2包 dic_atp2ato_pkt[2] = [ q_atopermit, q_ato_hardpermit, q_leftdoorpermit, q_rightdoorpermit, q_door_cmd_dir, q_tb, v_target, d_target, m_level, m_mode, o_train_pos, v_permitted, d_ma, m_ms_cmd, d_neu_sec, m_low_frequency, q_stopstatus, m_atp_stop_err, d_station_mid_pos, d_jz_sig_pos, d_cz_sig_pos, d_tsm, m_cab_state, m_position, m_tco_state, reserve ] elif nid == 3: item.get_segment_by_index(item.curBitsIndex, 32) elif nid == 4: dic_sdu['atp_v'] = item.get_segment_by_index( item.curBitsIndex, 16, sign=1) dic_sdu['atp_s'] = item.get_segment_by_index( item.curBitsIndex, 32) elif nid == 5: n_units = item.get_segment_by_index(item.curBitsIndex, 8) nid_operational = item.get_segment_by_index( item.curBitsIndex, 32) nid_driver = item.get_segment_by_index( item.curBitsIndex, 32) btm_antenna_position = item.get_segment_by_index( item.curBitsIndex, 8) l_door_dis = item.get_segment_by_index( item.curBitsIndex, 16) l_sdu_wh_size_1 = item.get_segment_by_index( item.curBitsIndex, 16) l_sdu_wh_size_2 = item.get_segment_by_index( item.curBitsIndex, 16) t_cutoff_traction = item.get_segment_by_index( item.curBitsIndex, 16) nid_engine = item.get_segment_by_index( item.curBitsIndex, 24) v_ato_permitted = item.get_segment_by_index( item.curBitsIndex, 4) dic_atp2ato_pkt[5] = [ n_units, nid_operational, nid_driver, btm_antenna_position, l_door_dis, l_sdu_wh_size_1, l_sdu_wh_size_2, t_cutoff_traction, nid_engine, v_ato_permitted ] elif nid == 6: item.get_segment_by_index(item.curBitsIndex, 8) item.get_segment_by_index(item.curBitsIndex, 8) item.get_segment_by_index(item.curBitsIndex, 8) item.get_segment_by_index(item.curBitsIndex, 8) item.get_segment_by_index(item.curBitsIndex, 8) item.get_segment_by_index(item.curBitsIndex, 8) elif nid == 7: nid_bg = item.get_segment_by_index(item.curBitsIndex, 24) t_middle = item.get_segment_by_index(item.curBitsIndex, 32) d_pos_adj = item.get_segment_by_index(item.curBitsIndex, 32, sign=1) NID_XUSER = item.get_segment_by_index(item.curBitsIndex, 9) # 解析到7包 dic_atp2ato_pkt[7] = [ nid_bg, t_middle, d_pos_adj, NID_XUSER ] if NID_XUSER == 13: q_scale = item.get_segment_by_index( item.curBitsIndex, 2) q_platform = item.get_segment_by_index( item.curBitsIndex, 2) q_door = item.get_segment_by_index( item.curBitsIndex, 2) n_d = item.get_segment_by_index(item.curBitsIndex, 24) d_stop = item.get_segment_by_index( item.curBitsIndex, 15) # 解析到7包 dic_atp2ato_pkt[7] = [ nid_bg, t_middle, d_pos_adj, NID_XUSER, q_scale, q_platform, q_door, n_d, d_stop ] elif nid == 8: q_tsrs = item.get_segment_by_index(item.curBitsIndex, 1) nid_c = item.get_segment_by_index(item.curBitsIndex, 10) nid_tsrs = item.get_segment_by_index(item.curBitsIndex, 14) nid_radio_h = item.get_segment_by_index( item.curBitsIndex, 32) nid_radio_l = item.get_segment_by_index( item.curBitsIndex, 32) q_sleepssion = item.get_segment_by_index( item.curBitsIndex, 1) m_type = item.get_segment_by_index(item.curBitsIndex, 3) # 解析到8包 dic_atp2ato_pkt[8] = [ q_tsrs, nid_c, nid_tsrs, nid_radio_h, nid_radio_l, q_sleepssion, m_type ] elif nid == 9: N_ITER = item.get_segment_by_index(item.curBitsIndex, 5) for i in range(N_ITER): item.get_segment_by_index(item.curBitsIndex, 32) item.get_segment_by_index(item.curBitsIndex, 32) item.get_segment_by_index(item.curBitsIndex, 4) else: print('err!!!!!') # 消息校验和监测 if all_len == (item.curBitsIndex - bit_idx): if (all_len + 16) % 8 == 0: # 刚好除整数 pass else: # 重新校正bit索引 padding_bit = int( ((all_len + 16 + 7) // 8) * 8) - all_len - 16 # 字节下跳的bit数,减去消息头16bit和内容bit后 item.get_segment_by_index(item.curBitsIndex, padding_bit) # 计算分析消息结尾 item.get_segment_by_index(item.curBitsIndex, 32) item.get_segment_by_index(item.curBitsIndex, 32) item.get_segment_by_index(item.curBitsIndex, 32) if l_msg == item.curBytesIndex - byte_idx + 2: pass # 消息校验正确不打印 else: print('fatal err! l_msg %d, real %d' % (l_msg, item.curBytesIndex - byte_idx + 2)) elif nid == 4: item.get_segment_by_index(item.curBitsIndex, 8) l_msg = item.get_segment_by_index(item.curBitsIndex, 8) # 记录bit数 bit_idx = item.curBitsIndex byte_idx = item.curBytesIndex # 以下为lpacket描述范围 item.get_segment_by_index(item.curBitsIndex, 8) all_len = item.get_segment_by_index(item.curBitsIndex, 13) # 监测ATO2ATP数据包(L_PACKET) while all_len != (item.curBitsIndex - bit_idx): nid = item.get_segment_by_index(item.curBitsIndex, 8) if nid == 130: m_ato_mode = item.get_segment_by_index( item.curBitsIndex, 4) m_door_mode = item.get_segment_by_index( item.curBitsIndex, 2) m_door_status = item.get_segment_by_index( item.curBitsIndex, 2) m_atoerror = item.get_segment_by_index( item.curBitsIndex, 16) m_ato_stop_error = item.get_segment_by_index( item.curBitsIndex, 16) dic_ato2atp_pkt[130] = [ m_atoerror, m_ato_mode, m_ato_stop_error, m_door_mode, m_door_status ] elif nid == 131: m_ato_tbs = item.get_segment_by_index(item.curBitsIndex, 2) m_ato_skip = item.get_segment_by_index( item.curBitsIndex, 2) m_ato_plan = item.get_segment_by_index( item.curBitsIndex, 2) m_ato_time = item.get_segment_by_index( item.curBitsIndex, 16) m_tcms_com = item.get_segment_by_index( item.curBitsIndex, 2) m_gprs_radio = item.get_segment_by_index( item.curBitsIndex, 2) m_gprs_session = item.get_segment_by_index( item.curBitsIndex, 2) m_ato_control_strategy = item.get_segment_by_index( item.curBitsIndex, 4) paddings = item.get_segment_by_index(item.curBitsIndex, 16) dic_ato2atp_pkt[131] = [ m_ato_plan, m_ato_skip, m_ato_tbs, m_ato_time, m_gprs_radio, m_gprs_session, m_tcms_com, m_ato_control_strategy, paddings ] elif nid == 132: pass elif nid == 133: item.get_segment_by_index(item.curBitsIndex, 10) item.get_segment_by_index(item.curBitsIndex, 14) item.get_segment_by_index(item.curBitsIndex, 64) elif nid == 134: item.get_segment_by_index(item.curBitsIndex, 8) item.get_segment_by_index(item.curBitsIndex, 1) L_TEXT = item.get_segment_by_index(item.curBitsIndex, 8) for i in range(L_TEXT): item.get_segment_by_index(item.curBitsIndex, 8) else: print('err!!!!!') # 消息校验和监测 if all_len == (item.curBitsIndex - bit_idx): if (all_len + 16) % 8 == 0: # 刚好除整数 pass else: # 重新校正bit索引 padding_bit = int( ((all_len + 16 + 7) // 8) * 8) - all_len - 16 # 字节下跳的bit数,减去消息头16bit和内容bit后 item.get_segment_by_index(item.curBitsIndex, padding_bit) # 计算分析消息结尾 item.get_segment_by_index(item.curBitsIndex, 32) item.get_segment_by_index(item.curBitsIndex, 32) item.get_segment_by_index(item.curBitsIndex, 32) if l_msg == item.curBytesIndex - byte_idx + 2: pass # 消息校验正确不打印 else: print('fatal err! l_msg %d, real %d' % (l_msg, item.curBytesIndex - byte_idx + 2)) elif nid == 9: dic_sdu['ato_v'] = item.get_segment_by_index(item.curBitsIndex, 16) dic_sdu['ato_s'] = item.get_segment_by_index(item.curBitsIndex, 32) elif nid == 25: rp_start_train = item.get_segment_by_index(item.curBitsIndex, 1) rp_final_station = item.get_segment_by_index(item.curBitsIndex, 1) rp_q_pl_legal = item.get_segment_by_index(item.curBitsIndex, 1) rp_pl_update = item.get_segment_by_index(item.curBitsIndex, 32) rp_pl_num = item.get_segment_by_index(item.curBitsIndex, 2) for rp_cnt in range(rp_pl_num): rp_ob_sys_time = item.get_segment_by_index( item.curBitsIndex, 32) rp_wayside_time = item.get_segment_by_index( item.curBitsIndex, 32) rp_pl_legal_arrival_time = item.get_segment_by_index( item.curBitsIndex, 32) rp_pl_legal_depart_time = item.get_segment_by_index( item.curBitsIndex, 32) rp_pl_legal_arrival_track = item.get_segment_by_index( item.curBitsIndex, 24) rp_pl_legal_depart_track = item.get_segment_by_index( item.curBitsIndex, 24) rp_pl_legal_skip = item.get_segment_by_index( item.curBitsIndex, 2) rp_pl_legal_task = item.get_segment_by_index( item.curBitsIndex, 2) rp_pl_out_time = item.get_segment_by_index(item.curBitsIndex, 1) rp_pl_stn_state = item.get_segment_by_index(item.curBitsIndex, 2) rp_pl_track_balise = item.get_segment_by_index( item.curBitsIndex, 24) rp_pl_track_plan = item.get_segment_by_index(item.curBitsIndex, 24) rp_pl_in_use = item.get_segment_by_index(item.curBitsIndex, 2) rp_pl_valid = item.get_segment_by_index(item.curBitsIndex, 1) rp_pl_output_arr_time = item.get_segment_by_index( item.curBitsIndex, 32) rp_pl_output_depart_time = item.get_segment_by_index( item.curBitsIndex, 32) rp_pl_output_skip = item.get_segment_by_index(item.curBitsIndex, 2) rp_pl_output_task = item.get_segment_by_index(item.curBitsIndex, 2) elif nid == 53: # ATO停车状态 q_stable = str(item.get_segment_by_index(item.curBitsIndex, 2)) q_real_stable = str(item.get_segment_by_index( item.curBitsIndex, 2)) ato_stop_err = str( item.get_segment_by_index(item.curBitsIndex, 16, sign=1)) dic_msg['stop_state'] = (q_stable, q_real_stable, ato_stop_err) elif nid == 52: o_jd_stop = str(item.get_segment_by_index(item.curBitsIndex, 32)) o_stn_dis_stop = str( item.get_segment_by_index(item.curBitsIndex, 32)) o_mid_stop = str(item.get_segment_by_index(item.curBitsIndex, 32)) o_ma_stop = str(item.get_segment_by_index(item.curBitsIndex, 32)) o_stop_use = str(item.get_segment_by_index(item.curBitsIndex, 32)) q_platform = str(item.get_segment_by_index(item.curBitsIndex, 1)) dic_msg['stop'] = (o_jd_stop, o_stn_dis_stop, o_mid_stop, o_ma_stop, o_stop_use, q_platform) elif nid == 54: v_ato_cmd = str(item.get_segment_by_index(item.curBitsIndex, 16)) v_atp_cmd = str(item.get_segment_by_index(item.curBitsIndex, 16)) ctrl_machine = str(item.get_segment_by_index(item.curBitsIndex, 6)) adj_ramp = str( item.get_segment_by_index(item.curBitsIndex, 8, sign=1)) adj_es_ramp = str( item.get_segment_by_index(item.curBitsIndex, 8, sign=1)) v_s_target = str(item.get_segment_by_index(item.curBitsIndex, 16)) o_s_target = str(item.get_segment_by_index(item.curBitsIndex, 32)) lvl_raw = str( item.get_segment_by_index(item.curBitsIndex, 8, sign=1)) lvl_filter_b = str( item.get_segment_by_index(item.curBitsIndex, 8, sign=1)) lvl_filter_p = str( item.get_segment_by_index(item.curBitsIndex, 8, sign=1)) lvl_filter_ramp = str( item.get_segment_by_index(item.curBitsIndex, 8, sign=1)) lvl_filter_wind = str( item.get_segment_by_index(item.curBitsIndex, 8, sign=1)) lvl_filter_gfx = str( item.get_segment_by_index(item.curBitsIndex, 8, sign=1)) lvl_filter_out = str( item.get_segment_by_index(item.curBitsIndex, 8, sign=1)) q_ato_cutoff = str(item.get_segment_by_index(item.curBitsIndex, 4)) o_es_pos = str(item.get_segment_by_index(item.curBitsIndex, 32)) v_es_speed = str(item.get_segment_by_index(item.curBitsIndex, 16)) o_ma = str(item.get_segment_by_index(item.curBitsIndex, 32)) # 解析到了关键包 dic_msg['sc'] = (dic_msg['m_pos'], dic_msg['v_speed'], v_ato_cmd, v_atp_cmd, lvl_filter_out, lvl_filter_out, o_es_pos, v_es_speed, adj_ramp, adj_es_ramp, v_s_target, o_s_target, o_ma, o_ma, ctrl_machine) elif nid == 84: item.get_segment_by_index(item.curBitsIndex, 8) # 主控主版本 item.get_segment_by_index(item.curBitsIndex, 8) # 主控中版本 item.get_segment_by_index(item.curBitsIndex, 8) # 主控小版本 ret_sig = -2 return ret_sig # 存在启机过程,重新生成解析文件 else: item.get_segment_by_index(item.curBitsIndex, l_pkt - 21) #print('content:' + hex(item.get_segment_by_index(item.curBitsIndex, l_pkt - 21))) # 记录板消息,下一次监测前,判断退出 if item.curBitsIndex >= len( item.get_stream_in_bytes()) * 8 - 1 - 8 - 8: break else: pass # 解析完成 return ret_sig
def test_msgZeroHeadSetCase(self): item = BytesStream('') item.set_segment_by_index(100823, item.curBitsIndex, 24) if False: item.set_segment_by_index(13, item.curBitsIndex, 9) # 按照CTCS3+ATO中C13包 else: item.set_segment_by_index(0, item.curBitsIndex, 9) item.set_segment_by_index(0, item.curBitsIndex, 2) item.set_segment_by_index(0, item.curBitsIndex, 2) item.set_segment_by_index(0, item.curBitsIndex, 24) item.set_segment_by_index(0, item.curBitsIndex, 15) item.set_segment_by_index(1, item.curBitsIndex, 4) self.assertEqual("0189d700000000000001", item.hexStream) item = BytesStream('') item.fast_set_segment_by_index(100823, item.curBitsIndex, 24) if False: item.fast_set_segment_by_index(13, item.curBitsIndex, 9) # 按照CTCS3+ATO中C13包 else: item.fast_set_segment_by_index(0, item.curBitsIndex, 9) item.fast_set_segment_by_index(0, item.curBitsIndex, 2) item.fast_set_segment_by_index(0, item.curBitsIndex, 2) item.fast_set_segment_by_index(0, item.curBitsIndex, 24) item.fast_set_segment_by_index(0, item.curBitsIndex, 15) item.fast_set_segment_by_index(1, item.curBitsIndex, 4)
def test_msgSetGetCase(self): raw_hex = '2D49FA0EA01805276EF820C8180D40CAF012D40457000C79C2B03D425E4521A3459D89C30AC20003FFFFFFFC00637DFC006C' \ 'C68C00266B190006B7FBA0' stream_fast_get_b = BytesStream(raw_hex, endian=0) self.assertEqual( 45, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 73, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 250, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 468, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 13)) # sp3 self.assertEqual( 3, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 10808799, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) # sp4 self.assertEqual( 4, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 6403, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 16)) self.assertEqual( 27793758, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) # sp2 self.assertEqual( 2, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 1, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 1, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 2, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 2, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 2, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 0, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 2222, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 16)) self.assertEqual( 1635205, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( 3, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 3)) self.assertEqual( 0, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 4)) self.assertEqual( 1027759685, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( 8611, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 16)) self.assertEqual( 17821, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 16)) self.assertEqual( 2, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 9996, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 16)) self.assertEqual( 43, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 0, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 4)) self.assertEqual( 32768, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 16)) self.assertEqual( 4294967295, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( 1630079, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( 1782179, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( 629446, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( 1, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 440315, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( 2, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 2, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) stream_fast_set_b = BytesStream('', endian=0) stream_fast_set_b.fast_set_segment_by_index( 45, stream_fast_set_b.curBitsIndex, 8) stream_fast_set_b.fast_set_segment_by_index( 73, stream_fast_set_b.curBitsIndex, 8) stream_fast_set_b.fast_set_segment_by_index( 250, stream_fast_set_b.curBitsIndex, 8) stream_fast_set_b.fast_set_segment_by_index( 468, stream_fast_set_b.curBitsIndex, 13) # sp3 stream_fast_set_b.fast_set_segment_by_index( 3, stream_fast_set_b.curBitsIndex, 8) stream_fast_set_b.fast_set_segment_by_index( 10808799, stream_fast_set_b.curBitsIndex, 32) # sp4 stream_fast_set_b.fast_set_segment_by_index( 4, stream_fast_set_b.curBitsIndex, 8) stream_fast_set_b.fast_set_segment_by_index( 6403, stream_fast_set_b.curBitsIndex, 16) stream_fast_set_b.fast_set_segment_by_index( 27793758, stream_fast_set_b.curBitsIndex, 32) stream_fast_set_b.fast_set_segment_by_index( 2, stream_fast_set_b.curBitsIndex, 8) stream_fast_set_b.fast_set_segment_by_index( 1, stream_fast_set_b.curBitsIndex, 2) stream_fast_set_b.fast_set_segment_by_index( 1, stream_fast_set_b.curBitsIndex, 2) stream_fast_set_b.fast_set_segment_by_index( 2, stream_fast_set_b.curBitsIndex, 2) stream_fast_set_b.fast_set_segment_by_index( 2, stream_fast_set_b.curBitsIndex, 2) stream_fast_set_b.fast_set_segment_by_index( 2, stream_fast_set_b.curBitsIndex, 2) stream_fast_set_b.fast_set_segment_by_index( 0, stream_fast_set_b.curBitsIndex, 2) stream_fast_set_b.fast_set_segment_by_index( 2222, stream_fast_set_b.curBitsIndex, 16) stream_fast_set_b.fast_set_segment_by_index( 1635205, stream_fast_set_b.curBitsIndex, 32) stream_fast_set_b.fast_set_segment_by_index( 3, stream_fast_set_b.curBitsIndex, 3) stream_fast_set_b.fast_set_segment_by_index( 0, stream_fast_set_b.curBitsIndex, 4) stream_fast_set_b.fast_set_segment_by_index( 1027759685, stream_fast_set_b.curBitsIndex, 32) stream_fast_set_b.fast_set_segment_by_index( 8611, stream_fast_set_b.curBitsIndex, 16) stream_fast_set_b.fast_set_segment_by_index( 17821, stream_fast_set_b.curBitsIndex, 16) stream_fast_set_b.fast_set_segment_by_index( 2, stream_fast_set_b.curBitsIndex, 2) stream_fast_set_b.fast_set_segment_by_index( 9996, stream_fast_set_b.curBitsIndex, 16) stream_fast_set_b.fast_set_segment_by_index( 43, stream_fast_set_b.curBitsIndex, 8) stream_fast_set_b.fast_set_segment_by_index( 0, stream_fast_set_b.curBitsIndex, 4) stream_fast_set_b.fast_set_segment_by_index( 32768, stream_fast_set_b.curBitsIndex, 16) stream_fast_set_b.fast_set_segment_by_index( 4294967295, stream_fast_set_b.curBitsIndex, 32) stream_fast_set_b.fast_set_segment_by_index( 1630079, stream_fast_set_b.curBitsIndex, 32) stream_fast_set_b.fast_set_segment_by_index( 1782179, stream_fast_set_b.curBitsIndex, 32) stream_fast_set_b.fast_set_segment_by_index( 629446, stream_fast_set_b.curBitsIndex, 32) stream_fast_set_b.fast_set_segment_by_index( 1, stream_fast_set_b.curBitsIndex, 2) stream_fast_set_b.fast_set_segment_by_index( 440315, stream_fast_set_b.curBitsIndex, 32) stream_fast_set_b.fast_set_segment_by_index( 2, stream_fast_set_b.curBitsIndex, 2) stream_fast_set_b.fast_set_segment_by_index( 2, stream_fast_set_b.curBitsIndex, 2) self.assertEqual(stream_fast_set_b.hexStream.upper(), raw_hex) stream_get_b = BytesStream(raw_hex, endian=0) self.assertEqual( 45, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 8)) self.assertEqual( 73, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 8)) self.assertEqual( 250, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 8)) self.assertEqual( 468, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 13)) # sp3 self.assertEqual( 3, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 8)) self.assertEqual( 10808799, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 32)) # sp4 self.assertEqual( 4, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 8)) self.assertEqual( 6403, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 16)) self.assertEqual( 27793758, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 32)) # sp2 self.assertEqual( 2, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 8)) self.assertEqual( 1, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 2)) self.assertEqual( 1, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 2)) self.assertEqual( 2, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 2)) self.assertEqual( 2, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 2)) self.assertEqual( 2, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 2)) self.assertEqual( 0, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 2)) self.assertEqual( 2222, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 16)) self.assertEqual( 1635205, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 32)) self.assertEqual( 3, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 3)) self.assertEqual( 0, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 4)) self.assertEqual( 1027759685, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 32)) self.assertEqual( 8611, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 16)) self.assertEqual( 17821, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 16)) self.assertEqual( 2, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 2)) self.assertEqual( 9996, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 16)) self.assertEqual( 43, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 8)) self.assertEqual( 0, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 4)) self.assertEqual( 32768, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 16)) self.assertEqual( 4294967295, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 32)) self.assertEqual( 1630079, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 32)) self.assertEqual( 1782179, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 32)) self.assertEqual( 629446, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 32)) self.assertEqual( 1, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 2)) self.assertEqual( 440315, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 32)) self.assertEqual( 2, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 2)) self.assertEqual( 2, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 2)) stream_set_b = BytesStream('', endian=0) stream_set_b.set_segment_by_index(45, stream_set_b.curBitsIndex, 8) stream_set_b.set_segment_by_index(73, stream_set_b.curBitsIndex, 8) stream_set_b.set_segment_by_index(250, stream_set_b.curBitsIndex, 8) stream_set_b.set_segment_by_index(468, stream_set_b.curBitsIndex, 13) # sp3 stream_set_b.set_segment_by_index(3, stream_set_b.curBitsIndex, 8) stream_set_b.set_segment_by_index(10808799, stream_set_b.curBitsIndex, 32) # sp4 stream_set_b.set_segment_by_index(4, stream_set_b.curBitsIndex, 8) stream_set_b.set_segment_by_index(6403, stream_set_b.curBitsIndex, 16) stream_set_b.set_segment_by_index(27793758, stream_set_b.curBitsIndex, 32) stream_set_b.set_segment_by_index(2, stream_set_b.curBitsIndex, 8) stream_set_b.set_segment_by_index(1, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(1, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(2, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(2, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(2, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(0, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(2222, stream_set_b.curBitsIndex, 16) stream_set_b.set_segment_by_index(1635205, stream_set_b.curBitsIndex, 32) stream_set_b.set_segment_by_index(3, stream_set_b.curBitsIndex, 3) stream_set_b.set_segment_by_index(0, stream_set_b.curBitsIndex, 4) stream_set_b.set_segment_by_index(1027759685, stream_set_b.curBitsIndex, 32) stream_set_b.set_segment_by_index(8611, stream_set_b.curBitsIndex, 16) stream_set_b.set_segment_by_index(17821, stream_set_b.curBitsIndex, 16) stream_set_b.set_segment_by_index(2, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(9996, stream_set_b.curBitsIndex, 16) stream_set_b.set_segment_by_index(43, stream_set_b.curBitsIndex, 8) stream_set_b.set_segment_by_index(0, stream_set_b.curBitsIndex, 4) stream_set_b.set_segment_by_index(32768, stream_set_b.curBitsIndex, 16) stream_set_b.set_segment_by_index(4294967295, stream_set_b.curBitsIndex, 32) stream_set_b.set_segment_by_index(1630079, stream_set_b.curBitsIndex, 32) stream_set_b.set_segment_by_index(1782179, stream_set_b.curBitsIndex, 32) stream_set_b.set_segment_by_index(629446, stream_set_b.curBitsIndex, 32) stream_set_b.set_segment_by_index(1, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(440315, stream_set_b.curBitsIndex, 32) stream_set_b.set_segment_by_index(2, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(2, stream_set_b.curBitsIndex, 2) self.assertEqual(stream_set_b.hexStream.upper(), raw_hex) # test raw_hex2 = '2D18FB026C1E2FFFEB8FFFF8' stream_fast_get_b = BytesStream(raw_hex2, endian=0) # sp131 self.assertEqual( 45, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 24, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 251, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 77, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 13)) self.assertEqual( 131, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 3, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 0, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 1, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 32767, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 16)) self.assertEqual( 1, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 1, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 3, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 1, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 4)) self.assertEqual( 65535, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 16)) stream_set_b = BytesStream('', endian=0) stream_set_b.set_segment_by_index(45, stream_set_b.curBitsIndex, 8) stream_set_b.set_segment_by_index(24, stream_set_b.curBitsIndex, 8) stream_set_b.set_segment_by_index(251, stream_set_b.curBitsIndex, 8) stream_set_b.set_segment_by_index(77, stream_set_b.curBitsIndex, 13) stream_set_b.set_segment_by_index(131, stream_set_b.curBitsIndex, 8) stream_set_b.set_segment_by_index(3, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(0, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(1, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(32767, stream_set_b.curBitsIndex, 16) stream_set_b.set_segment_by_index(1, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(1, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(3, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(1, stream_set_b.curBitsIndex, 4) stream_set_b.set_segment_by_index(65535, stream_set_b.curBitsIndex, 16) self.assertEqual(stream_set_b.hexStream.upper(), raw_hex2) # test raw_hex3 = '7EC92718003B00781726AADAEE5D9799E0DA1B1F05F1090228F9D85069D21808255D80000000000000000000000000000000812AED55FFFFFFFFFFFC0562168000000000320B4204A8DC28FFFFFFFFFFFFFC00000000000000014060FD2D1DFA03A81817209FD02705F004113C98000765F102E413FA9B42FC660008AB81800000000000000000055000089C45555180000000000000000000000000007F' stream_fast_get_b = BytesStream(raw_hex3, endian=0) self.assertEqual( 126, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 201, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 156, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 10)) self.assertEqual( 1, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 1, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 1)) self.assertEqual( 483343, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( 48551259, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( 1573630707, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( 1008419683, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( -8002, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 16, sign=1)) self.assertEqual( 1, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 3)) # p9 self.assertEqual( 9, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 69, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 13)) self.assertEqual( 7995, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 16)) self.assertEqual( 168639043, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) # p1 self.assertEqual( 1, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 149, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 13)) self.assertEqual( int(0x76000000000000000000000000000000), stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 149 - 21)) # p2 self.assertEqual( 2, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 149, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 13)) self.assertEqual( int(0x76aafffffffffffe02b10b4000000000), stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 149 - 21)) # p25 # self.assertEqual(25, stream_fast_get_b.fast_get_segment_by_index(stream_fast_get_b.curBitsIndex, 8)) # self.assertEqual(180, stream_fast_get_b.fast_get_segment_by_index(stream_fast_get_b.curBitsIndex, 13)) # self.assertEqual(0, stream_fast_get_b.fast_get_segment_by_index(stream_fast_get_b.curBitsIndex, 1)) # self.assertEqual(0, stream_fast_get_b.fast_get_segment_by_index(stream_fast_get_b.curBitsIndex, 1)) # self.assertEqual(1, stream_fast_get_b.fast_get_segment_by_index(stream_fast_get_b.curBitsIndex, 1)) # self.assertEqual(39087636, stream_fast_get_b.fast_get_segment_by_index(stream_fast_get_b.curBitsIndex, 32)) # self.assertEqual(1, stream_fast_get_b.fast_get_segment_by_index(stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 25, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 180, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 13)) self.assertEqual( 0, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 1)) self.assertEqual( 0, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 1)) self.assertEqual( 1, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 1)) self.assertEqual( 39087636, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( 1, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 2))
class TestCommonParse(unittest.TestCase): def setUp(self): self.hex_string = '452F68' self.bytesStream_b_endian = BytesStream(self.hex_string, 0) self.hex_string = '12345678' self.hex_string2 = '039A120D' self.bytesStream_l_endian = BytesStream(self.hex_string, 1) self.bytesStream_l_endian2 = BytesStream(self.hex_string2, 1) self.hex_string = '5a' self.bytesStream_l_set = BytesStream(self.hex_string, 1) self.hex_string = '005a' self.bytesStream_l_set2 = BytesStream(self.hex_string, 1) def tearDown(self): pass def test_getSegmentByIndex(self): # regular test self.assertEqual(5, self.bytesStream_b_endian.get_segment_by_index(4, 4)) self.assertEqual(15, self.bytesStream_b_endian.get_segment_by_index(12, 4)) self.assertEqual(5, self.bytesStream_b_endian.get_segment_by_index(8, 5)) self.assertEqual( 7, self.bytesStream_b_endian.get_segment_by_index( self.bytesStream_b_endian.curBitsIndex, 3)) self.assertEqual( 3, self.bytesStream_b_endian.get_segment_by_index( self.bytesStream_b_endian.curBitsIndex, 3)) self.assertEqual( 2, self.bytesStream_b_endian.get_segment_by_index( self.bytesStream_b_endian.curBitsIndex, 3)) self.assertEqual( 0, self.bytesStream_b_endian.get_segment_by_index( self.bytesStream_b_endian.curBitsIndex, 2)) # boundary test self.assertEqual(0, self.bytesStream_b_endian.get_segment_by_index(0, 1)) self.assertEqual(1, self.bytesStream_b_endian.get_segment_by_index(0, 2)) self.assertEqual(4534120, self.bytesStream_b_endian.get_segment_by_index(0, 24)) self.assertEqual(0, self.bytesStream_b_endian.get_segment_by_index(23, 1)) # regular test self.assertEqual(22068, self.bytesStream_l_endian.get_segment_by_index(8, 16)) self.assertEqual(86, self.bytesStream_l_endian.get_segment_by_index(16, 8)) self.assertEqual( 30806, self.bytesStream_l_endian.get_segment_by_index(16, 16)) self.assertEqual(7886388, self.bytesStream_l_endian.get_segment_by_index(8, 24)) # exception test # self.assertEqual(5, self.bytesStream.getSegmentByIndex(0, 0)) # self.assertEqual(4534120, self.bytesStream.getSegmentByIndex(24, 1)) def test_fastGetSegmentByIndex(self): # regular test self.assertEqual( 22068, self.bytesStream_l_endian.fast_get_segment_by_index(8, 16)) self.assertEqual( 86, self.bytesStream_l_endian.fast_get_segment_by_index(16, 8)) self.assertEqual( 30806, self.bytesStream_l_endian.fast_get_segment_by_index(16, 16)) self.assertEqual( 7886388, self.bytesStream_l_endian.fast_get_segment_by_index(8, 24)) self.assertEqual( 3346, self.bytesStream_l_endian2.fast_get_segment_by_index(16, 16)) def test_setSegmentByIndex(self): self.assertEqual( '5a6d', self.bytesStream_l_set.set_segment_by_index(109, 8, 8)) self.assertEqual('5a80', self.bytesStream_l_set.set_segment_by_index(4, 8, 3)) self.assertEqual('5f', self.bytesStream_l_set.set_segment_by_index(7, 5, 3)) self.assertEqual('56', self.bytesStream_l_set.set_segment_by_index(3, 4, 3)) self.assertEqual('565a', self.bytesStream_l_set.set_segment_by_index(90, 8, 8)) self.assertEqual( '7e', self.bytesStream_l_set.set_segment_by_index(126, 0, 8)) self.assertEqual( '7ec9', self.bytesStream_l_set.set_segment_by_index( 201, self.bytesStream_l_set.curBitsIndex, 8)) hex_string = '005a' bytesStream_l_set2 = BytesStream(hex_string) self.assertEqual('005a0000fe', bytesStream_l_set2.set_segment_by_index(254, 32, 8)) def test_fastSetSegmentByIndex(self): self.assertEqual( '5a6d', self.bytesStream_l_set.fast_set_segment_by_index(109, 8, 8)) self.assertEqual( '5a80', self.bytesStream_l_set.fast_set_segment_by_index(4, 8, 3)) self.assertEqual( '5f', self.bytesStream_l_set.fast_set_segment_by_index(7, 5, 3)) self.assertEqual( '56', self.bytesStream_l_set.fast_set_segment_by_index(3, 4, 3)) self.assertEqual( '565a', self.bytesStream_l_set.fast_set_segment_by_index(90, 8, 8)) self.assertEqual( '7e', self.bytesStream_l_set.fast_set_segment_by_index(126, 0, 8)) self.assertEqual( '005a0000fe', self.bytesStream_l_set2.fast_set_segment_by_index(254, 32, 8)) def test_msgSetGetCase(self): raw_hex = '2D49FA0EA01805276EF820C8180D40CAF012D40457000C79C2B03D425E4521A3459D89C30AC20003FFFFFFFC00637DFC006C' \ 'C68C00266B190006B7FBA0' stream_fast_get_b = BytesStream(raw_hex, endian=0) self.assertEqual( 45, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 73, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 250, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 468, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 13)) # sp3 self.assertEqual( 3, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 10808799, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) # sp4 self.assertEqual( 4, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 6403, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 16)) self.assertEqual( 27793758, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) # sp2 self.assertEqual( 2, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 1, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 1, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 2, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 2, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 2, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 0, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 2222, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 16)) self.assertEqual( 1635205, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( 3, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 3)) self.assertEqual( 0, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 4)) self.assertEqual( 1027759685, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( 8611, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 16)) self.assertEqual( 17821, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 16)) self.assertEqual( 2, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 9996, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 16)) self.assertEqual( 43, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 0, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 4)) self.assertEqual( 32768, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 16)) self.assertEqual( 4294967295, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( 1630079, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( 1782179, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( 629446, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( 1, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 440315, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( 2, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 2, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) stream_fast_set_b = BytesStream('', endian=0) stream_fast_set_b.fast_set_segment_by_index( 45, stream_fast_set_b.curBitsIndex, 8) stream_fast_set_b.fast_set_segment_by_index( 73, stream_fast_set_b.curBitsIndex, 8) stream_fast_set_b.fast_set_segment_by_index( 250, stream_fast_set_b.curBitsIndex, 8) stream_fast_set_b.fast_set_segment_by_index( 468, stream_fast_set_b.curBitsIndex, 13) # sp3 stream_fast_set_b.fast_set_segment_by_index( 3, stream_fast_set_b.curBitsIndex, 8) stream_fast_set_b.fast_set_segment_by_index( 10808799, stream_fast_set_b.curBitsIndex, 32) # sp4 stream_fast_set_b.fast_set_segment_by_index( 4, stream_fast_set_b.curBitsIndex, 8) stream_fast_set_b.fast_set_segment_by_index( 6403, stream_fast_set_b.curBitsIndex, 16) stream_fast_set_b.fast_set_segment_by_index( 27793758, stream_fast_set_b.curBitsIndex, 32) stream_fast_set_b.fast_set_segment_by_index( 2, stream_fast_set_b.curBitsIndex, 8) stream_fast_set_b.fast_set_segment_by_index( 1, stream_fast_set_b.curBitsIndex, 2) stream_fast_set_b.fast_set_segment_by_index( 1, stream_fast_set_b.curBitsIndex, 2) stream_fast_set_b.fast_set_segment_by_index( 2, stream_fast_set_b.curBitsIndex, 2) stream_fast_set_b.fast_set_segment_by_index( 2, stream_fast_set_b.curBitsIndex, 2) stream_fast_set_b.fast_set_segment_by_index( 2, stream_fast_set_b.curBitsIndex, 2) stream_fast_set_b.fast_set_segment_by_index( 0, stream_fast_set_b.curBitsIndex, 2) stream_fast_set_b.fast_set_segment_by_index( 2222, stream_fast_set_b.curBitsIndex, 16) stream_fast_set_b.fast_set_segment_by_index( 1635205, stream_fast_set_b.curBitsIndex, 32) stream_fast_set_b.fast_set_segment_by_index( 3, stream_fast_set_b.curBitsIndex, 3) stream_fast_set_b.fast_set_segment_by_index( 0, stream_fast_set_b.curBitsIndex, 4) stream_fast_set_b.fast_set_segment_by_index( 1027759685, stream_fast_set_b.curBitsIndex, 32) stream_fast_set_b.fast_set_segment_by_index( 8611, stream_fast_set_b.curBitsIndex, 16) stream_fast_set_b.fast_set_segment_by_index( 17821, stream_fast_set_b.curBitsIndex, 16) stream_fast_set_b.fast_set_segment_by_index( 2, stream_fast_set_b.curBitsIndex, 2) stream_fast_set_b.fast_set_segment_by_index( 9996, stream_fast_set_b.curBitsIndex, 16) stream_fast_set_b.fast_set_segment_by_index( 43, stream_fast_set_b.curBitsIndex, 8) stream_fast_set_b.fast_set_segment_by_index( 0, stream_fast_set_b.curBitsIndex, 4) stream_fast_set_b.fast_set_segment_by_index( 32768, stream_fast_set_b.curBitsIndex, 16) stream_fast_set_b.fast_set_segment_by_index( 4294967295, stream_fast_set_b.curBitsIndex, 32) stream_fast_set_b.fast_set_segment_by_index( 1630079, stream_fast_set_b.curBitsIndex, 32) stream_fast_set_b.fast_set_segment_by_index( 1782179, stream_fast_set_b.curBitsIndex, 32) stream_fast_set_b.fast_set_segment_by_index( 629446, stream_fast_set_b.curBitsIndex, 32) stream_fast_set_b.fast_set_segment_by_index( 1, stream_fast_set_b.curBitsIndex, 2) stream_fast_set_b.fast_set_segment_by_index( 440315, stream_fast_set_b.curBitsIndex, 32) stream_fast_set_b.fast_set_segment_by_index( 2, stream_fast_set_b.curBitsIndex, 2) stream_fast_set_b.fast_set_segment_by_index( 2, stream_fast_set_b.curBitsIndex, 2) self.assertEqual(stream_fast_set_b.hexStream.upper(), raw_hex) stream_get_b = BytesStream(raw_hex, endian=0) self.assertEqual( 45, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 8)) self.assertEqual( 73, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 8)) self.assertEqual( 250, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 8)) self.assertEqual( 468, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 13)) # sp3 self.assertEqual( 3, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 8)) self.assertEqual( 10808799, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 32)) # sp4 self.assertEqual( 4, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 8)) self.assertEqual( 6403, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 16)) self.assertEqual( 27793758, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 32)) # sp2 self.assertEqual( 2, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 8)) self.assertEqual( 1, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 2)) self.assertEqual( 1, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 2)) self.assertEqual( 2, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 2)) self.assertEqual( 2, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 2)) self.assertEqual( 2, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 2)) self.assertEqual( 0, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 2)) self.assertEqual( 2222, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 16)) self.assertEqual( 1635205, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 32)) self.assertEqual( 3, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 3)) self.assertEqual( 0, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 4)) self.assertEqual( 1027759685, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 32)) self.assertEqual( 8611, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 16)) self.assertEqual( 17821, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 16)) self.assertEqual( 2, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 2)) self.assertEqual( 9996, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 16)) self.assertEqual( 43, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 8)) self.assertEqual( 0, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 4)) self.assertEqual( 32768, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 16)) self.assertEqual( 4294967295, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 32)) self.assertEqual( 1630079, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 32)) self.assertEqual( 1782179, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 32)) self.assertEqual( 629446, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 32)) self.assertEqual( 1, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 2)) self.assertEqual( 440315, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 32)) self.assertEqual( 2, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 2)) self.assertEqual( 2, stream_get_b.get_segment_by_index(stream_get_b.curBitsIndex, 2)) stream_set_b = BytesStream('', endian=0) stream_set_b.set_segment_by_index(45, stream_set_b.curBitsIndex, 8) stream_set_b.set_segment_by_index(73, stream_set_b.curBitsIndex, 8) stream_set_b.set_segment_by_index(250, stream_set_b.curBitsIndex, 8) stream_set_b.set_segment_by_index(468, stream_set_b.curBitsIndex, 13) # sp3 stream_set_b.set_segment_by_index(3, stream_set_b.curBitsIndex, 8) stream_set_b.set_segment_by_index(10808799, stream_set_b.curBitsIndex, 32) # sp4 stream_set_b.set_segment_by_index(4, stream_set_b.curBitsIndex, 8) stream_set_b.set_segment_by_index(6403, stream_set_b.curBitsIndex, 16) stream_set_b.set_segment_by_index(27793758, stream_set_b.curBitsIndex, 32) stream_set_b.set_segment_by_index(2, stream_set_b.curBitsIndex, 8) stream_set_b.set_segment_by_index(1, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(1, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(2, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(2, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(2, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(0, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(2222, stream_set_b.curBitsIndex, 16) stream_set_b.set_segment_by_index(1635205, stream_set_b.curBitsIndex, 32) stream_set_b.set_segment_by_index(3, stream_set_b.curBitsIndex, 3) stream_set_b.set_segment_by_index(0, stream_set_b.curBitsIndex, 4) stream_set_b.set_segment_by_index(1027759685, stream_set_b.curBitsIndex, 32) stream_set_b.set_segment_by_index(8611, stream_set_b.curBitsIndex, 16) stream_set_b.set_segment_by_index(17821, stream_set_b.curBitsIndex, 16) stream_set_b.set_segment_by_index(2, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(9996, stream_set_b.curBitsIndex, 16) stream_set_b.set_segment_by_index(43, stream_set_b.curBitsIndex, 8) stream_set_b.set_segment_by_index(0, stream_set_b.curBitsIndex, 4) stream_set_b.set_segment_by_index(32768, stream_set_b.curBitsIndex, 16) stream_set_b.set_segment_by_index(4294967295, stream_set_b.curBitsIndex, 32) stream_set_b.set_segment_by_index(1630079, stream_set_b.curBitsIndex, 32) stream_set_b.set_segment_by_index(1782179, stream_set_b.curBitsIndex, 32) stream_set_b.set_segment_by_index(629446, stream_set_b.curBitsIndex, 32) stream_set_b.set_segment_by_index(1, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(440315, stream_set_b.curBitsIndex, 32) stream_set_b.set_segment_by_index(2, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(2, stream_set_b.curBitsIndex, 2) self.assertEqual(stream_set_b.hexStream.upper(), raw_hex) # test raw_hex2 = '2D18FB026C1E2FFFEB8FFFF8' stream_fast_get_b = BytesStream(raw_hex2, endian=0) # sp131 self.assertEqual( 45, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 24, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 251, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 77, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 13)) self.assertEqual( 131, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 3, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 0, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 1, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 32767, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 16)) self.assertEqual( 1, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 1, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 3, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 1, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 4)) self.assertEqual( 65535, stream_fast_get_b.fast_get_segment_by_index( stream_fast_get_b.curBitsIndex, 16)) stream_set_b = BytesStream('', endian=0) stream_set_b.set_segment_by_index(45, stream_set_b.curBitsIndex, 8) stream_set_b.set_segment_by_index(24, stream_set_b.curBitsIndex, 8) stream_set_b.set_segment_by_index(251, stream_set_b.curBitsIndex, 8) stream_set_b.set_segment_by_index(77, stream_set_b.curBitsIndex, 13) stream_set_b.set_segment_by_index(131, stream_set_b.curBitsIndex, 8) stream_set_b.set_segment_by_index(3, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(0, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(1, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(32767, stream_set_b.curBitsIndex, 16) stream_set_b.set_segment_by_index(1, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(1, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(3, stream_set_b.curBitsIndex, 2) stream_set_b.set_segment_by_index(1, stream_set_b.curBitsIndex, 4) stream_set_b.set_segment_by_index(65535, stream_set_b.curBitsIndex, 16) self.assertEqual(stream_set_b.hexStream.upper(), raw_hex2) # test raw_hex3 = '7EC92718003B00781726AADAEE5D9799E0DA1B1F05F1090228F9D85069D21808255D80000000000000000000000000000000812AED55FFFFFFFFFFFC0562168000000000320B4204A8DC28FFFFFFFFFFFFFC00000000000000014060FD2D1DFA03A81817209FD02705F004113C98000765F102E413FA9B42FC660008AB81800000000000000000055000089C45555180000000000000000000000000007F' stream_fast_get_b = BytesStream(raw_hex3, endian=0) self.assertEqual( 126, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 201, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 156, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 10)) self.assertEqual( 1, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 1, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 1)) self.assertEqual( 483343, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( 48551259, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( 1573630707, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( 1008419683, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( -8002, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 16, sign=1)) self.assertEqual( 1, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 3)) # p9 self.assertEqual( 9, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 69, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 13)) self.assertEqual( 7995, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 16)) self.assertEqual( 168639043, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) # p1 self.assertEqual( 1, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 149, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 13)) self.assertEqual( int(0x76000000000000000000000000000000), stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 149 - 21)) # p2 self.assertEqual( 2, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 149, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 13)) self.assertEqual( int(0x76aafffffffffffe02b10b4000000000), stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 149 - 21)) # p25 # self.assertEqual(25, stream_fast_get_b.fast_get_segment_by_index(stream_fast_get_b.curBitsIndex, 8)) # self.assertEqual(180, stream_fast_get_b.fast_get_segment_by_index(stream_fast_get_b.curBitsIndex, 13)) # self.assertEqual(0, stream_fast_get_b.fast_get_segment_by_index(stream_fast_get_b.curBitsIndex, 1)) # self.assertEqual(0, stream_fast_get_b.fast_get_segment_by_index(stream_fast_get_b.curBitsIndex, 1)) # self.assertEqual(1, stream_fast_get_b.fast_get_segment_by_index(stream_fast_get_b.curBitsIndex, 1)) # self.assertEqual(39087636, stream_fast_get_b.fast_get_segment_by_index(stream_fast_get_b.curBitsIndex, 32)) # self.assertEqual(1, stream_fast_get_b.fast_get_segment_by_index(stream_fast_get_b.curBitsIndex, 2)) self.assertEqual( 25, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 8)) self.assertEqual( 180, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 13)) self.assertEqual( 0, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 1)) self.assertEqual( 0, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 1)) self.assertEqual( 1, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 1)) self.assertEqual( 39087636, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 32)) self.assertEqual( 1, stream_fast_get_b.get_segment_by_index( stream_fast_get_b.curBitsIndex, 2)) def test_msgZeroHeadSetCase(self): item = BytesStream('') item.set_segment_by_index(100823, item.curBitsIndex, 24) if False: item.set_segment_by_index(13, item.curBitsIndex, 9) # 按照CTCS3+ATO中C13包 else: item.set_segment_by_index(0, item.curBitsIndex, 9) item.set_segment_by_index(0, item.curBitsIndex, 2) item.set_segment_by_index(0, item.curBitsIndex, 2) item.set_segment_by_index(0, item.curBitsIndex, 24) item.set_segment_by_index(0, item.curBitsIndex, 15) item.set_segment_by_index(1, item.curBitsIndex, 4) self.assertEqual("0189d700000000000001", item.hexStream) item = BytesStream('') item.fast_set_segment_by_index(100823, item.curBitsIndex, 24) if False: item.fast_set_segment_by_index(13, item.curBitsIndex, 9) # 按照CTCS3+ATO中C13包 else: item.fast_set_segment_by_index(0, item.curBitsIndex, 9) item.fast_set_segment_by_index(0, item.curBitsIndex, 2) item.fast_set_segment_by_index(0, item.curBitsIndex, 2) item.fast_set_segment_by_index(0, item.curBitsIndex, 24) item.fast_set_segment_by_index(0, item.curBitsIndex, 15) item.fast_set_segment_by_index(1, item.curBitsIndex, 4)