autolab-project / autolab

Python package for laboratory instruments control and scientific experiments automation
https://autolab.readthedocs.io/
GNU General Public License v3.0
32 stars 7 forks source link

Example driver using serial #2

Closed RR-C2N closed 6 months ago

RR-C2N commented 4 years ago

Hey guys,

Do you have an example of a driver that uses a serial port? and the way to properly write the Local Configuration file for it?

Thanks, R

Python-simulation commented 6 months ago

Hi! Pyvisa can be used to connect to serial ports, you just need to use the correct address and specify the correct baud rate when instantiating your instrument. For example, the address can be 'ASRL::2::INSTR' and the baud rate 115200.

You can look at the driver arduino_CUSTOMSHUTTERS as an example. Its driver class look like this:

class Driver_VISA(Driver):
    def __init__(self, address='ASRL::2::INSTR', **kwargs):
        import pyvisa as visa

        self.BAUDRATE = 115200 

        # Instantiation
        rm = visa.ResourceManager()
        self.inst = rm.open_resource(address)
        self.inst.timeout = 5000 #ms
        self.inst.baud_rate = self.BAUDRATE

        Driver.__init__(self)

    def close(self):
        try : self.inst.close()
        except : pass

    def query(self,command):
        return self.inst.query(command).strip('\n')

    def write(self,command):
        self.inst.write(command)

For the config file, it will look like this:

[my_arduino_CUSTOMSHUTTERS]
driver = arduino_CUSTOMSHUTTERS
connection = VISA
address = ASRL::2::INSTR