1+ from __future__ import absolute_import
2+ from __future__ import division
3+ from __future__ import print_function
4+
5+ import time
6+
7+ import six
8+
9+ import nixnet
10+ from nixnet import constants
11+ from nixnet import types
12+
13+
14+ def main ():
15+ interface1 = 'CAN1'
16+ interface2 = 'CAN2'
17+
18+ #Create the objects for the synchronized interface
19+ synchronized_input = nixnet .FrameInStreamSession (interface1 )
20+ synchronized_output = nixnet .FrameOutStreamSession (interface1 )
21+
22+ #Set session and interface properties such as termination
23+ #Note that interface properties only need to be set once per interface
24+ synchronized_input .intf .baud_rate = 125000
25+ synchronized_input .intf .can_term = constants .CanTerm .ON
26+
27+ #Connect the start trigger terminals for the synchronized interface
28+ synchronized_input .connect_terminals (nixnet ._cconsts .NX_TERM_FRONT_PANEL0 , nixnet ._cconsts .NX_TERM_START_TRIGGER )
29+
30+ #Start the synchronized sessions as session only
31+ synchronized_input .start ()
32+ synchronized_output .start ()
33+
34+ #Create the session objects for the synchronizing interface
35+ synchronizing_input = nixnet .FrameInStreamSession (interface2 )
36+ synchronizing_output = nixnet .FrameOutStreamSession (interface2 )
37+
38+ #Set session and interface properties such as termination
39+ #Note that interface properties only need to be set once per interface
40+ synchronizing_input .intf .baud_rate = 125000
41+ synchronizing_input .intf .can_term = constants .CanTerm .ON
42+ synchronizing_input .intf .echo_tx = True
43+
44+ #Connect the start trigger terminals for the synchronized interface
45+ synchronizing_input .connect_terminals (nixnet ._cconsts .NX_TERM_START_TRIGGER , nixnet ._cconsts .NX_TERM_FRONT_PANEL1 )
46+
47+ #Starting the synchronizing sessions will trigger the start of the synchronized sessions
48+ synchronizing_input .start (constants .StartStopScope .SESSION_ONLY )
49+ synchronizing_output .start ()
50+
51+ #Request values to transmit
52+ user_value = six .moves .input ('Enter payload [int, int]: ' )
53+ try :
54+ payload_list = [int (x .strip ()) for x in user_value .split ("," )]
55+ except ValueError :
56+ payload_list = [2 , 4 , 8 , 16 ]
57+ print ('Unrecognized input ({}). Setting data buffer to {}' .format (user_value , payload_list ))
58+
59+ id = types .CanIdentifier (1 )
60+ payload = bytearray (payload_list )
61+ frame = types .CanFrame (id , constants .FrameType .CAN_DATA , payload )
62+
63+ print ('The same values should be received. Press q to quit' )
64+ i = 0
65+ while True :
66+ #Apply the user input to the frame payload
67+ for index , byte in enumerate (payload ):
68+ payload [index ] = byte + i
69+
70+ frame .payload = payload
71+ #Write the specified frame
72+ synchronizing_output .frames .write ([frame ])
73+ print ('Sent frame with ID {} payload: {}' .format (id , payload ))
74+
75+ #Read frames back from both input sessions
76+ framesToRead = 1
77+ rxTimestamp = 0
78+ echoTimestamp = 0
79+ receivedFrames = synchronized_input .frames .read (framesToRead )
80+ echoedFrames = synchronizing_input .frames .read (framesToRead )
81+ for frame in receivedFrames :
82+ rxTimestamp = frame .timestamp
83+ print ('Received frame: {}' .format (frame ))
84+ print (' payload={}' .format (list (six .iterbytes (frame .payload ))))
85+
86+ for frame in echoedFrames :
87+ echoTimestamp = frame .timestamp
88+ print ('Echoed frame: {}' .format (frame ))
89+ print (' payload={}' .format (list (six .iterbytes (frame .payload ))))
90+
91+ #Calculate the delta between timestamps
92+ deltaT = echoTimestamp - rxTimestamp
93+ print ('The delta between received timestamp and echo is: {}' , deltaT )
94+
95+ i += 1
96+ if max (payload ) + i > 0xFF :
97+ i = 0
98+ inp = six .moves .input ('Hit enter to continue (q to quit): ' )
99+ if inp .lower () == 'q' :
100+ break
101+ print ('Data acquisition stopped.' )
102+
103+
104+ if __name__ == '__main__' :
105+ main ()
0 commit comments