예제 #1
0
        override public void handleMessage(CANMessage a_message)
        {
            if (_queue == null)
            {
                _queue = new CANMessage[16];
                _receiveMessageIndex = 0;
                _readMessageIndex    = 0;
            }

            // add the message to a queue for later processing ...
            // the queue is a ringbuffer for CANMessage objects.
            // X objects are supported
            // we need a receive and a read pointer for this to work properly
            messageReceived = false;
            //_queue[_receiveMessageIndex] = a_message;
            _queue[_receiveMessageIndex] = new CANMessage();
            _queue[_receiveMessageIndex].setData(a_message.getData());
            _queue[_receiveMessageIndex].setID(a_message.getID());
            _queue[_receiveMessageIndex].setLength(a_message.getLength());

            _receiveMessageIndex++;
            if (_receiveMessageIndex > _queue.Length - 1)
            {
                _receiveMessageIndex = 0;                                          // make it circular
            }
            //DetermineSize();


            /*
             * lock (m_canMessage)
             * {
             *  if (a_message.getID() == m_waitMsgID)
             *  {
             *      m_canMessage = a_message;
             *      messageReceived = true;
             *  }
             * }
             * if (messageReceived)
             * {
             *  m_resetEvent.Set();
             * }*/
        }
예제 #2
0
        public override void handleMessage(CANMessage a_message)
        {
            if (_queue == null)
            {
                _queue = new CANMessage[16];
                _receiveMessageIndex = 0;
                _readMessageIndex = 0;
            }

            // add the message to a queue for later processing ...
            // the queue is a ringbuffer for CANMessage objects.
            // X objects are supported
            // we need a receive and a read pointer for this to work properly
            messageReceived = false;
            //_queue[_receiveMessageIndex] = a_message;
            _queue[_receiveMessageIndex] = new CANMessage();
            _queue[_receiveMessageIndex].setData(a_message.getData());
            _queue[_receiveMessageIndex].setID(a_message.getID());
            _queue[_receiveMessageIndex].setLength(a_message.getLength());

            _receiveMessageIndex++;
            if(_receiveMessageIndex > _queue.Length - 1) _receiveMessageIndex = 0; // make it circular

            //DetermineSize();

            /*
            lock (m_canMessage)
            {
                if (a_message.getID() == m_waitMsgID)
                {
                    m_canMessage = a_message;
                    messageReceived = true;
                }
            }
            if (messageReceived)
            {
                m_resetEvent.Set();
            }*/
        }
예제 #3
0
        //-------------------------------------------------------------------------
        /**
        Waits for arrival of a specific CAN message or any message if ID = 0.

        @param      a_canID     message ID
        @param      timeout     timeout, ms
        @param      canMsg      message

        @return                 message ID
        */
        public override uint waitForMessage(uint a_canID, uint timeout,
        out CANMessage canMsg)
        {
            canMsg = new CANMessage();
            Debug.Assert(canMsg != null);
            canMsg.setID(0);

            caCombiAdapter.caCANFrame frame = new caCombiAdapter.caCANFrame();
            if (this.combi.CAN_GetMessage(ref frame, timeout) &&
            (frame.id == a_canID || a_canID == 0))
            {
            // message received
            canMsg.setID(frame.id);
            canMsg.setLength(frame.length);
            canMsg.setData(frame.data);

            return frame.id;
            }

            // timed out
            return 0;
        }
예제 #4
0
        public override bool sendMessage(CANMessage a_message)
        {
            string sendString = "  ";


            for (uint i = 0; i < a_message.getLength(); i++) // leave out the length field, the ELM chip assigns that for us
            {
                //if (i <= 7)
                {
                    sendString += a_message.getCanData(i).ToString("X2");
                }

                /*else
                 * {
                 *  sendString += "00"; // fill with zeros
                 * }*/
            }

            sendString += "\r";
            if (m_serialPort.IsOpen)
            {
                m_serialPort.Write(sendString);
                //Console.WriteLine("TX: " + sendString);
            }

            // bitrate = 38400bps -> 3840 bytes per second
            // sending each byte will take 0.2 ms approx
            Thread.Sleep(a_message.getLength()); // sleep length ms

            //            Thread.Sleep(10);
            //receiveString = "49 01 01 00 00 00 31 \n\r49 02 02 44 34 47 50 \n\r49 02 03 30 30 52 35 \n\r49 02 04 25 42";// m_serialPort.ReadTo(">");

            /*receiveString = m_serialPort.ReadTo(">");
             * char[] chrArray = receiveString.ToCharArray();
             * byte[] reply = new byte[0xFF];
             * int insertPos = 1;
             * int index = 0;
             * string subString = "";
             * while (receiveString.Length > 4)
             * {
             *
             *  //Remove first three bytes
             *
             *  //TODO. Remove Mode and PIDs
             *  for (int i = 0; i < 3; i++)
             *  {
             *      index = receiveString.IndexOf(" ");
             *      receiveString = receiveString.Remove(0, index + 1);
             *  }
             *  //Read data for the rest of the row.
             *  for (int i = 0; i < 4; i++)
             *  {
             *      index = receiveString.IndexOf(" ");
             *      if (index == 0) //Last row not 4 bytes of data.
             *      {
             *          continue;
             *      }
             *      subString = receiveString.Substring(0, index);
             *      reply[insertPos] = (byte)Convert.ToInt16("0x" + subString, 16);
             *      insertPos++;
             *      receiveString = receiveString.Remove(0, index + 1);
             *  }
             *
             * }
             *
             * reply[0] = (byte)insertPos; //Length
             *
             * r_reply = new KWPReply(reply, a_request.getNrOfPID());
             * return RequestResult.NoError;*/
            return(true); // remove after implementation
        }
