Origin RS232 Interface using Python

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:

  1. HP 34401A Multimeter
  2. TRENDnet USB to Serial 9-pin Converter Cable.
Connect the multimeter to the computer USB port with USB-to-serial Converter cable. Turn on the multimeter.
 
You will also need to install the Python module pyserial. To install module in Origin’s embedded Python, please use the menu Connectivity: Python Packages… 
 

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:
1. In Origin workspace, select menu Connectivity: Open Untitled.py… to create a python file in Code Builder window.  Paste in the script below and click Run button (F5) to run the script. (You will need to change the port string accordingly. ) The readings of the multimeter will get imported into a new worksheet. You can also run the script in external Python. It will launch an Origin instance and read in the measurements to a worksheet.
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.

 
1. Create an empty data file data.csv.  
Note: This data file is acting as a temporary place to receive measured data from the device.  Python code reads new measurement from the device and overwrites the old measurement in the file. 
 
2. Launch Origin with a new project file and save it to the folder with name getData.opju. In the project create a new worksheet, then select from menu Data: Connect to File: Text/CSV.  Select data.csv to open, with default settings in Import Options dialog, click OK. Now a data connector icon shows in the upper left corner of the worksheet, indicating the link of the worksheet to data.csv is generated.
 
3. Make following changes to the data connector settings. 
a) Click on the data connector icon, select Auto Import: On Change
b) Click on the data connector icon, select Common Data Path, select Relative to Project path and set the path to the current folder.
c) Open Script Window, run LabTalk script wks.dc.flags=256;
Note:  This data connector flag ensures that incoming data is appended to the Origin worksheet. 
 
 
4. Create Python file import.py in folder. Paste in the following code. (You will need to change the port string accordingly.) This code reads 10 consecutive measurements from the multimeter and updates data.csv with each new measurement. 
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()
5. Run the code with getData.opju opened in Origin. You should see the worksheet get updated with one measurement at a time. 
 
 

Leave a Reply

Your email address will not be published. Required fields are marked *