Esempio n. 1
0
 private void DumpCanMsg(EASYSYNC.CANMsg r_canMsg, bool IsTransmit)
 {
     /*DateTime dt = DateTime.Now;
      * try
      * {
      *  using (StreamWriter sw = new StreamWriter(Path.Combine(m_startuppath, dt.Year.ToString("D4") + dt.Month.ToString("D2") + dt.Day.ToString("D2") + "-CanTrace.log"), true))
      *  {
      *      if (IsTransmit)
      *      {
      *          // get the byte transmitted
      *          int transmitvalue = (int)(r_canMsg.data & 0x000000000000FF00);
      *          transmitvalue /= 256;
      *
      *          sw.WriteLine(dt.ToString("dd/MM/yyyy HH:mm:ss") + " TX: id=" + r_canMsg.id.ToString("D2") + " len= " + r_canMsg.len.ToString("X8") + " data=" + r_canMsg.data.ToString("X16") + " " + r_canMsg.flags.ToString("X2") + " character = " + GetCharString(transmitvalue) + "\t ts: " + r_canMsg.timestamp.ToString("X16") + " flags: " + r_canMsg.flags.ToString("X2"));
      *      }
      *      else
      *      {
      *          // get the byte received
      *          int receivevalue = (int)(r_canMsg.data & 0x0000000000FF0000);
      *          receivevalue /= (256 * 256);
      *          sw.WriteLine(dt.ToString("dd/MM/yyyy HH:mm:ss") + " RX: id=" + r_canMsg.id.ToString("D2") + " len= " + r_canMsg.len.ToString("X8") + " data=" + r_canMsg.data.ToString("X16") + " " + r_canMsg.flags.ToString("X2") + " character = " + GetCharString(receivevalue) + "\t ts: " + r_canMsg.timestamp.ToString("X16") + " flags: " + r_canMsg.flags.ToString("X2"));
      *      }
      *  }
      * }
      * catch (Exception E)
      * {
      *  Console.WriteLine("Failed to write to logfile: " + E.Message);
      * }*/
 }
Esempio n. 2
0
        /// <summary>
        /// waitForMessage waits for a specific CAN message give by a CAN id.
        /// </summary>
        /// <param name="a_canID">The CAN id to listen for</param>
        /// <param name="timeout">Listen timeout</param>
        /// <param name="r_canMsg">The CAN message with a_canID that we where listening for.</param>
        /// <returns>The CAN id for the message we where listening for, otherwise 0.</returns>
        private uint waitForMessage(uint a_canID, uint timeout, out EASYSYNC.CANMsg r_canMsg)
        {
            int readResult = 0;
            int nrOfWait   = 0;

            while (nrOfWait < timeout)
            {
                readResult = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg);
                if (readResult == EASYSYNC.ERROR_CANUSB_OK)
                {
                    if (r_canMsg.id == 0x00)
                    {
                        nrOfWait++;
                    }
                    else if (r_canMsg.id != a_canID)
                    {
                        continue;
                    }
                    return((uint)r_canMsg.id);
                }
                else if (readResult == EASYSYNC.ERROR_CANUSB_NO_MESSAGE)
                {
                    Thread.Sleep(1);
                    nrOfWait++;
                }
            }
            r_canMsg = new EASYSYNC.CANMsg();
            return(0);
        }