예제 #5
0
 /// <summary>
 /// This method is called by ICANDevices where derived objects of this class
 /// are registered. The method is called for each received CANMessage.
 /// What this method does is application dependent.
 /// </summary>
 /// <param name="a_canMessage">The CANMessage to be handled by this method.</param>
 public abstract void handleMessage(CANMessage a_canMessage);
예제 #6
0
        public override bool sendMessage(CANMessage a_message)
        {
            string sendString = "  ";

            for (uint i = 0; i < a_message.getLength(); i++) // leave out the length field, the ELM chip assigns that for us
            {
                //if (i <= 7)
                {
                    sendString += a_message.getCanData(i).ToString("X2");
                }
                /*else
                {
                    sendString += "00"; // fill with zeros
                }*/
            }

            sendString += "\r";
            if (m_serialPort.IsOpen)
            {

                m_serialPort.Write(sendString);
                //Console.WriteLine("TX: " + sendString);
            }

            // bitrate = 38400bps -> 3840 bytes per second
            // sending each byte will take 0.2 ms approx
            Thread.Sleep(a_message.getLength()); // sleep length ms

            //            Thread.Sleep(10);
            //receiveString = "49 01 01 00 00 00 31 \n\r49 02 02 44 34 47 50 \n\r49 02 03 30 30 52 35 \n\r49 02 04 25 42";// m_serialPort.ReadTo(">");
            /*receiveString = m_serialPort.ReadTo(">");
            char[] chrArray = receiveString.ToCharArray();
            byte[] reply = new byte[0xFF];
            int insertPos = 1;
            int index = 0;
            string subString = "";
            while (receiveString.Length > 4)
            {

                //Remove first three bytes

                //TODO. Remove Mode and PIDs
                for (int i = 0; i < 3; i++)
                {
                    index = receiveString.IndexOf(" ");
                    receiveString = receiveString.Remove(0, index + 1);
                }
                //Read data for the rest of the row.
                for (int i = 0; i < 4; i++)
                {
                    index = receiveString.IndexOf(" ");
                    if (index == 0) //Last row not 4 bytes of data.
                    {
                        continue;
                    }
                    subString = receiveString.Substring(0, index);
                    reply[insertPos] = (byte)Convert.ToInt16("0x" + subString, 16);
                    insertPos++;
                    receiveString = receiveString.Remove(0, index + 1);
                }

            }

            reply[0] = (byte)insertPos; //Length

            r_reply = new KWPReply(reply, a_request.getNrOfPID());
            return RequestResult.NoError;*/
            return true; // remove after implementation
        }
예제 #7
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()
        {
            Console.WriteLine("Sending session request");
            // 0x220 is for T7
            // 0x7E0 is for T8
            CANMessage msg1 = new CANMessage(0x7E0, 0, 8);
            LAWICEL.CANMsg msg = new LAWICEL.CANMsg();
            msg1.setData(0x000040021100813f);

            if (!sendMessage(msg1))
            {
                Console.WriteLine("Unable to send session request");
                return false;
            }
            if (waitForMessage(0x7E8, 1000, out msg) == 0x7E8)
            {
                Console.WriteLine("Message 0x7E8 seen");
                //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);
                Console.WriteLine("sendSessionRequest: TRUE");

                return true;
            }
            Console.WriteLine("sendSessionRequest: FALSE");
            return false;
        }
