Example #1
0
        def setup_serial(self, port_name=None):
            self.usb_send, self.usb_recv, self.usb_err = os.popen3(
                self.server_executable_path + (" %d" % -self.device_index),
                'b', -1)
            self.usb_timestamp = 0
            self.__usb_read_leftovers = ''
            try:
                firstmsg = ''
                badloops = 0
                while badloops < 5 and (not firstmsg
                                        or not firstmsg[-1] in '\r\n'):
                    try:
                        firstmsg += self.usb_err.readline(1000)
                    except:
                        traceback.print_exc()
                        badloops += 1

                if badloops == 5:
                    raise LabProError(
                        "cannot find or communicate with USB Server: " +
                        str(self.server_executable_path))
                if firstmsg.find("No LabPro Found") >= 0:
                    raise LabProError(
                        "USB Server could not connect to a LabPro device at index %d"
                        % self.device_index)

                self.__keep_running = 1
                self.status_monitor = threading.Thread(
                    target=self.read_status, name='USB LabPro status monitor')
                self.status_monitor.start()
                self.__saved_realtime_fragment = ''
            except:
                self.__keep_running = 0
                self.close()
                raise
Example #2
0
        def read(self, maxlen=None, mode=None):
            "read data from USB.  If mode is None or 0, strip trailing nulls for ASCII, otherwise leave alone"
            self.check_usb_status()
            res = ''

            db = self.__usb_read_leftovers

            while (not maxlen or (maxlen and len(res) < maxlen)):
                while len(
                        db
                ) < 76:  #64 bytes + 8 byte timestamp + 4 byte 0xffffffff flag
                    try:
                        db += self.usb_recv.read()
                    except IOError:
                        err = sys.exc_info()[1].args
                        if err[0] in (
                                11, 29, 35
                        ):  #these errors are sometimes returned on a nonblocking empty read
                            pass  #just return empty data
                        else:
                            print "USB server disconnected unexpectedly", err
                            raise LabProError(
                                "USB server disconnected unexpectedly",
                                sys.exc_info()[1].args)

                    if not db:
                        break  #no data at all, just fall out of this inner loop

                if not db:
                    break  #doing uncounted read, just take data until it quits coming

                flag, tv_sec, tv_usec = struct.unpack('LLL', db[:12])
                if flag != 0x00ffffff:
                    raise LabProError("Bad packet header from LabPro: " +
                                      ("%04x %08x %08x" %
                                       (flag, tv_sec, tv_usec)))
                self.data_timestamp = float(tv_sec) + float(tv_usec) * 1e-6
                res += db[12:76]
                db = db[76:]

            if not mode:
                zp = res.find('\0')
                if zp >= 0:
                    res = res[:zp]  #trim any nulls

            self.__usb_read_leftovers = db

            return res
Example #3
0
        def setup_serial(self, port_name=None):
            self.usb_send, self.usb_recv, self.usb_err = os.popen3(
                self.server_executable_path + (" %d" % -self.device_index),
                'b', 0)
            self.usb_timestamp = 0
            self.__usb_read_leftovers = ''
            try:
                # pipes must be nonblocking
                fcntl.fcntl(self.usb_recv, fcntl.F_SETFL, os.O_NONBLOCK)
                # pipes must be nonblocking
                fcntl.fcntl(self.usb_err, fcntl.F_SETFL, os.O_NONBLOCK)
                firstmsg = ''
                badloops = 0
                while badloops < 5 and not firstmsg:
                    time.sleep(0.5)
                    try:
                        firstmsg = self.usb_err.read()
                    except:
                        badloops += 1

                if badloops == 5:
                    raise LabProError(
                        "cannot find or communicate with USB Server: " +
                        str(self.server_executable_path))
                if firstmsg.find("No LabPro Found") >= 0:
                    raise LabProError(
                        "USB Server could not connect to a LabPro device at index %d"
                        % self.device_index)

                self.__keep_running = 1
                self.status_monitor = threading.Thread(
                    target=self.read_status, name='USB LabPro status monitor')
                self.status_monitor.start()
                self.__saved_realtime_fragment = ''
            except:
                self.__keep_running = 0
                self.close()
                raise
Example #4
0
        def read(self, maxlen=None, mode=None):
            """read data from USB.  If mode is None or 0, strip trailing nulls for ASCII, otherwise leave alone."""
            self.check_usb_status()
            res = ''
            db = self.__usb_read_leftovers

            while (not maxlen or len(res) < maxlen):
                # 64 bytes + 8 byte timestamp + 4 byte 0xffffffff flag
                while len(db) < 76:

                    # a pipe is a seek-and-tallable object, so we can see how much data
                    # is there this way
                    self.usb_recv.seek(0, 2)
                    count = self.usb_recv.tell()
                    self.usb_recv.seek(0)

                    if count:
                        db += self.usb_recv.read(count)
                    if not db:
                        break  # no data at all, just fall out of this inner loop

                if not db:
                    break  # doing uncounted read, just take data until it quits coming

                flag, tv_sec, tv_usec = struct.unpack('LLL', db[:12])
                if flag != 0x00ffffff:
                    raise LabProError("Bad packet header from LabPro: " +
                                      ("%04x %08x %08x" %
                                       (flag, tv_sec, tv_usec)))
                self.data_timestamp = float(tv_sec) + float(tv_usec) * 1e-6
                res += db[12:76]
                db = db[76:]

            if not mode:
                zp = res.find('\0')
                if zp >= 0:
                    res = res[:zp]  # trim any nulls

            self.__usb_read_leftovers = db
            return res
Example #5
0
 def check_usb_status(self):
     if not self.__keep_running or not self.status_monitor.isAlive():
         raise LabProError("LabPro USB server has died...")