Esempio n. 1
0
    def run(self):
        global frames
        global frameCounts
        global lastFramesByID

        frames = []
        frameCounts = {}
        lastFrameByID = {}

        startTime = getts()
        lastRefresh = startTime
        lastErrorCheck = startTime
        while not self._abort:
            # Check for bus errors once per second
            # if (getts()-lastErrorCheck) > 1.0:
            #     lastErrorCheck = getts()
            #     status_response = self._canusb.get_status_flags()

            #     print status_response

            #     if len(status_response) > 2:
            #         # status is 'F' followed by  2 bytes of hexadecimal BCD that represent the 8 bit status
            #         status = (int(chr(status_response[1]),16) << 4) + int(chr(status_response[2]),16)

            #         if status > 0:
            #             status_message = ""
            #             rxFifoFull = status & 0x01
            #             if rxFifoFull > 0:
            #                 status_message += " (RX Fifo full) "
            #             txFifoFull = status & 0x02
            #             if txFifoFull > 0:
            #                 status_message += " (TX Fifo full) "
            #             errorWarning = status & 0x04
            #             if errorWarning > 0:
            #                 status_message += " (Error Warning) "
            #             dataOverrun = status & 0x08
            #             if dataOverrun > 0:
            #                 status_message += " (Data Overrun) "
            #             errorPassive = status & 0x20
            #             if errorPassive > 0:
            #                 status_message += " (Error Passive) "
            #             arbitrationLost = status & 0x40
            #             if arbitrationLost > 0:
            #                 status_message += " (Arbitration Lost) "
            #             busError = status & 0x80
            #             if busError > 0:
            #                 status_message += " (Bus Error) "

            #             print "Bus status changed: 0x%x %s" % (status, status_message)

            # Check whether it is time to send scheduled frames
            # and insert scheduled frames in the tx queue
            if 0 == len(self._tx_frames):
                for f in self._tx_scheduled_frames:
                    self._tx_frames.appendleft(f)

            try:
                # is it time to send a scheduled frame
                if len(self._tx_frames) > 0:
                    # send any available frame, this will also read incoming frames
                    f = self._tx_frames.pop()
                    # r = self._canusb.transmit_frame(f)
                    # print "Send Frame ",str(f)
                    # print type(f.rtr)
                    # print "f.rtr",f.rtr
                    if f.rtr == 0:
                        tempframe = can.Frame(
                            f.msg_id,
                            f.ndata, [
                                f.data[0], f.data[1], f.data[2], f.data[3],
                                f.data[4], f.data[5], f.data[6], f.data[7]
                            ],
                            frame_type=can.FrameType.DataFrame,
                            is_extended_id=f.xtd)
                    elif f.rtr == 1:
                        tempframe = can.Frame(
                            f.msg_id,
                            f.ndata, [
                                f.data[0], f.data[1], f.data[2], f.data[3],
                                f.data[4], f.data[5], f.data[6], f.data[7]
                            ],
                            frame_type=can.FrameType.RemoteFrame,
                            is_extended_id=f.xtd)
                    self._canusb.send(tempframe)
                    # print "pop a frame"
                else:
                    # poll if there are no frames to send
                    # r = self._canusb.poll()
                    # print "poll a frame"
                    pass
            # except lawicel_canusb.CANUSBError, e:
            except Exception, e:
                # re-raise error if it's not timeout
                # if str(e).find("timeout") < 0:
                #     raise
                # else:
                #     continue
                print e
                print "There is some error"
            rawframe = self._canusb.recv_buff(8192)
            # rxfifoLen = self._canusb.get_rxfifo_len()
            rxfifoLen = len(rawframe)
            self._lock.acquire()
            if rxfifoLen != 0:
                for i in range(0, rxfifoLen):
                    # f = self._canusb.get_rx_frame()
                    tempf = rawframe[i]
                    # print "Get a Frame ",str(tempf)

                    f = CANMessage.CANFrame(msg_id=(tempf.id), xtd=tempf.is_extended_id, \
                        rtr=tempf.frame_type, ndata=tempf.dlc, \
                        data=(tempf.data[0],tempf.data[1],tempf.data[2],tempf.data[3],\
                            tempf.data[4],tempf.data[5],tempf.data[6],tempf.data[7]) )
                    print "f ", str(f)
                    frames.append(f)
                    if f.get_msg_id() not in frameCounts:
                        frameCounts[f.get_msg_id()] = 1
                    else:
                        frameCounts[
                            f.get_msg_id()] = frameCounts[f.get_msg_id()] + 1

                    lastFramesByID[f.get_msg_id()] = f
            self._lock.release()

            # notify that new frames have been received if a callback
            # was provided
            # Only notify 5 times/s or every 100 frames to avoid
            # over-loading the UI thread with events
            if rxfifoLen != 0:
                if (self._callback != None) and ((len(frames) % 100 == 0) or (
                    (getts() - lastRefresh) > 0.2)):
                    self._callback(len(frames))
                    lastRefresh = getts()
Esempio n. 2
0
            try:
                r = c.poll()
            except CANUSBError, e:
                # re-raise error if it's not timeout
                if str(e).find("timeout") < 0:
                    raise

            l = c.get_rxfifo_len()
            for i in range(0, l):
                f = c.get_rx_frame()
                print f
                count = count + 1

            if count > 10:
                strcount = str(i + count)
                databytes = map(ord, strcount)

                f = CANMessage.CANFrame(102, 1, 0, len(databytes),
                                        tuple([b for b in databytes]))
                c.transmit_frame(f)
#            if count >= 500:
#                duration = time.time()-startTime
#                rate = float(count) / duration
#                print "received %d frames in %f s (%f frames/s)" % (count, duration, rate)
#                startTime = time.time()
#                count = 0;
    except KeyboardInterrupt:
        pass
    finally:
        c.close_channel()