static void Main(string[] args)
        {
            //Change these to load correct driver and connect it to correct bus
            string   driver  = "can_usb_win32";
            string   bus     = "COM4";
            BUSSPEED bitrate = BUSSPEED.BUS_500Kbit;


            try
            {
                DriverLoader   loader   = new DriverLoader();
                DriverInstance instance = loader.loaddriver(driver);

                Console.WriteLine("Opening CAN device ");
                instance.open(bus, bitrate);

                instance.rxmessage += Instance_rxmessage;
                Console.WriteLine("listening for any traffic, press any key to exit ..");
                while (!Console.KeyAvailable)
                {
                }

                instance.close();
            }
            catch (Exception e)
            {
                Console.WriteLine("That did not work out, exception message was \n" + e.ToString());
            }

            Console.WriteLine("Press any key to exit");

            while (!Console.KeyAvailable)
            {
            }
        }
Example #2
0
        /// <summary>
        /// Open the CAN hardware device via the CanFestival driver, NB this is currently a simple version that will
        /// not work with drivers that have more complex bus ids so only supports com port (inc usb serial) devices for the moment
        /// </summary>
        /// <param name="comport">COM PORT number</param>
        /// <param name="speed">CAN Bit rate</param>
        /// <param name="drivername">Driver to use</param>
        public void open(int comport, BUSSPEED speed, string drivername)
        {
            driver = loader.loaddriver(drivername);
            driver.open(string.Format("COM{0}", comport), speed);

            driver.rxmessage += Driver_rxmessage;

            threadrun = true;
            Thread thread = new Thread(new ThreadStart(asyncprocess));

            thread.Name = "CAN Open worker";
            thread.Start();
        }
Example #3
0
        /// <summary>
        /// Open the CAN hardware device via the CanFestival driver, NB this is currently a simple version that will
        /// not work with drivers that have more complex bus ids so only supports com port (inc usb serial) devices for the moment
        /// </summary>
        /// <param name="comport">COM PORT number</param>
        /// <param name="speed">CAN Bit rate</param>
        /// <param name="drivername">Driver to use</param>
        public void open(string comport, BUSSPEED speed, string drivername)
        {
            driver = loader.loaddriver(drivername);
            driver.open(string.Format("{0}", comport), speed);

            driver.rxmessage += Driver_rxmessage;

            threadrun = true;
            Thread thread = new Thread(new ThreadStart(asyncprocess));

            thread.Name = "CAN Open worker";
            thread.Start();

            if (connectionevent != null)
            {
                connectionevent(this, new ConnectionChangedEventArgs(true));
            }
        }
Example #4
0
        /// <summary>
        /// A simple test that uses libcanopensimple to open a can device, set up some COB specific callbacks
        /// then send a bus reset all nodes NMT command after 5 seconds
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //Change these to load correct driver and connect it to correct bus
            string   driver  = "can_usb_win32";
            string   bus     = "COM4";
            BUSSPEED bitrate = BUSSPEED.BUS_500Kbit;

            try
            {
                libCanopenSimple.libCanopenSimple lco = new libCanopenSimple.libCanopenSimple();

                lco.nmtevent   += Lco_nmtevent;
                lco.nmtecevent += Lco_nmtecevent;
                lco.pdoevent   += Lco_pdoevent;
                lco.sdoevent   += Lco_sdoevent;

                lco.open(4, bitrate, driver);

                Console.WriteLine("listening for any traffic");

                Console.WriteLine("Sending NMT reset all nodes in 5 seconds");

                System.Threading.Thread.Sleep(5000);

                lco.NMT_ResetNode(); //reset all

                Console.WriteLine("Press any key to exit test..");

                while (!Console.KeyAvailable)
                {
                }

                lco.close();
            }
            catch (Exception e)
            {
                Console.WriteLine("That did not work out, exception message was \n" + e.ToString());
            }
        }
Example #5
0
        /// <summary>
        /// Open the CAN device, the bus ID and bit rate are passed to driver. For Serial/USb Seral pass COMx etc.
        /// </summary>
        /// <param name="bus">The requested bus ID are provided here.</param>
        /// <param name="speed">The requested CAN bit rate</param>
        /// <returns>True on succesful opening of device</returns>
        public bool open(string bus, BUSSPEED speed)
        {
            try
            {
                brd.busname = bus;

                // Map BUSSPEED to CanFestival speed options
                switch (speed)
                {
                case BUSSPEED.BUS_10Kbit:
                    brd.baudrate = "10K";
                    break;

                case BUSSPEED.BUS_20Kbit:
                    brd.baudrate = "20K";
                    break;

                case BUSSPEED.BUS_50Kbit:
                    brd.baudrate = "50K";
                    break;

                case BUSSPEED.BUS_100Kbit:
                    brd.baudrate = "100K";
                    break;

                case BUSSPEED.BUS_125Kbit:
                    brd.baudrate = "125K";
                    break;

                case BUSSPEED.BUS_250Kbit:
                    brd.baudrate = "250K";
                    break;

                case BUSSPEED.BUS_500Kbit:
                    brd.baudrate = "500K";
                    break;

                case BUSSPEED.BUS_1Mbit:
                    brd.baudrate = "1M";
                    break;
                }

                brdptr = Marshal.AllocHGlobal(Marshal.SizeOf(brd));
                Marshal.StructureToPtr(brd, brdptr, false);

                handle = canOpen(brdptr);

                if (handle != IntPtr.Zero)
                {
                    rxthread = new System.Threading.Thread(rxthreadworker);
                    rxthread.Start();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception)
            {
                return(false);
            }
        }