pyvisa / pyvisa-py

A pure python PyVISA backend
https://pyvisa-py.readthedocs.io
MIT License
287 stars 124 forks source link

[COM] Communication issue with Cobright Dx4 laser using Windows 10 #334

Open ScotchWhiskey opened 2 years ago

ScotchWhiskey commented 2 years ago

Instrument details

Output of pyvisa-info

Version: 1.12.0 (bundled with PyVISA)

1: C:\Windows\system32\visa32.dll:

     found by: auto
     bitness: 64
     Vendor: National Instruments
     Impl. Version: 22021376
     Spec. Version: 5244928
  #2: C:\Windows\system32\visa64.dll:
     found by: auto
     bitness: 64
     Vendor: National Instruments
     Impl. Version: 22021376
     Spec. Version: 5244928

py: Version: 0.5.3 ASRL INSTR: Available via PySerial (3.5) USB INSTR: PyUSB does not seem to be properly installed. Please refer to PyUSB documentation and install a suitable backend like libusb 0.1, libusb 1.0, libusbx, libusb-win32 or OpenUSB. No backend available USB RAW: PyUSB does not seem to be properly installed. Please refer to PyUSB documentation and install a suitable backend like libusb 0.1, libusb 1.0, libusbx, libusb-win32 or OpenUSB. No backend available TCPIP INSTR: Available TCPIP SOCKET: Available GPIB INSTR: Please install linux-gpib (Linux) or gpib-ctypes (Windows, Linux) to use this resource type. Note that installing gpib-ctypes will give you access to a broader range of funcionality. No module named 'gpib' sim: Version: 0.5.1 Spec version: 1.1

Hi,

I am trying to connect a Cobright DX4 laser to my computer to control it from my computer. However I am unable to do so. I have tried using both rm.openresource() and through pyserial. However neither seem to work.

#import visa
import serial
import numpy as np
#rm = visa.ResourceManager() #checks devices that are
rm = visa.ResourceManager('@py') #to take the python backend
#rm = visa.ResourceManager('@sim')

print(rm.list_resources())

inst1 = rm.list_resources() 

('ASRL5::INSTR')

However when I query the ID:

inst2 = rm.open_resource("ASRL5::INSTR",read_termination = '\n', write_termination="\r\n")

print(inst2.query("*IDN?"))

I get a timeout error:

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

I have also tried to connect using pyserial.

import pyvisa as visa # note the recommended way to import pyvisa changed as there is an unrelated visa module
#import visa
import serial
import numpy as np

import serial.tools.list_ports
import sys

list = serial.tools.list_ports.comports()
connected = []
for element in list:
    connected.append(element.device)
print("Connected COM ports: " + str(connected))
# compliments of https://stackoverflow.com/questions/12090503/listing-available-com-ports-with-python#14224477
""" Lists serial port names

    :raises EnvironmentError:
        On unsupported or unknown platforms
    :returns:
        A list of the serial ports available on the system
"""
if sys.platform.startswith('win'):
# !attention assumes pyserial 3.x
    ports = ['COM%s' % (i + 1) for i in range(256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
    # this excludes your current terminal "/dev/tty"
    ports = glob.glob('/dev/tty[A-Za-z]*')
elif sys.platform.startswith('darwin'):
    ports = glob.glob('/dev/tty.*')
else:
    raise EnvironmentError('Unsupported platform')

result = []
for port in ports:
    try:
        s = serial.Serial(port)
        s.close()
        result.append(port)
    except (OSError, serial.SerialException):
        pass
print("Availible COM Ports: " + str(result))

Connected COM ports: ['COM5'] Availible COM Ports: ['COM5']

ser = serial.Serial(
    port="COM5", # assumes pyserial 3.x, for 2.x use integer values
    baudrate=9600,
    bytesize=8,
    parity="E",  # options are: {N,E,O,S,M}
    stopbits=1,
    timeout=1)

print(ser)

Serial(port='COM5', baudrate=9600, bytesize=8, parity='E', stopbits=1, timeout=1, xonxoff=False, rtscts=False, dsrdtr=False)

ser.isOpen()

True

Now when I try to send a command eg

ser.write(str.encode('*IDN?'))

5

is the output. I am unsure as to what the issue. Ihave tried other commands and get numbers also eg 8, 16. I cannot decode having encoded. I coonected a thorlab power meter and this was recognised. Any help is appreciated!

ScotchWhiskey commented 2 years ago

image

image

Images if of use

ScotchWhiskey commented 2 years ago

Just to add, someone used a hyper terminal and could talk to the device. So I now have the idn. But I still cannot execute through python. Their hyper terminal is from hilgraeve and has to be paid for it seems. So I cannot use it. So any suggestions as to what the problem is? I can connect to the device but not send commands or receive information from the device.

I do not have a C-compiler on my desktop, could this be an issue?

MatthieuDartiailh commented 2 years ago

A C compiler is not required. And I would suggest you use the ivi backend (no argument to ResourceManager) over the py backend since you have a working VISA installation it seems.

Looking at your device manual you should use either ; or \r as write_termination and ; as read termination. Using the TCPIP interface would also eliminate possible issues with a bad configuration of the serial link. finally you can use inst2.read_bytes(1) to read a single byte at a time which can allow to figure out if the instrument is responding and what is the actual term char.

Hope this helps

ScotchWhiskey commented 2 years ago

Thankyou. Is the fact that I do not have a backend for PyUSB an issue? I am struggling to understand how to actually set it up as I have not done something like it before. I tried using ; and it did not work. But I will give the TCIP a go whn I can.

MatthieuDartiailh commented 2 years ago

Your instrument is providing a serial emulation over usb as a consequence you do not need to care about pyusb. By the return value of pyserial.write is simply the number of character that was sent. You need to read to see any response from your device.

ScotchWhiskey commented 2 years ago

The TCIP didn't work either. We cannot ping the IP address of the laser, we get 'request timed out'.

With IVI backend we get an empty list.

We tried a different computer and the same.

However I think the problem is I was running

#import visa
import serial
import numpy as np
#rm = visa.ResourceManager() #checks devices that are
rm = visa.ResourceManager('@py') #to take the python backend
#rm = visa.ResourceManager('@sim')

print(rm.list_resources())

inst1 = rm.list_resources() 

folowed by the import serial part. S0 it is working now i.e. I can get the idn. Trying to figure out how to send commands now as getting err 101's when trying to to eg set the wavelength using 'Wav 1550;'. If you have any tips for this let me know please. And thankyou for your help!

MatthieuDartiailh commented 2 years ago

Pyvisa automatically adds the write termination for you so try removing the trailing ;