Esempio n. 3
0
        /// <summary>
        /// waitForMessage waits for a specific CAN message give by a CAN id.
        /// </summary>
        /// <param name="a_canID">The CAN id to listen for</param>
        /// <param name="timeout">Listen timeout</param>
        /// <param name="r_canMsg">The CAN message with a_canID that we where listening for.</param>
        /// <returns>The CAN id for the message we where listening for, otherwise 0.</returns>
        public override uint waitForMessage(uint a_canID, uint timeout, out CANMessage canMsg)
        {
            EASYSYNC.CANMsg r_canMsg;
            canMsg = new CANMessage();
            int readResult = 0;
            int nrOfWait   = 0;

            while (nrOfWait < timeout)
            {
                r_canMsg   = new EASYSYNC.CANMsg();
                readResult = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg);
                if (readResult == EASYSYNC.ERROR_CANUSB_OK)
                {
                    if (r_canMsg.id == 0x00)
                    {
                        nrOfWait++;
                    }
                    else if (r_canMsg.id != a_canID)
                    {
                        continue;
                    }
                    canMsg.setData(r_canMsg.data);
                    canMsg.setID(r_canMsg.id);
                    canMsg.setLength(r_canMsg.len);
                    return((uint)r_canMsg.id);
                }
                else if (readResult == EASYSYNC.ERROR_CANUSB_NO_MESSAGE)
                {
                    Thread.Sleep(1);
                    nrOfWait++;
                }
            }
            r_canMsg = new EASYSYNC.CANMsg();
            return(0);
        }
Esempio n. 4
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()
        {
            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);
        }