예제 #8
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>
 public override bool sendMessage(CANMessage a_message)
 {
     LAWICEL.CANMsg msg = new LAWICEL.CANMsg();
     msg.id = a_message.getID();
     msg.len = a_message.getLength();
     msg.flags = a_message.getFlags();
     msg.flags = LAWICEL.CANMSG_EXTENDED; // Test for now
     msg.data = a_message.getData();
     int writeResult;
     //AddToCanTrace("Sending message");
     AddToCanTrace("TX: " + msg.id.ToString("X6") + " " + msg.data.ToString("X16"));
     writeResult = LAWICEL.canusb_Write(m_deviceHandle, ref msg);
     if (writeResult == LAWICEL.ERROR_CANUSB_OK)
     {
         //AddToCanTrace("Message sent successfully");
         txCount++;
         bitsPerSecond += 109;
         return true;
     }
     else
     {
         errCount++;
         switch (writeResult)
         {
             case LAWICEL.ERROR_CANUSB_COMMAND_SUBSYSTEM:
                 AddToCanTrace("Message failed to send: ERROR_CANUSB_COMMAND_SUBSYSTEM");
                 break;
             case LAWICEL.ERROR_CANUSB_INVALID_PARAM:
                 AddToCanTrace("Message failed to send: ERROR_CANUSB_INVALID_PARAM");
                 break;
             case LAWICEL.ERROR_CANUSB_NO_MESSAGE:
                 AddToCanTrace("Message failed to send: ERROR_CANUSB_NO_MESSAGE");
                 break;
             case LAWICEL.ERROR_CANUSB_NOT_OPEN:
                 AddToCanTrace("Message failed to send: ERROR_CANUSB_NOT_OPEN");
                 break;
             case LAWICEL.ERROR_CANUSB_OPEN_SUBSYSTEM:
                 AddToCanTrace("Message failed to send: ERROR_CANUSB_OPEN_SUBSYSTEM");
                 break;
             case LAWICEL.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;
     }
 }
예제 #9
0
 public InformationFrameEventArgs(CANMessage message)
 {
     this._message = message;
 }
예제 #10
0
        public int query_data(byte unit, byte data_id, out byte[] answer)
        {
            byte[] data   = new byte[8];
            byte   length = 0;
            byte   i      = 0;

            answer = new byte[8]; // test <GS-11012011>
            int rcv_length;

            byte[] query = new byte[8] {
                0x40, 0x00, 0x02, 0x21, 0x00, 0x00, 0x00, 0x00
            };
            byte[] ack = new byte[8] {
                0x40, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00
            };
            //ulong query_long = 0x0000000021020040;
            //ulong ack_long = 0x00000000003F0040;
            query[1] = unit;
            ack[1]   = unit;
            // If data_id is zero, decrease length field
            if (data_id != 0x00)
            {
                query[4] = data_id;
            }
            else
            {
                query[2] = 0x01;
            }

            data[0]    = 0x00;
            rcv_length = 0;

            //for (int i = 0; i < 5; i++)
            {
                CANMessage msg = new CANMessage(0x240, 0, 8);
                //msg.setData(query_long);
                msg.setCanData(query[0], 0);
                msg.setCanData(query[1], 1);
                msg.setCanData(query[2], 2);
                msg.setCanData(query[3], 3);
                msg.setCanData(query[4], 4);
                msg.setCanData(query[5], 5);
                msg.setCanData(query[6], 6);
                msg.setCanData(query[7], 7);
                msg.setID(0x240);
                AddToCanTrace("Sending: 0x" + msg.getData().ToString("X16") + " id: 0x" + msg.getID().ToString("X4"));

                if (!sendMessage(msg))
                {
                    return(-1);
                }
                uint reply_unit = GetReplyforUnit(unit);
                reply_unit = 0x258; // for testing purposes
                while (data[0] != 0x80 && data[0] != 0xC0)
                {
                    CANMessage replyMessage = new CANMessage();
                    AddToCanTrace("Waiting for ID: 0x" + reply_unit.ToString("X4"));
                    if (waitForMessage(reply_unit, 1000, out replyMessage) == reply_unit)
                    {
                        AddToCanTrace("Rx data: " + replyMessage.getData().ToString("X16"));
                        data[0] = replyMessage.getCanData(0);
                        data[1] = replyMessage.getCanData(1);
                        data[2] = replyMessage.getCanData(2);
                        data[3] = replyMessage.getCanData(3);
                        data[4] = replyMessage.getCanData(4);
                        data[5] = replyMessage.getCanData(5);
                        data[6] = replyMessage.getCanData(6);
                        data[7] = replyMessage.getCanData(7);
                        int idx = 0;
                        if ((data[0] & 0x40) > 0)
                        {
                            if (data[2] > 0x02)
                            {
                                length  = data[2];  // subtract two non-payload bytes
                                length -= 2;
                                answer  = new byte[length];
                            }
                            else
                            {
                                length = 0;
                            }

                            if (--length > 0)
                            {
                                answer[idx++] = data[5];
                                rcv_length++;
                            }
                            if (--length > 0)
                            {
                                answer[idx++] = data[6];
                                rcv_length++;
                            }
                            if (--length > 0)
                            {
                                answer[idx++] = data[7];
                                rcv_length++;
                            }
                        }
                        else
                        {
                            for (i = 0; i < 6; i++)
                            {
                                answer[idx++] = data[2 + i];
                                length--;
                                rcv_length++;
                                if (length == 0)
                                {
                                    i = 6;
                                }
                            }
                        }
                        // Send acknowledgement
                        ack[3] = Convert.ToByte(data[0] & 0xBF);
                        CANMessage ackMessage = new CANMessage();
                        //ackMessage.setData(ack_long);
                        ackMessage.setCanData(ack[0], 0);
                        ackMessage.setCanData(ack[1], 1);
                        ackMessage.setCanData(ack[2], 2);
                        ackMessage.setCanData(ack[3], 3);
                        ackMessage.setCanData(ack[4], 4);
                        ackMessage.setCanData(ack[5], 5);
                        ackMessage.setCanData(ack[6], 6);
                        ackMessage.setCanData(ack[7], 7);

                        ackMessage.setID(0x266);
                        sendMessage(ackMessage);
                    }
                    else
                    {
                        // Timeout
                        AddToCanTrace("Timeout waiting for 0x" + reply_unit.ToString("X3"));
                        return(-1);
                    }
                }
            }
            return(rcv_length);
        }
예제 #11
0
 abstract public uint waitForMessage(uint a_canID, uint timeout, out CANMessage canMsg);
예제 #12
0
 /// <summary>
 /// This message sends a CANMessage to the CAN device.
 /// The open method must have been called and returned possitive result
 /// before this method is called.
 /// </summary>
 /// <param name="a_message">The CANMessage</param>
 /// <returns>true on success, otherwise false.</returns>
 abstract public bool sendMessage(CANMessage a_message);
예제 #13
0
 /// <summary>
 /// This method is called by ICANDevices where derived objects of this class
 /// are registered. The method is called for each received CANMessage.
 /// What this method does is application dependent.
 /// </summary>
 /// <param name="a_canMessage">The CANMessage to be handled by this method.</param>
 public abstract void handleMessage(CANMessage a_canMessage);
예제 #14
0
        //-------------------------------------------------------------------------
        /**
        Tries to connect to CAN bus using the specified bitrate.

        @param      bitrate     bitrate

        @return                 succ / fail
        */
        private bool try_bitrate(uint bitrate)
        {
            try
            {
            // try connecting
            this.combi.CAN_SetBitrate(bitrate);
            this.combi.CAN_Open(true);

            // look for bus activity
            CANMessage msg = new CANMessage();
            Debug.Assert(msg != null);

            if (this.waitForMessage(0, 1000, out msg) < 1)
            {
                throw new Exception("No traffic at given bitrate");
            }

            return true;
            }

            catch
            {
            // failed
            this.combi.CAN_Open(false);
            return false;
            }
        }
예제 #15
0
 public InformationFrameEventArgs(CANMessage message)
 {
     this._message = message;
 }
예제 #16
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;
            LAWICEL.CANMsg r_canMsg = new LAWICEL.CANMsg();
            CANMessage canMessage = new CANMessage();
            Console.WriteLine("readMessages started");
            while (true)
            {
                lock (m_synchObject)
                {
                    if (m_endThread)
                    {
                        Console.WriteLine("readMessages ended");
                        return;
                    }
                }
                readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg);
                if (readResult == LAWICEL.ERROR_CANUSB_OK)
                {
                    //Console.WriteLine(r_canMsg.id.ToString("X6") + " " + r_canMsg.data.ToString("X16"));
                    //if (MessageContainsInformationForRealtime(r_canMsg.id))
                    {
                        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)
                        {
                            bitsPerSecond += 109;
                            AddToCanTrace("RX: " + r_canMsg.id.ToString("X6") + " " + r_canMsg.data.ToString("X16"));
                            rxCount++;
                            foreach (ICANListener listener in m_listeners)
                            {
                                //while (listener.messagePending()) ; // dirty, make this better
                                listener.handleMessage(canMessage);
                            }
                            CastInformationEvent("", rxCount, txCount, errCount); // <GS-05042011> re-activated this function
                        }
                        //Thread.Sleep(1);
                    }

                    // cast event to application to process message
                    //if (MessageContainsInformationForRealtime(r_canMsg.id))
                    //{
                        //TODO: process all other known msg id's into the realtime view
                      //  CastInformationEvent(canMessage); // <GS-05042011> re-activated this function
                    //}
                }
                else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE)
                {
                   // Console.WriteLine("No message");
                    Thread.Sleep(1);
                }
                else
                {
                    Console.WriteLine("Result: " + readResult.ToString("X8"));
                }
                /*int stat = LAWICEL.canusb_Status(m_deviceHandle);
                if (stat != 0)
                {
                    Console.WriteLine("status: " + stat.ToString("X4"));
                }*/
            }
        }
