Software/Python

From ZaberWiki
Jump to: navigation, search

This page shows an older example of Python code that can control a Zaber device that's using the Zaber Binary protocol. We have a more recent and complete Python library available here: Zaber Core Serial Library in Python, including API documentation and examples.

Displaying a list of available serial ports

The following program will work in Windows or Linux. It should display all resident serial ports, but in Linux it may miss virtual ports (ex USB to serial converters).

 #! /usr/bin/env python
 
 import serial
 
 def scan():
    # scan for available ports. return a list of tuples (num, name)
    available = []
    for i in range(256):
        try:
            s = serial.Serial(i)
            available.append( (i, s.portstr))
            s.close()
        except serial.SerialException:
            pass
    return available
 
 print "Found ports:"
 for n,s in scan(): print "(%d) %s" % (n,s)


The following program will work only in Linux. It will display all serial ports including virtual ports (ex USB to serial converters).

 #! /usr/bin/env python
 import serial, glob
 
 def scan():
    # scan for available ports. return a list of device names.
    return glob.glob('/dev/ttyS*') + glob.glob('/dev/ttyUSB*') + glob.glob('/dev/ttyACM*')
 
 print "Found ports:"
 for name in scan(): print name

Opening a serial port, sending an instruction, and receiving a reply

The following code opens the serial port "/dev/ttyUSB0" (a USB to serial adapter installed on a Linux machine) and sends an instruction to a Zaber device to return its power supply voltage. After 1 second, the program checks for a response and displays it.

 #!/usr/bin/env python
 
 import serial, sys, time, glob, struct
 
 def send(device, command, data=0):
    # send a packet using the specified device number, command number, and data
    # The data argument is optional and defaults to zero
    packet = struct.pack('<BBl', device, command, data)
    ser.write(packet)
 
 def receive():
    # return 6 bytes from the receive buffer
    # there must be 6 bytes to receive (no error checking)
    r = [0,0,0,0,0,0]
    for i in range (6):
        r[i] = ord(ser.read(1))
    return r
 
 # open serial port
 # replace "/dev/ttyUSB0" with "COM1", "COM2", etc in Windows
 try:
    ser = serial.Serial("/dev/ttyUSB0", 9600, 8, 'N', 1, timeout=5)   
 except:
    print("Error opening com port. Quitting.")
    sys.exit(0)
 print("Opening " + ser.portstr)
 
 device = 1
 command = 52
 data = 0
 print('Sending instruction. Device: %i, Command: %i, Data: %i' % (device, command, data))
 send(device, command, data)
 time.sleep(1) # wait for 1 second
 
 try:
    reply = receive()
    # Reply data is calculated from all reply bytes
    replyData = (256.0**3.0*reply[5]) + (256.0**2.0*reply[4]) + (256.0*reply[3]) + (reply[2])
    if reply[5] > 127:
       replyData -= 256.0**4
 
    print("Receiving reply " + str(reply))
    print("Device number: " + str(reply[0]))
    print("Command number: " +  str(reply[1]))
    print("Supply voltage: " + str(replyData/10) + "V") # Supply voltage must be divided by ten
 except:
    print("No reply was received.")
 
 print("Closing " + ser.portstr)
 ser.close()