private void ThreadTest() { this.isRunning = true; 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.Filter.AddRangeFilter(Filter.IdType.Standard, 0x100, 0x7FF); canController.Filter.AddRangeFilter(Filter.IdType.Extended, 0x100, 0x999); canController.MessageReceived += this.CanController_MessageReceived; canController.ErrorReceived += this.CanController_ErrorReceived; canController.Enable(); while (this.isRunning) { Thread.Sleep(100); } this.isRunning = false; canController.Disable(); }
private static void Main() { var btn1 = GpioController.GetDefault().OpenPin(G80.GpioPin.PE0); btn1.SetDriveMode(GpioPinDriveMode.InputPullUp); var can = CanController.FromName(G80.CanBus.Can1); // Settings for 500kbps var propagation = 1; var phase1 = 12; var phase2 = 2; var baudratePrescaler = 6; var synchronizationJumpWidth = 1; var useMultiBitSampling = false; can.SetBitTiming(new CanBitTiming(propagation, phase1, phase2, baudratePrescaler, synchronizationJumpWidth, useMultiBitSampling)); can.Enable(); var message = new CanMessage() { Data = new byte[] { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 }, ArbitrationId = 0x99, Length = 6, IsRemoteTransmissionRequest = false, IsExtendedId = false }; can.MessageReceived += Can_MessageReceived; can.ErrorReceived += Can_ErrorReceived; while (true) { if (btn1.Read() == GpioPinValue.Low) { can.WriteMessage(message); } Thread.Sleep(100); } }
public static void TestCanSpi() { // Initialize CanSpi Click module on SC20260D socket #2 _canSpi = new CanSpiClick(Hardware.SC20260_2); if (_canSpi.Init("CAN#1", CanSpiClick.Baudrate500k, CanSpiClick.normalMode)) { Debug.WriteLine("CAN#1 @ 500kbps"); } else { throw new NotImplementedException("CanSpiClick initialization failed!"); } _canSpi.MessageReceived += CAN1_MessageReceived; // Initialize SC20260D onboard Can _onboardCan = CanController.FromName(STM32H7.CanBus.Can1); _onboardCan.SetNominalBitTiming(new CanBitTiming(propagationPhase1: 13, phase2: 2, baudratePrescaler: 6, synchronizationJumpWidth: 1, useMultiBitSampling: false)); _onboardCan.Enable(); _onboardCan.MessageReceived += Can_MessageReceived; _onboardCan.ErrorReceived += Can_ErrorReceived; CanSpiToOnboardCan(); OnboardCanToCanSpi(); }
static void Main() { //Instantiate the network class Network = new Network("192.168.181.210", "255.255.255.0", "192.168.181.1", "192.168.181.1", new byte[] { 0xA1, 0xA6, 0xB9, 0x3E, 0xD0, 0x1F }); //Initialize the network Network.InitializeNetwork(); //Create a UDP socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //Start the CAN at 250kbps can = CanController.FromName(SC20100.CanBus.Can1); can.SetNominalBitTiming(new CanBitTiming(13, 2, 12, 1, false)); can.ErrorReceived += Can_ErrorReceived; //Attach to the error event can.Enable(); //Enable the CAN peripheral new Thread(Print).Start(); //Start a thread that will print how many messages we processed last second //A byte array used in the loop byte[] data = new byte[8]; while (true) { //If we have messages to read if (can.MessagesToRead > 0) { //Read the message can.ReadMessage(out msg); msgCount++; //Increment the count Array.Copy(msg.Data, data, 8); //Copy the data so we only take the first 8 received bytes instead of all 64 //sock.SendTo(data, endPoint); //Send the data to the endpoint over the socket. REMOVE THIS LINE TO BE ABLE TO PROCESS AS MUCH MESSAGES AS CAN ALLOWS } } }
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); }