def open(self):
        self._dev = d2xx.openEx(self._real_serial_number)

        # Recommended setup from Thorlabs APT Programming Guide
        self._dev.setBaudRate(d2xx.BAUD_115200)
        self._dev.setDataCharacteristics(d2xx.BITS_8, d2xx.STOP_BITS_1,
                                         d2xx.PARITY_NONE)
        time.sleep(50e-3)  # Wait 50 ms before and after purge
        self._dev.purge()  # Clear I/O queue
        time.sleep(50e-3)
        self._dev.resetDevice()
        self._dev.setRts()  # Assert the request-to-send line
    def open(self):
        self._dev = d2xx.openEx(self._real_serial_number)

        # Recommended setup from Thorlabs APT Programming Guide
        self._dev.setBaudRate(d2xx.BAUD_115200)
        self._dev.setDataCharacteristics(d2xx.BITS_8, d2xx.STOP_BITS_1,
            d2xx.PARITY_NONE)
        time.sleep(50e-3)  # Wait 50 ms before and after purge
        self._dev.purge()  # Clear I/O queue
        time.sleep(50e-3)
        self._dev.resetDevice()
        self._dev.setRts()  # Assert the request-to-send line
示例#3
0
def openBySerialNumber(serialNumber,
                       lowByteValue=MASK.SPI_S,
                       lowByteDirection=MASK.SPI_O):
    return Mpsse(d2xx.openEx(serialNumber, d2xx.OPEN_BY_SERIAL_NUMBER),
                 lowByteValue, lowByteDirection)
示例#4
0
def openBySerialNumber(serialNumber,
		lowByteValue = MASK.SPI_S,
		lowByteDirection = MASK.SPI_O):
    return Mpsse(d2xx.openEx(serialNumber, d2xx.OPEN_BY_SERIAL_NUMBER),
		lowByteValue, lowByteDirection)
示例#5
0
    def __init__(self, SN, device_type=None):
        print('Opening device %s:' % (str(SN)))
        self._ready = False
        self._debug = False
        self.port = d2xx.openEx(str(SN))
        self.info = self.port.getDeviceInfo()
        self.port.setTimeouts(1000, 1000)
        self.port.setBaudRate(d2xx.BAUD_115200)
        self.port.setDataCharacteristics(d2xx.BITS_8, d2xx.STOP_BITS_1,
                                         d2xx.PARITY_NONE)
        time.sleep(0.05)
        self.port.purge(d2xx.PURGE_RX | d2xx.PURGE_TX)
        time.sleep(0.05)
        self.port.setFlowControl(d2xx.FLOW_RTS_CTS, 0, 0)
        self.port.setRts()

        self.TXstack = []
        self.write_lock = threading.Lock()
        self.RXstack = []
        self.read_lock = threading.Lock()
        self._events = {}
        self._return_codes = {}
        self._channels = {}

        self.require_update = False

        self.device_type = device_type
        if self.device_type in APT_messages.return_codes and self.device_type in APT_messages.channels:
            self._channels = dict(
                zip(APT_messages.channels[self.device_type],
                    range(len(APT_messages.channels[self.device_type]))))
            self._channel_lookup = {v: k for k, v in self._channels.items()}

            self._return_codes = APT_messages.return_codes[self.device_type]
            for request_code in [
                    self._return_codes[key] for key in self._return_codes
            ]:
                for chan in self._channels:
                    self.__create_event(request_code, chan)

        class data_thread(threading.Thread):
            def __init__(self, device):
                threading.Thread.__init__(self)
                self.device = device
                self.abort_lock = threading.Lock()
                self.update_time = 0

            def run(self):
                while True:
                    if not self.abort_lock.acquire(False):
                        break
                    start_time = time.time()

                    time.sleep(0.001)
                    if len(self.device.TXstack) > 0:
                        try:
                            self.device.write_lock.acquire(True)
                            message = self.device.TXstack.pop()
                            self.device.write_lock.release()

                            TXchunk = APT_messages.encode_header(
                                message['header'])
                            if 'data' in message:
                                data_dict = APT_messages.data_type[
                                    message['header']['code']]
                                TXchunk += struct.pack(
                                    data_dict['format'], *[
                                        message['data'].get(key, 0)
                                        for key in data_dict['parameters']
                                    ])

                            self.device.port.write(TXchunk)
                        except Exception as ex:
                            if self.device._debug:
                                print(
                                    '(S/N %s) Error writing data to device: %s'
                                    % (self.device.info['serial'], ex))
                            break

                    time.sleep(0.001)
                    if self.device.port.getStatus()[0]:
                        try:
                            message = {}
                            RXchunk = self.device.port.read(6)

                            #print('RX chunk: %s' % RXchunk.encode('hex'))
                            message['header'] = APT_messages.decode_header(
                                RXchunk)
                            if message['header']['data_follows'] == True:
                                message['data'] = {}
                                data = self.device.port.read(
                                    message['header']['length'])
                                #print('   - Data: %s' % data)
                                data_dict = APT_messages.data_type.get(
                                    message['header']['code'], None)
                                if data_dict is not None:
                                    data_stripped = struct.unpack(
                                        data_dict['format'],
                                        data[:struct.
                                             calcsize(data_dict['format'])])
                                    for n, key in enumerate(
                                            data_dict['parameters']):
                                        if key is None:
                                            continue
                                        message['data'][key] = data_stripped[n]
                                else:
                                    message['data']['raw'] = data

                            self.device.read_lock.acquire(True)
                            self.device.RXstack.append(message)
                            self.device.read_lock.release()
                        except Exception as ex:
                            if self.device._debug:
                                print(
                                    '(S/N %s) Error reading data from device: %s'
                                    % (self.device.info['serial'], ex))
                            break

                    self.update_time = time.time() - start_time
                    self.abort_lock.release()
                self.abort_lock.release()
                print('   Thread stopped.')
                return

            def abort(self):
                self.abort_lock.acquire(True)

        class processing_thread(threading.Thread):
            def __init__(self, device):
                threading.Thread.__init__(self)
                self.device = device
                self.abort_lock = threading.Lock()

            def run(self):
                # DO NOT FORGET TO SET THE DEVICE TO READY
                while not self.device._ready:
                    pass

                while True:
                    if not self.abort_lock.acquire(False):
                        break
                    time.sleep(0.001)
                    if len(self.device.RXstack) > 0:
                        self.device.read_lock.acquire(True)
                        message = self.device.RXstack.pop()
                        self.device.read_lock.release()

                        header = message['header']
                        data = message.get('data', {})

                        if header['source'] in self.device._channels:
                            self.device._processRX(message)
                            self.device._set_event(header['code'],
                                                   header['source'])
                        else:
                            if self.device._debug:
                                print(
                                    '(S/N %s) Unrecognized message: %s - data: %s'
                                    %
                                    (self.device.info['serial'], header, data))
                    self.abort_lock.release()
                self.abort_lock.release()

            def abort(self):
                self.abort_lock.acquire(True)

        print('   Creating data & processing threads...')
        self._data_thread = data_thread(self)
        self._processing_thread = processing_thread(self)
        print('   Starting threads...')
        self._data_thread.start()
        self._processing_thread.start()
        print('   Device opened.')