Esempio n. 5
0
        /*private void AddToCanTrace(string line)
         * {
         *  if (m_EnableCanLog)
         *  {
         *      DateTime dtnow = DateTime.Now;
         *      using (StreamWriter sw = new StreamWriter(System.Windows.Forms.Application.StartupPath + "\\CanTraceCANUSBDevice.txt", true))
         *      {
         *          sw.WriteLine(dtnow.ToString("dd/MM/yyyy HH:mm:ss") + " - " + line);
         *      }
         *  }
         * }*/
        /// <summary>
        /// sendMessage send a CANMessage.
        /// </summary>
        /// <param name="a_message">A CANMessage.</param>
        /// <returns>true on success, othewise false.</returns>
        override public bool sendMessage(CANMessage a_message)
        {
            EASYSYNC.CANMsg msg = new EASYSYNC.CANMsg();
            msg.id    = a_message.getID();
            msg.len   = a_message.getLength();
            msg.flags = a_message.getFlags();
            msg.data  = a_message.getData();
            //Console.WriteLine("Data to send: " + msg.data.ToString("X16"));
            //Console.WriteLine("Data (original) to send: " + a_message.getData().ToString("X16"));

            int writeResult;

            AddToCanTrace("Sending message");
            writeResult = EASYSYNC.canusb_Write(m_deviceHandle, ref msg);
            if (writeResult == EASYSYNC.ERROR_CANUSB_OK)
            {
                AddToCanTrace("Message sent successfully");
                return(true);
            }
            else
            {
                switch (writeResult)
                {
                case EASYSYNC.ERROR_CANUSB_COMMAND_SUBSYSTEM:
                    AddToCanTrace("Message failed to send: ERROR_CANUSB_COMMAND_SUBSYSTEM");
                    break;

                case EASYSYNC.ERROR_CANUSB_INVALID_PARAM:
                    AddToCanTrace("Message failed to send: ERROR_CANUSB_INVALID_PARAM");
                    break;

                case EASYSYNC.ERROR_CANUSB_NO_MESSAGE:
                    AddToCanTrace("Message failed to send: ERROR_CANUSB_NO_MESSAGE");
                    break;

                case EASYSYNC.ERROR_CANUSB_NOT_OPEN:
                    AddToCanTrace("Message failed to send: ERROR_CANUSB_NOT_OPEN");
                    break;

                case EASYSYNC.ERROR_CANUSB_OPEN_SUBSYSTEM:
                    AddToCanTrace("Message failed to send: ERROR_CANUSB_OPEN_SUBSYSTEM");
                    break;

                case EASYSYNC.ERROR_CANUSB_TX_FIFO_FULL:
                    AddToCanTrace("Message failed to send: ERROR_CANUSB_TX_FIFO_FULL");
                    break;

                default:
                    AddToCanTrace("Message failed to send: " + writeResult.ToString());
                    break;
                }
                return(false);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Check if there is connection with a CAN bus.
        /// </summary>
        /// <returns>true on connection, otherwise false</returns>
        private bool boxIsThere()
        {
            EASYSYNC.CANMsg msg = new EASYSYNC.CANMsg();
            Console.WriteLine("Waiting for any message on the bus...");
            if (waitAnyMessage(2000, out msg) != 0)
            {
                return(true);
            }
            Console.WriteLine("No message seen, sending session request...");
            if (sendSessionRequest())
            {
                return(true);
            }

            return(false);
        }
Esempio n. 7
0
        /// <summary>
        /// waitAnyMessage waits for any message to be received.
        /// </summary>
        /// <param name="timeout">Listen timeout</param>
        /// <param name="r_canMsg">The CAN message that was first received</param>
        /// <returns>The CAN id for the message received, otherwise 0.</returns>
        private uint waitAnyMessage(uint timeout, out EASYSYNC.CANMsg r_canMsg)
        {
            int readResult = 0;
            int nrOfWait   = 0;

            while (nrOfWait < timeout)
            {
                readResult = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg);
                if (readResult == EASYSYNC.ERROR_CANUSB_OK)
                {
                    return((uint)r_canMsg.id);
                }
                else if (readResult == EASYSYNC.ERROR_CANUSB_NO_MESSAGE)
                {
                    Thread.Sleep(1); // changed to 0 to see performance impact
                    nrOfWait++;
                }
            }
            r_canMsg = new EASYSYNC.CANMsg();
            return(0);
        }
Esempio n. 8
0
        /// <summary>
        /// Send a message that starts a session. This is used to test if there is
        /// a connection.
        /// </summary>
        /// <returns></returns>
        private bool sendSessionRequest()
        {
            CANMessage msg1 = new CANMessage(0x220, 0, 8);

            EASYSYNC.CANMsg msg = new EASYSYNC.CANMsg();
            msg1.setData(0x000040021100813f);

            if (!sendMessage(msg1))
            {
                return(false);
            }
            if (waitForMessage(0x238, 1000, out msg) == 0x238)
            {
                //Ok, there seems to be a ECU somewhere out there.
                //Now, sleep for 10 seconds to get a session timeout. This is needed for
                //applications on higher level. Otherwise there will be no reply when the
                //higher level application tries to start a session.
                Thread.Sleep(10000);
                return(true);
            }
            return(false);
        }
Esempio n. 9
0
        /// <summary>
        /// readMessages is the "run" method of this class. It reads all incomming messages
        /// and publishes them to registered ICANListeners.
        /// </summary>
        public void readMessages()
        {
            int readResult = 0;

            EASYSYNC.CANMsg r_canMsg   = new EASYSYNC.CANMsg();
            CANMessage      canMessage = new CANMessage();

            while (true)
            {
                lock (m_synchObject)
                {
                    if (m_endThread)
                    {
                        return;
                    }
                }
                readResult = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg);
                if (readResult == EASYSYNC.ERROR_CANUSB_OK)
                {
                    canMessage.setID(r_canMsg.id);
                    canMessage.setLength(r_canMsg.len);
                    canMessage.setTimeStamp(r_canMsg.timestamp);
                    canMessage.setFlags(r_canMsg.flags);
                    canMessage.setData(r_canMsg.data);
                    lock (m_listeners)
                    {
                        foreach (ICANListener listener in m_listeners)
                        {
                            listener.handleMessage(canMessage);
                        }
                    }
                }
                else if (readResult == EASYSYNC.ERROR_CANUSB_NO_MESSAGE)
                {
                    Thread.Sleep(1);
                }
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Check if there is connection with a CAN bus.
        /// </summary>
        /// <returns>true on connection, otherwise false</returns>
        private bool boxIsThere()
        {
            EASYSYNC.CANMsg msg = new EASYSYNC.CANMsg();
            Console.WriteLine("Waiting for any message on the bus...");
            if (waitAnyMessage(2000, out msg) != 0)
                return true;
            Console.WriteLine("No message seen, sending session request...");
            if (sendSessionRequest())
                return true;

            return false;
        }
Esempio n. 11
0
        /// <summary>
        /// sendMessage send a CANMessage.
        /// </summary>
        /// <param name="a_message">A CANMessage.</param>
        /// <returns>true on success, othewise false.</returns>
        public override bool sendMessage(CANMessage a_message)
        {
            EASYSYNC.CANMsg msg = new EASYSYNC.CANMsg();
            msg.id = a_message.getID();
            msg.len = a_message.getLength();
            msg.flags = a_message.getFlags();
            msg.data = a_message.getData();
            //Console.WriteLine("Data to send: " + msg.data.ToString("X16"));
            //Console.WriteLine("Data (original) to send: " + a_message.getData().ToString("X16"));

            int writeResult;
            AddToCanTrace("Sending message");
            writeResult = EASYSYNC.canusb_Write(m_deviceHandle, ref msg);
            if (writeResult == EASYSYNC.ERROR_CANUSB_OK)
            {
                AddToCanTrace("Message sent successfully");
                return true;
            }
            else
            {
                switch (writeResult)
                {
                    case EASYSYNC.ERROR_CANUSB_COMMAND_SUBSYSTEM:
                        AddToCanTrace("Message failed to send: ERROR_CANUSB_COMMAND_SUBSYSTEM");
                        break;
                    case EASYSYNC.ERROR_CANUSB_INVALID_PARAM:
                        AddToCanTrace("Message failed to send: ERROR_CANUSB_INVALID_PARAM");
                        break;
                    case EASYSYNC.ERROR_CANUSB_NO_MESSAGE:
                        AddToCanTrace("Message failed to send: ERROR_CANUSB_NO_MESSAGE");
                        break;
                    case EASYSYNC.ERROR_CANUSB_NOT_OPEN:
                        AddToCanTrace("Message failed to send: ERROR_CANUSB_NOT_OPEN");
                        break;
                    case EASYSYNC.ERROR_CANUSB_OPEN_SUBSYSTEM:
                        AddToCanTrace("Message failed to send: ERROR_CANUSB_OPEN_SUBSYSTEM");
                        break;
                    case EASYSYNC.ERROR_CANUSB_TX_FIFO_FULL:
                        AddToCanTrace("Message failed to send: ERROR_CANUSB_TX_FIFO_FULL");
                        break;
                    default:
                        AddToCanTrace("Message failed to send: " + writeResult.ToString());
                        break;
                }
                return false;
            }
        }
Esempio n. 12
0
 /// <summary>
 /// readMessages is the "run" method of this class. It reads all incomming messages
 /// and publishes them to registered ICANListeners.
 /// </summary>
 public void readMessages()
 {
     int readResult = 0;
     EASYSYNC.CANMsg r_canMsg = new EASYSYNC.CANMsg();
     CANMessage canMessage = new CANMessage();
     while (true)
     {
         lock (m_synchObject)
         {
             if (m_endThread)
                 return;
         }
         readResult = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg);
         if (readResult == EASYSYNC.ERROR_CANUSB_OK)
         {
             canMessage.setID(r_canMsg.id);
             canMessage.setLength(r_canMsg.len);
             canMessage.setTimeStamp(r_canMsg.timestamp);
             canMessage.setFlags(r_canMsg.flags);
             canMessage.setData(r_canMsg.data);
             lock (m_listeners)
             {
                 foreach (ICANListener listener in m_listeners)
                 {
                     listener.handleMessage(canMessage);
                 }
             }
         }
         else if (readResult == EASYSYNC.ERROR_CANUSB_NO_MESSAGE)
         {
             Thread.Sleep(1);
         }
     }
 }
Esempio n. 13
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>
        public override 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 (boxIsThere())
            {
                if(m_readThread.ThreadState == ThreadState.Unstarted)
                    m_readThread.Start();
                return OpenResult.OK;
            }

            close();
            return OpenResult.OpenError;
        }
