/// <summary>
        /// Opens a new CAN bus connection.
        /// </summary>
        public void Open()
        {
            LasException = null;

            if (!IsConnected)
            {
                throw new CanAdapterException("Adapter is Disconnected. Code:-8604."); //Doc
            }
            if (!_isOpen)
            {
                IncomingMsgQueue = new SafeQueue <CanMessage>();
                OutgoingMsgQueue = new SafeQueue <CanMessage>();

                _isOpen = true;
                isAbort = false;
                _asyncReadBeginResult = null;

                Services.Start();

                if (Attributes.State != CanState.SDEV_START)
                {
                    throw new CanAdapterException("Unable to open the bus. Code:-8607."); //Doc
                }
                Usb.Pipes[USB_MSG_IN_ADDR].Flush();

                _asynReadCpltCallback = new AsyncCallback(MsgPipeReadComplate);
                _asyncReadResult      = Usb.Pipes[USB_MSG_IN_ADDR].BeginRead(rxMsgPacketBuffer, 0, UsbMsgInEpSize, _asynReadCpltCallback, Usb);
            }
            else
            {
                throw new CanAdapterException("The bus is already opened. Code:-8608."); //Doc
            }
        }
Exemple #2
0
        /// <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>
        /// Closes the CAN connection, sets the IsOpen property to false, and disposes of the internal Stream object.
        /// </summary>
        public void Close()
        {
            if (_isOpen)
            {
                try
                {
                    isAbort = true;

                    Usb.Pipes[USB_MSG_IN_ADDR].Abort();
                    IncomingMsgQueue.Clear();
                    OutgoingMsgQueue.Clear();
                    _isOpen = false;
                    Services.Reset();
                    IncomingMsgQueue = null;
                    OutgoingMsgQueue = null;
                }
                catch { };
            }
        }