/// <summary>
        /// The open method tries to connect to both busses to see if one of them is connected and
        /// active. The first strategy is to listen for any CAN message. If this fails there is a
        /// check to see if the application is started after an interrupted flash session. This is
        /// done by sending a message to set address and length (only for P-bus).
        /// </summary>
        /// <returns>OpenResult.OK is returned on success. Otherwise OpenResult.OpenError is
        /// returned.</returns>
        override public OpenResult open()
        {
            EASYSYNC.CANMsg msg = new EASYSYNC.CANMsg();
            //Check if I bus is connected
            close();

            /*m_deviceHandle = EASYSYNC.canusb_Open(IntPtr.Zero,
             *  "0xcb:0x9a",                        // Slow
             *  EASYSYNC.CANUSB_ACCEPTANCE_CODE_ALL,
             *  EASYSYNC.CANUSB_ACCEPTANCE_MASK_ALL,
             *  EASYSYNC.CANUSB_FLAG_TIMESTAMP);
             * if (waitAnyMessage(1000, out msg) != 0)
             * {
             *  if (m_readThread.ThreadState == ThreadState.Unstarted)
             *      m_readThread.Start();
             *  return OpenResult.OK;
             * }
             * close();
             */
            //I bus wasn't connected.
            //Check if P bus is connected

            /* char[] adapter = new char[16];
             * int result = EASYSYNC.canusb_getFirstAdapter(adapter, 16);
             * Console.WriteLine("Number of adapters found: " + result.ToString("X8"));*/

            m_deviceHandle = EASYSYNC.canusb_Open(IntPtr.Zero, "500", IntPtr.Zero, IntPtr.Zero, 1);


/*            m_deviceHandle = EASYSYNC.canusb_Open( null,
 *          EASYSYNC.CAN_BAUD_500K,              //500Kb/s
 *          null,//EASYSYNC.CANUSB_ACCEPTANCE_CODE_ALL,
 *          null,//EASYSYNC.CANUSB_ACCEPTANCE_MASK_ALL,
 *          EASYSYNC.CANUSB_FLAG_TIMESTAMP);*/
            int errorval = (int)m_deviceHandle;

            Console.WriteLine(errorval.ToString());
            if (m_deviceHandle < 0)
            {
                Console.WriteLine("Invalid handle received");
                return(OpenResult.OpenError);
            }

            if (DisableCanConnectionCheck || boxIsThere())
            {
                if (m_readThread.ThreadState == ThreadState.Unstarted)
                {
                    m_readThread.Start();
                }
                return(OpenResult.OK);
            }

            close();
            return(OpenResult.OpenError);
        }
Exemple #2
0
        /// <summary>
        /// The open method tries to connect to both busses to see if one of them is connected and
        /// active. The first strategy is to listen for any CAN message. If this fails there is a
        /// check to see if the application is started after an interrupted flash session. This is
        /// done by sending a message to set address and length (only for P-bus).
        /// </summary>
        /// <returns>OpenResult.OK is returned on success. Otherwise OpenResult.OpenError is
        /// returned.</returns>
        override public OpenResult open()
        {
            if (m_deviceHandle != 0)
            {
                close();
            }
            // get first adapter
            //char[] adapter = new char[10];
            StringBuilder adapter = new StringBuilder(10);

            int result = EASYSYNC.canusb_getFirstAdapter(adapter, 10);

            if (result > 0)
            {
                Console.WriteLine("Number of adapters found: " + result.ToString("X8"));
                Console.WriteLine("Adapter serialnumber: " + adapter);
                m_deviceHandle = EASYSYNC.canusb_Open(null,
                                                            //"0x00:0xB9:0x07",
                                                            //"00:B9:07",
                                                      "00B907",
                                                            //"600",
                                                            //null,
                                                      null, //EASYSYNC.CANUSB_ACCEPTANCE_CODE_ALL,
                                                      null, //EASYSYNC.CANUSB_ACCEPTANCE_MASK_ALL,
                                                      EASYSYNC.CANUSB_FLAG_TIMESTAMP);
                Console.WriteLine("Handle: " + m_deviceHandle.ToString("X8"));
                if (m_deviceHandle > 0 && m_deviceHandle != 0xFFFFFFFD)
                {
                    EASYSYNC.canusb_Flush(m_deviceHandle);
                    Console.WriteLine("Creating new reader thread");

                    StringBuilder sb = new StringBuilder(100);
                    EASYSYNC.canusb_VersionInfo(m_deviceHandle, sb);
                    Console.WriteLine("Versionifo: " + sb.ToString());

                    Console.WriteLine("Status: " + EASYSYNC.canusb_Status(m_deviceHandle).ToString("X8"));

                    m_readThread = new Thread(readMessages);
                    try
                    {
                        m_readThread.Priority = ThreadPriority.Normal; // realtime enough
                    }
                    catch (Exception E)
                    {
                        Console.WriteLine(E.Message);
                    }
                    if (m_readThread.ThreadState == ThreadState.Unstarted)
                    {
                        m_readThread.Start();
                    }
                    return(OpenResult.OK);
                }
                else
                {
                    // second try after unload of dlls?

                    //close();
                    return(OpenResult.OpenError);
                }
            }
            return(OpenResult.OpenError);
        }