Beispiel #1
0
    def _recv_internal(
            self, timeout: Optional[float]) -> Tuple[Optional[Message], bool]:
        end_time = time.time() + timeout if timeout is not None else None

        while True:
            try:
                if self.fd:
                    msg = self._recv_canfd()
                else:
                    msg = self._recv_can()

            except VectorOperationError as exception:
                if exception.error_code != xldefine.XL_Status.XL_ERR_QUEUE_IS_EMPTY:
                    raise
            else:
                if msg:
                    return msg, self._is_filtered

            # if no message was received, wait or return on timeout
            if end_time is not None and time.time() > end_time:
                return None, self._is_filtered

            if HAS_EVENTS:
                # Wait for receive event to occur
                if end_time is None:
                    time_left_ms = INFINITE
                else:
                    time_left = end_time - time.time()
                    time_left_ms = max(0, int(time_left * 1000))
                WaitForSingleObject(self.event_handle.value,
                                    time_left_ms)  # type: ignore
            else:
                # Wait a short time until we try again
                time.sleep(self.poll_interval)
Beispiel #2
0
    def recv(self, timeout=None):
        if HAS_EVENTS:
            # We will utilize events for the timeout handling
            timeout_ms = int(timeout * 1000) if timeout is not None else INFINITE
        elif timeout is not None:
            # Calculate max time
            end_time = timeout_clock() + timeout

        log.debug("Trying to read a msg")

        result = None
        while result is None:
            result = self.m_objPCANBasic.Read(self.m_PcanHandle)
            if result[0] == PCAN_ERROR_QRCVEMPTY:
                if HAS_EVENTS:
                    result = None
                    val = WaitForSingleObject(self._recv_event, timeout_ms)
                    if val != WAIT_OBJECT_0:
                        return None
                elif timeout is not None and timeout_clock() >= end_time:
                    return None
                else:
                    result = None
                    time.sleep(0.001)
            elif result[0] & (PCAN_ERROR_BUSLIGHT | PCAN_ERROR_BUSHEAVY):
                log.warning(self._get_formatted_error(result[0]))
                return None
            elif result[0] != PCAN_ERROR_OK:
                raise PcanError(self._get_formatted_error(result[0]))

        theMsg = result[1]
        itsTimeStamp = result[2]

        log.debug("Received a message")

        bIsRTR = (theMsg.MSGTYPE & PCAN_MESSAGE_RTR.value) == PCAN_MESSAGE_RTR.value
        bIsExt = (theMsg.MSGTYPE & PCAN_MESSAGE_EXTENDED.value) == PCAN_MESSAGE_EXTENDED.value

        if bIsExt:
            #rx_msg.id_type = ID_TYPE_EXTENDED
            log.debug("CAN: Extended")
        else:
            #rx_msg.id_type = ID_TYPE_STANDARD
            log.debug("CAN: Standard")

        dlc = theMsg.LEN
        timestamp = boottimeEpoch + ((itsTimeStamp.micros + (1000 * itsTimeStamp.millis)) / (1000.0 * 1000.0))

        rx_msg = Message(timestamp=timestamp,
                         arbitration_id=theMsg.ID,
                         extended_id=bIsExt,
                         is_remote_frame=bIsRTR,
                         dlc=dlc,
                         data=theMsg.DATA[:dlc])

        return rx_msg
    def WaitForMultipleObjects(events, wait_all, timeout):
        if not wait_all:
            raise NotImplementedError()

        for ev in events:
            res = WaitForSingleObject(ev, timeout)
            if res != WAIT_OBJECT_0:
                err = win32.GetLastError()
                msg = _overlapped.FormatMessage(err)
                raise WindowsError(err, msg)

        return WAIT_OBJECT_0
Beispiel #4
0
 def _wait_for_exit():
     WaitForSingleObject(evt, INFINITE)
     run_exit_tasks()
     ExitProcess(0)
