From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

Assigning parameters for write_ascii_values in pyvisa for serial port

Hi all,

 

I want to control my Keithley 6485 Picoammeter externally by connecting it via RS232toUSB to my Linux PC (CentOS 6.9) and writing a code in python (version 2.7.13) with pyvisa:

 

import sys
import visa
from visa import constants

rm = visa.ResourceManager('/usr/local/vxipnp/linux/lib64/libvisa.so')
#open serial connection and set baud to 9600, 8 data bits, CR termination, one stop bit, none parity, no flow control
amm = rm.open_resource('ASRL2::INSTR', baud_rate = 9600, data_bits = 8, write_termination= '\r', read_termination = '\r')
constants.VI_ASRL_STOP_ONE     
constants.VI_ASRL_PAR_NONE
constants.VI_ASRL_FLOW_NONE                  
amm.write("*RST")                       # Return 6485 to RST default
amm.write("SYS:ERR:ALL?")               # Return error message 
amm.write("TRIG:DEL 0")                 # Set trigger delay to zero seconds
amm.write("TRIG:COUNT 2500")            # Set trigger count to 2500
amm.write("SENS:CURR:RANG:AUTO OFF")    # Turn auto range off
amm.write("SENS:CURR:NPLC .01")         # Set integration rate to NPLC 0.01
amm.write("SENS:CURR:RANG 2e-7")        # Use 200 nA range 
amm.write("SYST:ZCH OFF")               # Turn zero check off
amm.write("SYST:AZER:STAT OFF")         # Turn auto zero off
amm.write("DISP:ENAB OFF")              # Turn Display off
amm.write("*CLS")                       # Clear status model
amm.write("TRAC:POIN 2500")             # Set buffer size to 2500
amm.write("TRAC:CLE")                   # Clear buffer
amm.write("TRAC:FEED:CONT NEXT")        # Set storage control to start on next reading
amm.write("STAT:MEAS:ENAB 512")         # Enable buffer full measurement event
amm.write("*SRE 1")                     # Enable SRQ on buffer full measurement event
amm.write("*OPC?")                      # operation complete query (synchronize completion of commands)    
amm.write("INIT")                       # start taking and storing readings wait for GPIB SRQ line to go true
amm.write("DISP:ENAB ON")               # Turn display on
print(amm.query_ascii_values("TRAC:DATA?")) # Request data from buffer

The problem when I run this script I just get "1" as the print output, although it should be returned in ASCII like this: Reading, Timestamp, Status and the error message after amm.write("*RST"): -113 undefined header. So I think the messages and not transferred correctly.

I know over the RS-232 interface, only the ASCII format is allowed. But when I follow the example in the pyvisa instruction with write_ascii_values(text, values) and assigning it a list, I only get an error message from the device -100 Command error.

Can somebody please tell me how to set the variables in write_ascii_values correctly or what I am doing wrong? Are my settings for the serial device wrong? Sometimes when I execute 2 times I get the error "VI_ERROR_ASRL_FRAMING (-1073807253): A framing error occurred during transfer ." too. I just do not know what to do.

Thank you!

Regards, Roland

0 Kudos
Message 1 of 7
(6,676 Views)

Try adding a "\n" to every command and query. 

amm.write("*RST\n")

Also, how are you handling the queries?  All I see are amm.write commands.  Where are the amm.read commands following a write command with a "?" within the quotes?  Once you send a command containing a "?" in the message, the instrument will then want to respond to that request.  The "1" that you receive in your print function is probably from the earlier command

amm.write("SYST:ERR:ALL?)

I do know that when you send the

 

amm.write("*OPC?")

command that it will respond with a "1" to signify that the instrument has completed processing all earlier commands.

 

Help the Community (and future reviewers) by marking posts as follows:
If it helped - KUDOS
If it answers the issue - SOLUTION
0 Kudos
Message 2 of 7
(6,651 Views)

Hi,

 

thanks for your answer. The printed "1" comes indeed from the command

amm.write("*OPC?").

When I erase OPC? and SYS:ERR:ALL? from the code I get the error:

File "./AMDAQ.py", line 31, in <module> print(amm.query("TRAC:DATA?"))

pyvisa.errors.VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.

 

The adding of "\n" did not help (still get the -113 error). I assign the read and write termination as "\r" here:

amm = rm.open_resource('ASRL2::INSTR', baud_rate = 9600, data_bits = 8, write_termination= '\r', read_termination = '\r'

 Adding \n would lead to \n\r, so a LFCR termination for the device. However I tried every termination possibility with the device: CR, LF, CRLF, LFCR, but it does not change anything.

0 Kudos
Message 3 of 7
(6,641 Views)

I am not that familiar with this instrument, but typically, the first thing to do after a reset is to configure the instrument for the type of measuring.

CONF:CURR:DC

Also, did you try using "READ?" for your print function

 

print(amm.query("READ?"))
Help the Community (and future reviewers) by marking posts as follows:
If it helped - KUDOS
If it answers the issue - SOLUTION
0 Kudos
Message 4 of 7
(6,626 Views)

The manual is confusing as it sometimes refers to the same command with or without the prefix of ":"

:SYST:CLE
or
SYST:CLE

Not sure if it matters?

 

Help the Community (and future reviewers) by marking posts as follows:
If it helped - KUDOS
If it answers the issue - SOLUTION
0 Kudos
Message 5 of 7
(6,623 Views)

BTW: Keep the *OPC? statement where it is, just change it to a query as it allows the system time to configure itself properly before responding.

amm.query("*OPC?")
Help the Community (and future reviewers) by marking posts as follows:
If it helped - KUDOS
If it answers the issue - SOLUTION
0 Kudos
Message 6 of 7
(6,617 Views)

Thanks again for your help. I amended my script according to your change advises. It does not change anything.

 

I also change from RS232 to GPIB, but I get the same VI_ERROR_TMO (-1073807339) error. Also when I change the language from SCPI to 488.1. So there is definitely something wrong with the translation when it wants to read the data out of it. But I am out of ideas...

0 Kudos
Message 7 of 7
(6,588 Views)