コード例 #1
0
ファイル: Channel.cs プロジェクト: elliotwoods/MuscleMemory
        public override void SetBitrate(int value)
        {
            var initConfig = new NativeFunctions.INIT_CONFIG();

            initConfig.AccCode = 0;
            initConfig.AccMask = 0xffffff;
            initConfig.Filter  = 0;

            switch (value)
            {
            case 1000000:
                initConfig.Timing0 = 0;
                initConfig.Timing1 = 0x14;
                break;

            case 800000:
                initConfig.Timing0 = 0;
                initConfig.Timing1 = 0x16;
                break;

            case 666000:
                initConfig.Timing0 = 0x80;
                initConfig.Timing1 = 0xb6;
                break;

            case 500000:
                initConfig.Timing0 = 0;
                initConfig.Timing1 = 0x1c;
                break;

            case 400000:
                initConfig.Timing0 = 0x80;
                initConfig.Timing1 = 0xfa;
                break;

            case 250000:
                initConfig.Timing0 = 0x01;
                initConfig.Timing1 = 0x1c;
                break;

            case 200000:
                initConfig.Timing0 = 0x81;
                initConfig.Timing1 = 0xfa;
                break;

            case 125000:
                initConfig.Timing0 = 0x03;
                initConfig.Timing1 = 0x1c;
                break;

            case 100000:
                initConfig.Timing0 = 0x04;
                initConfig.Timing1 = 0x1c;
                break;

            case 80000:
                initConfig.Timing0 = 0x83;
                initConfig.Timing1 = 0xff;
                break;

            case 50000:
                initConfig.Timing0 = 0x09;
                initConfig.Timing1 = 0x1c;
                break;

            default:
                throw (new Exception("Bitrate not supported"));
            }

            initConfig.Mode = 0;

            this.FDevice.PerformBlocking(() =>
            {
                NativeFunctions.ThrowIfError(
                    NativeFunctions.InitCAN(
                        this.FDevice.DeviceType
                        , this.FDevice.DeviceIndex
                        , this.FChannelIndex
                        , ref initConfig)

                    , this.FDevice.DeviceType
                    , this.FDevice.DeviceIndex
                    , this.FChannelIndex);;
            });
        }
コード例 #2
0
ファイル: Channel.cs プロジェクト: elliotwoods/MuscleMemory
        public void ThreadedUpdate()
        {
            // Send frames
            {
                var framesToSend = new List <NativeFunctions.CAN_OBJ>();

                // Pull them from the queue
                {
                    NativeFunctions.CAN_OBJ frame;
                    while (this.FTxQueue.TryTake(out frame))
                    {
                        framesToSend.Add(frame);
                    }
                }

                // Send them out
                if (framesToSend.Count > 0)
                {
                    var framesSent = NativeFunctions.Transmit(
                        this.FDevice.DeviceType
                        , this.FDevice.DeviceIndex
                        , this.FChannelIndex
                        , framesToSend.ToArray()
                        , (UInt16)framesToSend.Count);

                    // Queue again anything that didn't send
                    for (int i = (int)framesSent; i < framesToSend.Count; i++)
                    {
                        this.FTxQueue.Add(framesToSend[i]);
                    }
                }

                // Alternative method : force send everything
                //int txOffset = 0;
                //while(txOffset != framesToSend.Count)
                //{
                //	var framesSent = NativeFunctions.Transmit(
                //			this.FDevice.DeviceType
                //			, this.FDevice.DeviceIndex
                //			, this.FChannelIndex
                //			, framesToSend.GetRange(txOffset, framesToSend.Count - txOffset).ToArray()
                //			, (UInt16)framesToSend.Count);
                //	txOffset += (int) framesSent;
                //}
            }

            // Receive frames
            {
                var rxCountOnDevice = NativeFunctions.GetReceiveNum(this.FDevice.DeviceType
                                                                    , this.FDevice.DeviceIndex
                                                                    , this.FChannelIndex);

                if (rxCountOnDevice > 0)
                {
                    var rxBuffer = new NativeFunctions.CAN_OBJ[rxCountOnDevice];

                    var rxCount = NativeFunctions.ReceiveArray(this.FDevice.DeviceType
                                                               , this.FDevice.DeviceIndex
                                                               , this.FChannelIndex
                                                               , rxBuffer
                                                               , (UInt32)rxBuffer.Length
                                                               , 0);

                    for (int i = 0; i < rxCount; i++)
                    {
                        var nativeFrame = rxBuffer[i];

                        var frame = new Candle.Frame();
                        {
                            frame.Identifier = nativeFrame.ID;
                            frame.Extended   = nativeFrame.ExternFlag == 1;
                            frame.RTR        = nativeFrame.RemoteFlag == 1;
                            frame.Error      = false;                        // Not sure how we get error frames with GCAN

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

                            frame.Timestamp = nativeFrame.TimeStamp;
                        }

                        this.IncrementRx(frame.LengthOnBus);
                        this.FRxQueue.Add(frame);
                    }
                }
            }
        }