Monday, May 7, 2012

Using Prologix GPIB-USB Controller.











Following is a tutorial to access Prologix GPIB-USB controller in Python.
(Can be translated to any other programming language).






Disclaimer:


It was a quick and dirty project so please bear with me !




Description:


A cheap GPIB-USB controller to control GPIB enabled instruments. Based on USB to COM FTDI chip that converts data from USB to serial. So, you just have to read and write GPIB instructions over a virtual COM port.


In Short:


  1.  Instantiate a GPIB object by opening the COM PORT and fixing the baud rate.
  2. Then you need to select a device to write or query. Select the device address by "++addr<ADDRESS>" command
  3. Then you write or query command by writing it to the COM port and reading it back.
  4. To read back for querying you should write "++read eoi" before reading bytes from COM. This allows read until end of instruction is seen by the device.





For more details use the following manual:


DATASHEET






Python Code:




import serial
import sys


USAGE = '''
USB-GPIB.py: Utility to control GPIB instruments through Prologix
USB-GPIB interface.
'''


class spawn:

#instantiate new object for an instrument
def __init__(self,COM=3,BAUD=115200):
try:
self.ser = serial.Serial(COM,BAUD,timeout = 5)
except:
print"\a"
print"Exiting due to no connection with COM port. Check COM PORT!\n"
sys.exit(1)


#Select device to address
def SelectDevice(self,GPIBaddrs=9):
self.ser.write("++addr %d\r" %GPIBaddrs)


#write
def usbGPIB_write(self,cmd):
self.ser.write("%s\r"%cmd)

#Query
def usbGPIB_read(self,len):
#Read until eoi asserted 
self.ser.write("++read eoi\r")
read = self.ser.read(len).strip()
return read


if __name__ == "__main__":

#instantiate a gpib object
gpib = spawn(COM=3,BAUD=115200)

#Select GPIB device by mentioning the GPIB address
gpib.SelectDevice(9)

#Write on GPIB - Query Device ID
gpib.usbGPIB_write("*IDN?\r")

#Read bytes from GPIB  - Read back ID string
read_add= gpib.usbGPIB_read(80)

print read_add

#Select second GPIB device
gpib.SelectDevice(19)

gpib.usbGPIB_write("*IDN?\r")

read_add= gpib.usbGPIB_read(80)

print read_add




I guess the code is self explanatory. If you have any questions/suggestions please let me know.



No comments:

Post a Comment