예제 #17
0
        public CANMessage waitMessage(int a_timeout)
        {
            sw.Reset();
            sw.Start();
            CANMessage retMsg = new CANMessage();

            while (sw.ElapsedMilliseconds < a_timeout)
            {
                // search queue for the desired message
                if (_receiveMessageIndex < _readMessageIndex)
                {
                    // first upto (_queue.Length - 1)
                    for (int idx = _readMessageIndex; idx < _queue.Length; idx++)
                    {
                        if (_queue[idx].getID() == this.m_waitMsgID)
                        {
                            retMsg            = _queue[idx];
                            _readMessageIndex = idx + 1;
                            if (_readMessageIndex > _queue.Length - 1)
                            {
                                _readMessageIndex = 0;                                        // make it circular
                            }
                            sw.Stop();
                            return(retMsg);
                        }
                    }
                    for (int idx = 0; idx < _receiveMessageIndex; idx++)
                    {
                        if (_queue[idx].getID() == this.m_waitMsgID)
                        {
                            retMsg            = _queue[idx];
                            _readMessageIndex = idx + 1;
                            if (_readMessageIndex > _queue.Length - 1)
                            {
                                _readMessageIndex = 0;                                        // make it circular
                            }
                            sw.Stop();
                            return(retMsg);
                        }
                    }
                }
                else
                {
                    for (int idx = _readMessageIndex; idx < _receiveMessageIndex; idx++)
                    {
                        if (_queue[idx].getID() == this.m_waitMsgID)
                        {
                            retMsg            = _queue[idx];
                            _readMessageIndex = idx + 1;
                            if (_readMessageIndex > _queue.Length - 1)
                            {
                                _readMessageIndex = 0;                                        // make it circular
                            }
                            sw.Stop();
                            return(retMsg);
                        }
                    }
                }
                Thread.Sleep(1);
            }
            sw.Stop();
            return(retMsg);

            /*
             * CANMessage retMsg;
             *
             * m_resetEvent.WaitOne(a_timeout, true);
             * lock (m_canMessage)
             * {
             *  retMsg = m_canMessage;
             * }
             * messageReceived = false;
             * return retMsg;*/
        }
예제 #18
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)
 {
     /*
     int readResult = 0;
     int nrOfWait = 0;
     while (nrOfWait < timeout)
     {
         LAWICEL.CANMsg r_canMsg = new LAWICEL.CANMsg();
         canMsg = new CANMessage();
         readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg);
         if (readResult == LAWICEL.ERROR_CANUSB_OK)
         {
             //Console.WriteLine("rx id: 0x" + r_canMsg.id.ToString("X4"));
             if (r_canMsg.id != a_canID)
             {
                 nrOfWait++;
                 continue;
             }
             else
             {
                 canMsg.setID(r_canMsg.id);
                 canMsg.setData(r_canMsg.data);
                 canMsg.setFlags(r_canMsg.flags);
                 return (uint)r_canMsg.id;
             }
         }
         else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE)
         {
             Thread.Sleep(1);
             nrOfWait++;
         }
     }
     canMsg = new CANMessage();
     return 0;*/
     LAWICEL.CANMsg r_canMsg;
     canMsg = new CANMessage();
     int readResult = 0;
     int nrOfWait = 0;
     while (nrOfWait < timeout)
     {
         r_canMsg = new LAWICEL.CANMsg();
         readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg);
         if (readResult == LAWICEL.ERROR_CANUSB_OK)
         {
             Thread.Sleep(1);
             AddToCanTrace("rx: 0x" + r_canMsg.id.ToString("X4") + r_canMsg.data.ToString("X16"));
             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 == LAWICEL.ERROR_CANUSB_NO_MESSAGE)
         {
             Thread.Sleep(1);
             nrOfWait++;
         }
     }
     r_canMsg = new LAWICEL.CANMsg();
     return 0;
 }
