添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
霸气的花卷  ·  python list 错位相减 ...·  1 月前    · 
憨厚的大脸猫  ·  python ...·  1 月前    · 
小猫猫  ·  python tornado ...·  3 周前    · 
有腹肌的充值卡  ·  pycharm console 清屏 ...·  6 天前    · 
腹黑的铅笔  ·  oracle ...·  1 年前    · 
爱旅游的红茶  ·  4G内存就能跑 ...·  1 年前    · 
活泼的木耳  ·  windows10 ...·  1 年前    · 
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
                are you sure your baud rate settings are correct? This looks like an issue that could be caused by wrong baud rates.
– mic_e
                Apr 3, 2014 at 16:38
                Also, does anyone know  where I can find in depth documentation specifically about pyvisa for RS232 communication?
– Crawdad_Buckwheat
                Apr 3, 2014 at 18:19
                I'm thinking maybe pyVISA doesn't know that it is trying to communicate with an rs232 device. When I print "si.instrument_type" it prints "4" What does 4 mean?
– Crawdad_Buckwheat
                Apr 4, 2014 at 17:47
                If anyone knows where I can find an in depth tutorial on pyvisa for serial devices it would probably solve all of my problems.  Thanks.
– Crawdad_Buckwheat
                Apr 4, 2014 at 17:54

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.