Esempio n. 14
0
        /// <summary>
        /// sendMessage send a CANMessage.
        /// </summary>
        /// <param name="a_message">A CANMessage.</param>
        /// <returns>true on success, othewise false.</returns>
        public override bool sendMessage(CANMessage a_message)
        {
            EASYSYNC.CANMsg msg = new EASYSYNC.CANMsg();
            msg.id = (ushort)a_message.getID();
            msg.len = a_message.getLength();
            msg.flags = a_message.getFlags();
            ulong msgdata = a_message.getData();
            // store in data (ulong)
            /*byte databyte = a_message.getCanData(7);
            msg.data_1 = GetDataMSBADCII(databyte);
            msg.data_2 = GetDataLSBADCII(databyte);
            databyte = a_message.getCanData(6);
            msg.data_3 = GetDataMSBADCII(databyte);
            msg.data_4 = GetDataLSBADCII(databyte);
            databyte = a_message.getCanData(5);
            msg.data_5 = GetDataMSBADCII(databyte);
            msg.data_6 = GetDataLSBADCII(databyte);
            databyte = a_message.getCanData(4);
            msg.data_7 = GetDataMSBADCII(databyte);
            msg.data_8 = GetDataLSBADCII(databyte);
            databyte = a_message.getCanData(3);
            msg.data_9 = GetDataMSBADCII(databyte);
            msg.data_10 = GetDataLSBADCII(databyte);
            databyte = a_message.getCanData(2);
            msg.data_11 = GetDataMSBADCII(databyte);
            msg.data_12 = GetDataLSBADCII(databyte);
            databyte = a_message.getCanData(1);
            msg.data_13 = GetDataMSBADCII(databyte);
            msg.data_14 = GetDataLSBADCII(databyte);
            databyte = a_message.getCanData(0);
            msg.data_15 = GetDataMSBADCII(databyte);
            msg.data_16 = GetDataLSBADCII(databyte);
            */

            msg.data = a_message.getData(); // this data should be in ascii: unsigned char data[16];			// Databytes 0...7
            // example:
            /*
            msg.data[0]='A';
            msg.data[1]='1';

            msg.data[2]='B';
            msg.data[3]='2';

            msg.data[4]='C';
            msg.data[5]='3';

            msg.data[6]='D';
            msg.data[7]='4';

            msg.data[8]='E';
            msg.data[9]='5';

            msg.data[10]='F';
            msg.data[11]='6';

            msg.data[12]='1';
            msg.data[13]='2';

            msg.data[14]='3';
            msg.data[15]='4'; * */
            if (m_DoLogging)
            {
                DumpCanMsg(msg, true);
            }

            int writeResult;
            Console.WriteLine("Writing to handle: " + m_deviceHandle.ToString("X8"));
            writeResult = EASYSYNC.canusb_Write(m_deviceHandle, ref msg);

            if (writeResult == EASYSYNC.ERROR_CANUSB_OK)
                return true;
            else
            {
                //EASYSYNC.canusb_Flush(m_deviceHandle);
                Console.WriteLine("Failed to send message: " + writeResult.ToString("X8"));
                return false;
            }
        }