예제 #19
0
 public void CastInformationEvent(CANMessage message)
 {
     if (onReceivedAdditionalInformationFrame != null)
     {
         onReceivedAdditionalInformationFrame(this, new InformationFrameEventArgs(message));
     }
 }
예제 #20
0
        public void readMessages()
        {
            CANMessage canMessage = new CANMessage();
            byte[] receiveBuffer = new byte[1024]; // circular buffer for reception of data
            string receiveString = string.Empty;

            Console.WriteLine("readMessages started");
            while (true)
            {
                lock (m_synchObject)
                {
                    if (m_endThread)
                    {
                        Console.WriteLine("readMessages ended");
                        return;
                    }
                }
                if (m_serialPort.IsOpen)
                {
                    if (m_serialPort.BytesToRead > 0)
                    {
                        receiveString += m_serialPort.ReadExisting();
                        //Console.WriteLine("BUF1: " + receiveString);
                        receiveString = receiveString.Replace(">", ""); // remove prompt characters... we don't need that stuff
                        receiveString = receiveString.Replace("NO DATA", ""); // remove prompt characters... we don't need that stuff
                        while (receiveString.StartsWith("\n") || receiveString.StartsWith("\r"))
                        {
                            receiveString = receiveString.Substring(1, receiveString.Length - 1);
                        }

                        while (receiveString.Contains('\r'))
                        {
                            // process the line
                            int idx = receiveString.IndexOf('\r');
                            string rxMessage = receiveString.Substring(0, idx);
                            receiveString = receiveString.Substring(idx + 1, receiveString.Length - idx - 1);
                            while (receiveString.StartsWith("\n") || receiveString.StartsWith("\r"))
                            {
                                receiveString = receiveString.Substring(1, receiveString.Length - 1);
                            }
                            //Console.WriteLine("BUF2: " + receiveString);
                            // is it a valid line
                            if (rxMessage.Length >= 6)
                            {
                                try
                                {
                                    uint id = Convert.ToUInt32(rxMessage.Substring(0, 3), 16);
                                    if (MessageContainsInformationForRealtime(id))
                                    {
                                        canMessage.setID(id);
                                        canMessage.setLength(8); // TODO: alter to match data
                                        canMessage.setData(0x0000000000000000); // reset message content
                                        byte b1 = Convert.ToByte(rxMessage.Substring(4, 2), 16);
                                        if (b1 < 7)
                                        {
                                            canMessage.setCanData(b1, 0);
                                            //Console.WriteLine("Byte 1: " + Convert.ToByte(rxMessage.Substring(4, 2), 16).ToString("X2"));
                                            if (b1 >= 1) canMessage.setCanData(Convert.ToByte(rxMessage.Substring(7, 2), 16), 1);
                                            if (b1 >= 2) canMessage.setCanData(Convert.ToByte(rxMessage.Substring(10, 2), 16), 2);
                                            if (b1 >= 3) canMessage.setCanData(Convert.ToByte(rxMessage.Substring(13, 2), 16), 3);
                                            if (b1 >= 4) canMessage.setCanData(Convert.ToByte(rxMessage.Substring(16, 2), 16), 4);
                                            if (b1 >= 5) canMessage.setCanData(Convert.ToByte(rxMessage.Substring(19, 2), 16), 5);
                                            if (b1 >= 6) canMessage.setCanData(Convert.ToByte(rxMessage.Substring(22, 2), 16), 6);
                                            if (b1 >= 7) canMessage.setCanData(Convert.ToByte(rxMessage.Substring(25, 2), 16), 7);
                                        }
                                        else
                                        {
                                            canMessage.setCanData(b1, 0);
                                            //Console.WriteLine("Byte 1: " + Convert.ToByte(rxMessage.Substring(4, 2), 16).ToString("X2"));
                                            canMessage.setCanData(Convert.ToByte(rxMessage.Substring(7, 2), 16), 1);
                                            canMessage.setCanData(Convert.ToByte(rxMessage.Substring(10, 2), 16), 2);
                                            canMessage.setCanData(Convert.ToByte(rxMessage.Substring(13, 2), 16), 3);
                                            canMessage.setCanData(Convert.ToByte(rxMessage.Substring(16, 2), 16), 4);
                                            canMessage.setCanData(Convert.ToByte(rxMessage.Substring(19, 2), 16), 5);
                                            canMessage.setCanData(Convert.ToByte(rxMessage.Substring(22, 2), 16), 6);
                                            canMessage.setCanData(Convert.ToByte(rxMessage.Substring(25, 2), 16), 7);
                                        }

                                        lock (m_listeners)
                                        {
                                            AddToCanTrace("RX: " + canMessage.getData().ToString("X16"));
                                            //Console.WriteLine("MSG: " + rxMessage);
                                            foreach (ICANListener listener in m_listeners)
                                            {
                                                listener.handleMessage(canMessage);
                                            }
                                        }
                                    }

                                }
                                catch (Exception)
                                {
                                    Console.WriteLine("MSG: " + rxMessage);
                                }
                            }
                        }
                    }
                    else
                    {
                        Thread.Sleep(1); // give others some air
                    }
                }

            }
            // parse the receive string

            /*int readResult = 0;
            LAWICEL.CANMsg r_canMsg = new LAWICEL.CANMsg();
            CANMessage canMessage = new CANMessage();
            Console.WriteLine("readMessages started");
            while (true)
            {
                lock (m_synchObject)
                {
                    if (m_endThread)
                    {
                        Console.WriteLine("readMessages ended");
                        return;
                    }
                }
                readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg);
                if (readResult == LAWICEL.ERROR_CANUSB_OK)
                {
                    //Console.WriteLine(r_canMsg.id.ToString("X3") + " " + r_canMsg.data.ToString("X8"));
                    if (MessageContainsInformationForRealtime(r_canMsg.id))
                    {
                        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)
                        {
                            AddToCanTrace("RX: " + r_canMsg.data.ToString("X16"));
                            foreach (ICANListener listener in m_listeners)
                            {
                                //while (listener.messagePending()) ; // dirty, make this better
                                listener.handleMessage(canMessage);
                            }
                            //CastInformationEvent(canMessage); // <GS-05042011> re-activated this function
                        }
                        //Thread.Sleep(1);
                    }

                    // cast event to application to process message
                    //if (MessageContainsInformationForRealtime(r_canMsg.id))
                    //{
                    //TODO: process all other known msg id's into the realtime view
                    //  CastInformationEvent(canMessage); // <GS-05042011> re-activated this function
                    //}
                }
                else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE)
                {
                    Thread.Sleep(1);
                }
            }
            */
        }
