/// <summary> /// /// </summary> /// <param name="queue"></param> /// <param name="msgInPacket"></param> /// <param name="buffer"></param> /// <param name="size"></param> internal static void QueueToMsgPacket(SafeQueue <CanMessage> queue, int msgInPacket, out byte[] buffer, out int size) { int items = queue.Count; CanMessage[] frames; if (items != 0) { if (items > msgInPacket) { frames = new CanMessage[msgInPacket]; for (int i = 0; i < msgInPacket; i++) { frames[i] = queue.Dequeue(); } } else { frames = new CanMessage[items]; for (int i = 0; i < items; i++) { frames[i] = queue.Dequeue(); } } buffer = new byte[64]; CanMessage.CanFramesToMsgPacket(frames, buffer, out size); } else { buffer = new byte[0]; size = 0; } }
/// <summary> /// /// </summary> /// <param name="iftAr"></param> void MsgPipeReadComplate(IAsyncResult iftAr) { int bytes = 0; CanMessage[] frames; //lock (LockObj) Ezt nem lehet lock-ni, mivel adatvesztés következik be.! { if (!this.isAbort) { try { bytes = Usb.Pipes[USB_MSG_IN_ADDR].EndRead(iftAr); } catch (Exception ex) { /*Itt valahogy jelezni kell, hogy ha hibábval ért véget a pipe olvasása*/ /*Ha itt hiba történt akkor vége.*/ LasException = ex; this.isAbort = true; } } if (bytes != 0) { CanMessage.MsgPacketToCanFrames(rxMsgPacketBuffer, bytes, out frames); for (int i = 0; i < frames.Length; i++) { RxCanMsgAbsTime = (frames[i].TimestampTick * 10000) + RxCanMsgAbsTime; frames[i].TimestampTick = RxCanMsgAbsTime; if (IncomingMsgQueue != null) { IncomingMsgQueue.Enqueue(frames[i]); } } if (_asyncReadBeginResult != null && !_asyncReadBeginResult.IsCompleted) { if (IncomingMsgQueue.Count >= _readBeginLength) { Read(_readBeginFrameArray, _readBeginOffset, _readBeginLength); _asyncReadBeginResult.OnCompletion(false, null, _readBeginLength, true); } } } if (!this.isAbort) { Usb.Pipes[USB_MSG_IN_ADDR].BeginRead(rxMsgPacketBuffer, 0, UsbMsgInEpSize, _asynReadCpltCallback, null); } } }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <returns></returns> public override bool Equals(object obj) { // If parameter is null return false. if (obj == null) { return(false); } // If parameter cannot be cast to MctComponentItem return false. CanMessage p = (CanMessage)obj; if ((System.Object)p == null) { return(false); } if (p.arbitrationId == this.arbitrationId && p.IsRemote == this.IsRemote && p.DataLength == this.DataLength) { if (p.data == null ^ this.data == null) { return(false); } else if (p.data == null && this.data == null) { return(true); } else if (p.Data.Length != this.Data.Length) { return(false); } else if (p.Data.SequenceEqual(this.Data)) { return(true); } else { return(false); } } else { return(false); } }
/// <summary> /// /// </summary> /// <param name="frames"></param> /// <param name="offset"></param> /// <param name="length"></param> internal void WriteToQueue(CanMessage[] frames, int offset, int length) { lock (LockObj) { for (int i = 0; i < length; i++) { OutgoingMsgQueue.Enqueue(frames[i + offset]); } try { int size; byte[] buffer; do { CanMessage.QueueToMsgPacket(OutgoingMsgQueue, MaxCanMsgInPacket, out buffer, out size); if (size != 0) { Usb.Pipes[USB_MSG_OUT_ADDR].Write(buffer, 0, size); } } while (OutgoingMsgQueue.Count != 0); } catch (WinUSB.USBException usbex) { if (usbex.InnerException.InnerException is System.ComponentModel.Win32Exception) { var i = usbex.InnerException.InnerException as System.ComponentModel.Win32Exception; if (i.NativeErrorCode == 121) { throw new CanAdapterException("Write timeout error. Code:-8609."); //Doc } else { throw new WinUSB.USBException(usbex.Message); } } else { throw new WinUSB.USBException(usbex.Message); } } } }
/// <summary> /// /// </summary> /// <param name="buffer"></param> /// <param name="size"></param> /// <param name="frames"></param> internal static void MsgPacketToCanFrames(byte[] buffer, int size, out CanMessage[] frames) { CanMessage[] list = null; int offset; byte alwaysZero; UInt16 alwaysOne; int msgCnt; if (size == 0) { frames = new CanMessage[0]; return; } offset = 0; msgCnt = buffer[offset]; list = new CanMessage[msgCnt]; offset += 1; alwaysZero = buffer[offset]; offset += 1; alwaysOne = (UInt16)BitConverter.ToInt16(buffer, offset); offset += 2; for (int i = 0; i < msgCnt; i++) { list[i].TwoByte = (UInt16)BitConverter.ToInt16(buffer, offset); offset += 2; list[i].TimestampTick = (UInt32)BitConverter.ToInt32(buffer, offset); offset += 4; list[i].ArbitrationId = (UInt32)BitConverter.ToInt32(buffer, offset); offset += 4; list[i].IsRemote = buffer[offset]; offset += 1; list[i].DataLength = buffer[offset]; offset += 1; list[i].Data = new byte[list[i].DataLength]; Buffer.BlockCopy(buffer, offset, list[i].Data, 0, list[i].DataLength); offset += 8; } frames = list; }