Esempio n. 15
0
        /// <summary>
        /// readMessages is the "run" method of this class. It reads all incomming messages
        /// and publishes them to registered ICANListeners.
        /// </summary>
        public void readMessages()
        {
            int readResult = 0;
            EASYSYNC.CANMsg r_canMsg = new EASYSYNC.CANMsg();
            CANMessage canMessage = new CANMessage();

            /* Stopwatch
             while (true)
             {
                 lock (m_synchObject)
                 {
                     if (m_endThread)
                         return;
                 }
                 readResult = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg);
                 if (readResult > 0)
                 {
                     canMessage.setID(r_canMsg.id);
                     canMessage.setLength(r_canMsg.len);
                     canMessage.setTimeStamp(r_canMsg.timestamp);
                     canMessage.setFlags(r_canMsg.flags);
                     canMessage.setData(r_canMsg.data);
                     if (m_DoLogging)
                     {
                         DumpCanMsg(r_canMsg, false);
                     }
                     lock (m_listeners)
                     {
                         foreach (ICANListener listener in m_listeners)
                         {
                             listener.handleMessage(canMessage);
                         }
                     }
                 }
                 else if (readResult == EASYSYNC.ERROR_CANUSB_NO_MESSAGE)
                 {
                     Thread.Sleep(1);
                 }
             }*/

            while (true)
            {
                /*if ((thrdcnt++ % 1000) == 0)
                {
                    Console.WriteLine("Reading messages");
                }*/
                lock (m_synchObject)
                {
                    if (m_endThread)
                    {
                        m_endThread = false;
                        return;
                    }
                }
                readResult = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg);
                if (readResult > 0)
                {
                    canMessage.setID((uint)r_canMsg.id);
                    canMessage.setLength(r_canMsg.len);
                    canMessage.setTimeStamp((uint)r_canMsg.timestamp);
                    canMessage.setFlags(r_canMsg.flags);
                    //TODO: data hier nog vullen canMessage.setData(r_canMsg.data);
                    if (m_DoLogging)
                    {
                        DumpCanMsg(r_canMsg, false);
                    }
                    lock (m_listeners)
                    {
                        foreach (ICANListener listener in m_listeners)
                        {
                            listener.handleMessage(canMessage);
                        }
                    }
                }
                else if (readResult == EASYSYNC.ERROR_CANUSB_NO_MESSAGE)
                {
                    Thread.Sleep(1); // changed to 0 to see performance impact
                }
            }
        }
