Ejemplo n.º 1
0
        public static void Main()
        {
            int  cnt        = 0;
            bool bootState  = false;
            bool txLedState = false;
            bool rxLedState = false;

            OutputPort txled   = new OutputPort(Pins.GPIO_PIN_D8, txLedState);
            OutputPort rxled   = new OutputPort(Pins.GPIO_PIN_D7, rxLedState);
            OutputPort bootLed = new OutputPort(Pins.ONBOARD_LED, bootState);

            // Blink the netduino led to indicate boot state.
            for (int i = 0; i < 3; i++)
            {
                bootLed.Write(bootState);
                Thread.Sleep(500);
                bootState = !bootState;
            }

            // Create the CAN Handler.
            MCP2515 CANHandler = new MCP2515();

            CANHandler.InitCAN(MCP2515.enBaudRate.CAN_BAUD_500K);
            // Set to normal operation mode.
            CANHandler.SetCANNormalMode();

            // Create standard TX message.
            MCP2515.CANMSG txMessage = new MCP2515.CANMSG();
            txMessage.data  = new byte[] { 0xCC, 0xAA, 0xAA, 0xAA, 0x11, 0x00, 0xFF, 0xFF };
            txMessage.CANID = 0x1AA;

            // Create extended TX message.
            MCP2515.CANMSG txMessageExt = new MCP2515.CANMSG();
            txMessageExt.CANID = 0x1FEDCBA1;
            txMessageExt.data  = new byte[] { 0x00, 0xEE };

            // Create the message that will hold received messages.
            MCP2515.CANMSG rxMessage = new MCP2515.CANMSG();

            while (true)
            {
                // Put a counter in the message.
                txMessage.data[6] = (byte)(cnt >> 8);
                txMessage.data[7] = (byte)cnt;
                // Transmit messages.
                CANHandler.Transmit(txMessage, 10);
                CANHandler.Transmit(txMessageExt, 10);
                // Blink CAN shield TX led every 5 cycles.
                txLedState = (cnt % 5 == 0) ? !txLedState : txLedState;
                txled.Write(txLedState);
                // Check if a message was received.
                if (CANHandler.Receive(out rxMessage, 20))
                {
                    rxled.Write(rxLedState);
                    if (rxMessage.IsExtended)
                    {
                        rxLedState = !rxLedState;
                    }
                    else
                    {
                    }
                }
                // Increase counter.
                cnt++;
            }
        }
Ejemplo n.º 2
0
        public static void Main()
        {
            txMessage.CANID   = 0x09;
            txMessage.data[0] = 0x11;
            txMessage.data[1] = 0x22;
            txMessage.data[2] = 0x33;
            txMessage.data[3] = 0x44;
            txMessage.data[4] = 0x55;
            txMessage.data[5] = 0x66;
            txMessage.data[6] = 0x77;
            txMessage.data[7] = 0x88;

            canCS.Write(false);
            Thread.Sleep(50);
            CANHandler.InitCAN(MCP2515.enBaudRate.CAN_BAUD_500K, filter, mask);
            Thread.Sleep(50);
            canCS.Write(true);
            Thread.Sleep(50);

            canCS.Write(false);
            Thread.Sleep(50);
            CANHandler.canReset();
            Thread.Sleep(50);
            canCS.Write(true);
            Thread.Sleep(50);

            // SEt baud rate
            //canCS.Write(false);
            //Thread.Sleep(50);
            //CANHandler.SetCANBaud(MCP2515.enBaudRate.CAN_BAUD_500K);
            //Thread.Sleep(50);
            //canCS.Write(true);
            //Thread.Sleep(50);

            // Baud rate commands split up so we can manually do chip select..
            canCS.Write(false);
            Thread.Sleep(50);
            CANHandler.baud1();
            Thread.Sleep(50);
            canCS.Write(true);
            Thread.Sleep(50);

            canCS.Write(false);
            Thread.Sleep(50);
            CANHandler.baud2();
            Thread.Sleep(50);
            canCS.Write(true);
            Thread.Sleep(50);

            canCS.Write(false);
            Thread.Sleep(50);
            CANHandler.baud3();
            Thread.Sleep(50);
            canCS.Write(true);
            Thread.Sleep(50);

            // Set normal mode
            canCS.Write(false);
            Thread.Sleep(50);
            CANHandler.SetCANNormalMode();
            canCS.Write(true);
            Thread.Sleep(50);

            while (true)
            {
                // The method with auto chip select
                //CANHandler.Transmit(txMessage, 100);

                // Now with manual chip select.
                canCS.Write(false);
                CANHandler.tId(txMessage);
                canCS.Write(true);

                canCS.Write(false);
                CANHandler.tRemote();
                canCS.Write(true);

                canCS.Write(false);
                CANHandler.tSend(txMessage);
                canCS.Write(true);

                canCS.Write(false);
                CANHandler.tTransmit();
                canCS.Write(true);

                Thread.Sleep(1000);
            }
        }