Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Does any one needs CAN Bus on Netduino?


  • Please log in to reply
15 replies to this topic

#1 weixiongmei

weixiongmei

    Advanced Member

  • Members
  • PipPipPip
  • 68 posts
  • LocationPhiladelphia, Pennsylvania, United States

Posted 19 November 2013 - 09:47 PM

 If so,i can port it from my board to Netduino,and here's the sample code how to use the CAN bus in .Net Micro Framework.

 

QQ??20131119165105.jpg

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



#2 Juzzer

Juzzer

    Advanced Member

  • Members
  • PipPipPip
  • 135 posts
  • LocationHertfordshire, UK

Posted 20 November 2013 - 10:07 AM

I would be interested :)



#3 stotech

stotech

    Advanced Member

  • Members
  • PipPipPip
  • 143 posts
  • LocationAustralia

Posted 20 November 2013 - 11:55 AM

I'm interested as well.

 

What are the hardware requirements?

 

Are you planning to sell it the code or a hardware solution?

 

Is the hardware the same as an rs485 solution/shield?



#4 weixiongmei

weixiongmei

    Advanced Member

  • Members
  • PipPipPip
  • 68 posts
  • LocationPhiladelphia, Pennsylvania, United States

Posted 20 November 2013 - 12:02 PM

Hi,Grant,the hardware requirement is a STM32F4 series microcontroller,the netduino can has this feature.

 

Are you planning to sell it the code or a hardware solution?,To your this question,i can not answer it in this forum,if you really want to know,you can sent me a private message.

 

Is the hardware the same as an rs485 solution/shield?,No,it uses the internal hardware CAN bus,it's different to the rs485,the CAN bus is much better than rs485.

 

And im sure i will port the CAN driver to netduino for free if there are many people need it.(But not open source at this moment).Thankyou for you replied.



#5 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 20 November 2013 - 12:55 PM

Hi,Grant,the hardware requirement is a STM32F4 series microcontroller,the netduino can has this feature.

 

Are you planning to sell it the code or a hardware solution?,To your this question,i can not answer it in this forum,if you really want to know,you can sent me a private message.

 

Is the hardware the same as an rs485 solution/shield?,No,it uses the internal hardware CAN bus,it's different to the rs485,the CAN bus is much better than rs485.

 

And im sure i will port the CAN driver to netduino for free if there are many people need it.(But not open source at this moment).Thankyou for you replied.

 

Nice job.

However, "much better than RS485" is something relative. A typical RS485 hardware interface is cheaper and simpler than the CAN way. Moreover, the RS485 can be easily opto-isolated, but the CAN does not.

Their speed limitation is pretty similar, but on medium/long-running cabling the galvanic opto-isolation is mandatory (for a reliable life of the system).

 

CAN bus is beating RS485 in small-run networks, where the noise is typically prohibitive: a car is the most famous context.

 

They're simply different: I wouldn't say one is beating the other in a absolute way.

Ciao


Biggest fault of Netduino? It runs by electricity.

#6 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 20 November 2013 - 01:09 PM

Hi,Grant,the hardware requirement is a STM32F4 series microcontroller,the netduino can has this feature.

Don't forget CAN physical layer transceiver, STM32F4 does not have it, so you'd need to add SN65HVxx, MCP2551 or similar.



#7 weixiongmei

weixiongmei

    Advanced Member

  • Members
  • PipPipPip
  • 68 posts
  • LocationPhiladelphia, Pennsylvania, United States

Posted 20 November 2013 - 01:16 PM

Hi,Mario Vernari,you are correct,but to my self,i still feeling the CAN bus is better than RS485,because the CAN bus has the error detection features,it will not interrupts the whole bus if there is one of the Notes running in hardware error,if this happen to RS485,the whole RS485 will interrupted.And the transmit distance of CAN can goes up to 10KM,but the RS485 only 1KM



#8 weixiongmei

weixiongmei

    Advanced Member

  • Members
  • PipPipPip
  • 68 posts
  • LocationPhiladelphia, Pennsylvania, United States

Posted 20 November 2013 - 01:17 PM

CW2,yes,if the people know how to use the CAN bus,they will know a CAN PHY is needed.



#9 stotech

