""" Created 2020/02 Author: Jahns_P Version Number: 2 Date of last change: 2021/03/12 Requires: R&S RTH, FW 1.80.3.4 or newer and adequate options Installed VISA e.g. R&S Visa 5.12.x or newer Description: Simple example of setting up and measuring the CAN bus Low Wire. Save the decoded frame data to a .txt file. General Information: Please always check this example script for unsuitable setting that may destroy your DUT befor connecting it to the instrument! This example does not claim to be complete. All information has been compiled with care. However errors can not be ruled out. """ ## ## Predefine variables ## x=0 # counter for diverse applications idnResponse=" " # string for identification request Response=" " # string for diverse requests NOF=0 # used for number of frames FDATA=" " # string for frame data ## Set communication and device on a certain base ## RTH.instrument.clear() RTH.instrument.write_termination = '\n' ## ## Perform instrument reset and get to a defined state ## RTH.write("*RST") time.sleep (1) # Request for the device idnResponse = RTH.query('*IDN?') print 'Hello, I am ' + idnResponse ## ## Set instrument to defined mode and operating state ## # Start SCOPE mode RTH.write('OP:MODE YT') # Perform autoset RTH.write('AUToscale') # Set Time Base RTH.write('Timebase:SCALe 2e-3') # Set BUS type protcol to CAN RTH.write('BUS:TYPE CAN') # Set BUS source to CH1 RTH.write('BUS:CAN:DATA:SOURce C1') # Set CAN type to CANL RTH.write('BUS:CAN:Type CANL') # Set CAN bitrate RTH.write('BUS:CAN:BITR 50000') # Set CAN technology to User (finding own Threshold Level) RTH.write('BUS:CAN:TECHnology USER') # Find the right threshold level RTH.write('CHANnel1:THReshold:FINDlevel') # Set BUS decoding to desired format (BIN | OCT | DEC | HEX | ASCii) RTH.write('BUS:FORMat HEX') # Switch on BUS decoding RTH.write('BUS:STATE ON') ## ## Requesting some CAN decoding information ## # Set unit to single trigger mode RTH.write('TRIGger:MODE SINGle') time.sleep(3) Response = RTH.query("*OPC?") print Response # # A dedicated trigger type can defined for the CAN bus as well # Please refer to the TRIGger:CAN: commands in the RTH manual. # After preset it's defined to react on diverse conditions. # Give the unit some additional time to decode frames time.sleep (0.4) Response = RTH.query("Syst:Err?") print Response # Get the number of decoded frames NOF = RTH.query("BUS:CAN:FCOunt?") print 'Number of decoded frames: ' + NOF NOF=int(NOF) ## ## now write the data of all frames into a file ## # Open file "data.txt" for write access # File must exist before! fobj_in = open("E:\Python\data.txt") fobj_out = open("E:\Python\data.txt","w") # Define and write data row names fobj_out.write("Frame;") fobj_out.write("Data;"+chr(10)) # Start loop to read all the frame data and write it into the file x=0 while x<(NOF): x=x+1 value=str(x) FDATA = RTH.query ("BUS:CAN:FRAMe" + value + ":DATA?") # There is also some more information available to read out: # e.g. frame start, type, ID state/value/type fobj_out.write(value+";") fobj_out.write(FDATA+";"+chr(10)) Response = RTH.query("*OPC?") print Response Response = RTH.query("Syst:Err?") print Response x=0 # End of control loop # Close the file now and end the job fobj_in.close() fobj_out.close() print 'Job done'