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()
The output is ('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<id=0x243e051beb0, open=True>(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. I cannot decode having encoded.
Any help appreciated, and sorry for the long post! I have also connected a power meter which worked fine.