public __CanMsg ReadCan(short Ch, bool FDFlag = false) { __CanMsg Msg = new __CanMsg(); TPCANStatus stsResult; // We execute the "Read" function of the PCANBasic // Msg.DATA = new byte[8]; Msg.ID = -1; Msg.Length = 0; #if PROGRAM_RUNNING try { try { for (int i = 0; i < 8; i++) { Msg.DATA[i] = 0x00; } if (FDFlag == false) { TPCANMsg CANMsg; TPCANTimestamp CANTimeStamp; stsResult = PCANBasic.Read(m_PcanHandle[Ch], out CANMsg, out CANTimeStamp); if (stsResult == TPCANStatus.PCAN_ERROR_OK) { Msg.ID = (int)CANMsg.ID; Msg.Length = CANMsg.LEN; for (int i = 0; i < CANMsg.LEN; i++) { Msg.DATA[i] = CANMsg.DATA[i]; } if (NgCountCheckFlag == true) { if (OkCount < 100) { OkCount++; } } } if (mControl.PublicFunction != null) { Last = mControl.PublicFunction.timeGetTimems(); if (1000 <= (Last - First)) { CanReceiveReset(Ch); First = mControl.PublicFunction.timeGetTimems(); } } } else { TPCANMsgFD CANMsg; TPCANTimestampFD CANTimeStamp; // We execute the "Read" function of the PCANBasic // stsResult = PCANBasic.ReadFD(m_PcanHandle[Ch], out CANMsg, out CANTimeStamp); if (stsResult == TPCANStatus.PCAN_ERROR_OK) { Msg.ID = (int)CANMsg.ID; Msg.Length = CANMsg.DLC; for (int i = 0; i < CANMsg.DLC; i++) { Msg.DATA[i] = CANMsg.DATA[i]; } if (NgCountCheckFlag == true) { if (OkCount < 100) { OkCount++; } } } } } catch (Exception fMsg) { //MessageBox.Show(fMsg.Message + "\n" + fMsg.StackTrace); uMessageBox.Show(title: "경고", promptText: fMsg.Message + "\n" + fMsg.StackTrace); //MessageBox.Show(GetFormatedError(stsResult)); } } finally { } #endif return(Msg); }
unsafe private void receivingThread() { int i; int rx_index = 0; rxCANOBJLIST.Clear(); TPCANStatus stsResult = 0; while (true) { //arrCANOBJ[33000] = new VCI_CAN_OBJ(); do { if (Form_SetCANParam.form_setcan.checkCANFDMode.Checked == false)//非FD { TPCANMsg CANMsg; TPCANTimestamp CANTimeStamp; stsResult = PCANBasic.Read(PCAN_PARA1.PCANIndex, out CANMsg, out CANTimeStamp); //if (stsResult == TPCANStatus.PCAN_ERROR_OK) if (stsResult != TPCANStatus.PCAN_ERROR_QRCVEMPTY) { arrCANOBJ[rx_index].ID = CANMsg.ID; arrCANOBJ[rx_index].DataLen = CANMsg.LEN; if (((byte)CANMsg.MSGTYPE & (byte)0x02) == 0x02)//是扩展帧 { arrCANOBJ[rx_index].ExternFlag = 1; } else { arrCANOBJ[rx_index].ExternFlag = 0; } if (((byte)CANMsg.MSGTYPE & (byte)0x01) == 0x01)//是请求帧 { arrCANOBJ[rx_index].RemoteFlag = 1; } else { arrCANOBJ[rx_index].RemoteFlag = 0; } arrCANOBJ[rx_index].TimeStamp = Convert.ToUInt64(CANTimeStamp.micros + 1000 * CANTimeStamp.millis + 0x100000000 * 1000 * CANTimeStamp.millis_overflow); for (i = 0; i < CANMsg.LEN; i++)//原来用8固定长度 { arrCANOBJ[rx_index].Data[i] = CANMsg.DATA[i]; } rx_pkg_counts++; rx_index++; if (rx_index > 32768) { rx_index = 32767; } //Form_BasicFunction.form_basic.RXCountsLabel.Text = "Rx:" + rx_pkgcounts;//耗时2ms /* if (ProcessingFrame == null) * { * if (rxCANOBJLIST.Count >= 100000) * { * MessageBox.Show("设备" + (PCAN_PARA1.PCANIndex - 0x51) + "接收缓冲区已满,已自动清空"); * rxCANOBJLIST.Clear(); * } * rxCANOBJLIST.Add(arrCANOBJ[0]); * } * else * { * ProcessingFrame(ref arrCANOBJ[0]); * }*/ } if (stsResult == TPCANStatus.PCAN_ERROR_ILLOPERATION) { break; } //else } else { TPCANMsgFD CANMsg; TPCANTimestampFD CANTimeStamp; stsResult = PCANBasic.ReadFD(PCAN_PARA1.PCANIndex, out CANMsg, out CANTimeStamp); if (stsResult == TPCANStatus.PCAN_ERROR_ILLOPERATION) { break; } //if (stsResult == TPCANStatus.PCAN_ERROR_OK) if (stsResult != TPCANStatus.PCAN_ERROR_QRCVEMPTY) { arrCANOBJ[rx_index].ID = CANMsg.ID; arrCANOBJ[rx_index].DataLen = CANMsg.DLC; if (((byte)CANMsg.MSGTYPE & (byte)0x02) == 0x02)//是扩展帧 { arrCANOBJ[rx_index].ExternFlag = 1; } else { arrCANOBJ[rx_index].ExternFlag = 0; } if (((byte)CANMsg.MSGTYPE & (byte)0x01) == 0x01)//是请求帧 { arrCANOBJ[rx_index].RemoteFlag = 1; } else { arrCANOBJ[rx_index].RemoteFlag = 0; } if (((byte)CANMsg.MSGTYPE & (byte)0x04) == 0x04)//是FD帧 { arrCANOBJ[rx_index].FDflag = 1; } else { arrCANOBJ[rx_index].FDflag = 0; } if (((byte)CANMsg.MSGTYPE & (byte)0x08) == 0x08)//是FD-BRS { arrCANOBJ[rx_index].FDflag = 1; } else { arrCANOBJ[rx_index].FDflag = 0; } if (((byte)CANMsg.MSGTYPE & (byte)0x10) == 0x10)//是FD-ESI error status indicator { arrCANOBJ[rx_index].FDEsi = 1; } else { arrCANOBJ[rx_index].FDEsi = 0; } if (((byte)CANMsg.MSGTYPE & (byte)0x40) == 0x40)//是error frame { arrCANOBJ[rx_index].ErrorFrame = 1; } else { arrCANOBJ[rx_index].ErrorFrame = 0; } arrCANOBJ[rx_index].TimeStamp = CANTimeStamp; for (i = 0; i < CANMsg.DLC; i++)//原来用8固定长度 { arrCANOBJ[0].Data[i] = CANMsg.DATA[i]; } rx_pkg_counts++; rx_index++; if (rx_index > 32768) { rx_index = 32767; } // for (i = 0; i < len; i++) //ProcessingFrame(ref arrCANOBJ[0]); /*if (ProcessingFrame == null) * { * if (rxCANOBJLIST.Count >= 100000) * { * MessageBox.Show("设备" + (PCAN_PARA1.PCANIndex - 0x51) + "接收缓冲区已满,已自动清空"); * rxCANOBJLIST.Clear(); * } * rxCANOBJLIST.Add(arrCANOBJ[0]); * } * else * { * ProcessingFrame(ref arrCANOBJ[0]); * }*/ } //else } } while (!Convert.ToBoolean(stsResult & TPCANStatus.PCAN_ERROR_QRCVEMPTY)); if (rx_index > 0) { for (i = 0; i < rx_index; i++) { ProcessingFrame(ref arrCANOBJ[i]); Thread.Sleep(1); } rx_index = 0; } Thread.Sleep(2); } }