Exemplo n.º 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();
             * }*/
        }
Exemplo n.º 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();
            }*/
        }
Exemplo n.º 3
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;
     }
 }
Exemplo n.º 4
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;
 }
Exemplo n.º 5
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;
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
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);
 }
Exemplo n.º 8
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;
            }
        }