private void CanController_ErrorReceived(CanController sender, ErrorReceivedEventArgs e) { try { // Reset CAN sender.Disable(); Thread.Sleep(10); sender.Enable(); } catch { } }
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 } } }