def network_management(msg): '节点状态' global device_status node_id = getNodeID(msg) if str(node_id) not in device_status: # 新建一个设备缓存 device_status[str(node_id)] = { "frame": [], 'frame_status': 0, "can_status": 0, "work_status": 0, "put_status": 0 } jsonobject = { 'stationid': '{:0>12d}'.format(node_id), 'comment': '', 'status': 'off', 'errorcode': '00000', } HttpHandle.devicePost(jsonobject) # 上传新建的设备状态 device_status[str(node_id)]['work_status'] = DEVICE_STATUS_CODE[msg.dlc] device_status[str(node_id)]['can_status'] = 2 CANSendQueue.put( UsrCAN.Message(arbitration_id=msg.arbitration_id, extended_id=USE_EXTENDED_FRAME, is_remote_frame=True, dlc=0))
def syncTime(msg): '同步时间' time_stamp_struct = list(time.localtime()) # 获取本地时间 time_stamp_struct[0] -= 2000 # 将2019转换成19 time_stamp_struct[6] = 0 # 星期转成0 time_stamp_struct[7] = ( time_stamp_struct[0] + time_stamp_struct[1] + time_stamp_struct[2] + time_stamp_struct[3] + time_stamp_struct[4] + time_stamp_struct[5] + time_stamp_struct[6] + time_stamp_struct[7]) & 0xff # 和校验 time_stamp_struct.pop() CANSendQueue.put( UsrCAN.Message(arbitration_id=msg.arbitration_id, extended_id=USE_EXTENDED_FRAME, data=time_stamp_struct))
def deviceStart(node_id, cmd): '上位机命令' global device_status print(device_status) if cmd == 'open_device': id_cmd = node_id | FUN_CODE_DICT['open_device'] << FUN_CODE_BIT elif cmd == 'close_device': id_cmd = node_id | FUN_CODE_DICT['close_device'] << FUN_CODE_BIT elif cmd == 'test_device': id_cmd = node_id | FUN_CODE_DICT['test_device'] << FUN_CODE_BIT elif cmd == 'train_device': id_cmd = node_id | FUN_CODE_DICT['train_device'] << FUN_CODE_BIT CANSendQueue.put( UsrCAN.Message(arbitration_id=id_cmd, extended_id=USE_EXTENDED_FRAME, is_remote_frame=True))
def CANRecv(): 'CAN接收' print(threading.current_thread().name, 'CANRecv is running...') while exit_flag != True: try: databyte = socket_conn.recv(16) if databyte != None: msg = UsrCAN.Message(arbitration_id=struct.unpack( 'i', databyte[4:0:-1]), is_remote_frame=databyte[0] & 0x40, extended_id=databyte[0] & 0x80, dlc=databyte[0] & 0x0f, data=databyte[5:13]) CAN_Analysis.CANRecvQueue.put(msg) except TimeoutError: print('disconnect')
def promiseRequest(msg): '处理数据包发送请求' global data_Receiving # 如果是0表示非接收态,否则等于正在发送的节点ID global dog_count global device_status node_id = getNodeID(msg) if str(node_id) in device_status: #设备已运行然后接收到数据 if data_Receiving != 0: dataRequestQueue.put(msg) #接收任务阻塞中,暂存队列 # print(msg,'received but can not handle') elif time.time() - msg.timestamp < 2 and device_status[str( node_id)]['can_status'] > 0: # 等待时间不超过2秒,并且CAN设备在线 CANSendQueue.put( UsrCAN.Message(arbitration_id=msg.arbitration_id, extended_id=USE_EXTENDED_FRAME, is_remote_frame=True)) data_Receiving = getNodeID(msg) #开始接收 dog_count = 2 # print(msg.arbitration_id,' start handle') else: print("Can't handle it", node_id, data_Receiving) #超时不接收