using System;using System.IO;using System.IO.Ports;using System.Text;using System.Threading;using System.Collections;using Microsoft.SPOT;using Microsoft.SPOT.IO;using Microsoft.SPOT.Hardware;using TiroCore.SPOT.Hardware;namespace CAN_Bus{ public class Program { public static void Main() { //This static function must to be called once before usage CAN.Initialize(); #region CAN1_Configuration //Create the configuration for CAN at speed 1 Mbps CAN.Configuration _CAN_Configuration_CAN1 = new CAN.Configuration() { Prescaler = 2, Mode = CAN.CAN_Mode.Normal, SJW = CAN.CAN_TimeQuantum.TimeQuantum1, BS1 = CAN.CAN_TimeQuantum.TimeQuantum8, BS2 = CAN.CAN_TimeQuantum.TimeQuantum12, TTCM = false, ABOM = false, NART = false, RFLM = false, TXFP = false }; //Create the filter configuration for CAN CAN.FilterConfiguration _CAN_FilterConfiguration_CAN1 = new CAN.FilterConfiguration() { Number = 0, Mode = CAN.CAN_FilterMode.IdMask, FIFO = CAN.CAN_FIFO.FIFO0, Scale = CAN.CAN_FilterScale.FilterScale_32Bit, IdHeigh = 0x0000, IdLow = 0x0000, MaskHeigh = 0x0000, MaskLow = 0x0000, Activation = true }; //Create the reusable transmit message for CAN CAN.Message _CAN_Message_Transmit_CAN1 = new CAN.Message() { StandardId = 0x321, ExtendedId = 0x01, RTR = CAN.CAN_RemoteTransmissionRequest.Data, IDE = CAN.CAN_IdentifierType.Standard, DLC = 8, Data = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 } }; //Create the CAN bus CAN _CAN_CAN1 = new CAN(CAN.CAN_Device.CAN1, _CAN_Configuration_CAN1); //Binding the receive event to the CAN bus _CAN_CAN1.OnReceive += CAN_OnReceive; //Open the CAN bus _CAN_CAN1.Open(); //Set the filter configuration _CAN_CAN1.SetFilter(_CAN_FilterConfiguration_CAN1); //Enable FIFO0 receive interrupt for CAN bus _CAN_CAN1.SetInterruptState(CAN.CAN_Interrupt.FMP0, true); #endregion #region CAN2_Configuration //Create the configuration for CAN at speed 1 Mbps CAN.Configuration _CAN_Configuration_CAN2 = new CAN.Configuration() { Prescaler = 2, Mode = CAN.CAN_Mode.Normal, SJW = CAN.CAN_TimeQuantum.TimeQuantum1, BS1 = CAN.CAN_TimeQuantum.TimeQuantum8, BS2 = CAN.CAN_TimeQuantum.TimeQuantum12, TTCM = false, ABOM = false, NART = false, RFLM = false, TXFP = false }; //Create the filter configuration for CAN CAN.FilterConfiguration _CAN_FilterConfiguration_CAN2 = new CAN.FilterConfiguration() { Number = 14, Mode = CAN.CAN_FilterMode.IdMask, FIFO = CAN.CAN_FIFO.FIFO0, Scale = CAN.CAN_FilterScale.FilterScale_32Bit, IdHeigh = 0x0000, IdLow = 0x0000, MaskHeigh = 0x0000, MaskLow = 0x0000, Activation = true }; //Create the reusable transmit message for CAN CAN.Message _CAN_Message_Transmit_CAN2 = new CAN.Message() { StandardId = 0x123, ExtendedId = 0x01, RTR = CAN.CAN_RemoteTransmissionRequest.Data, IDE = CAN.CAN_IdentifierType.Standard, DLC = 8, Data = new byte[] { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 } }; //Create the CAN bus CAN _CAN_CAN2 = new CAN(CAN.CAN_Device.CAN2, _CAN_Configuration_CAN2); //Binding the receive event to the CAN bus _CAN_CAN2.OnReceive += CAN_OnReceive; //Open the CAN bus _CAN_CAN2.Open(); //Set the filter configuration _CAN_CAN2.SetFilter(_CAN_FilterConfiguration_CAN2); //Enable FIFO0 receive interrupt for CAN bus _CAN_CAN2.SetInterruptState(CAN.CAN_Interrupt.FMP0, true); #endregion while (true) { _CAN_CAN1.Transmit(_CAN_Message_Transmit_CAN1); _CAN_CAN2.Transmit(_CAN_Message_Transmit_CAN2); _CAN_Message_Transmit_CAN1.Data[0]++; _CAN_Message_Transmit_CAN2.Data[7]++; Thread.Sleep(1000); } } static void CAN_OnReceive(CAN.InterruptEventArgs args, DateTime time) { if (args.Message != null) { Debug.Print("CAN" + (args.CAN.Device + 1).ToString() + " Data Received:" + "{" + "0x" + args.Message.Data[0].ToString("X2") + "," + "0x" + args.Message.Data[1].ToString("X2") + "," + "0x" + args.Message.Data[2].ToString("X2") + "," + "0x" + args.Message.Data[3].ToString("X2") + "," + "0x" + args.Message.Data[4].ToString("X2") + "," + "0x" + args.Message.Data[5].ToString("X2") + "," + "0x" + args.Message.Data[6].ToString("X2") + "," + "0x" + args.Message.Data[7].ToString("X2") + "} Length:" + args.Message.DLC.ToString() + " RAM:" + Debug.GC(false).ToString()); } } }}
Attached Files
- blackt1ger likes this