public static bool GenericLinReceive(CanData can) { Canlib.canStatus status; // status = Canlib.canRead(GenericCanBus.CAN_handle0, out can.id, can.msg, out can.dlc, out can.flags, out can.time); status = Canlib.canRead(can.handle, out can.id, can.msg, out can.dlc, out can.flags, out can.time); //Debug.WriteLine("InRead"); //MainWindow.ErrorDisplayString(CommonUtils.DisplayMsg(can)); if (status != Canlib.canStatus.canOK) { //Debug.WriteLine("CanReceive -- false"); return(false); } else { //Debug.WriteLine("CanReceive -- true"); return(true); } }
} // End GenericCanTransmitMultiple // CAN Receive public static bool CanReceive(CanData can) { // string hardwareString0 = can.hardware.Replace(" ", ""); // string[] msgOutput = hardwareString0.Split(';'); // to pull the can.handle from the hardware selection can.handle = BusInterface.ReturnHandle(can.hardware); //MainWindow.ErrorDisplayString("can.handle: " + can.handle + " ; msg: " + msgOutput[3]); // Kvaser CAN Read Array.Clear(can.msg, 0, 8); can.status = Canlib.canRead(can.handle, out can.id, can.msg, out can.dlc, out can.flags, out can.time); // MainWindow.ErrorDisplayString("can.handle in receive: " + can.handle); CheckFlags(can); if (can.status != Canlib.canStatus.canOK) { return(false); } else { return(true); } } // End Read
private void initLoop() { //ActiveCanDriver = Static_Utils.ActiveCANDriver; //if (ActiveCanDriver==Common.Consts.CAN_DRIVER_KVASER) { // if (runningKvaser) return; // runningKvaser = true; int faultedPackage = 0; int serialPortReadBytes = 0; Task.Factory.StartNew( action: () => { while (true) { if (ActiveCanDriver == Common.Consts.CAN_DRIVER_KVASER) { if (hasChangeInNodesList) { lock (LockCanIdsDic) { dic_Loop_CanChanels = dic_CanChanels.ToDictionary(x => x.Key, x => x.Value); hasChangeInNodesList = false; } } if (_writePackage.Count != 0 && sw.ElapsedMilliseconds >= Globals.sleepForRefresh) { lock (SyncRoot) { if (_writePackage[0] == null) { Canlib.canFlushReceiveQueue(CANHandle); } else if /*_writepackage[0] reference a nodeid that does't exist*/ ( !dic_Loop_CanChanels.ContainsKey(_writePackage[0].getInt(8))) { if (faultedPackage > 5) { _writePackage.RemoveAt(0); faultedPackage = 0; continue; } else { faultedPackage++; Thread.Sleep(5); continue; } } else { var pckt = _writePackage[0].Where((s, i) => i < 8).ToArray(); Canlib.canWrite( handle: dic_Loop_CanChanels[_writePackage[0].getInt(8)], id: FrequencyManager.MAGIC_NUMBER, msg: pckt, dlc: 8, /*size of array*/ flag: 2); } sw = Stopwatch.StartNew(); _writePackage.RemoveAt(0); } } foreach (var nodeId_x_handle in dic_Loop_CanChanels) { byte[] msgReceive = new byte[16]; int msgReceiveSize = 8; int flag = 2; int _id = 0; //(idArr[0] << 8) + (idArr[1]); long time = 100; Canlib.canStatus err; //err = Canlib.canRead(CANHandle, out _id, msgReceive, out msgReceiveSize, out flag, out time); err = Canlib.canRead( nodeId_x_handle.Value, out _id, msgReceive, out msgReceiveSize, out flag, out time); if (err != Canlib.canStatus.canOK) { if (_writePackage.Count == 0) { Thread.Sleep(10); } continue; } /*let everyone know wich nodeId sent this message*/ msgReceive.putInt(12, nodeId_x_handle.Key); innerTranmit(msgReceive); //Debug.WriteLine(msgReceive.ToArray()); } } else if (ActiveCanDriver == Common.Consts.INTERFACE_RS232) { if (_writePackage.Count != 0 && sw.ElapsedMilliseconds >= Globals.sleepForRefresh) { lock (SyncRoot) { } } } } }); //} else if (ActiveCanDriver == Common.Consts.INTERFACE_RS232) { } }
/// <summary> /// CAN读取接收的一帧数据 /// </summary> /// <param name="id"></param> /// <param name="dat"></param> /// <param name="dlc"></param> /// <param name="time"></param> /// <returns></returns> public bool ReadData(out int id, ref byte[] dat, out int dlc, out long time) { int flag = 0; return(Canlib.canRead(canHandler, out id, dat, out dlc, out flag, out time) == Canlib.canStatus.canOK); }
/* * Looks for messages and sends them to the output box. */ void Receiver_MessageHandlerLoop(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; Canlib.canStatus status = Canlib.canStatus.canERR_NOMSG; bool noError = true; int errorFrameCount = 0; //Create a Windows event handle Object winHandle = new IntPtr(-1); status = Canlib.canIoCtl(readHandle, Canlib.canIOCTL_GET_EVENTHANDLE, ref winHandle); if (status != Canlib.canStatus.canOK) { worker.ReportProgress((int)ReceiverReport.ERROR_STOP, status); noError = false; } status = Canlib.canAccept(readHandle, 0x610, Canlib.canFILTER_SET_CODE_STD); if (status != Canlib.canStatus.canOK) { worker.ReportProgress((int)ReceiverReport.ERROR_STOP, status); noError = false; } status = Canlib.canAccept(readHandle, 0xFFE, Canlib.canFILTER_SET_MASK_STD); if (status != Canlib.canStatus.canOK) { worker.ReportProgress((int)ReceiverReport.ERROR_STOP, status); noError = false; } WaitHandle waitHandle = (WaitHandle) new CanlibWaitEvent(winHandle); while (noError && isReaderBusOn && readHandle >= 0) { //Wait for 25ms for an event to occur on the channel (BBMS CAN communication period: 25ms) bool eventHappened = waitHandle.WaitOne(25); if (!eventHappened) { continue; } int id; byte[] data = new byte[Defined.MAX_DLC]; int dlc; int flags; long time; string msgLog; //To attain the initial msg status = Canlib.canRead(readHandle, out id, data, out dlc, out flags, out time); //This while loop is to repeat canRead() until the msg boxes for readHandle becomes exhausted. while (status == Canlib.canStatus.canOK) { if ((flags & Canlib.canMSG_ERROR_FRAME) == Canlib.canMSG_ERROR_FRAME) { msgLog = "***ERROR FRAME RECEIVED*** Count = " + (++errorFrameCount); if (errorFrameCount > Defined.ERROR_FRAME_ENDURANCE_COUNT_LIMIT) { worker.ReportProgress((int)ReceiverReport.REPORT_RUNNING, "Too many error frames received. Bus off."); break; } } else { msgLog = String.Format("0x{0:x3} {1} {2:x2} {3:x2} {4:x2} {5:x2} {6:x2} {7:x2} {8:x2} {9:x2} @{10}", id, dlc, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], time); } //Sends the message to the ProcessMessage method worker.ReportProgress((int)ReceiverReport.REPORT_RUNNING, "[Rx] " + msgLog); //Result status of the following canRead() will finish the while loop if status!=Canlib.canStatus.canOK status = Canlib.canRead(readHandle, out id, data, out dlc, out flags, out time); } //If the reason to exit the while loop of canRead() was not canERR_NOMSG, an error is expected. if (status != Canlib.canStatus.canERR_NOMSG) { //Sends the error status to the ProcessMessage method and breaks the loop worker.ReportProgress((int)ReceiverReport.ERROR_STOP, status); noError = false; } } if (isReaderBusOn) { worker.ReportProgress((int)ReceiverReport.FORCED_STOP, "Force Bus Off"); } }