728x90

PCAN ISOTP API를 이용해서 데이터를 수신할 때 단순히 Read하는 것이 아니라 ISOTP Read 과정이 모두 완료 되었는지 확인하고 Read를 진행할 수 있는 방법이 있습니다.

int PCANISOTP::read_segmented_message(cantp_handle channel, int &nbMsgRead)
{
    cantp_msg rx_msg;
    cantp_msgprogress progress;     // ISO-TP 메시지가 다 들어왔는지 체크하기 위함

    memset(&progress, 0, sizeof(cantp_msgprogress));
    status = CANTP_Read_2016(channel, &rx_msg, NULL, PCANTP_MSGTYPE_NONE);

    if (CANTP_StatusIsOk_2016(status, PCANTP_STATUS_OK, false) ) {
        if (rx_msg.msgdata.any->flags & PCANTP_MSGFLAG_LOOPBACK) {
            return -1;
        } // loopback message의 경우 pass

        if (((PCANTP_MSGTYPE_ISOTP & rx_msg.type) == PCANTP_MSGTYPE_ISOTP)
            && ((rx_msg.msgdata.isotp->netaddrinfo.msgtype & PCANTP_ISOTP_MSGTYPE_FLAG_INDICATION_RX) == PCANTP_ISOTP_MSGTYPE_FLAG_INDICATION_RX)) {
            // The message is being received, wait and show progress
            do {
                status = CANTP_GetMsgProgress_2016(channel, &rx_msg, PCANTP_MSGDIRECTION_RX, &progress);
            } while (progress.state == PCANTP_MSGPROGRESS_STATE_PROCESSING);

            // The message is received, read it
            status = CANTP_Read_2016(channel, &rx_msg, NULL, PCANTP_MSGTYPE_NONE);
        } // 잔여 데이터가 있을 경우 계속 체크 후 다 들어왔다면 다시 한번 Read
        emit emit_sendISOTPPacketData(enRx, rx_msg);
    }

    return 1;
}

위 코드에서 cantp_msgprogress 구조체를 사용하는데 메시지 처리 상태를 확인할 수 있습니다. 진행 상태, 진행률 등을 볼 수 있으며 이 상태를 체크하면서 while로 state가 PCANTP_MSGPROGRESS_STATE_COMPLETED이 될 때 까지 기다립니다.

728x90

+ Recent posts