Esempio n. 16
0
        /// <summary>
        /// Send a message that starts a session. This is used to test if there is 
        /// a connection.
        /// </summary>
        /// <returns></returns>
        private bool sendSessionRequest()
        {
            CANMessage msg1 = new CANMessage(0x220, 0, 8);
            EASYSYNC.CANMsg msg = new EASYSYNC.CANMsg();
            msg1.setData(0x000040021100813f);

            if (!sendMessage(msg1))
                return false;
            if (waitForMessage(0x238, 1000, out msg) == 0x238)
            {
                //Ok, there seems to be a ECU somewhere out there.
                //Now, sleep for 10 seconds to get a session timeout. This is needed for
                //applications on higher level. Otherwise there will be no reply when the
                //higher level application tries to start a session.
                Thread.Sleep(10000);
                return true;
            }
            return false;
        }
Esempio n. 17
0
 /// <summary>
 /// waitAnyMessage waits for any message to be received.
 /// </summary>
 /// <param name="timeout">Listen timeout</param>
 /// <param name="r_canMsg">The CAN message that was first received</param>
 /// <returns>The CAN id for the message received, otherwise 0.</returns>
 private uint waitAnyMessage(uint timeout, out EASYSYNC.CANMsg r_canMsg)
 {
     int readResult = 0;
     int nrOfWait = 0;
     while (nrOfWait < timeout)
     {
         readResult = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg);
         if (readResult == EASYSYNC.ERROR_CANUSB_OK)
         {
             return (uint)r_canMsg.id;
         }
         else if (readResult == EASYSYNC.ERROR_CANUSB_NO_MESSAGE)
         {
             Thread.Sleep(1); // changed to 0 to see performance impact
             nrOfWait++;
         }
     }
     r_canMsg = new EASYSYNC.CANMsg();
     return 0;
 }
