def send_message(adu, serial_port): """ Send ADU over serial to to server and return parsed response. :param adu: Request ADU. :param sock: Serial port instance. :return: Parsed response from server. """ serial_port.write(adu) serial_port.flush() # Check exception ADU (which is shorter than all other responses) first. exception_adu_size = 19 response_error_adu = recv_exactly(serial_port.read, exception_adu_size) aes = AES.new(key, AES.MODE_CBC, IV) response_error_pdu = aes.decrypt(response_error_adu[1:-2]) aes = AES.new(key, AES.MODE_CBC, IV) req_pdu = aes.decrypt(adu[1:-2]) raise_for_exception_adu(response_error_pdu) expected_response_size = expected_response_pdu_size_from_request_pdu( req_pdu) + 3 response_remainder = recv_exactly( serial_port.read, expected_response_size - exception_adu_size) return parse_response_adu(response_error_adu + response_remainder, adu)
def send_message(adu, sock): """ Send ADU over socket to to server and return parsed response. :param adu: Request ADU. :param sock: Socket instance. :return: Parsed response from server. """ sock.sendall(adu) # Check exception ADU (which is shorter than all other responses) first. exception_adu_size = 9 response_error_adu = recv_exactly(sock.recv, exception_adu_size) raise_for_exception_adu(response_error_adu) expected_response_size = \ expected_response_pdu_size_from_request_pdu(adu[7:]) + 7 response_remainder = recv_exactly( sock.recv, expected_response_size - exception_adu_size) return parse_response_adu(response_error_adu + response_remainder, adu)
def send_message(adu, serial_port, flush_on_write=False): """ Send ADU over serial to to server and return parsed response. :param adu: Request ADU. :param sock: Serial port instance. :return: Parsed response from server. """ serial_port.write(adu) time.sleep(0.05) #Delay for slow devices if flush_on_write: serial_port.flush() # Check exception ADU (which is shorter than all other responses) first. exception_adu_size = 5 response_error_adu = recv_exactly(serial_port.read, exception_adu_size) raise_for_exception_adu(response_error_adu) expected_response_size = \ expected_response_pdu_size_from_request_pdu(adu[1:-2]) + 3 response_remainder = recv_exactly( serial_port.read, expected_response_size - exception_adu_size) return parse_response_adu(response_error_adu + response_remainder, adu)