Beispiel #5
0
    def _recv_internal(self, timeout):
        end_time = time.time() + timeout if timeout is not None else None

        if self.fd:
            event = vxlapi.XLcanRxEvent()
        else:
            event = vxlapi.XLevent()
            event_count = ctypes.c_uint()

        while True:
            if self.fd:
                try:
                    vxlapi.xlCanReceive(self.port_handle, event)
                except VectorError as exc:
                    if exc.error_code != vxlapi.XL_ERR_QUEUE_IS_EMPTY:
                        raise
                else:
                    if event.tag == vxlapi.XL_CAN_EV_TAG_RX_OK or event.tag == vxlapi.XL_CAN_EV_TAG_TX_OK:
                        msg_id = event.tagData.canRxOkMsg.canId
                        dlc = dlc2len(event.tagData.canRxOkMsg.dlc)
                        flags = event.tagData.canRxOkMsg.msgFlags
                        timestamp = event.timeStamp * 1e-9
                        channel = self.index_to_channel.get(event.chanIndex)
                        msg = Message(
                            timestamp=timestamp + self._time_offset,
                            arbitration_id=msg_id & 0x1FFFFFFF,
                            is_extended_id=bool(msg_id & vxlapi.XL_CAN_EXT_MSG_ID),
                            is_remote_frame=bool(flags & vxlapi.XL_CAN_RXMSG_FLAG_RTR),
                            is_error_frame=bool(flags & vxlapi.XL_CAN_RXMSG_FLAG_EF),
                            is_fd=bool(flags & vxlapi.XL_CAN_RXMSG_FLAG_EDL),
                            error_state_indicator=bool(flags & vxlapi.XL_CAN_RXMSG_FLAG_ESI),
                            bitrate_switch=bool(flags & vxlapi.XL_CAN_RXMSG_FLAG_BRS),
                            dlc=dlc,
                            data=event.tagData.canRxOkMsg.data[:dlc],
                            channel=channel)
                        return msg, self._is_filtered
            else:
                event_count.value = 1
                try:
                    vxlapi.xlReceive(self.port_handle, event_count, event)
                except VectorError as exc:
                    if exc.error_code != vxlapi.XL_ERR_QUEUE_IS_EMPTY:
                        raise
                else:
                    if event.tag == vxlapi.XL_RECEIVE_MSG:
                        msg_id = event.tagData.msg.id
                        dlc = event.tagData.msg.dlc
                        flags = event.tagData.msg.flags
                        timestamp = event.timeStamp * 1e-9
                        channel = self.index_to_channel.get(event.chanIndex)
                        msg = Message(
                            timestamp=timestamp + self._time_offset,
                            arbitration_id=msg_id & 0x1FFFFFFF,
                            is_extended_id=bool(msg_id & vxlapi.XL_CAN_EXT_MSG_ID),
                            is_remote_frame=bool(flags & vxlapi.XL_CAN_MSG_FLAG_REMOTE_FRAME),
                            is_error_frame=bool(flags & vxlapi.XL_CAN_MSG_FLAG_ERROR_FRAME),
                            is_fd=False,
                            dlc=dlc,
                            data=event.tagData.msg.data[:dlc],
                            channel=channel)
                        return msg, self._is_filtered

            if end_time is not None and time.time() > end_time:
                return None, self._is_filtered

            if HAS_EVENTS:
                # Wait for receive event to occur
                if timeout is None:
                    time_left_ms = INFINITE
                else:
                    time_left = end_time - time.time()
                    time_left_ms = max(0, int(time_left * 1000))
                WaitForSingleObject(self.event_handle.value, time_left_ms)
            else:
                # Wait a short time until we try again
                time.sleep(self.poll_interval)
Beispiel #6
0
    def _recv_internal(self, timeout):

        if HAS_EVENTS:
            # We will utilize events for the timeout handling
            timeout_ms = int(timeout *
                             1000) if timeout is not None else INFINITE
        elif timeout is not None:
            # Calculate max time
            end_time = timeout_clock() + timeout

        #log.debug("Trying to read a msg")

        result = None
        while result is None:
            if self.fd:
                result = self.m_objPCANBasic.ReadFD(self.m_PcanHandle)
            else:
                result = self.m_objPCANBasic.Read(self.m_PcanHandle)
            if result[0] == PCAN_ERROR_QRCVEMPTY:
                if HAS_EVENTS:
                    result = None
                    val = WaitForSingleObject(self._recv_event, timeout_ms)
                    if val != WAIT_OBJECT_0:
                        return None, False
                elif timeout is not None and timeout_clock() >= end_time:
                    return None, False
                else:
                    result = None
                    time.sleep(0.001)
            elif result[0] & (PCAN_ERROR_BUSLIGHT | PCAN_ERROR_BUSHEAVY):
                log.warning(self._get_formatted_error(result[0]))
                return None, False
            elif result[0] != PCAN_ERROR_OK:
                raise PcanError(self._get_formatted_error(result[0]))

        theMsg = result[1]
        itsTimeStamp = result[2]

        #log.debug("Received a message")

        is_extended_id = (
            theMsg.MSGTYPE
            & PCAN_MESSAGE_EXTENDED.value) == PCAN_MESSAGE_EXTENDED.value
        is_remote_frame = (theMsg.MSGTYPE
                           & PCAN_MESSAGE_RTR.value) == PCAN_MESSAGE_RTR.value
        is_fd = (theMsg.MSGTYPE
                 & PCAN_MESSAGE_FD.value) == PCAN_MESSAGE_FD.value
        bitrate_switch = (theMsg.MSGTYPE
                          & PCAN_MESSAGE_BRS.value) == PCAN_MESSAGE_BRS.value
        error_state_indicator = (
            theMsg.MSGTYPE & PCAN_MESSAGE_ESI.value) == PCAN_MESSAGE_ESI.value
        is_error_frame = (
            theMsg.MSGTYPE
            & PCAN_MESSAGE_ERRFRAME.value) == PCAN_MESSAGE_ERRFRAME.value

        if self.fd:
            dlc = dlc2len(theMsg.DLC)
            timestamp = boottimeEpoch + (itsTimeStamp.value /
                                         (1000.0 * 1000.0))
        else:
            dlc = theMsg.LEN
            timestamp = boottimeEpoch + (
                (itsTimeStamp.micros + 1000 * itsTimeStamp.millis +
                 0x100000000 * 1000 * itsTimeStamp.millis_overflow) /
                (1000.0 * 1000.0))

        rx_msg = Message(timestamp=timestamp,
                         arbitration_id=theMsg.ID,
                         is_extended_id=is_extended_id,
                         is_remote_frame=is_remote_frame,
                         is_error_frame=is_error_frame,
                         dlc=dlc,
                         data=theMsg.DATA[:dlc],
                         is_fd=is_fd,
                         bitrate_switch=bitrate_switch,
                         error_state_indicator=error_state_indicator)

        return rx_msg, False