示例#6
0
    def __init__(self, SN, device_type=None):
        print('Opening device %s:' % (str(SN)))
        self._ready = False
        self._debug = False
        self.port = d2xx.openEx(str(SN))
        self.info = self.port.getDeviceInfo()
        self.port.setTimeouts(1000,1000)
        self.port.setBaudRate(d2xx.BAUD_115200)
        self.port.setDataCharacteristics(d2xx.BITS_8, d2xx.STOP_BITS_1, d2xx.PARITY_NONE)
        time.sleep(0.05)
        self.port.purge(d2xx.PURGE_RX | d2xx.PURGE_TX)
        time.sleep(0.05)
        self.port.setFlowControl(d2xx.FLOW_RTS_CTS, 0, 0)
        self.port.setRts()
        
        self.TXstack = []
        self.write_lock = threading.Lock()
        self.RXstack = []        
        self.read_lock = threading.Lock()
        self._events = {}
        self._return_codes = {}
        self._channels = {}
        
        self.require_update = False
        
        self.device_type = device_type
        if self.device_type in APT_messages.return_codes and self.device_type in APT_messages.channels:
            self._channels = dict(zip(APT_messages.channels[self.device_type], 
                                      range(len(APT_messages.channels[self.device_type]))))
            self._channel_lookup = {v:k for k,v in self._channels.items()}
            
            self._return_codes = APT_messages.return_codes[self.device_type]
            for request_code in [self._return_codes[key] for key in self._return_codes]:
                for chan in self._channels:
                    self.__create_event(request_code, chan)
                        
        class data_thread(threading.Thread):
            def __init__(self, device):
                threading.Thread.__init__(self)
                self.device = device
                self.abort_lock = threading.Lock()
                self.update_time = 0
        
            def run(self):
                while True:
                    if not self.abort_lock.acquire(False):
                        break
                    start_time = time.time()
                    
                    time.sleep(0.001)
                    if len(self.device.TXstack) > 0:
                        try:
                            self.device.write_lock.acquire(True)
                            message = self.device.TXstack.pop()
                            self.device.write_lock.release()
                            
                            TXchunk = APT_messages.encode_header(message['header'])
                            if 'data' in message:
                                data_dict = APT_messages.data_type[message['header']['code']]
                                TXchunk += struct.pack(data_dict['format'], *[message['data'].get(key,0) for key in data_dict['parameters']])
                            
                            self.device.port.write(TXchunk)
                        except Exception as ex:
                            if self.device._debug:
                                print('(S/N %s) Error writing data to device: %s' % (self.device.info['serial'], ex))
                            break
                    
                    time.sleep(0.001)
                    if self.device.port.getStatus()[0]:                    
                        try:
                            message = {}
                            RXchunk = self.device.port.read(6)
                            
                            #print('RX chunk: %s' % RXchunk.encode('hex'))
                            message['header'] = APT_messages.decode_header(RXchunk)
                            if message['header']['data_follows'] == True:
                                message['data'] = {}
                                data = self.device.port.read(message['header']['length'])
                                #print('   - Data: %s' % data)
                                data_dict = APT_messages.data_type.get(message['header']['code'], None)
                                if data_dict is not None:
                                    data_stripped = struct.unpack(data_dict['format'], data[:struct.calcsize(data_dict['format'])])
                                    for n,key in enumerate(data_dict['parameters']):
                                        if key is None:
                                            continue
                                        message['data'][key] = data_stripped[n]                                        
                                else:
                                    message['data']['raw'] = data

                            self.device.read_lock.acquire(True)
                            self.device.RXstack.append(message)
                            self.device.read_lock.release()
                        except Exception as ex:
                            if self.device._debug:
                                print('(S/N %s) Error reading data from device: %s' % (self.device.info['serial'], ex))
                            break
                    
                    self.update_time = time.time() - start_time
                    self.abort_lock.release()
                self.abort_lock.release()
                print('   Thread stopped.')
                return
                
            def abort(self):
                self.abort_lock.acquire(True)
                
        class processing_thread(threading.Thread):
            def __init__(self, device):
                threading.Thread.__init__(self)
                self.device = device
                self.abort_lock = threading.Lock()
                
            def run(self):
                # DO NOT FORGET TO SET THE DEVICE TO READY
                while not self.device._ready:
                    pass
                
                while True:
                    if not self.abort_lock.acquire(False):
                        break
                    time.sleep(0.001)
                    if len(self.device.RXstack) > 0:
                        self.device.read_lock.acquire(True)
                        message = self.device.RXstack.pop()
                        self.device.read_lock.release() 
                        
                        header = message['header']
                        data = message.get('data', {})
                        
                        if header['source'] in self.device._channels:
                            self.device._processRX(message)
                            self.device._set_event(header['code'], header['source'])
                        else:
                            if self.device._debug:
                                print('(S/N %s) Unrecognized message: %s - data: %s' % (self.device.info['serial'], header, data))
                    self.abort_lock.release()
                self.abort_lock.release()
                
            def abort(self):
                self.abort_lock.acquire(True)
        
        print('   Creating data & processing threads...')
        self._data_thread = data_thread(self)
        self._processing_thread = processing_thread(self)
        print('   Starting threads...')
        self._data_thread.start()
        self._processing_thread.start()
        print('   Device opened.')
示例#7
0
文件: fcsendusb.py 项目: timbr/fcSend
    except getopt.GetoptError:
        # print help information and exit:
        usage()
        sys.exit(2)

    if  len(args) == 4:
        addr = int(args[0])
        cmd = int(args[1])
        dat1 = int(args[2])
        dat2 = int(args[3])
        cksum = (addr + cmd + dat1 + dat2) %256
        wbuffer = chr(0) + chr(0) + chr(addr) + chr(cmd) + chr(dat1) + chr(dat2) + chr(cksum) + chr(0) # filled out to 8 bytes
        
        #open the port
        try:
            h=d2xx.openEx('SEMSCA', d2xx.OPEN_BY_DESCRIPTION)
            h.setTimeouts(500, 500)
            h.setLatencyTimer(2)
            
        except:
            sys.stderr.write("Could not open port\n")
            sys.exit(1)

        h.write(wbuffer)

        print
        print '>>>Reply: ',

        returneddata = h.read(8)
        if returneddata[1] == '\x00': # If the second byte sent is not x00 then an error has occurred - see Firmware command list for details
            print [ord(byte) for byte in returneddata][2:6]