def publish_signals(stub): source = common_pb2.ClientId(id="app_identifier") namespace = common_pb2.NameSpace(name="VirtualCanInterface") signal = common_pb2.SignalId(name="BenchC_c_5", namespace=namespace) signal_with_payload = network_api_pb2.Signal(id=signal) signal_with_payload.integer = 4 signal2 = common_pb2.SignalId(name="BenchC_c_2", namespace=namespace) signal_with_payload_2 = network_api_pb2.Signal(id=signal2) signal_with_payload_2.double = 3.4 signal3 = common_pb2.SignalId(name="BenchC_d_2", namespace=namespace) signal_with_payload_3 = network_api_pb2.Signal(id=signal3) signal_with_payload_3.arbitration = True publisher_info = network_api_pb2.PublisherConfig( clientId=source, signals=network_api_pb2.Signals( signal=[signal_with_payload, signal_with_payload_2]), frequency=0) try: stub.PublishSignals(publisher_info) time.sleep(1) except grpc._channel._Rendezvous as err: print(err)
def ecu_A(stub, pause): while True: global increasing_counter namespace = "ecu_A" clientId = common_pb2.ClientId(id="id_ecu_A") # counter counter = common_pb2.SignalId(name="counter", namespace=common_pb2.NameSpace(name = namespace)) counter_with_payload = network_api_pb2.Signal(id = counter, integer = increasing_counter) # TestFr06_Child02 - another signal testFr06_Child02 = common_pb2.SignalId(name="TestFr06_Child02", namespace=common_pb2.NameSpace(name = namespace)) testFr06_Child02_with_payload = network_api_pb2.Signal(id = testFr06_Child02, integer = increasing_counter+1) # TestFr04 - now a frame testFr04 = common_pb2.SignalId(name="TestFr04", namespace=common_pb2.NameSpace(name = namespace)) testFr04_with_payload = network_api_pb2.Signal(id = testFr04, integer = increasing_counter+2) print("\necu_A, seed is ", increasing_counter) publish_signals(clientId, stub, [counter_with_payload, testFr06_Child02_with_payload, testFr04_with_payload]) time.sleep(pause) # read the other value and output result counter_times_2 = common_pb2.SignalId(name="counter_times_2", namespace=common_pb2.NameSpace(name = namespace)) read_counter_times_2 = read_signal(stub, counter_times_2) print("ecu_A, (result) counter_times_2 is ", read_counter_times_2.signal[0].integer) increasing_counter = (increasing_counter + 1) % 10
def read_diagnostics_odb(stub): source = common_pb2.ClientId(id="app_identifier") namespace = common_pb2.NameSpace(name = "PropulsionCANhs") upLink = common_pb2.SignalId(name="EtcToAllCarbPropDiagReqFrame", namespace=namespace) downLink = common_pb2.SignalId(name="EcmToEtcCarbPropDiagResFrame", namespace=namespace) request = diagnostics_api_pb2.DiagnosticsRequest(upLink = upLink, downLink = downLink, serviceId = b'\x01', dataIdentifier = b'\x42') try: response = stub.SendDiagnosticsQuery(request) print(response) print(binascii.hexlify(response.raw)) except grpc._channel._Rendezvous as err: print(err)
def ecu_B_subscribe_2(stub): """Shows possibility to subscribe to same signal multiple times, also using array to subscribe to additional signals. Logs on purpose tabbed with single space. Parameters ---------- stub : NetworkServiceStub Object instance of class """ namespace = "ecu_B" client_id = common_pb2.ClientId(id="id_ecu_B") # Signal value 'counter' counter = common_pb2.SignalId( name="counter", namespace=common_pb2.NameSpace(name=namespace) ) # Signal value 'TestFr06_Child02' testFr06_Child02 = common_pb2.SignalId( name="TestFr06_Child02", namespace=common_pb2.NameSpace(name=namespace) ) # Frame value 'TestFr04' testFr04 = common_pb2.SignalId( name="TestFr04", namespace=common_pb2.NameSpace(name=namespace) ) # Subscribes to 'counter', 'TestFr06_Child02' and 'TestFr04' sub_info = network_api_pb2.SubscriberConfig( clientId=client_id, signals=network_api_pb2.SignalIds( signalId=[counter, testFr06_Child02, testFr04] ), onChange=True, ) try: for response in stub.SubscribeToSignals(sub_info): # Since we subscribed to a set of signals we need to check which arrived. for signal in response.signal: if signal.id.name == "counter": print(" ecu_B, (subscribe_2) counter is ", signal.integer) if signal.id.name == "TestFr06_Child02": print( " ecu_B signal: " + signal.id.name + " arrived: " + str(signal.integer) ) if signal.id.name == "TestFr04": print(" ecu_B, (subscribe_2) raw is ", binascii.hexlify(signal.raw)) except grpc._channel._Rendezvous as err: print(err)
def subscribe_to_signal(stub): source = common_pb2.ClientId(id="app_identifier") signals = [ common_pb2.SignalId(name="SteerWhlAgSafe", namespace=common_pb2.NameSpace(name = "ChassisCANhs")), common_pb2.SignalId(name="DrvrDesDir", namespace=common_pb2.NameSpace(name = "ChassisCANhs")), common_pb2.SignalId(name="RecOfImpctSafeCntr", namespace=common_pb2.NameSpace(name = "BodyCANhs")), ] sub_info = network_api_pb2.SubscriberConfig(clientId=source, signals=network_api_pb2.SignalIds(signalId=signals), onChange=False) try: for response in stub.SubscribeToSignals(sub_info): print(response) except grpc._channel._Rendezvous as err: print(err)
def read_diagnostics_vin(stub): source = common_pb2.ClientId(id="app_identifier") namespace = common_pb2.NameSpace(name = "ChassisCANhs") upLink = common_pb2.SignalId(name="VddmToAllFuncChasDiagReqFrame", namespace=namespace) downLink = common_pb2.SignalId(name="PscmToVddmChasDiagResFrame", namespace=namespace) request = diagnostics_api_pb2.DiagnosticsRequest(upLink = upLink, downLink = downLink, serviceId = b'\x22', dataIdentifier = b'\xF1\x90') try: response = stub.SendDiagnosticsQuery(request) print(response) print(binascii.hexlify(response.raw)) except grpc._channel._Rendezvous as err: print(err)
def read_diagnostics_odb(stub): source = common_pb2.ClientId(id="app_identifier") namespace = common_pb2.NameSpace(name = "DiagnosticsCanInterface") upLink = common_pb2.SignalId(name="DiagReqBroadCastFrame_2015", namespace=namespace) # if you dont see any response try the other resp frames defined in the diagnostics dbc file. downLink = common_pb2.SignalId(name="DiagResFrame_2024", namespace=namespace) # service 01 pid 12 - engine rpm request = diagnostics_api_pb2.DiagnosticsRequest(upLink = upLink, downLink = downLink, serviceId = b'\x01', dataIdentifier = b'\x12') try: response = stub.SendDiagnosticsQuery(request) print(response) print(binascii.hexlify(response.raw)) except grpc._channel._Rendezvous as err: print(err)
def ecu_B_subscribe(stub): namespace = "ecu_B" client_id = common_pb2.ClientId(id="id_ecu_B") counter = common_pb2.SignalId(name="counter", namespace=common_pb2.NameSpace(name = namespace)) sub_info = network_api_pb2.SubscriberConfig(clientId=client_id, signals=network_api_pb2.SignalIds(signalId=[counter]), onChange=False) try: for subs_counter in stub.SubscribeToSignals(sub_info): print("ecu_B, (subscribe) counter is ", subs_counter.signal[0].integer) counter_times_2 = common_pb2.SignalId(name="counter_times_2", namespace=common_pb2.NameSpace(name = namespace)) signal_with_payload = network_api_pb2.Signal(id = counter_times_2, integer = subs_counter.signal[0].integer * 2) publish_signals(client_id, stub, [signal_with_payload]) except grpc._channel._Rendezvous as err: print(err)
def ecu_B_subscribe_(stub): """Subscribe to a value published by ecu_A and output value Parameters ---------- stub : NetworkServiceStub Object instance of class """ namespace = "custom_can" client_id = common_pb2.ClientId(id="id_ecu_B") # Subscribe to value 'SteerAngle' steer_angle = common_pb2.SignalId( name="SteerAngle", namespace=common_pb2.NameSpace(name=namespace)) sub_info = network_api_pb2.SubscriberConfig( clientId=client_id, signals=network_api_pb2.SignalIds(signalId=[steer_angle]), onChange=True, ) # Output subscribed signal try: for subs_counter in stub.SubscribeToSignals(sub_info): # For clean exit when stopping script if exit_event.is_set(): break print("ecu_B, (subscribe) SteerAngle is ", subs_counter.signal[0]) except grpc._channel._Rendezvous as err: print(err)
def run(): channel = grpc.insecure_channel('127.0.0.1:50051') network_stub = network_api_pb2_grpc.NetworkServiceStub(channel) system_stub = system_api_pb2_grpc.SystemServiceStub(channel) check_license(system_stub) # request_license(system_stub) # download_and_install_license(system_stub, "your_emailed_hash_without_quotes") upload_folder(system_stub, "configuration_udp") # upload_folder(system_stub, "configuration") reload_configuration(system_stub) # list available signals configuration = system_stub.GetConfiguration(common_pb2.Empty()) for networkInfo in configuration.networkInfo: print("signals in namespace ", networkInfo.namespace.name, system_stub.ListSignals(networkInfo.namespace)) ecu_A_thread = Thread(target = ecu_A, args = (network_stub, 1,)) ecu_A_thread.start() ecu_B_thread_read = Thread(target = ecu_B_read, args = (network_stub, 1,)) ecu_B_thread_read.start() ecu_B_thread_subscribe = Thread(target = ecu_B_subscribe, args = (network_stub,)) ecu_B_thread_subscribe.start() ecu_B_thread_subscribe_2 = Thread(target = ecu_B_subscribe_2, args = (network_stub,)) ecu_B_thread_subscribe_2.start() read_signals = [common_pb2.SignalId(name="counter", namespace=common_pb2.NameSpace(name = "ecu_A")), common_pb2.SignalId(name="TestFr06_Child02", namespace=common_pb2.NameSpace(name = "ecu_A"))] ecu_read_demo = Thread(target = read_on_timer, args = (network_stub, read_signals, 10)) ecu_read_demo.start()
def read_diagnostics_engine_speed(stub): while True: source = common_pb2.ClientId(id="app_identifier") namespace = common_pb2.NameSpace(name = "DiagnosticsCanInterface") upLink = common_pb2.SignalId(name="DiagReqBroadCastFrame_2015", namespace=namespace) downLink = common_pb2.SignalId(name="DiagResFrame_2024", namespace=namespace) request = diagnostics_api_pb2.DiagnosticsRequest(upLink = upLink, downLink = downLink, serviceId = b'\x01', dataIdentifier = b'\x0C') try: response = stub.SendDiagnosticsQuery(request) print(response) # print(int.from_bytes(response.raw)) python 3.2 print(int(codecs.encode(response.raw, 'hex'), 16)) print(binascii.hexlify(response.raw)) except grpc._channel._Rendezvous as err: print(err)
def ecu_B_read(stub, pause): while True: namespace = "ecu_B" client_id = common_pb2.ClientId(id="id_ecu_B") counter = common_pb2.SignalId(name="counter", namespace=common_pb2.NameSpace(name = namespace)) read_counter = read_signal(stub, counter) print("ecu_B, (read) counter is ", read_counter.signal[0].integer) time.sleep(pause)
def subscribe_to_arbitration(stub): source = common_pb2.ClientId(id="app_identifier") namespace = common_pb2.NameSpace(name = "VirtualCanInterface") signal = common_pb2.SignalId(name="BenchC_c_5", namespace=namespace) sub_info = network_api_pb2.SubscriberConfig(clientId=source, signals=network_api_pb2.SignalIds(signalId=[signal]), onChange=False) try: for response in stub.SubscribeToSignals(sub_info): print(response) except grpc._channel._Rendezvous as err: print(err)
def run(): channel = grpc.insecure_channel('192.168.1.184:50051') network_stub = network_api_pb2_grpc.NetworkServiceStub(channel) system_stub = system_api_pb2_grpc.SystemServiceStub(channel) client_id = common_pb2.ClientId(id="app_identifier") # If you need to change the id of the req/resp modify here configuration/can/diagnostics.dbc diag_frame_req = common_pb2.SignalId( name="DiagReqFrame_2016", namespace=common_pb2.NameSpace(name="DiagnosticsCanInterface")) diag_frame_resp = signal = common_pb2.SignalId( name="DiagResFrame_2024", namespace=common_pb2.NameSpace(name="DiagnosticsCanInterface")) upload_folder(system_stub, "configuration") reload_configuration(system_stub) print( "-------------- Subscribe to diag_req, on request submit resp_frame --------------" ) subscribe_to_diag(client_id, network_stub, diag_frame_req, diag_frame_resp)
def ecu_B_subscribe_2(stub): namespace = "ecu_B" # specify some signals client_id = common_pb2.ClientId(id="id_ecu_B") counter = common_pb2.SignalId(name="counter", namespace=common_pb2.NameSpace(name = namespace)) testFr06_Child02 = common_pb2.SignalId(name="TestFr06_Child02", namespace=common_pb2.NameSpace(name = namespace)) testFr04 = common_pb2.SignalId(name="TestFr04", namespace=common_pb2.NameSpace(name = namespace)) sub_info = network_api_pb2.SubscriberConfig(clientId=client_id, signals=network_api_pb2.SignalIds(signalId=[counter, testFr06_Child02, testFr04]), onChange=False) try: for response in stub.SubscribeToSignals(sub_info): # since we subscribe to a set of signal we need to check which arrived. for signal in response.signal: if (signal.id.name == "counter"): print(" ecu_B, (subscribe_2) counter is ", signal.integer) if (signal.id.name == "TestFr06_Child02"): print(" ecu_B signal: " + signal.id.name + " arrived: " + str(signal.integer)) if (signal.id.name == "TestFr04"): print(" ecu_B, (subscribe_2) raw is ", binascii.hexlify(signal.raw)) except grpc._channel._Rendezvous as err: print(err)
def run(argv): # This script will use below ip-address if no argument is passed to the script ip = "127.0.0.1" # Keep this port port = ":50051" try: opts, args = getopt.getopt(argv, "h", ["ip="]) except getopt.GetoptError: print("Usage: resp_to_diag_req.py --ip <ip_address>") sys.exit(2) for opt, arg in opts: if opt == "-h": print("Usage: resp_to_diag_req.py --ip <ip_address>") sys.exit(2) elif opt == "--ip": ip = arg channel = grpc.insecure_channel(ip + port) network_stub = network_api_pb2_grpc.NetworkServiceStub(channel) system_stub = system_api_pb2_grpc.SystemServiceStub(channel) client_id = common_pb2.ClientId(id="app_identifier") # If you need to change the id of the req/resp modify here configuration/can/diagnostics.dbc diag_frame_req = common_pb2.SignalId( name="DiagReqFrame_2016", namespace=common_pb2.NameSpace(name="DiagnosticsCanInterface"), ) diag_frame_resp = signal = common_pb2.SignalId( name="DiagResFrame_2024", namespace=common_pb2.NameSpace(name="DiagnosticsCanInterface"), ) upload_folder(system_stub, "configuration") reload_configuration(system_stub) print( "-------------- Subscribe to diag_req, on request submit resp_frame --------------" ) subscribe_to_diag(client_id, network_stub, diag_frame_req, diag_frame_resp)
def ecu_B_subscribe(stub): """Subscribe to a value published by ecu_A and publish doubled value back to ecu_A Parameters ---------- stub : NetworkServiceStub Object instance of class """ namespace = "ecu_B" client_id = common_pb2.ClientId(id="id_ecu_B") # Subscribe to value 'counter' counter = common_pb2.SignalId( name="counter", namespace=common_pb2.NameSpace(name=namespace) ) sub_info = network_api_pb2.SubscriberConfig( clientId=client_id, signals=network_api_pb2.SignalIds(signalId=[counter]), onChange=True, ) # Publish doubled value as 'counter_times_2' try: for subs_counter in stub.SubscribeToSignals(sub_info): print("ecu_B, (subscribe) counter is ", subs_counter.signal[0].integer) counter_times_2 = common_pb2.SignalId( name="counter_times_2", namespace=common_pb2.NameSpace(name=namespace) ) signal_with_payload = network_api_pb2.Signal( id=counter_times_2, integer=subs_counter.signal[0].integer * 2 ) publish_signals(client_id, stub, [signal_with_payload]) except grpc._channel._Rendezvous as err: print(err)
def ecu_A(stub, pause): """Publishes a value, read other value (published by ecu_B) Parameters ---------- stub : NetworkServiceStub Object instance of class pause : int Amount of time to pause, in seconds """ while True: global increasing_counter namespace = "ecu_A" clientId = common_pb2.ClientId(id="id_ecu_A") # Publishes value 'counter' counter = common_pb2.SignalId( name="counter", namespace=common_pb2.NameSpace(name=namespace)) counter_with_payload = network_api_pb2.Signal( id=counter, integer=increasing_counter) publish_signals(clientId, stub, [counter_with_payload]) print("\necu_A, seed is ", increasing_counter) time.sleep(pause) # Read the other value 'counter_times_2' and output result counter_times_2 = common_pb2.SignalId( name="counter_times_2", namespace=common_pb2.NameSpace(name=namespace)) read_counter_times_2 = read_signal(stub, counter_times_2) print( "ecu_A, (result) counter_times_2 is ", read_counter_times_2.signal[0].integer, ) increasing_counter = (increasing_counter + 1) % 4
def ecu_B_read(stub, pause): """Read a value published by ecu_A Parameters ---------- stub : NetworkServiceStub Object instance of class pause : int Amount of time to pause, in seconds """ while True: namespace = "ecu_B" client_id = common_pb2.ClientId(id="id_ecu_B") # Read value 'counter' counter = common_pb2.SignalId( name="counter", namespace=common_pb2.NameSpace(name=namespace)) read_counter = read_signal(stub, counter) print("ecu_B, (read) counter is ", read_counter.signal[0].integer) time.sleep(pause)
def ecu_B_read(stub, pause): """Read some value published by ecu_A Parameters ---------- stub : NetworkServiceStub Object instance of class pause : int Amount of time to pause, in seconds """ while not exit_event.is_set(): namespace = "custom_can" client_id = common_pb2.ClientId(id="id_ecu_B") # Read value 'SteerAngle' steer_angle = common_pb2.SignalId( name="SteerAngle", namespace=common_pb2.NameSpace(name=namespace)) response = read_signal(stub, steer_angle) print("ecu_B, (read) SteerAngle is ", response.signal[0].double) time.sleep(pause)
import network_api_pb2_grpc import common_pb2 __author__ = "Aleksandar Filipov and Alvaro Alonso" __copyright__ = "Copyright 2019, Volvo Cars Group" __version__ = "0.0.1" __maintainer__ = "Alvaro Alonso" __email__ = "*****@*****.**" __status__ = "Development" if __name__ == '__main__': # Create a channel channel = grpc.insecure_channel('localhost:50051') # Create the stub network_stub = network_api_pb2_grpc.NetworkServiceStub(channel) # Create a signal namespace = common_pb2.NameSpace(name="VirtualInterface") signal = common_pb2.SignalId(name="virtual_signal", namespace=namespace) # Create a subscriber config client_id = common_pb2.ClientId(id="virtual_example_sub") signals = network_api_pb2.SignalIds(signalId=[signal]) sub_info = network_api_pb2.SubscriberConfig(clientId=client_id, signals=signals, onChange=False) # Subscribe try: for response in network_stub.SubscribeToSignals(sub_info): print(response) except grpc._channel._Rendezvous as err: print(err)
check_license(system_stub) upload_folder(system_stub, "configuration") reload_configuration(system_stub) # create the identifier of *this* client client_id = common_pb2.ClientId(id="virtual_example_pub") # For 10 messages for _ in range(10): input_value = input("Enter a number: ") try: signal_value = int(input_value) except ValueError: print(f"{input_value} is not a number. Only numbers are allowed") else: # Create a signal namespace = common_pb2.NameSpace(name="VirtualInterface") signal = common_pb2.SignalId(name="my_madeup_virtual_signal", namespace=namespace) # Add payload to the signal signal_with_payload = network_api_pb2.Signal(id=signal) # 20 bytes chosen as an arbitrary number signal_with_payload.raw = signal_value.to_bytes(20, 'big') # long vectors are valid on virtual networks! # signal_with_payload.integer = signal_value # Create a publisher config signals = network_api_pb2.Signals(signal=(signal_with_payload, )) publisher_info = network_api_pb2.PublisherConfig( clientId=client_id, signals=signals, frequency=0) # Publish try: network_stub.PublishSignals(publisher_info) except grpc._channel._Rendezvous as err: print(err)
if __name__ == '__main__': # Create a channel channel = grpc.insecure_channel('localhost:50051') # Create the stub network_stub = network_api_pb2_grpc.NetworkServiceStub(channel) # For 10 messages for _ in range(10): input_value = input("Enter a number: ") try: signal_value = int(input_value) except ValueError: print(f"{input_value} is not a number. Only numbers are allowed") else: # Create a signal namespace = common_pb2.NameSpace(name="VirtualInterface") signal = common_pb2.SignalId(name='virtual_signal', namespace=namespace) # Add payload to the signal signal_with_payload = network_api_pb2.Signal(id=signal) signal_with_payload.integer = signal_value # Create a publisher config client_id = common_pb2.ClientId(id="virtual_example_pub") signals = network_api_pb2.Signals(signal=(signal_with_payload, )) publisher_info = network_api_pb2.PublisherConfig( clientId=client_id, signals=signals, frequency=0) # Publish try: network_stub.PublishSignals(publisher_info) except grpc._channel._Rendezvous as err: print(err)
def run(argv): """Main function, checking arguments passed to script, setting up stubs, configuration and starting Threads. Parameters ---------- argv : list Arguments passed when starting script """ # Checks argument passed to script, ecu.py will use below ip-address if no argument is passed to the script ip = "127.0.0.1" # Keep this port port = ":50051" try: opts, args = getopt.getopt(argv, "h", ["ip="]) except getopt.GetoptError: print("Usage: ecu.py --ip <ip_address>") sys.exit(2) for opt, arg in opts: if opt == "-h": print("Usage: ecu.py --ip <ip_address>") sys.exit(2) elif opt == "--ip": ip = arg # Setting up stubs and configuration channel = grpc.insecure_channel(ip + port) network_stub = network_api_pb2_grpc.NetworkServiceStub(channel) system_stub = system_api_pb2_grpc.SystemServiceStub(channel) # check_license(system_stub) upload_folder(system_stub, "configuration_lin") reload_configuration(system_stub) # grace period to let clients time to pull their configuration time.sleep(5) clientId = common_pb2.ClientId(id="this_python_app_identifier") # slave, prepare to answer to diag request, this gets loaded pening in the transiever (waiting for aribitration) namespace_slave = "ecu_B" signal = common_pb2.SignalId( name="SlaveResp", namespace=common_pb2.NameSpace(name=namespace_slave)) signal_with_payload = network_api_pb2.Signal( id=signal, raw=b"\x81\x09\x0a\x0b\x0c\x0d\x0e\x0f") publish_signals(network_stub, clientId, [signal_with_payload], 0) print("Slave is ready to reply to diag request %s" % (signal_with_payload)) # master, subscribe to answer from client (just listen) namespace_master = "ecu_A" frame = common_pb2.SignalId( name="SlaveResp", namespace=common_pb2.NameSpace(name=namespace_master)) # frame = common_pb2.SignalId(name="counter_times_2", namespace=common_pb2.NameSpace(name = namespace_master)) ecu_m_subscribe = Thread(target=ecu_master_subscribe, args=(network_stub, clientId, [frame], False)) ecu_m_subscribe.daemon = True # allow this thread to be automatically stopped on exit. ecu_m_subscribe.start() # Just listen from slave, for debug purposes # frame2 = common_pb2.SignalId(name="MasterReq", namespace=common_pb2.NameSpace(name = namespace_slave)) # ecu_s_subscribe = Thread(target = ecu_slave_subscribe, args = (network_stub, clientId, [frame, frame2], False)) # ecu_s_subscribe.start() # # send arbitration from master (not needed arbitration is enabled inte inderfaces.json), expect nothing back! (send arbitration) # # signal = common_pb2.SignalId(name="SlaveResp", namespace=common_pb2.NameSpace(name = namespace_master)) # signal_with_payload = network_api_pb2.Signal(id = signal, arbitration = True) # publish_signals(network_stub, clientId, [signal_with_payload], 0) # print("sent %s from master expect nothing back" % (signal_with_payload)) # time.sleep(3) # send master request, now with propriate NAD, DEVS2 has NAD 0x81 (which is the fist byte below) signal = common_pb2.SignalId( name="MasterReq", namespace=common_pb2.NameSpace(name=namespace_master)) signal_with_payload = network_api_pb2.Signal( id=signal, raw=b"\x81\x02\x03\x04\x05\x06\x07\x08") publish_signals(network_stub, clientId, [signal_with_payload], 0) print("sent %s from master expect ANSWER" % (signal_with_payload)) time.sleep(3) # # send arbitration from master, expect answer since nad is matching # signal = common_pb2.SignalId(name="SlaveResp", namespace=common_pb2.NameSpace(name = namespace_master)) # signal_with_payload = network_api_pb2.Signal(id = signal, arbitration = True) # publish_signals(network_stub, clientId, [signal_with_payload], 0) # print("sent %s from master expect ANSWER" % (signal_with_payload)) # time.sleep(3) # send master request, now with another NAD, make sure client doesn't (which is the fist byte below) signal = common_pb2.SignalId( name="MasterReq", namespace=common_pb2.NameSpace(name=namespace_master)) signal_with_payload = network_api_pb2.Signal( id=signal, raw=b"\x82\x02\x03\x04\x05\x06\x07\x08") publish_signals(network_stub, clientId, [signal_with_payload], 0) # send arbitration from master, expect nothing back (nad is not matching)! # signal = common_pb2.SignalId(name="SlaveResp", namespace=common_pb2.NameSpace(name = namespace_master)) # signal_with_payload = network_api_pb2.Signal(id = signal, arbitration = True) # publish_signals(network_stub, clientId, [signal_with_payload], 0) print("sent %s from master NOT expecting ANSWER" % (signal_with_payload)) time.sleep(3) print("still quiet.... done")
def run(argv): """Main function, checking arguments passed to script, setting up stubs, configuration and starting Threads. Parameters ---------- argv : list Arguments passed when starting script """ # Checks argument passed to script, ecu_advanced.py will use below ip-address if no argument is passed to the script ip = "127.0.0.1" # Keep this port port = ":50051" try: opts, args = getopt.getopt(argv, "h", ["ip="]) except getopt.GetoptError: print("Usage: ecu_advanced.py --ip <ip_address>") sys.exit(2) for opt, arg in opts: if opt == "-h": print("Usage: ecu_advanced.py --ip <ip_address>") sys.exit(2) elif opt == "--ip": ip = arg # Setting up stubs and configuration # allows to custom set message max size. (default 4Mb, 4194304) # MAX_MESSAGE_LENGTH = 6000000 # channel = grpc.insecure_channel('127.0.0.1:50051', options=[ # ('grpc.max_send_message_length', MAX_MESSAGE_LENGTH), # ('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH), # ], # ) channel = grpc.insecure_channel(ip + port) network_stub = network_api_pb2_grpc.NetworkServiceStub(channel) system_stub = system_api_pb2_grpc.SystemServiceStub(channel) check_license(system_stub) # request_license(system_stub) # download_and_install_license(system_stub, "your_emailed_hash") upload_folder(system_stub, "configuration_udp") # upload_folder(system_stub, "configuration_lin") # upload_folder(system_stub, "configuration_can") reload_configuration(system_stub) # Lists available signals configuration = system_stub.GetConfiguration(common_pb2.Empty()) for networkInfo in configuration.networkInfo: print( "signals in namespace ", networkInfo.namespace.name, system_stub.ListSignals(networkInfo.namespace), ) # Starting Threads ecu_A_thread = Thread( target=ecu_A, args=( network_stub, 1, ), ) ecu_A_thread.start() ecu_B_thread_read = Thread( target=ecu_B_read, args=( network_stub, 1, ), ) ecu_B_thread_read.start() ecu_B_thread_subscribe = Thread(target=ecu_B_subscribe, args=(network_stub,)) ecu_B_thread_subscribe.start() ecu_B_thread_subscribe_2 = Thread(target=ecu_B_subscribe_2, args=(network_stub,)) ecu_B_thread_subscribe_2.start() read_signals = [ common_pb2.SignalId( name="counter", namespace=common_pb2.NameSpace(name="ecu_A") ), common_pb2.SignalId( name="TestFr06_Child02", namespace=common_pb2.NameSpace(name="ecu_A") ), ] ecu_read_on_timer = Thread( target=read_on_timer, args=(network_stub, read_signals, 10) ) ecu_read_on_timer.start()
def signal(self, name, namespace_name): k = (namespace_name, name) if (k not in self._sinfos) and (namespace_name not in self._virtual): raise Exception(f"signal not declared (namespace, signal): {k}") return common_pb2.SignalId( name=name, namespace=common_pb2.NameSpace(name=namespace_name))