예제 #21
0
        public int query_data(byte unit, byte data_id, out byte[] answer)
        {
            byte[] data = new byte[8];
            byte length= 0;
            byte i = 0;
            answer = new byte[8]; // test <GS-11012011>
            int rcv_length;
            byte[] query = new byte[8] { 0x40, 0x00, 0x02, 0x21, 0x00, 0x00, 0x00, 0x00 };
            byte[] ack = new byte[8] { 0x40, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00 };
            //ulong query_long = 0x0000000021020040;
            //ulong ack_long = 0x00000000003F0040;
            query[1] = unit;
            ack[1] = unit;
            // If data_id is zero, decrease length field
            if (data_id != 0x00) query[4] = data_id;
            else query[2] = 0x01;

            data[0] = 0x00;
            rcv_length = 0;

            //for (int i = 0; i < 5; i++)
            {

                CANMessage msg = new CANMessage(0x240, 0, 8);
                //msg.setData(query_long);
                msg.setCanData(query[0], 0);
                msg.setCanData(query[1], 1);
                msg.setCanData(query[2], 2);
                msg.setCanData(query[3], 3);
                msg.setCanData(query[4], 4);
                msg.setCanData(query[5], 5);
                msg.setCanData(query[6], 6);
                msg.setCanData(query[7], 7);
                msg.setID(0x240);
                AddToCanTrace("Sending: 0x" + msg.getData().ToString("X16") + " id: 0x" + msg.getID().ToString("X4"));

                if (!sendMessage(msg))
                {
                    return -1;
                }
                uint reply_unit = GetReplyforUnit(unit);
                reply_unit = 0x258; // for testing purposes
                while (data[0] != 0x80 && data[0] != 0xC0)
                {
                    CANMessage replyMessage = new CANMessage();
                    AddToCanTrace("Waiting for ID: 0x" + reply_unit.ToString("X4"));
                    if (waitForMessage(reply_unit, 1000, out replyMessage) == reply_unit)
                    {
                        AddToCanTrace("Rx data: " + replyMessage.getData().ToString("X16"));
                        data[0] = replyMessage.getCanData(0);
                        data[1] = replyMessage.getCanData(1);
                        data[2] = replyMessage.getCanData(2);
                        data[3] = replyMessage.getCanData(3);
                        data[4] = replyMessage.getCanData(4);
                        data[5] = replyMessage.getCanData(5);
                        data[6] = replyMessage.getCanData(6);
                        data[7] = replyMessage.getCanData(7);
                        int idx = 0;
                        if ((data[0] & 0x40) > 0)
                        {
                            if (data[2] > 0x02)
                            {
                                length = data[2];   // subtract two non-payload bytes
                                length -= 2;
                                answer = new byte[length];
                            }
                            else length = 0;

                            if (--length > 0)
                            {
                                answer[idx++] = data[5];
                                rcv_length++;
                            }
                            if (--length > 0)
                            {
                                answer[idx++] = data[6];
                                rcv_length++;
                            }
                            if (--length > 0)
                            {
                                answer[idx++] = data[7];
                                rcv_length++;
                            }
                        }
                        else
                        {
                            for (i = 0; i < 6; i++)
                            {
                                answer[idx++] = data[2 + i];
                                length--;
                                rcv_length++;
                                if (length == 0) i = 6;
                            }
                        }
                        // Send acknowledgement
                        ack[3] = Convert.ToByte(data[0] & 0xBF);
                        CANMessage ackMessage = new CANMessage();
                        //ackMessage.setData(ack_long);
                        ackMessage.setCanData(ack[0], 0);
                        ackMessage.setCanData(ack[1], 1);
                        ackMessage.setCanData(ack[2], 2);
                        ackMessage.setCanData(ack[3], 3);
                        ackMessage.setCanData(ack[4], 4);
                        ackMessage.setCanData(ack[5], 5);
                        ackMessage.setCanData(ack[6], 6);
                        ackMessage.setCanData(ack[7], 7);

                        ackMessage.setID(0x266);
                        sendMessage(ackMessage);
                    }
                    else
                    {
                        // Timeout
                        AddToCanTrace("Timeout waiting for 0x" + reply_unit.ToString("X3"));
                        return -1;
                    }
                }
            }
            return rcv_length;
        }
예제 #22
0
 public override uint waitForMessage(uint a_canID, uint timeout, out CANMessage canMsg)
 {
     canMsg = new CANMessage();
     return 0;
 }
예제 #23
0
 /// <summary>
 /// This message sends a CANMessage to the CAN device.
 /// The open method must have been called and returned possitive result
 /// before this method is called.
 /// </summary>
 /// <param name="a_message">The CANMessage</param>
 /// <returns>true on success, otherwise false.</returns>
 public abstract bool sendMessage(CANMessage a_message);
