def __init__(self, arg=None): self.client = apx.Client() if arg is not None: if isinstance(arg, str): self.client.create_node(arg) elif isinstance(arg, apx.Node): self.client.attach_node(arg)
def test_create_provide_ports(self): apx_text = """APX/1.2 N"TestNode" T"VehicleSpeed_T"S T"EngineSpeed_T"S P"VehicleSpeed"T[0] P"EngineSpeed"T[1] """ client = apx.Client() client.create_node(apx_text) client.write(0, 0x1234) self.assertEqual(client.nodeData.outPortDataFile.read(0,2), bytes([0x34, 0x12])) self.assertEqual(client.nodeData.outPortDataFile.read(2,2), bytes([0, 0])) client.write('EngineSpeed', 0xFFFF) self.assertEqual(client.nodeData.outPortDataFile.read(0,2), bytes([0x34, 0x12])) self.assertEqual(client.nodeData.outPortDataFile.read(2,2), bytes([0xFF, 0xFF]))
def test_create_require_ports(self): apx_text = """APX/1.2 N"TestNode" T"VehicleSpeed_T"S T"EngineSpeed_T"S T"InactiveActive_T"C(0,3):VT("InactiveActive_Inactive","InactiveActive_Active","InactiveActive_Error","InactiveActive_NotAvailable") R"VehicleSpeed"T[0]:=0xFFFF R"EngineSpeed"T[1]:=0xFFFF R"ParkBrakeState"T["InactiveActive_T"]:=3 """ client = apx.Client() client.create_node(apx_text) ParkBrakeState_port = client.find('ParkBrakeState') self.assertEqual(client.read(0), 0xFFFF) self.assertEqual(client.read(ParkBrakeState_port), 3) client.nodeData.inPortDataFile.write(0, bytes([0x34,0x12])) self.assertEqual(client.read(0), 0x1234)
@apx.DataListener.register class MyDataListener(apx.DataListener): def on_data(self, port, data): global value global client print("%s: %s" % (port.name, str(data))) if port.name == 'TestSignal2' and value is not None: value = (value + 1) & 0xFFFF #wraparound to zero after 65535 client.write_port('TestSignal1', value) if __name__ == '__main__': node = apx.Node('TestNode1') node.append(apx.ProvidePort('TestSignal1', 'S')) node.append(apx.RequirePort('TestSignal2', 'S')) value = 1 with apx.Client(node) as client: client.set_listener(MyDataListener()) client.write_port('TestSignal1', value) if client.connect_tcp('localhost', 5000): while True: try: time.sleep(10) value = None break except (KeyboardInterrupt, SystemExit): value = None break
ref_time = time.time() @apx.DataListener.register class MyDataListener(apx.DataListener): global ref_time def on_data(self, port, data): delta_time = time.time() - ref_time print("%.3f: %s=%s" % (delta_time, port.name, str(data))) if __name__ == '__main__': apx_text = """APX/1.2 N"Listener" T"VehicleSpeed_T"S T"EngineSpeed_T"S R"VehicleSpeed"T["VehicleSpeed_T"]:=65535 R"EngineSpeed"T["EngineSpeed_T"]:=65535 """ with apx.Client(apx_text) as client: client.set_listener(MyDataListener()) if client.connect_tcp('localhost', 5000): while True: try: time.sleep(1) except (KeyboardInterrupt, SystemExit): break