From 19f92722bf4f3a4637ff1aa41b7445511e61a4d1 Mon Sep 17 00:00:00 2001 From: Ajay Jashnani Date: Tue, 27 Jun 2017 14:28:41 -0500 Subject: [PATCH 1/5] Adding Output to the frame stream input example --- nixnet/constants.py | 1 + nixnet/nx.py | 26 ++++--- nixnet/types.py | 2 +- nixnet_examples/can_frame_stream_input.py | 82 ++++++++++++++++++----- 4 files changed, 80 insertions(+), 31 deletions(-) diff --git a/nixnet/constants.py b/nixnet/constants.py index 92bfadec..679e0e65 100644 --- a/nixnet/constants.py +++ b/nixnet/constants.py @@ -10,4 +10,5 @@ TIMEOUT_NONE = _cconsts.NX_TIMEOUT_NONE TIMEOUT_INFINITE = _cconsts.NX_TIMEOUT_INFINITE +FRAME_TYPE_CAN_DATA = _cconsts.NX_FRAME_TYPE_CAN_DATA READ_ALL_AVAILABLE = -1 diff --git a/nixnet/nx.py b/nixnet/nx.py index eb3d3380..a7f6b302 100644 --- a/nixnet/nx.py +++ b/nixnet/nx.py @@ -62,6 +62,10 @@ def close(self): self._handle = None + def start(self, scope): + "http://zone.ni.com/reference/en-XX/help/372841N-01/nixnet/nxstart/" + _funcs.nx_start(self, scope) + def read_frame( self, number_to_read=constants.READ_ALL_AVAILABLE, @@ -77,6 +81,14 @@ def read_frame( """ raise NotImplementedError("Placeholder") + def write_frame( + self, + frames, + number_of_bytes_for_frames, + timeout=10): + "http://zone.ni.com/reference/en-XX/help/372841N-01/nixnet/nxwriteframe/" + raise NotImplementedError("Placeholder") + @property def application_protocol(self): return constants.AppProtocol(_props.get_session_application_protocol(self._handle)) @@ -826,14 +838,6 @@ def read_state( raise NotImplementedError("Placeholder") -def write_frame( - session_ref, - buffer, - number_of_bytes_for_frames, - timeout): - raise NotImplementedError("Placeholder") - - def write_signal_single_point( session_ref, value_buffer): @@ -909,12 +913,6 @@ def flush( _funcs.nx_flush(session_ref) -def start( - session_ref, - scope): - _funcs.nx_start(session_ref, scope) - - def stop( session_ref, scope): diff --git a/nixnet/types.py b/nixnet/types.py index 891c96e6..c04e53f9 100644 --- a/nixnet/types.py +++ b/nixnet/types.py @@ -4,7 +4,7 @@ from __future__ import unicode_literals -class CanFrame(object): +class CANFrame(object): """CAN Frame. Attributes: diff --git a/nixnet_examples/can_frame_stream_input.py b/nixnet_examples/can_frame_stream_input.py index 85bb7efb..67ba56ac 100644 --- a/nixnet_examples/can_frame_stream_input.py +++ b/nixnet_examples/can_frame_stream_input.py @@ -4,8 +4,8 @@ from __future__ import unicode_literals import pprint - import six +import time from nixnet import constants from nixnet import nx @@ -18,26 +18,76 @@ def main(): database_name = ':memory:' cluster_name = '' list = '' - interface = 'CAN2' - mode = constants.CreateSessionMode.FRAME_IN_STREAM + interface1 = 'CAN1' + interface2 = 'CAN2' + input_mode = constants.CreateSessionMode.FRAME_IN_STREAM + output_mode = constants.CreateSessionMode.FRAME_OUT_STREAM + + with nx.Session(database_name, cluster_name, list, interface1, input_mode) as input_session: + print('Input session created successfully.') + with nx.Session(database_name, cluster_name, list, interface2, output_mode) as output_session: + print('Output session created successfully.') + + print('Are you using a terminated cable? Enter Y or N') + terminated_cable = six.input() + if terminated_cable.lower() == "y": + output_session.intf_can_term = contants.CanTerm.OFF + else: + input_session.intf_can_term = contants.CanTerm.ON + output_session.intf_can_term = contants.CanTerm.ON + + input_session.intf_baud_rate = 125000 + output_session.intf_baud_rate = 125000 + print('Properties set successfully.') + + # Start the input session manually to make sure that the first + # frame value sent before the initial read will be received. + input_session.start(constants.StartStopScope.NORMAL) + print('Input session started manually.') + + try: + id = int(six.input('Enter identifier: ')) + except ValueError: + print('Not a number') + + try: + payloadList = [int(x) for x in six.input('Enter payload: ').split()] + except ValueError: + print('Not a number') + + payload = bytearray(payloadList) + number_of_bytes_for_frames = len(payload) + extended = False + + print('The same values should be received. Press q to quit') + i = 0 + while true: + for byte in payload: + byte = byte + i + with CANFrame(identifier, extended, constants.FRAME_TYPE_CAN_DATA, payload) as frame: + output_session.write_frame(list(frame), number_of_bytes_for_frames, 10) + print('Sent frame with ID %s payload: %s' % (id, list(payload))) - with nx.Session(database_name, cluster_name, list, interface, mode) as session: - session.intf_baud_rate = 125000 + # Wait 100 ms and then read the received values. + # They should be the same as the ones sent. + time.sleep(0.1) - print('Logging all received frames. Press q to quit') - while True: - count = 250 - timeout = constants.TIMEOUT_NONE - frames = session.read_frame(count, timeout) + count = constants.READ_ALL_AVAILABLE + timeout = constants.TIMEOUT_NONE + frames = input_session.read_frame(count, timeout) + for frame in frames: + print('Received frame: ') + pp.pprint(frame) - for frame in frames: - pp.pprint(frame) + i += 1 + if max(payload) + i > 0xFF: + i = 0 - inp = six.input() - if inp == 'q': - break + inp = six.input() + if inp == 'q': + break - print('Data acquisition stopped.') + print('Data acquisition stopped.') if __name__ == '__main__': From 28fbb7db8fffdf8cbcd65fdfeec2e3508f5c5b3f Mon Sep 17 00:00:00 2001 From: Ajay Jashnani Date: Tue, 27 Jun 2017 14:55:00 -0500 Subject: [PATCH 2/5] Rename example file --- .../{can_frame_stream_input.py => can_frame_stream_io.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename nixnet_examples/{can_frame_stream_input.py => can_frame_stream_io.py} (100%) diff --git a/nixnet_examples/can_frame_stream_input.py b/nixnet_examples/can_frame_stream_io.py similarity index 100% rename from nixnet_examples/can_frame_stream_input.py rename to nixnet_examples/can_frame_stream_io.py From d68ca803381c6b38c4e80ed29d4c59958ed73678 Mon Sep 17 00:00:00 2001 From: Ajay Jashnani Date: Tue, 27 Jun 2017 15:40:37 -0500 Subject: [PATCH 3/5] Fixing compilation errors --- nixnet_examples/can_frame_stream_io.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/nixnet_examples/can_frame_stream_io.py b/nixnet_examples/can_frame_stream_io.py index 67ba56ac..32c429c5 100644 --- a/nixnet_examples/can_frame_stream_io.py +++ b/nixnet_examples/can_frame_stream_io.py @@ -9,6 +9,7 @@ from nixnet import constants from nixnet import nx +from nixnet import types pp = pprint.PrettyPrinter(indent=4) @@ -31,10 +32,10 @@ def main(): print('Are you using a terminated cable? Enter Y or N') terminated_cable = six.input() if terminated_cable.lower() == "y": - output_session.intf_can_term = contants.CanTerm.OFF + output_session.intf_can_term = constants.CanTerm.OFF else: - input_session.intf_can_term = contants.CanTerm.ON - output_session.intf_can_term = contants.CanTerm.ON + input_session.intf_can_term = constants.CanTerm.ON + output_session.intf_can_term = constants.CanTerm.ON input_session.intf_baud_rate = 125000 output_session.intf_baud_rate = 125000 @@ -51,20 +52,20 @@ def main(): print('Not a number') try: - payloadList = [int(x) for x in six.input('Enter payload: ').split()] + payload_list = [int(x) for x in six.input('Enter payload: ').split()] except ValueError: print('Not a number') - payload = bytearray(payloadList) + payload = bytearray(payload_list) number_of_bytes_for_frames = len(payload) extended = False print('The same values should be received. Press q to quit') i = 0 - while true: + while True: for byte in payload: byte = byte + i - with CANFrame(identifier, extended, constants.FRAME_TYPE_CAN_DATA, payload) as frame: + with types.CANFrame(id, extended, constants.FRAME_TYPE_CAN_DATA, payload) as frame: output_session.write_frame(list(frame), number_of_bytes_for_frames, 10) print('Sent frame with ID %s payload: %s' % (id, list(payload))) From 517d1c4b7b4e4778e68df4bcef1da7d43c1a25e2 Mon Sep 17 00:00:00 2001 From: Ajay Jashnani Date: Wed, 28 Jun 2017 14:42:53 -0500 Subject: [PATCH 4/5] Addressing review comments --- nixnet/constants.py | 1 - nixnet/nx.py | 1 - nixnet/types.py | 2 +- nixnet_examples/can_frame_stream_io.py | 38 +++++++++++++------------- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/nixnet/constants.py b/nixnet/constants.py index 679e0e65..92bfadec 100644 --- a/nixnet/constants.py +++ b/nixnet/constants.py @@ -10,5 +10,4 @@ TIMEOUT_NONE = _cconsts.NX_TIMEOUT_NONE TIMEOUT_INFINITE = _cconsts.NX_TIMEOUT_INFINITE -FRAME_TYPE_CAN_DATA = _cconsts.NX_FRAME_TYPE_CAN_DATA READ_ALL_AVAILABLE = -1 diff --git a/nixnet/nx.py b/nixnet/nx.py index a7f6b302..04587482 100644 --- a/nixnet/nx.py +++ b/nixnet/nx.py @@ -84,7 +84,6 @@ def read_frame( def write_frame( self, frames, - number_of_bytes_for_frames, timeout=10): "http://zone.ni.com/reference/en-XX/help/372841N-01/nixnet/nxwriteframe/" raise NotImplementedError("Placeholder") diff --git a/nixnet/types.py b/nixnet/types.py index c04e53f9..891c96e6 100644 --- a/nixnet/types.py +++ b/nixnet/types.py @@ -4,7 +4,7 @@ from __future__ import unicode_literals -class CANFrame(object): +class CanFrame(object): """CAN Frame. Attributes: diff --git a/nixnet_examples/can_frame_stream_io.py b/nixnet_examples/can_frame_stream_io.py index 32c429c5..9d9336af 100644 --- a/nixnet_examples/can_frame_stream_io.py +++ b/nixnet_examples/can_frame_stream_io.py @@ -25,57 +25,57 @@ def main(): output_mode = constants.CreateSessionMode.FRAME_OUT_STREAM with nx.Session(database_name, cluster_name, list, interface1, input_mode) as input_session: - print('Input session created successfully.') with nx.Session(database_name, cluster_name, list, interface2, output_mode) as output_session: - print('Output session created successfully.') - print('Are you using a terminated cable? Enter Y or N') terminated_cable = six.input() if terminated_cable.lower() == "y": output_session.intf_can_term = constants.CanTerm.OFF + input_session.intf_can_term = constants.CanTerm.ON else: input_session.intf_can_term = constants.CanTerm.ON output_session.intf_can_term = constants.CanTerm.ON input_session.intf_baud_rate = 125000 output_session.intf_baud_rate = 125000 - print('Properties set successfully.') # Start the input session manually to make sure that the first # frame value sent before the initial read will be received. input_session.start(constants.StartStopScope.NORMAL) - print('Input session started manually.') try: - id = int(six.input('Enter identifier: ')) + id = int(six.input('Enter identifier (int): ')) except ValueError: - print('Not a number') + print('Not a number. Setting identifier to 1') + id = 1 try: - payload_list = [int(x) for x in six.input('Enter payload: ').split()] + payload_list = [int(x) for x in six.input('Enter payload [int, int, ...]: ').split()] except ValueError: - print('Not a number') + print('Not a valid list of numbers. Setting payload to [2, 4, 8, 16]') + payload_list = [2, 4, 8, 16] payload = bytearray(payload_list) - number_of_bytes_for_frames = len(payload) extended = False + frame = CanFrame(id, extended, constants.FrameType.CAN_DATA, payload) + write_timeout = 10 print('The same values should be received. Press q to quit') i = 0 while True: - for byte in payload: - byte = byte + i - with types.CANFrame(id, extended, constants.FRAME_TYPE_CAN_DATA, payload) as frame: - output_session.write_frame(list(frame), number_of_bytes_for_frames, 10) - print('Sent frame with ID %s payload: %s' % (id, list(payload))) + for index, byte in enumerate(payload): + payload[index] = byte + i + + frame.payload = payload + output_session.write_frame([frame], write_timeout) + print('Sent frame with ID %s payload: %s' % (id, list(payload))) - # Wait 100 ms and then read the received values. + # Wait 1 s and then read the received values. # They should be the same as the ones sent. - time.sleep(0.1) + time.sleep(1) count = constants.READ_ALL_AVAILABLE - timeout = constants.TIMEOUT_NONE - frames = input_session.read_frame(count, timeout) + read_timeout = constants.TIMEOUT_NONE + frames = input_session.read_frame(count, read_timeout) for frame in frames: print('Received frame: ') pp.pprint(frame) From 5b5faadf1dd5a43c2466d694b3e2da729b8fbbf3 Mon Sep 17 00:00:00 2001 From: Ajay Jashnani Date: Wed, 28 Jun 2017 15:39:53 -0500 Subject: [PATCH 5/5] Fix CanFrame reference --- nixnet_examples/can_frame_stream_io.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nixnet_examples/can_frame_stream_io.py b/nixnet_examples/can_frame_stream_io.py index 9d9336af..adc4eb9d 100644 --- a/nixnet_examples/can_frame_stream_io.py +++ b/nixnet_examples/can_frame_stream_io.py @@ -56,7 +56,7 @@ def main(): payload = bytearray(payload_list) extended = False - frame = CanFrame(id, extended, constants.FrameType.CAN_DATA, payload) + frame = types.CanFrame(id, extended, constants.FrameType.CAN_DATA, payload) write_timeout = 10 print('The same values should be received. Press q to quit')