# HG changeset patch # User David Douard # Date 1197914385 -3600 # Node ID 78e3e839658ba4ee133053f6626a84468af91a86 # Parent a04bea92c5098f42fff1d083d44b6bc7a843f566 some forgotten added files diff -r a04bea92c509 -r 78e3e839658b HP3562A/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HP3562A/__init__.py Mon Dec 17 18:59:45 2007 +0100 @@ -0,0 +1,104 @@ +""" +HP3562A +======= + +Module for communicating with the HP 3562A Digital Signal Analyzer. + +Subpackages +----------- + + +Constants +--------- + +""" + +from gpib import gpib + +##################### +# HP3562A constants + +# GPIB buffer size is 3x80 characters lines +class STATUS_BYTE(gpib.Constants): + # HP3562A Status Byte, as returned by a serial poll + _constants = [(0x40, "RQS", "Request Service"), # when sent in response to a serial poll + (0x20, "ERR", "GPIB error"), + (0x10, "RDY", "ready to accept GPIB commands"), + ] + + conditions = [(0, "NSR", "No service requested"), + (1, "USRQ1", "User SRQ #1"), + (2, "USRQ1", "User SRQ #2"), + (3, "USRQ1", "User SRQ #3"), + (4, "USRQ1", "User SRQ #4"), + (5, "USRQ1", "User SRQ #5"), + (6, "USRQ1", "User SRQ #6"), + (7, "USRQ1", "User SRQ #7"), + (8, "USRQ1", "User SRQ #8"), + (9, "EOD", "End of disk action"), + (10, "EOP", "End of plot action"), + (11, "STCH", "Instrument status changed"), # any change in + # the status register sets this condition + (12, "PWR", "Power on"), + (13, "KEY", "Key pressed"), + (14, "DCP", "Device clear plotter (listen)"), + # ... + ] + def __init__(self): + super(STATUS_BYTE, self).__init__() + self._conditions = dict([(x[0], x[1]) for x in self.conditions]) + self._rev_conditions = dict([(x[1], x[0]) for x in self.conditions]) + self._long_conditions = dict([(x[0], x[2]) for x in self.conditions]) + + def byte_condition(self, byte): + byte = byte & 0x8F + return self._conditions.get(byte, "N/A") + +class IS_REGISTER(gpib.Constants): + _constants = [(0x01, "MEASP", "measeurement pause"), + (0x02, "ASQP", "Auto sequence pause"), + (0X04, "EOM", "End of measurement, capture or throughput"), + (0x08, "EOAS", "End of auto sequence"), + (0x10, "SWPR", "Sweep point ready"), + (0x20, "CH1OV", "Channel 1 overrange"), + (0x40, "CH2OV", "Channel 2 overrange"), + (0X80, "CH1HR", "Channel 1 half range"), + (0x100, "CH2HR", "Channel 2 half range"), + (0x200, "SFALT", "Source falt"), + (0x400, "RUNL", "Reference unlock"), + (0x800, "RMKT", "Remote marker knob turn"), + (0x1000, "REKT", "Remote entry knob turn"), + (0x2000, "ASRC", "Asctive Status Register changed"), + (0x4000, "PWRF", "Power-on test failed"), + ] + +class StatusQuery(gpib.Constants): + _command = "STA?" + _constants = [(0x01, "N/A", "Not used"), + (0x02, "N/A", "Not used"), + (0x04, "KEY", "Key pressed"), + (0x08, "N/A", "Not used"), + (0x10, "RDY", "Ready"), + (0x20, "ERR", "Error"), + (0x40, "RQS", "Request"), + (0x80, "MOS", "Message on screen"), + (0x100, "MEASP", "measeurement pause"), + (0x200, "ASQP", "Auto sequence pause"), + (0X400, "EOM", "End of measurement, capture or throughput"), + (0x800, "EOAS", "End of auto sequence"), + (0x1000, "SWPR", "Sweep point ready"), + (0x2000, "CH1OV", "Channel 1 overrange"), + (0x4000, "CH2OV", "Channel 2 overrange"), + (0x8000, "MAOV", "Math overflow"), + ] +class ActivityStatysRegister(gpib.Constants): + _command = "AS?" + _constants = [(0x01, "CKFL", "Check fault log"), + (0x02, "FITR", "Filling time record"), + (0x04, "FLTR", "Filters settings"), + (0x08, "CFTP", "Curve fir in progress"), + (0x10, "MSSM", "Missed sample"), + (0x20, "TMPR", "Timed preview"), + (0x40, "ACDA", "Accept date"), + #... + ] diff -r a04bea92c509 -r 78e3e839658b prologix.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/prologix.py Mon Dec 17 18:59:45 2007 +0100 @@ -0,0 +1,117 @@ +""" +Prologix +======== + +Module defining a communication object to talk to Prologix USB-GPIB controler. + +""" +import serial +import time + +import gpib +from gpib import ConnectionError + +GPIB_CONTROLLER = 1 +GPIB_DEVICE = 0 + +class GPIB(object): + _retries = 15 + def __init__(self, device="/dev/ttyUSB0", baudrate=115200, timeout=0.1, + address=0, mode=1): + """ + Create a new GPIB controller for the Prologix USB-GPIB device + located on serial device 'device'. + """ + self._cnx = serial.Serial(port=device, baudrate=baudrate, timeout=timeout) + self._timeout = timeout + + self.set_mode(mode) + self.set_address(address) + + def set_address(self, address): + """ + Set the address of the GPIB device: + + - if the device is the Controller In Charge, this is the + address of the device commands are sent to, + + - if the device is in GPIB_DEVICE mode, this is its address. + """ + self._set_cmd('addr', address) + self._adress = address + + def set_mode(self, mode): + """ + Set GPIB device mode to 'mode': + + - GPIB_CONTROLLER: set the device as the Controller In Charge + on the GPIB bus + + - GPIB_DEVICE: set the device as a standard GPIB device on the + bus. + """ + self._set_cmd('mode', mode) + self._mode = mode + + def set_controller(self): + """ + Set GPIB device the Controller In Charge on the GPIB bus. + """ + self.set_mode(1) + + def set_device(self): + """ + Set the GPIB device as a simple device on the GPIB bus. + """ + self.set_mode(0) + + def send_command(self, cmd): + """ + Send the specified GPIB command on the bus (must be the CIC), + and read the answer. + """ + assert self._mode == 1 + self._cnx.write(cmd+'\r') + time.sleep(self._timeout) # required? + ret = self._cnx.readlines() + return ''.join(ret) + + def check_srq(self): + """ + Check the SRQ line + """ + assert self._mode == 1, "must be the Controller In Charge" + self._cnx.write('++srq\r') + ret = self._cnx.readline().strip() + if ret: + return bool(int(ret)) + return None + + def poll(self): + """ + Poll every address, and return a dictionnary + {add: status, ...} + """ + assert self._mode == 1, "must be the Controller In Charge" + dico = {} + for add in range(31): + self._cnx.write('++spoll %d\r'%i) + ret = self._cnx.readline().strip() + if ret: + dico[i] = int(ret) + return dico + + def _read(self): + for i in range(self._retries): + rdata = self._cnx.readline() + if rdata.strip() != "": + break + time.sleep(self._timeout) + return rdata + + def _set_cmd(self, cmd, value): + self._cnx.write('++%s %d\r'%(cmd, value)) + self._cnx.write('++%s\r'%(cmd)) + rval = self._read().strip() + if not rval.isdigit() or int(rval) != value: + raise ConnectionError("Can't set GPIB %s to %s [ret=%s]"%(cmd, value, repr(rval)))