예제 #24
0
        public CANMessage waitMessage(int a_timeout)
        {
            sw.Reset();
            sw.Start();
            CANMessage retMsg = new CANMessage();
            while (sw.ElapsedMilliseconds < a_timeout)
            {
                // search queue for the desired message
                if (_receiveMessageIndex < _readMessageIndex)
                {
                    // first upto (_queue.Length - 1)
                    for (int idx = _readMessageIndex; idx < _queue.Length; idx++)
                    {
                        if (_queue[idx].getID() == this.m_waitMsgID)
                        {
                            retMsg = _queue[idx];
                            _readMessageIndex = idx + 1;
                            if (_readMessageIndex > _queue.Length - 1) _readMessageIndex = 0; // make it circular

                            sw.Stop();
                            return retMsg;
                        }
                    }
                    for (int idx = 0; idx < _receiveMessageIndex; idx++)
                    {
                        if (_queue[idx].getID() == this.m_waitMsgID)
                        {
                            retMsg = _queue[idx];
                            _readMessageIndex = idx + 1;
                            if (_readMessageIndex > _queue.Length - 1) _readMessageIndex = 0; // make it circular
                            sw.Stop();
                            return retMsg;
                        }
                    }
                }
                else
                {
                    for (int idx = _readMessageIndex; idx < _receiveMessageIndex; idx++)
                    {
                        if (_queue[idx].getID() == this.m_waitMsgID)
                        {
                            retMsg = _queue[idx];
                            _readMessageIndex = idx + 1;
                            if (_readMessageIndex > _queue.Length - 1) _readMessageIndex = 0; // make it circular
                            sw.Stop();
                            return retMsg;
                        }
                    }
                }
                Thread.Sleep(1);
            }
            sw.Stop();
            return retMsg;

            /*
            CANMessage retMsg;

            m_resetEvent.WaitOne(a_timeout, true);
            lock (m_canMessage)
            {
                retMsg = m_canMessage;
            }
            messageReceived = false;
            return retMsg;*/
        }
예제 #25
0
 /// <summary>
 /// Send a message that starts a session. This is used to test if there is 
 /// a connection.
 /// </summary>
 /// <returns></returns>
 public bool sendSessionRequest(byte unit)
 {
     AddToCanTrace("Sending session request to unit 0x" + unit.ToString("X2"));
     for (int i = 0; i < 5; i++)
     {
         CANMessage msg1 = new CANMessage(0x7E0, 0, 8);
         msg1.setData(0x000040021100813f);
         //msg1.setData(0x000040021100813f);
         msg1.setCanData(unit, 5); // overwrite the 0x11 with UNIT
         AddToCanTrace("Sending: " + msg1.getData().ToString("X16") + " id: 0x" + msg1.getID().ToString("X4") + " " + i.ToString());
         if (!sendMessage(msg1))
         {
             AddToCanTrace("Unable to send session request");
             return false;
         }
         uint reponseID = GetIDforUnit(unit);
         AddToCanTrace("Waiting for ID: 0x" + reponseID.ToString("X4"));
         if (waitForMessage(reponseID, 500, out msg1) == reponseID)
         {
             AddToCanTrace("ResponseID seen");
             Console.WriteLine("ResponseID seen");
             return true;
         }
         AddToCanTrace("no reponse seen from unit 0x" + unit.ToString("X2"));
         Console.WriteLine("no reponse seen from unit 0x" + unit.ToString("X2"));
     }
     return false;
 }
예제 #26
0
 public override uint waitForMessage(uint a_canID, uint timeout, out CANMessage canMsg)
 {
     canMsg = new CANMessage();
     return(0);
 }
예제 #27
0
 public abstract uint waitForMessage(uint a_canID, uint timeout, out CANMessage canMsg);