Esempio n. 18
0
        /// <summary>
        /// sendMessage send a CANMessage.
        /// </summary>
        /// <param name="a_message">A CANMessage.</param>
        /// <returns>true on success, othewise false.</returns>
        override public bool sendMessage(CANMessage a_message)
        {
            EASYSYNC.CANMsg msg = new EASYSYNC.CANMsg();
            msg.id    = (ushort)a_message.getID();
            msg.len   = a_message.getLength();
            msg.flags = a_message.getFlags();
            ulong msgdata = a_message.getData();

            // store in data (ulong)

            /*byte databyte = a_message.getCanData(7);
             * msg.data_1 = GetDataMSBADCII(databyte);
             * msg.data_2 = GetDataLSBADCII(databyte);
             * databyte = a_message.getCanData(6);
             * msg.data_3 = GetDataMSBADCII(databyte);
             * msg.data_4 = GetDataLSBADCII(databyte);
             * databyte = a_message.getCanData(5);
             * msg.data_5 = GetDataMSBADCII(databyte);
             * msg.data_6 = GetDataLSBADCII(databyte);
             * databyte = a_message.getCanData(4);
             * msg.data_7 = GetDataMSBADCII(databyte);
             * msg.data_8 = GetDataLSBADCII(databyte);
             * databyte = a_message.getCanData(3);
             * msg.data_9 = GetDataMSBADCII(databyte);
             * msg.data_10 = GetDataLSBADCII(databyte);
             * databyte = a_message.getCanData(2);
             * msg.data_11 = GetDataMSBADCII(databyte);
             * msg.data_12 = GetDataLSBADCII(databyte);
             * databyte = a_message.getCanData(1);
             * msg.data_13 = GetDataMSBADCII(databyte);
             * msg.data_14 = GetDataLSBADCII(databyte);
             * databyte = a_message.getCanData(0);
             * msg.data_15 = GetDataMSBADCII(databyte);
             * msg.data_16 = GetDataLSBADCII(databyte);
             */

            msg.data = a_message.getData(); // this data should be in ascii: unsigned char data[16];			// Databytes 0...7
            // example:

            /*
             * msg.data[0]='A';
             *          msg.data[1]='1';
             *
             *          msg.data[2]='B';
             *          msg.data[3]='2';
             *
             *          msg.data[4]='C';
             *          msg.data[5]='3';
             *
             *          msg.data[6]='D';
             *          msg.data[7]='4';
             *
             *          msg.data[8]='E';
             *          msg.data[9]='5';
             *
             *          msg.data[10]='F';
             *          msg.data[11]='6';
             *
             *          msg.data[12]='1';
             *          msg.data[13]='2';
             *
             *          msg.data[14]='3';
             *          msg.data[15]='4'; * */
            if (m_DoLogging)
            {
                DumpCanMsg(msg, true);
            }

            int writeResult;

            Console.WriteLine("Writing to handle: " + m_deviceHandle.ToString("X8"));
            writeResult = EASYSYNC.canusb_Write(m_deviceHandle, ref msg);

            if (writeResult == EASYSYNC.ERROR_CANUSB_OK)
            {
                return(true);
            }
            else
            {
                //EASYSYNC.canusb_Flush(m_deviceHandle);
                Console.WriteLine("Failed to send message: " + writeResult.ToString("X8"));
                return(false);
            }
        }
Esempio n. 19
0
        /// <summary>
        /// readMessages is the "run" method of this class. It reads all incomming messages
        /// and publishes them to registered ICANListeners.
        /// </summary>
        public void readMessages()
        {
            int readResult = 0;

            EASYSYNC.CANMsg r_canMsg   = new EASYSYNC.CANMsg();
            CANMessage      canMessage = new CANMessage();

            /* Stopwatch
             * while (true)
             * {
             *   lock (m_synchObject)
             *   {
             *       if (m_endThread)
             *           return;
             *   }
             *   readResult = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg);
             *   if (readResult > 0)
             *   {
             *       canMessage.setID(r_canMsg.id);
             *       canMessage.setLength(r_canMsg.len);
             *       canMessage.setTimeStamp(r_canMsg.timestamp);
             *       canMessage.setFlags(r_canMsg.flags);
             *       canMessage.setData(r_canMsg.data);
             *       if (m_DoLogging)
             *       {
             *           DumpCanMsg(r_canMsg, false);
             *       }
             *       lock (m_listeners)
             *       {
             *           foreach (ICANListener listener in m_listeners)
             *           {
             *               listener.handleMessage(canMessage);
             *           }
             *       }
             *   }
             *   else if (readResult == EASYSYNC.ERROR_CANUSB_NO_MESSAGE)
             *   {
             *       Thread.Sleep(1);
             *   }
             * }*/

            while (true)
            {
                /*if ((thrdcnt++ % 1000) == 0)
                 * {
                 *  Console.WriteLine("Reading messages");
                 * }*/
                lock (m_synchObject)
                {
                    if (m_endThread)
                    {
                        m_endThread = false;
                        return;
                    }
                }
                readResult = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg);
                if (readResult > 0)
                {
                    canMessage.setID((uint)r_canMsg.id);
                    canMessage.setLength(r_canMsg.len);
                    canMessage.setTimeStamp((uint)r_canMsg.timestamp);
                    canMessage.setFlags(r_canMsg.flags);
                    //TODO: data hier nog vullen canMessage.setData(r_canMsg.data);
                    if (m_DoLogging)
                    {
                        DumpCanMsg(r_canMsg, false);
                    }
                    lock (m_listeners)
                    {
                        foreach (ICANListener listener in m_listeners)
                        {
                            listener.handleMessage(canMessage);
                        }
                    }
                }
                else if (readResult == EASYSYNC.ERROR_CANUSB_NO_MESSAGE)
                {
                    Thread.Sleep(1); // changed to 0 to see performance impact
                }
            }
        }