stotech

    Advanced Member

  • Members
  • PipPipPip
  • 143 posts
  • LocationAustralia

Posted 20 November 2013 - 10:15 PM

My brother is a mechanic and he's always raving about it being the bee's knee's. I've never worked with it before and was purely interested as an academic challenge. Can you point me to a hardware schematic that shows how to wire it all up. It would be an awesome trick to be able to sniff car network traffic. I always wanted to play with rs485 also though, for a hobby project, and to increase my skills I should play with both at some stage I guess.

 

Congratulations on the achievement. As I understand it, the task of adding it was quite large and complex. I hope you are able to commercialize in someway that makes it worth your while. I don't know if there's a shield out there or not, but that might be one way?

 

I can't even go close to contributing what it would be worth, but I'd be more than happy to make a sensible donation for your troubles. Just to have up my sleeve for if I get board one weekend.

 

And here is a cool idea. We should have a forum where people can post a code example with a donate now button. And have an open source target. Like $1000 or something. Where it becomes open source when it reaches a certain amount. That would be cool for cases like this I reckon.

 

Since I have very basic knowledge of software, I can't really add much to the community yet. But I know enough to appreciate the skills that others might take for granted and am always looking for opportunities to show a little appreciation. 

 

Do you need to recompile a custom firmware? Probably can't pull this off purely in managed code right?

 

Grant.



#10 weixiongmei

weixiongmei

    Advanced Member

  • Members
  • PipPipPip
  • 68 posts
  • LocationPhiladelphia, Pennsylvania, United States

Posted 21 November 2013 - 02:46 AM

Hi,Grant,thats a good idea for donation,if you would like to use the CAN bus,you have to buy two CAN PHY modules,as what i know is Waveshare is selling this module,its very easy to use,and they also providing the hardware schematic for it.

 

And yes,i need to recompile a custom firmware for Netduino,because the Netduino is using a difference frequency crystal.Also,the CAN bus driver can not write purely in managed code,because it can not handles the native interrupts.



#11 stotech

stotech

    Advanced Member

  • Members
  • PipPipPip
  • 143 posts
  • LocationAustralia

Posted 21 November 2013 - 03:46 AM

I'm afraid I must not be using the right terminology when searching because I haven't found much except this.

 

Does that look about right?

 

What are the hardware pins on the on the Netduino used?



#12 weixiongmei

weixiongmei

    Advanced Member

  • Members
  • PipPipPip
  • 68 posts
  • LocationPhiladelphia, Pennsylvania, United States

Posted 21 November 2013 - 04:38 AM

The schematic is fine,which netduino are you using?



#13 stotech

stotech

    Advanced Member

  • Members
  • PipPipPip
  • 143 posts
  • LocationAustralia

Posted 22 November 2013 - 12:00 AM

Netduio 2 and Netduino plus 2.

 

Please don't rush for me. I just got another job to do so I'll be busy for a while. Maybe when I'm ready I could hit you up for a pre-compiled firmware file or something. That way you could be sure the code couldn't be copied. But like I said. Going to be busy for a while now. 

 

Someone should ask Chris what he thinks about the donation idea thing. I wonder if it would be in breach of the forum rules or something?

 

Grant.



#14 blackt1ger

blackt1ger

    Member

  • Members
  • PipPip
  • 14 posts

Posted 28 November 2013 - 11:35 PM

 

 If so,i can port it from my board to Netduino,and here's the sample code how to use the CAN bus in .Net Micro Framework.

 

 

 

 

I'm interested.  Let me know.  I'm still on the 4.2.2 firmware, would it just be a patch to that?



#15 zeroaviation

zeroaviation

    Member

  • Members
  • PipPip
  • 28 posts

Posted 04 December 2013 - 09:21 PM

Subscribing to this thread! I'm highly, highly interested in using CAN on netduino. SPI-> CAN Interfaces are too slow and in my opinion buggy when using networks that are more than 70% utilization.

 

Looking forward to a release! :)



#16 weixiongmei

weixiongmei

    Advanced Member

  • Members
  • PipPipPip
  • 68 posts
  • LocationPhiladelphia, Pennsylvania, United States

Posted 05 December 2013 - 10:20 AM

Hi,zeroaviation,im working on it,hopefully i will release a test version of firmware for netduino plus 2.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.