예제 #28
0
        public void readMessages()
        {
            CANMessage canMessage = new CANMessage();

            byte[] receiveBuffer = new byte[1024]; // circular buffer for reception of data
            string receiveString = string.Empty;


            Console.WriteLine("readMessages started");
            while (true)
            {
                lock (m_synchObject)
                {
                    if (m_endThread)
                    {
                        Console.WriteLine("readMessages ended");
                        return;
                    }
                }
                if (m_serialPort.IsOpen)
                {
                    if (m_serialPort.BytesToRead > 0)
                    {
                        receiveString += m_serialPort.ReadExisting();
                        //Console.WriteLine("BUF1: " + receiveString);
                        receiveString = receiveString.Replace(">", "");       // remove prompt characters... we don't need that stuff
                        receiveString = receiveString.Replace("NO DATA", ""); // remove prompt characters... we don't need that stuff
                        while (receiveString.StartsWith("\n") || receiveString.StartsWith("\r"))
                        {
                            receiveString = receiveString.Substring(1, receiveString.Length - 1);
                        }

                        while (receiveString.Contains('\r'))
                        {
                            // process the line
                            int    idx       = receiveString.IndexOf('\r');
                            string rxMessage = receiveString.Substring(0, idx);
                            receiveString = receiveString.Substring(idx + 1, receiveString.Length - idx - 1);
                            while (receiveString.StartsWith("\n") || receiveString.StartsWith("\r"))
                            {
                                receiveString = receiveString.Substring(1, receiveString.Length - 1);
                            }
                            //Console.WriteLine("BUF2: " + receiveString);
                            // is it a valid line
                            if (rxMessage.Length >= 6)
                            {
                                try
                                {
                                    uint id = Convert.ToUInt32(rxMessage.Substring(0, 3), 16);
                                    if (MessageContainsInformationForRealtime(id))
                                    {
                                        canMessage.setID(id);
                                        canMessage.setLength(8);                // TODO: alter to match data
                                        canMessage.setData(0x0000000000000000); // reset message content
                                        byte b1 = Convert.ToByte(rxMessage.Substring(4, 2), 16);
                                        if (b1 < 7)
                                        {
                                            canMessage.setCanData(b1, 0);
                                            //Console.WriteLine("Byte 1: " + Convert.ToByte(rxMessage.Substring(4, 2), 16).ToString("X2"));
                                            if (b1 >= 1)
                                            {
                                                canMessage.setCanData(Convert.ToByte(rxMessage.Substring(7, 2), 16), 1);
                                            }
                                            if (b1 >= 2)
                                            {
                                                canMessage.setCanData(Convert.ToByte(rxMessage.Substring(10, 2), 16), 2);
                                            }
                                            if (b1 >= 3)
                                            {
                                                canMessage.setCanData(Convert.ToByte(rxMessage.Substring(13, 2), 16), 3);
                                            }
                                            if (b1 >= 4)
                                            {
                                                canMessage.setCanData(Convert.ToByte(rxMessage.Substring(16, 2), 16), 4);
                                            }
                                            if (b1 >= 5)
                                            {
                                                canMessage.setCanData(Convert.ToByte(rxMessage.Substring(19, 2), 16), 5);
                                            }
                                            if (b1 >= 6)
                                            {
                                                canMessage.setCanData(Convert.ToByte(rxMessage.Substring(22, 2), 16), 6);
                                            }
                                            if (b1 >= 7)
                                            {
                                                canMessage.setCanData(Convert.ToByte(rxMessage.Substring(25, 2), 16), 7);
                                            }
                                        }
                                        else
                                        {
                                            canMessage.setCanData(b1, 0);
                                            //Console.WriteLine("Byte 1: " + Convert.ToByte(rxMessage.Substring(4, 2), 16).ToString("X2"));
                                            canMessage.setCanData(Convert.ToByte(rxMessage.Substring(7, 2), 16), 1);
                                            canMessage.setCanData(Convert.ToByte(rxMessage.Substring(10, 2), 16), 2);
                                            canMessage.setCanData(Convert.ToByte(rxMessage.Substring(13, 2), 16), 3);
                                            canMessage.setCanData(Convert.ToByte(rxMessage.Substring(16, 2), 16), 4);
                                            canMessage.setCanData(Convert.ToByte(rxMessage.Substring(19, 2), 16), 5);
                                            canMessage.setCanData(Convert.ToByte(rxMessage.Substring(22, 2), 16), 6);
                                            canMessage.setCanData(Convert.ToByte(rxMessage.Substring(25, 2), 16), 7);
                                        }

                                        lock (m_listeners)
                                        {
                                            AddToCanTrace("RX: " + canMessage.getData().ToString("X16"));
                                            //Console.WriteLine("MSG: " + rxMessage);
                                            foreach (ICANListener listener in m_listeners)
                                            {
                                                listener.handleMessage(canMessage);
                                            }
                                        }
                                    }
                                }
                                catch (Exception)
                                {
                                    Console.WriteLine("MSG: " + rxMessage);
                                }
                            }
                        }
                    }
                    else
                    {
                        Thread.Sleep(1); // give others some air
                    }
                }
            }
            // parse the receive string


            /*int readResult = 0;
             * LAWICEL.CANMsg r_canMsg = new LAWICEL.CANMsg();
             * CANMessage canMessage = new CANMessage();
             * Console.WriteLine("readMessages started");
             * while (true)
             * {
             *  lock (m_synchObject)
             *  {
             *      if (m_endThread)
             *      {
             *          Console.WriteLine("readMessages ended");
             *          return;
             *      }
             *  }
             *  readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg);
             *  if (readResult == LAWICEL.ERROR_CANUSB_OK)
             *  {
             *      //Console.WriteLine(r_canMsg.id.ToString("X3") + " " + r_canMsg.data.ToString("X8"));
             *      if (MessageContainsInformationForRealtime(r_canMsg.id))
             *      {
             *          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)
             *          {
             *              AddToCanTrace("RX: " + r_canMsg.data.ToString("X16"));
             *              foreach (ICANListener listener in m_listeners)
             *              {
             *                  //while (listener.messagePending()) ; // dirty, make this better
             *                  listener.handleMessage(canMessage);
             *              }
             *              //CastInformationEvent(canMessage); // <GS-05042011> re-activated this function
             *          }
             *          //Thread.Sleep(1);
             *      }
             *
             *      // cast event to application to process message
             *      //if (MessageContainsInformationForRealtime(r_canMsg.id))
             *      //{
             *      //TODO: process all other known msg id's into the realtime view
             *      //  CastInformationEvent(canMessage); // <GS-05042011> re-activated this function
             *      //}
             *  }
             *  else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE)
             *  {
             *      Thread.Sleep(1);
             *  }
             * }
             */
        }
예제 #29
0
        //-------------------------------------------------------------------------
        //-------------------------------------------------------------------------
        /**
        Sends a 11 bit CAN data frame.

        @param      msg         CAN message

        @return                 success (true/false)
        */
        public override bool sendMessage(CANMessage msg)
        {
            //Console.WriteLine("TX: " + msg.getID().ToString("X4") + " " + msg.getData().ToString("X16"));
            this.AddToCanTrace("Sending message: " + msg.getID().ToString("X4") + " " + msg.getData().ToString("X16") + " " + msg.getLength().ToString("X2"));

            try
            {
            Combi.caCombiAdapter.caCANFrame frame;
            frame.id = msg.getID();
            frame.length = msg.getLength();
            frame.data = msg.getData();
            frame.is_extended = 0;
            frame.is_remote = 0;

            this.combi.CAN_SendMessage(ref frame);

            this.AddToCanTrace("Message sent successfully");
            return true;
            }

            catch (Exception e)
            {
            this.AddToCanTrace("Message failed to send: " + e.Message);
            return false;
            }
        }