public void CanTp_ReceptionInd() { // Create new object containing received data XLClass.xl_event receivedEvent = new XLClass.xl_event(); // Result of XL Driver function calls XLDefine.XL_Status xlStatus = XLDefine.XL_Status.XL_SUCCESS; // Result values of WaitForSingleObject XLDefine.WaitResults waitResult = new XLDefine.WaitResults(); byte diagFrameType; byte[] diagPDU = new byte[1024]; int diagPDULength = 0, diagPDUoffset = 0; byte diagSequenceNum = 0; byte diagFlowState; // Note: this thread will be destroyed by MAIN while (true) { // Wait for hardware events waitResult = (XLDefine.WaitResults)WaitForSingleObject(eventHandle, 1000); if (waitFrameType == 0) { continue; } // If event occurred... if (waitResult != XLDefine.WaitResults.WAIT_TIMEOUT) { // ...init xlStatus first xlStatus = XLDefine.XL_Status.XL_SUCCESS; // afterwards: while hw queue is not empty... while (xlStatus != XLDefine.XL_Status.XL_ERR_QUEUE_IS_EMPTY) { // ...block RX thread to generate RX-Queue overflows while (blockRxThread) { Thread.Sleep(1000); } // ...receive data from hardware. xlStatus = CANTPDriver.XL_Receive(portHandle, ref receivedEvent); // If receiving succeed.... if (xlStatus == XLDefine.XL_Status.XL_SUCCESS) { Console.WriteLine(receivedEvent.tagData.can_Msg.id); if ((receivedEvent.flags & XLDefine.XL_MessageFlags.XL_EVENT_FLAG_OVERRUN) != 0) { Console.WriteLine("-- XL_EVENT_FLAG_OVERRUN --"); } // ...and data is a Rx msg... if (receivedEvent.tag == XLDefine.XL_EventTags.XL_RECEIVE_MSG) { if (receivedEvent.tagData.can_Msg.id == this.RxID) { Console.WriteLine(CANTPDriver.XL_GetEventString(receivedEvent)); diagFrameType = (byte)(receivedEvent.tagData.can_Msg.data[0] >> 4); switch (diagFrameType) { //Single Frame case 0: if (waitFrameType == 1) { diagPDULength = receivedEvent.tagData.can_Msg.data[0] & 0x0F; for (int i = 0; i < diagPDULength; i++) { diagPDU[i] = receivedEvent.tagData.can_Msg.data[i + 1]; } OnDiagPduReceived(diagPDU, diagPDULength); Console.WriteLine("PDU Length:" + diagPDULength); } break; //FirstFrame case 1: if (waitFrameType == 1) { diagPDULength = (receivedEvent.tagData.can_Msg.data[0] & 0x0F) * 256 + (receivedEvent.tagData.can_Msg.data[1]); for (int i = 0; i < 6; i++) { diagPDU[i] = receivedEvent.tagData.can_Msg.data[i + 2]; } diagPDUoffset = 6; diagSequenceNum = 0; CanTpSendFlowControlFrame(); waitFrameType = 2; Console.WriteLine("PDU Length:" + diagPDULength); } break; //ConsecutiveFrame case 2: if (waitFrameType == 2) { if (diagSequenceNum + 1 == (byte)(receivedEvent.tagData.can_Msg.data[0] & 0x0F)) { diagSequenceNum = (byte)(receivedEvent.tagData.can_Msg.data[0] & 0x0F); for (int i = diagPDUoffset; i < diagPDUoffset + 7; i++) { diagPDU[i] = receivedEvent.tagData.can_Msg.data[i + 1]; } if (diagPDUoffset == diagPDULength) { OnDiagPduReceived(diagPDU, diagPDULength); } } } break; //FlowControl case 3: if (waitFrameType == 3) { diagFlowState = (byte)(receivedEvent.tagData.can_Msg.data[0] & 0x0F); this.TxdiagBlockSize = receivedEvent.tagData.can_Msg.data[1]; this.TxdiagSTmin = receivedEvent.tagData.can_Msg.data[2]; waitforFlowControlEvent.Set(); } break; } } //if ((receivedEvent.tagData.can_Msg.flags & XLDefine.XL_MessageFlags.XL_CAN_MSG_FLAG_OVERRUN) != 0) //{ // Console.WriteLine("-- XL_CAN_MSG_FLAG_OVERRUN --"); //} //// ...check various flags //if ((receivedEvent.tagData.can_Msg.flags & XLDefine.XL_MessageFlags.XL_CAN_MSG_FLAG_ERROR_FRAME) // == XLDefine.XL_MessageFlags.XL_CAN_MSG_FLAG_ERROR_FRAME) //{ // Console.WriteLine("ERROR FRAME"); //} //else if ((receivedEvent.tagData.can_Msg.flags & XLDefine.XL_MessageFlags.XL_CAN_MSG_FLAG_REMOTE_FRAME) // == XLDefine.XL_MessageFlags.XL_CAN_MSG_FLAG_REMOTE_FRAME) //{ // Console.WriteLine("REMOTE FRAME"); //} //else //{ // Console.WriteLine(CANTPDriver.XL_GetEventString(receivedEvent)); //} } } } } // No event occurred } }
// ----------------------------------------------------------------------------------------------- /// <summary> /// EVENT THREAD (RX) /// /// RX thread waits for Vector interface events and displays filtered CAN messages. /// </summary> // ----------------------------------------------------------------------------------------------- public void RXThread() { // Create new object containing received data XLClass.xl_event receivedEvent = new XLClass.xl_event(); // Result of XL Driver function calls XLDefine.XL_Status xlStatus = XLDefine.XL_Status.XL_SUCCESS; // Result values of WaitForSingleObject XLDefine.WaitResults waitResult = new XLDefine.WaitResults(); // Note: this thread will be destroyed by MAIN while (true) { // Wait for hardware events //waitResult = (XLDefine.WaitResults)WaitForSingleObject(eventHandle, 1000); // If event occurred... if (waitResult != XLDefine.WaitResults.WAIT_TIMEOUT) { // ...init xlStatus first xlStatus = XLDefine.XL_Status.XL_SUCCESS; // afterwards: while hw queue is not empty... while (xlStatus != XLDefine.XL_Status.XL_ERR_QUEUE_IS_EMPTY) { // ...block RX thread to generate RX-Queue overflows while (blockRxThread) { Thread.Sleep(1000); } // ...receive data from hardware. xlStatus = CANDemo.XL_Receive(portHandle, ref receivedEvent); // If receiving succeed.... if (xlStatus == XLDefine.XL_Status.XL_SUCCESS) { if ((receivedEvent.flags & XLDefine.XL_MessageFlags.XL_EVENT_FLAG_OVERRUN) != 0) { Console.WriteLine("-- XL_EVENT_FLAG_OVERRUN --"); } // ...and data is a Rx msg... if (receivedEvent.tag == XLDefine.XL_EventTags.XL_RECEIVE_MSG) { if ((receivedEvent.tagData.can_Msg.flags & XLDefine.XL_MessageFlags.XL_CAN_MSG_FLAG_OVERRUN) != 0) { Console.WriteLine("-- XL_CAN_MSG_FLAG_OVERRUN --"); } // ...check various flags if ((receivedEvent.tagData.can_Msg.flags & XLDefine.XL_MessageFlags.XL_CAN_MSG_FLAG_ERROR_FRAME) == XLDefine.XL_MessageFlags.XL_CAN_MSG_FLAG_ERROR_FRAME) { Console.WriteLine("ERROR FRAME"); } else if ((receivedEvent.tagData.can_Msg.flags & XLDefine.XL_MessageFlags.XL_CAN_MSG_FLAG_REMOTE_FRAME) == XLDefine.XL_MessageFlags.XL_CAN_MSG_FLAG_REMOTE_FRAME) { Console.WriteLine("REMOTE FRAME"); } else { Console.WriteLine(CANDemo.XL_GetEventString(receivedEvent)); } } } } } // No event occurred } }