コード例 #1
0
        private void CanController_MessageReceived(CanController sender, MessageReceivedEventArgs e)
        {
            var msgs = new GHIElectronics.TinyCLR.Devices.Can.CanMessage[e.Count];

            for (var i = 0; i < msgs.Length; i++)
            {
                msgs[i] = new GHIElectronics.TinyCLR.Devices.Can.CanMessage();
            }

            this.messageReceiveCount += sender.ReadMessages(msgs, 0, msgs.Length);

            for (var i = 0; i < msgs.Length; i++)
            {
                this.UpdateStatusText(ArbitrationId + msgs[i].ArbitrationId, true);
                this.UpdateStatusText(FdCanMode + msgs[i].FdCan, false);
                this.UpdateStatusText(ExtendedId + msgs[i].ExtendedId, false);
                this.UpdateStatusText(RTR + msgs[i].RemoteTransmissionRequest, false);
                this.UpdateStatusText(BitRateSwitch + msgs[i].BitRateSwitch, false);

                var dataText = string.Empty;

                for (var ii = 0; ii < 8; ii++)
                {
                    dataText += msgs[i].Data[ii] + " ";
                }

                this.UpdateStatusText(Data + dataText, false);
                this.UpdateStatusText(TotalReceived + this.messageReceiveCount, false);

                try {
                    msgs[i].ArbitrationId += 1;

                    sender.WriteMessage(msgs[i]);
                }
                catch {
                }
            }
        }
コード例 #2
0
        private bool DoTestCan()
        {
            this.AddNextButton();

            this.UpdateStatusText("Testing CAN1.", true);
            this.UpdateStatusText("- Open PCAN-View application.", false);
            this.UpdateStatusText("- Nominal speed: 250Kbit/s.", false);
            this.UpdateStatusText("- Data speed: 500Kbit/.", false);
            this.UpdateStatusText("- The test is waiting for any msg with arbitrationId 0x1234.", false);

            var canController = CanController.FromName(SC20260.CanBus.Can1);

            canController.SetNominalBitTiming(new GHIElectronics.TinyCLR.Devices.Can.CanBitTiming(15 + 8, 8, 6, 8, false)); // 250Kbit/s

            canController.SetDataBitTiming(new GHIElectronics.TinyCLR.Devices.Can.CanBitTiming(15 + 8, 8, 3, 8, false));    //500kbit/s

            canController.Enable();

            var message = new CanMessage()
            {
                ArbitrationId = 0x1234,
                ExtendedId    = true,
                FdCan         = true,
                BitRateSwitch = true,
                Data          = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 },
                Length        = 8
            };

            var result = false;

            while (this.doNext == false && this.isRunning)
            {
                if (canController.MessagesToRead == 0)
                {
                    try {
                        canController.WriteMessage(message);
                    }
                    catch {
                        canController.Disable();
                        Thread.Sleep(100);
                        canController.Enable();
                    }

                    Thread.Sleep(1000);

                    continue;
                }

                var msgs = new GHIElectronics.TinyCLR.Devices.Can.CanMessage[canController.MessagesToRead];

                for (var i = 0; i < msgs.Length; i++)
                {
                    msgs[i] = new GHIElectronics.TinyCLR.Devices.Can.CanMessage();
                }

                for (var i = 0; i < msgs.Length; i++)
                {
                    canController.ReadMessages(msgs, 0, msgs.Length);

                    if (msgs[i].ArbitrationId == message.ArbitrationId)
                    {
                        this.doNext = true;

                        result = true;
                        break;
                    }
                }

                if (result)
                {
                    break;
                }
            }

            canController.Disable();

            this.RemoveNextButton();

            return(result);
        }