In this blog, I am going to show you two examples of reading data from a multimeter into an Origin worksheet in real time. You will need Origin 2021 to try these examples.
The first example directly reads data from the instrument to an Origin worksheet. The second example first saves the readings to a csv file and then uses Origin data connector to auto import the data into an Origin worksheet.
Hardware used in this example:
- HP 34401A Multimeter
- TRENDnet USB to Serial 9-pin Converter Cable.
NOTE: The Python code in these examples work for both Embedded Python in Origin or External Python. For the Embedded Python, you will run the code from Origin’s Code Builder window. For external python, you will use your Python IDE.
Example 1:
import time import serial import originpro as op # If external Python, set Origin instance to show if op and op.oext: op.set_show(True) # make connection to the multimeter, set to Remote mode port = 'COM3' ser = serial.Serial(port,timeout = 1) msg = 'SYSTem:REMote\n' ser.write(msg.encode('ascii')) # Create a Worksheet wks = op.new_sheet() # save data into a csv file for ii in range(10): msg = 'Read?\n' ser.write(msg.encode('ascii')) wks.from_list2([time.perf_counter(), ser.read(20)], ii , 0) time.sleep(1) ser.close() # If external Python, must call this to shut down Origin if op and op.oext: op.exit()
Example 2:
We start by creating an empty folder to contain the necessary files. It can be placed anywhere. Three files are going to be created: data.csv, getData.opju, import.py.
wks.dc.flags=256;
import time import os import csv import serial # If external Python, set Origin instance to show
if op and op.oext:
op.set_show(True) # make connection to the multimeter, set to Remote mode port = 'COM3' ser = serial.Serial(port,timeout = 1) msg = 'SYSTem:REMote\n' ser.write(msg.encode('ascii')) # save data into a csv file ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) for _ in range(10): with open( os.path.join(ROOT_DIR, 'data.csv'), 'w') as file: wr = csv.writer(file) msg = 'Read?\n' ser.write(msg.encode('ascii')) wr.writerow([time.perf_counter(), ser.read(20)]) file.close() time.sleep(1) ser.close() # If external Python, must call this to shut down Origin
if op and op.oext:
op.exit()