Esempio n. 20
0
 /// <summary>
 /// waitForMessage waits for a specific CAN message give by a CAN id.
 /// </summary>
 /// <param name="a_canID">The CAN id to listen for</param>
 /// <param name="timeout">Listen timeout</param>
 /// <param name="r_canMsg">The CAN message with a_canID that we where listening for.</param>
 /// <returns>The CAN id for the message we where listening for, otherwise 0.</returns>
 private uint waitForMessage(uint a_canID, uint timeout, out EASYSYNC.CANMsg r_canMsg)
 {
     int readResult = 0;
     int nrOfWait = 0;
     while (nrOfWait < timeout)
     {
         readResult = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg);
         if (readResult == EASYSYNC.ERROR_CANUSB_OK)
         {
             if (r_canMsg.id == 0x00)
             {
                 nrOfWait++;
             }
             else if (r_canMsg.id != a_canID)
                 continue;
             return (uint)r_canMsg.id;
         }
         else if (readResult == EASYSYNC.ERROR_CANUSB_NO_MESSAGE)
         {
             Thread.Sleep(1);
             nrOfWait++;
         }
     }
     r_canMsg = new EASYSYNC.CANMsg();
     return 0;
 }
Esempio n. 21
0
 /// <summary>
 /// waitForMessage waits for a specific CAN message give by a CAN id.
 /// </summary>
 /// <param name="a_canID">The CAN id to listen for</param>
 /// <param name="timeout">Listen timeout</param>
 /// <param name="r_canMsg">The CAN message with a_canID that we where listening for.</param>
 /// <returns>The CAN id for the message we where listening for, otherwise 0.</returns>
 public override uint waitForMessage(uint a_canID, uint timeout, out CANMessage canMsg)
 {
     EASYSYNC.CANMsg r_canMsg;
     canMsg = new CANMessage();
     int readResult = 0;
     int nrOfWait = 0;
     while (nrOfWait < timeout)
     {
         r_canMsg = new EASYSYNC.CANMsg();
         readResult = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg);
         if (readResult == EASYSYNC.ERROR_CANUSB_OK)
         {
             if (r_canMsg.id == 0x00)
             {
                 nrOfWait++;
             }
             else if (r_canMsg.id != a_canID)
                 continue;
             canMsg.setData(r_canMsg.data);
             canMsg.setID(r_canMsg.id);
             canMsg.setLength(r_canMsg.len);
             return (uint)r_canMsg.id;
         }
         else if (readResult == EASYSYNC.ERROR_CANUSB_NO_MESSAGE)
         {
             Thread.Sleep(1);
             nrOfWait++;
         }
     }
     r_canMsg = new EASYSYNC.CANMsg();
     return 0;
 }