Ejemplo n.º 1
0
        public virtual void Open()
        {
            this.Close();

            if (!NativeFunctions.candle_dev_open(this.FDeviceHandle))
            {
                NativeFunctions.throwError(this.FDeviceHandle);
            }

            // Create queues
            this.FActionQueue    = new BlockingCollection <Action>();
            this.FExceptionQueue = new BlockingCollection <Exception>();

            // Flush Rx on device
            {
                NativeFunctions.candle_frame_t nativeFrame;
                while (NativeFunctions.candle_frame_read(this.FDeviceHandle
                                                         , out nativeFrame
                                                         , 0))
                {
                }
            }

            // Setup device thread
            this.FIsClosing   = false;
            this.FThread      = new Thread(this.ThreadedUpdate);
            this.FThread.Name = String.Format("Candle {0}", this.FInstanceIndex);

            // List Channels
            this.FChannels.Clear();
            byte channelCount;

            if (!NativeFunctions.candle_channel_count(this.FDeviceHandle, out channelCount))
            {
                NativeFunctions.throwError(this.FDeviceHandle);
            }
            for (byte i = 0; i < channelCount; i++)
            {
                this.FChannels.Add(i, new Channel(this, i));
            }

            // Start device thread
            this.FThread.Start();
        }
Ejemplo n.º 2
0
        void ThreadedUpdate()
        {
            while (!this.FIsClosing)
            {
                try
                {
                    // Rx frames
                    {
                        NativeFunctions.candle_frame_t nativeFrame;
                        if (NativeFunctions.candle_frame_read(this.FDeviceHandle
                                                              , out nativeFrame
                                                              , 0))
                        {
                            // Find the channel
                            if (this.FChannels.ContainsKey(nativeFrame.channel))
                            {
                                var frame = new Frame();

                                var flags = (NativeFunctions.candle_id_flags)(nativeFrame.can_id);
                                frame.Identifier = nativeFrame.can_id & ((1 << 29) - 1);
                                frame.Extended   = flags.HasFlag(NativeFunctions.candle_id_flags.CANDLE_ID_EXTENDED);
                                frame.RTR        = flags.HasFlag(NativeFunctions.candle_id_flags.CANDLE_ID_RTR);
                                frame.Error      = flags.HasFlag(NativeFunctions.candle_id_flags.CANDLE_ID_ERR);

                                frame.Data = new byte[nativeFrame.can_dlc];
                                Buffer.BlockCopy(nativeFrame.data, 0, frame.Data, 0, nativeFrame.can_dlc);

                                frame.Timestamp = nativeFrame.timestamp_us;

                                this.FChannels[nativeFrame.channel].NotifyReceive(frame);
                            }
                            else
                            {
                                throw (new Exception(String.Format("Cannot find channel {0}", nativeFrame.channel)));
                            }
                        }
                    }

                    // Perform actions
                    {
                        int count = 0;

                        Action action;
                        while (this.FActionQueue.TryTake(out action))
                        {
                            action();

                            if (count++ > 64)
                            {
                                // We have trouble when sending more than 92 message in a row
                                break;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.Write(e);
                }
            }
        }