|
| 1 | +from __future__ import absolute_import |
| 2 | +from __future__ import division |
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +import datetime |
| 6 | +import pprint |
| 7 | +import six |
| 8 | +import sys |
| 9 | +import time |
| 10 | + |
| 11 | +import nixnet |
| 12 | +from nixnet import constants |
| 13 | + |
| 14 | + |
| 15 | +pp = pprint.PrettyPrinter(indent=4) |
| 16 | + |
| 17 | + |
| 18 | +def convert_timestamp(timestamp): |
| 19 | + system_epoch = time.gmtime(0) |
| 20 | + system_epock_datetime = datetime.datetime(system_epoch.tm_year, system_epoch.tm_mon, system_epoch.tm_mday) |
| 21 | + xnet_epoch_datetime = datetime.datetime(1601, 1, 1) |
| 22 | + delta = system_epock_datetime - xnet_epoch_datetime |
| 23 | + date = datetime.datetime.fromtimestamp(timestamp * 100e-9) - delta |
| 24 | + return date |
| 25 | + |
| 26 | + |
| 27 | +def convert_datetime(date): |
| 28 | + system_epoch = time.gmtime(0) |
| 29 | + system_epock_datetime = datetime.datetime(system_epoch.tm_year, system_epoch.tm_mon, system_epoch.tm_mday) |
| 30 | + xnet_epoch_datetime = datetime.datetime(1601, 1, 1) |
| 31 | + delta = system_epock_datetime - xnet_epoch_datetime |
| 32 | + timestamp = (date + delta - datetime.datetime(1970, 1, 1)) / datetime.timedelta(seconds=1) * 100e9 |
| 33 | + return int(timestamp) |
| 34 | + |
| 35 | + |
| 36 | +def main(): |
| 37 | + database_name = 'NIXNET_example' |
| 38 | + cluster_name = 'CAN_Cluster' |
| 39 | + input_signals = ['CANEventSignal1', 'CANEventSignal2'] |
| 40 | + output_signals = ['CANEventSignal1', 'CANEventSignal2'] |
| 41 | + interface1 = 'CAN1' |
| 42 | + interface2 = 'CAN2' |
| 43 | + |
| 44 | + with nixnet.SignalInXYSession( |
| 45 | + interface1, |
| 46 | + database_name, |
| 47 | + cluster_name, |
| 48 | + input_signals) as input_session: |
| 49 | + with nixnet.SignalOutXYSession( |
| 50 | + interface2, |
| 51 | + database_name, |
| 52 | + cluster_name, |
| 53 | + output_signals) as output_session: |
| 54 | + terminated_cable = six.moves.input('Are you using a terminated cable (Y or N)? ') |
| 55 | + if terminated_cable.lower() == "y": |
| 56 | + input_session.intf.can_term = constants.CanTerm.ON |
| 57 | + output_session.intf.can_term = constants.CanTerm.OFF |
| 58 | + elif terminated_cable.lower() == "n": |
| 59 | + input_session.intf.can_term = constants.CanTerm.ON |
| 60 | + output_session.intf.can_term = constants.CanTerm.ON |
| 61 | + else: |
| 62 | + print("Unrecognised input ({}), assuming 'n'".format(terminated_cable)) |
| 63 | + input_session.intf.can_term = constants.CanTerm.ON |
| 64 | + output_session.intf.can_term = constants.CanTerm.ON |
| 65 | + |
| 66 | + # Start the input session manually to make sure that the first |
| 67 | + # signal value sent before the initial read will be received. |
| 68 | + input_session.start() |
| 69 | + |
| 70 | + out_waveforms = [] |
| 71 | + for out_signal in output_signals: |
| 72 | + user_value = six.moves.input('Enter "{}" signal values [float, ...]: '.format(out_signal)) |
| 73 | + try: |
| 74 | + out_waveforms.append([float(x.strip()) for x in user_value.split(",")]) |
| 75 | + except ValueError: |
| 76 | + out_waveforms.append([24.5343, 77.0129]) |
| 77 | + print('Unrecognized input ({}). Setting waveform to {}'.format(user_value, out_waveforms[-1])) |
| 78 | + |
| 79 | + print('The same values should be received. Press q to quit') |
| 80 | + i = 0 |
| 81 | + while True: |
| 82 | + for waveform_index, waveform in enumerate(out_waveforms): |
| 83 | + for value_index, value in enumerate(waveform): |
| 84 | + out_waveforms[waveform_index][value_index] = value + i |
| 85 | + output_session.signals.write(out_waveforms) |
| 86 | + print('Sent signal values: {}'.format(pp.pformat(out_waveforms))) |
| 87 | + |
| 88 | + # Wait 5 s and then read the received values. |
| 89 | + # They should be the same as the ones sent. |
| 90 | + time_limit = convert_datetime(datetime.datetime.now() + datetime.timedelta(seconds=5)) |
| 91 | + |
| 92 | + signals = input_session.signals.read(len(out_waveforms[0]), time_limit) |
| 93 | + print('Received signals:') |
| 94 | + for signal_values in signals: |
| 95 | + signal_values = [(value, convert_timestamp(timestamp)) for (value, timestamp) in signal_values] |
| 96 | + print(' {}', signal_values) |
| 97 | + |
| 98 | + i += 1 |
| 99 | + for waveform in out_waveforms: |
| 100 | + if max(waveform) + i > sys.float_info.max: |
| 101 | + i = 0 |
| 102 | + break |
| 103 | + |
| 104 | + inp = six.moves.input('Hit enter to continue (q to quit): ') |
| 105 | + if inp.lower() == 'q': |
| 106 | + break |
| 107 | + |
| 108 | + print('Data acquisition stopped.') |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == '__main__': |
| 112 | + main() |
0 commit comments