/* * Adds the messages to the output box */ private void ProcessMessage(object sender, ProgressChangedEventArgs e) { if (e.ProgressPercentage == 0) { //DumpMessage dumpMessage = (DumpMessage)e.UserState; //DumpMessage(dumpMessage.id, dumpMessage.data, dumpMessage.dlc, dumpMessage.flags, dumpMessage.time); } else if (e.ProgressPercentage == 1)//接收指定ID报文 { DumpMessage dumpMessage = (DumpMessage)e.UserState; DumpMessage(dumpMessage.id, dumpMessage.idStr, dumpMessage.data, dumpMessage.dlc, dumpMessage.flags, dumpMessage.time); } else { CheckStatus("Reading", (Canlib.canStatus)e.UserState); } }
/* * Looks for messages and sends them to the output box. */ private void DumpMessageLoop(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; Canlib.canStatus status; int id; byte[] data = new byte[8]; int dlc; int flags; long time; bool noError = true; var msg = ""; //Open up a new handle for reading readHandle = Canlib.canOpenChannel(channel, Canlib.canOPEN_ACCEPT_VIRTUAL); status = Canlib.canBusOn(readHandle); while (noError && onBus && readHandle >= 0) { #region 接收指定ID报文 var message = new byte[8]; var idRS16 = Convert.ToInt32(idBox.Text, 16); var idRS = Convert.ToInt32(idBox.Text); status = Canlib.canReadSpecific(readHandle, idRS16, message, out int dlcRS, out int flagRS, out long timeRS); if (status == Canlib.canStatus.canOK) { DumpMessage dumpMessage = new DumpMessage(); if ((flagRS & Canlib.canMSG_ERROR_FRAME) == Canlib.canMSG_ERROR_FRAME) { //msgRS = "ERROR FRAME RECEIVED" + Environment.NewLine; } else { dumpMessage.id = idRS16; dumpMessage.idStr = idBox.Text; dumpMessage.dlc = dlcRS; dumpMessage.data = message; dumpMessage.flags = flagRS; dumpMessage.time = timeRS; } //Sends the message to the ProcessMessage method worker.ReportProgress(1, dumpMessage); } else if (status != Canlib.canStatus.canERR_NOMSG) { //Sends the error status to the ProcessMessage method and breaks the loop worker.ReportProgress(100, status); } #endregion #region 接收所有报文 //status = Canlib.canReadWait(readHandle, out id, data, out dlc, out flags, out time, 50); //if (status == Canlib.canStatus.canOK) //{ // DumpMessage dumpMessage = new DumpMessage(); // if ((flags & Canlib.canMSG_ERROR_FRAME) == Canlib.canMSG_ERROR_FRAME) // { // //msg = "***ERROR FRAME RECEIVED***" + Environment.NewLine; // } // else // { // dumpMessage.id = id; // dumpMessage.dlc = dlc; // dumpMessage.data = data; // dumpMessage.flags = flags; // dumpMessage.time = time; // } // //Sends the message to the ProcessMessage method // worker.ReportProgress(0, dumpMessage); //} //else if (status != Canlib.canStatus.canERR_NOMSG) //{ // //Sends the error status to the ProcessMessage method and breaks the loop // worker.ReportProgress(100, status); // noError = false; //} #endregion } Canlib.canBusOff(readHandle); }