def cmd_serial(self, cmd_str):
        self.cmd_str = cmd_str
        try:
            if self.conn is None:
                raise dcsim.DCSimError('Communications port to power supply not open')

            self.conn.flushInput()
            self.conn.write(cmd_str)
        except Exception, e:
             raise dcsim.DCSimError(str(e))
 def cmd(self, cmd_str):
     self.cmd_str = cmd_str
     # self.ts.log_debug('cmd_str = %s' % cmd_str)
     try:
         self._cmd(cmd_str)
         resp = self._query('SYSTem:ERRor?\n') #\r
         #self.ts.log_debug('error resp = %s' % resp)
         if len(resp) > 0:
             if resp[0] != '0':
                 raise dcsim.DCSimError(resp + ' ' + self.cmd_str)
     except Exception, e:
         raise dcsim.DCSimError(str(e))
 def cmd_usb(self, cmd_str):
     try:
         if self.conn is None:
             self.ts.log('USB device = %s' % self.usb_name)
             rm = visa.ResourceManager()
             self.conn = rm.open_resource(self.usb_name)
         #self.conn.write('*RST\n')
         self.conn.write('*CLS\n')
         self.conn.write(cmd_str)
     except Exception, e:
         raise dcsim.DCSimError(str(e))
    def open(self):
        """
        Open the communications resources associated with the grid simulator.
        """
        try:
            self.conn = serial.Serial(port=self.serial_port, baudrate=self.baudrate, bytesize=8, stopbits=1,
                                           xonxoff=0, timeout=self.timeout, writeTimeout=self.write_timeout)

            time.sleep(2)
            #self.cmd('CONFigure:REMote ON\n')
        except Exception, e:
            raise dcsim.DCSimError(str(e))
    def query_usb(self, cmd_str):
        # setup connection if not already established.
        try:
            if self.conn is None:
                self.ts.log('USB device = %s' % self.usb_name)
                rm = visa.ResourceManager()
                self.conn = rm.open_resource(self.usb_name)
            resp = self.conn.query(cmd_str)
            #self.ts.log_debug('cmd_str = %s, resp = %s' % (cmd_str, resp))

        except Exception, e:
            raise dcsim.DCSimError('Timeout waiting for response')
    def cmd_tcp(self, cmd_str):
        try:
            if self.conn is None:
                self.ts.log('ipaddr = %s  ipport = %s' % (self.ipaddr, self.ipport))
                self.conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                self.conn.settimeout(self.timeout)
                self.conn.connect((self.ipaddr, self.ipport))

            # print 'cmd> %s' % (cmd_str)
            self.conn.send(cmd_str)
        except Exception, e:
            raise dcsim.DCSimError(str(e))
    def query_serial(self, cmd_str):
        resp = ''
        more_data = True

        self.cmd_serial(cmd_str)

        while more_data:
            try:
                count = self.conn.inWaiting()
                if count < 1:
                    count = 1
                data = self.conn.read(count)
                if len(data) > 0:
                    for d in data:
                        resp += d
                        if d == '\n':
                            more_data = False
                            break
                else:
                    raise dcsim.DCSimError('Timeout waiting for response')
            except dcsim.DCSimError:
                raise
            except Exception, e:
                raise dcsim.DCSimError('Timeout waiting for response - More data problem')
    def query_tcp(self, cmd_str):
        resp = ''
        more_data = True

        self._cmd(cmd_str)

        while more_data:
            try:
                data = self.conn.recv(self.buffer_size)
                if len(data) > 0:
                    for d in data:
                        resp += d
                        if d == '\n': #\r
                            more_data = False
                            break
            except Exception, e:
                raise dcsim.DCSimError('Timeout waiting for response')
 def query(self, cmd_str):
     # self.ts.log_debug('query cmd_str = %s' % cmd_str)
     try:
         resp = self._query(cmd_str).strip()
     except Exception, e:
         raise dcsim.DCSimError(str(e))