diff --git a/nixnet/nx.py b/nixnet/nx.py index eb3d3380..04587482 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,13 @@ def read_frame( """ raise NotImplementedError("Placeholder") + def write_frame( + self, + 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 +837,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 +912,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_examples/can_frame_stream_input.py b/nixnet_examples/can_frame_stream_input.py deleted file mode 100644 index 85bb7efb..00000000 --- a/nixnet_examples/can_frame_stream_input.py +++ /dev/null @@ -1,44 +0,0 @@ -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -from __future__ import unicode_literals - -import pprint - -import six - -from nixnet import constants -from nixnet import nx - - -pp = pprint.PrettyPrinter(indent=4) - - -def main(): - database_name = ':memory:' - cluster_name = '' - list = '' - interface = 'CAN2' - mode = constants.CreateSessionMode.FRAME_IN_STREAM - - with nx.Session(database_name, cluster_name, list, interface, mode) as session: - session.intf_baud_rate = 125000 - - print('Logging all received frames. Press q to quit') - while True: - count = 250 - timeout = constants.TIMEOUT_NONE - frames = session.read_frame(count, timeout) - - for frame in frames: - pp.pprint(frame) - - inp = six.input() - if inp == 'q': - break - - print('Data acquisition stopped.') - - -if __name__ == '__main__': - main() diff --git a/nixnet_examples/can_frame_stream_io.py b/nixnet_examples/can_frame_stream_io.py new file mode 100644 index 00000000..adc4eb9d --- /dev/null +++ b/nixnet_examples/can_frame_stream_io.py @@ -0,0 +1,95 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from __future__ import unicode_literals + +import pprint +import six +import time + +from nixnet import constants +from nixnet import nx +from nixnet import types + + +pp = pprint.PrettyPrinter(indent=4) + + +def main(): + database_name = ':memory:' + cluster_name = '' + list = '' + 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: + with nx.Session(database_name, cluster_name, list, interface2, output_mode) as output_session: + 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 + + # 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) + + try: + id = int(six.input('Enter identifier (int): ')) + except ValueError: + print('Not a number. Setting identifier to 1') + id = 1 + + try: + payload_list = [int(x) for x in six.input('Enter payload [int, int, ...]: ').split()] + except ValueError: + print('Not a valid list of numbers. Setting payload to [2, 4, 8, 16]') + payload_list = [2, 4, 8, 16] + + payload = bytearray(payload_list) + extended = False + frame = types.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 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 1 s and then read the received values. + # They should be the same as the ones sent. + time.sleep(1) + + count = constants.READ_ALL_AVAILABLE + read_timeout = constants.TIMEOUT_NONE + frames = input_session.read_frame(count, read_timeout) + for frame in frames: + print('Received frame: ') + pp.pprint(frame) + + i += 1 + if max(payload) + i > 0xFF: + i = 0 + + inp = six.input() + if inp == 'q': + break + + print('Data acquisition stopped.') + + +if __name__ == '__main__': + main()