Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I have an rs232 serial device and I am trying to read and write to it using
python with pyVISA. I am able to write my commands to it using "write" but
I If I try to us "read" or "ask" I get a timeout error.
I can read and write to it easily through labview or tera term but I can't read using python.
Here is the python code that is not working:
import visa as v
si = v.SerialInstrument("COM1", delay = 0.1)
si.clear()
si.timeout = 3
si.baud_rate = 9600
si.data_bits = 8
si.stop_bits = 1
command = '0'
while command != 'end':
rorw = raw_input('ask, read, or write? >>')
command = raw_input('enter command code >>')
if rorw == 'write':
write1 = si.write(command)
print write1
elif rorw == 'read':
read1 = si.read()
print read1
else:
ask1 = si.ask(command)
print ask
–
–
–
–
UPDATE - May 2020: So 6 years later I ran into the same problem.
At the time of writing the latest version is PyVisa 1.10.1
.
This is important to note because PyVisa 1.5
brought about some breaking
changes in syntax. The general form remains very similar nonetheless.
With serial communication it is crucially important to send the correct termination character
at the end of every command. The expected termination character (or character sequence) is
usually specified by the device manufacturer.
The most common termination characters are:
\r
or CR
: carriage return
\n
or LF
: line feed
\r\n
or CRLF
: sequence of the two above
The PyVisa library is a little obscure as to what default termchars are in use by the different subclasses (e.g. SerialInstrument
). If you can write to an instrument but cannot read from it, it is likely that your defined read_termination
character isn't correct.
Here's an example setting up my instrument whose termination character is \r
.
import pyvisa
rm = pyvisa.ResourceManager()
device = rm.open_resource('ASRL3::INSTR', baud_rate = 57600) # same as ‘COM3’ on Window$
device.write_termination = '\r'
device.read_termination = '\r'
The above can also be written in one line:
device = rm.open_resource('ASRL3::INSTR', baud_rate = 57600,
write_termination = '\r',
read_termination = '\r')
In case of an asymmetrical communication problem (i.e. you can write but cannot read) it is possible that your termination character is not correct at all but your device is less fussy about it the than your host, PyVisa. This is a problem I have run into recently leading to much head scratching:
# I set up my instrument like so:
device = rm.open_resource('COM3', baud_rate = 57600, write_termination = '\r\n', read_termination = '\r\n')
# '@021FF0' is just a command to set the outputs of my device
device.query('@021FF0') #< threw a time-out error
device.write('@021FF0') #< worked fine I could see the change in output,
device.read() #< but trying to read back the response from the device
#< gave me a time-out error once again.
In the end it turned out that the expected term-char was just a single \r
. But while my instrument was flexible enough (or dumb, depends on the perspective) to simply discard the extra \n
inside \r\n
, PyVisa was adamant on strictly enforcing the whole sequence of \r\n
for termination, which of course it never received as it was not provided by the instrument.
Setting read_termination = write_termination = '\r'
solved the problem.
My guess is you have a termchar issue. try set term_chars to \n or \r\n
If this doesnt work serial communication is very easy with pyserial.
You will want to define a custom method equivalent to pysvisa's ask using write and readline and probably a del method to close the port if anything goes wrong
Good luck
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.