//-------------------------------------------------------------------------

        /**
         *  Writes a CAN message to log file.
         *
         *  @param      r_canMsg        message
         *  @param      IsTransmit      ???
         */
        private void DumpCanMsg(CANMessage r_canMsg, bool IsTransmit)
        {
            DateTime dt = DateTime.Now;

            try
            {
                using (StreamWriter sw = new StreamWriter(Path.Combine(this.startup_path,
                                                                       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.getData() & 0x000000000000FF00);
                        transmitvalue /= 256;

                        sw.WriteLine(dt.ToString("dd/MM/yyyy HH:mm:ss") +
                                     " TX: id=" + r_canMsg.getID().ToString("D2") +
                                     " len= " + r_canMsg.getLength().ToString("X8") +
                                     " data=" + r_canMsg.getData().ToString("X16") +
                                     " " + r_canMsg.getFlags().ToString("X2") +
                                     " character = " + GetCharString(transmitvalue) +
                                     "\t ts: " + r_canMsg.getTimeStamp().ToString("X16") +
                                     " flags: " + r_canMsg.getFlags().ToString("X2"));
                    }
                    else
                    {
                        // get the byte received
                        int receivevalue = (int)(r_canMsg.getData() & 0x0000000000FF0000);
                        receivevalue /= (256 * 256);
                        sw.WriteLine(dt.ToString("dd/MM/yyyy HH:mm:ss") +
                                     " RX: id=" + r_canMsg.getID().ToString("D2") +
                                     " len= " + r_canMsg.getLength().ToString("X8") +
                                     " data=" + r_canMsg.getData().ToString("X16") +
                                     " " + r_canMsg.getFlags().ToString("X2") +
                                     " character = " + GetCharString(receivevalue) +
                                     "\t ts: " + r_canMsg.getTimeStamp().ToString("X16") +
                                     " flags: " + r_canMsg.getFlags().ToString("X2"));
                    }
                }
            }
            catch (Exception E)
            {
                Console.WriteLine("Failed to write to logfile: " + E.Message);
            }
        }
        //-------------------------------------------------------------------------

        /**
         *  Handles incoming messages.
         */
        private void read_messages()
        {
            uint  id;
            byte  length;
            ulong data;

            CANMessage msg = new CANMessage();

            Debug.Assert(msg != null);

            // main loop
            while (true)
            {
                // check tor thread termination request
                Debug.Assert(this.term_mutex != null);
                lock (this.term_mutex)
                {
                    if (this.term_requested)
                    {
                        return;
                    }
                }

                // receive messages
                while (MctAdapter_ReceiveMessage(out id, out length, out data))
                {
                    // convert message
                    msg.setID(id);
                    msg.setLength(length);
                    msg.setData(data);

                    // pass message to listeners
                    lock (this.m_listeners)
                    {
                        foreach (ICANListener listener in this.m_listeners)
                        {
                            listener.handleMessage(msg);
                        }
                    }
                }

                // give up CPU for a moment
                Thread.Sleep(1);
            }
        }
        /// <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)
        {
            LAWICEL.CANMsg msg = new LAWICEL.CANMsg();
            msg.id    = a_message.getID();
            msg.len   = a_message.getLength();
            msg.flags = a_message.getFlags();
            msg.data  = a_message.getData();
            if (m_DoLogging)
            {
                DumpCanMsg(msg, true);
            }
            if (m_port.IsOpen)
            {
                m_port.Write("\r");
                string txstring = "t";
                txstring += msg.id.ToString("X3");
                txstring += "8"; // always 8 bytes to transmit
                for (int t = 0; t < 8; t++)
                {
                    byte b = (byte)(((msg.data >> t * 8) & 0x0000000000000000FF));
                    txstring += b.ToString("X2");
                }
                txstring += "\r";
                m_port.Write(txstring);
//                Console.WriteLine("Send: " + txstring);
                return(true);
            }
            return(false);

            /*
             * int writeResult;
             * writeResult = LAWICEL.canusb_Write(m_deviceHandle, ref msg);
             * if (writeResult == LAWICEL.ERROR_CANUSB_OK)
             *  return true;
             * else
             *  return false;
             */
        }
Exemple #4
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)
        {
            LAWICEL.CANMsg msg = new LAWICEL.CANMsg();
            msg.id    = a_message.getID();
            msg.len   = a_message.getLength();
            msg.flags = a_message.getFlags();
            msg.data  = a_message.getData();
            if (m_DoLogging)
            {
                DumpCanMsg(msg, true);
            }

            int writeResult;

            writeResult = LAWICEL.canusb_Write(m_deviceHandle, ref msg);
            if (writeResult == LAWICEL.ERROR_CANUSB_OK)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        /// <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.data = a_message.getData();
            if (m_DoLogging)
            {
                DumpCanMsg(msg, true);
            }
            if (m_port.IsOpen)
            {
                m_port.Write("\r");
                string txstring = "t";
                txstring += msg.id.ToString("X3");
                txstring += "8"; // always 8 bytes to transmit
                for (int t = 0; t < 8; t++)
                {
                    byte b = (byte)(((msg.data >> t * 8) & 0x0000000000000000FF));
                    txstring += b.ToString("X2");
                }
                txstring += "\r";
                m_port.Write(txstring);
            //                Console.WriteLine("Send: " + txstring);
                return true;
            }
            return false;

            /*
            int writeResult;
            writeResult = LAWICEL.canusb_Write(m_deviceHandle, ref msg);
            if (writeResult == LAWICEL.ERROR_CANUSB_OK)
                return true;
            else
                return false;
             */
        }
        //-------------------------------------------------------------------------
        /**
        Sends a 11 bit CAN data frame.

        @param      message     CAN message

        @return                 success (true/false)
        */
        public override bool sendMessage(CANMessage a_message)
        {
            string sendString = "t";
            sendString += a_message.getID().ToString("X3");
            sendString += a_message.getLength().ToString("X1");
            for (uint i = 0; i < a_message.getLength(); i++) // leave out the length field, the ELM chip assigns that for us
            {
            sendString += a_message.getCanData(i).ToString("X2");
            }
            sendString += "\r";
            if (m_serialPort.IsOpen)
            {
            //AddToCanTrace("TX: " + a_message.getID().ToString("X3") + " " + a_message.getLength().ToString("X1") + " " + a_message.getData().ToString("X16"));
            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);
            Thread.Sleep(1);

            return true; // remove after implementation
        }
        //-------------------------------------------------------------------------
        /**
        Sends a 11 bit CAN data frame.

        @param      message     CAN message

        @return                 success (true/false)
        */
        public override bool sendMessage(CANMessage message)
        {
            return MctAdapter_SendMessage(message.getID(), message.getLength(),
            message.getData());
        }
        /// <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.data = a_message.getData();
            if (m_DoLogging)
            {
                DumpCanMsg(msg, true);
            }

            int writeResult;
            writeResult = LAWICEL.canusb_Write(m_deviceHandle, ref msg);
            if (writeResult == LAWICEL.ERROR_CANUSB_OK)
                return true;
            else
                return false;
        }
Exemple #9
0
        private string waitForResponse(int mstimeout)
        {
            string returnString = "";
            bool timeout = false;
            CANMessage response = new CANMessage();
            m_canListener.setupWaitMessage(0x00c);
            response = m_canListener.waitMessage(mstimeout);
            if (timeout)
            {
                return TIMEOUT;
            }
            /* if ((byte)response.getData() == 0xC4)
             {
                 response = m_canListener.waitForMessage(0x00C, 1000);
                 DumpCanMsg(response, false);
                 Console.WriteLine("received C4 command");
                 //throw new Exception("Error receiving data (1)");
             }

             else */
            if ((byte)response.getData() != 0xC6)
            {
                //DumpCanMsg(response, false);
                //Console.WriteLine("Error rx data (1)");
                //throw new Exception("Error receiving data (1)");
                return TIMEOUT;
            }
            if (response.getLength() < 8)
            {
                returnString = TIMEOUT;
                return returnString;
            }
            returnString += (char)((response.getData() >> 16) & 0xFF);
            sendAck();
            return returnString;
        }
Exemple #10
0
 /// <summary>
 /// Gets the checksum (C8 command) from the bootloader (non native command)
 /// </summary>
 /// <returns></returns>
 public byte[] getChecksum()
 {
     byte[] retData = new byte[8];
     CANMessage msg = new CANMessage(0x005, 0, 8);
     //            ulong cmd = 0xC000000000000000;
     ulong cmd = 0x00000000000000C8;
     msg.setData(cmd);
     m_canListener.setupWaitMessage(0x00c);
     if (!m_canDevice.sendMessage(msg))
         throw new Exception("Couldn't send message");
     CANMessage response = new CANMessage();
     response = m_canListener.waitMessage(10000);
     ulong data = response.getData();
     for (int i = 0; i < 8; i++)
         retData[7 - i] = (byte)(data >> i * 8);
     return retData;
 }
Exemple #11
0
 private void sendAck()
 {
     if (m_canDevice == null)
         throw new Exception("CAN device not set");
     CANMessage ack = new CANMessage(0x006, 0, 2);
     ack.setData(0x00000000000000C6);
     if (!m_canDevice.sendMessage(ack))
     {
      //   throw new Exception("Couldn't send message");
         Console.WriteLine("Couldn't send message");
     }
 }
        //-------------------------------------------------------------------------
        /**
        Sends a 11 bit CAN data frame.

        @param      msg         CAN message

        @return                 success (true/false)
        */
        public override bool sendMessage(CANMessage msg)
        {
            if (this.logging_enabled)
            {
            this.DumpCanMsg(msg, true);
            }

            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);
            return true;
            }

            catch (Exception e)
            {
            return false;
            }
        }
Exemple #13
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);
            }
        }
        //-------------------------------------------------------------------------
        /**
        Waits for any message to be received.

        @param      timeout     timeout
        @param      msg         message

        @return                 message ID if received, 0 otherwise
        */
        private uint wait_message(uint timeout, out CANMessage msg)
        {
            msg = new CANMessage();
            //Debug.Assert(msg != null);

            //int wait_cnt = 0;
            //uint id;
            //byte length;
            //ulong data;
            //while (wait_cnt < timeout)
            //{
            //    if (MctAdapter_ReceiveMessage(out id, out length, out data))
            //    {
            //        // message received
            //        msg.setID(id);
            //        msg.setLength(length);
            //        msg.setData(data);

            //        return id;
            //    }

            //    // wait a bit
            //    Thread.Sleep(1);
            //    ++wait_cnt;
            //}

            //// nothing was received
            return 0;
        }
Exemple #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
                }
            }
        }
Exemple #16
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;
            }
        }
Exemple #17
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);
Exemple #18
0
        //-------------------------------------------------------------------------
        /**
        Sends a 11 bit CAN data frame.

        @param      message     CAN message

        @return                 success (true/false)
        */
        public override bool sendMessage(CANMessage message)
        {
            //CANdoTransmit(CANdoUSB, ID_11_BIT, message.getID(), DATA_FRAME, 0, message.getData(),
            //return MctAdapter_SendMessage(message.getID(), message.getLength(),
            //message.getData());
            return true;
        }
Exemple #19
0
        private void sendCommandByteForRead(char a_commandByte)
        {
            //Console.WriteLine(">" + a_commandByte );
            CANMessage msg = new CANMessage(0x006, 0, 2);
            //Console.WriteLine("Send command byte for read");

            ulong cmd = 0x0000000000000000;
            cmd |= (ulong)a_commandByte;
            cmd <<= 8;
            cmd |= (ulong)0xC4;
            msg.setData(cmd);
            if (!m_canDevice.sendMessage(msg))
                throw new Exception("Couldn't send message");
        }
        // int thrdcnt = 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();

            while (true)
            {
                lock (m_synchObject)
                {
                    if (m_endThread)
                    {
                        m_endThread = false;
                        return;
                    }
                }
                if (m_port.IsOpen)
                {
                    // read the status?
                    string line = string.Empty;
                    m_port.Write("\r");
                    m_port.Write("A\r"); // poll for all pending CAN messages
                    //Console.WriteLine("Polled for frames");
                    Thread.Sleep(0);
                    bool endofFrames = false;
                    while (!endofFrames)
                    {
                        try
                        {
                            if (m_port.BytesToRead > 0)
                            {
                                line = m_port.ReadLine();
                                Console.Write("RX: ");
                                for (int tel = 0; tel < line.Length; tel++)
                                {
                                    byte b = (byte)line[tel];
                                    Console.Write(b.ToString("X2") + " ");
                                }
                                Console.WriteLine("");
                                Thread.Sleep(0);
                                if (line.Length > 0)
                                {
                                    if (line[0] == '\x07' || line[0] == '\r' || line[0] == 'A')
                                    {
                                        //Console.WriteLine("End of messages");
                                        endofFrames = true;
                                    }
                                    else
                                    {
                                        //t00C8C6003E0000000000
                                        //Console.WriteLine("line: " + line + " len: " + line.Length.ToString());
                                        if (line.Length == 21)
                                        {
                                            // three bytes identifier
                                            r_canMsg.id  = (uint)Convert.ToInt32(line.Substring(1, 3), 16);
                                            r_canMsg.len = (byte)Convert.ToInt32(line.Substring(4, 1), 16);
                                            ulong data = 0;
                                            // add all the bytes
                                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(5, 2), 16);
                                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(7, 2), 16) << 1 * 8;
                                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(9, 2), 16) << 2 * 8;
                                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(11, 2), 16) << 3 * 8;
                                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(13, 2), 16) << 4 * 8;
                                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(15, 2), 16) << 5 * 8;
                                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(17, 2), 16) << 6 * 8;
                                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(19, 2), 16) << 7 * 8;
                                            r_canMsg.data = data;
                                            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);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception E)
                        {
                            //Console.WriteLine("Failed to read frames from CANbus: " + E.Message);
                        }
                        Thread.Sleep(0);
                    }
                }
                Thread.Sleep(1);
            }
        }
Exemple #21
0
        //-------------------------------------------------------------------------
        /**
        Handles incoming messages.
        */
        private void read_messages()
        {
            uint id;
            byte length;
            ulong data;

            CANMessage msg = new CANMessage();
            Debug.Assert(msg != null);

            // main loop
            while (true)
            {
            // check tor thread termination request
            Debug.Assert(this.term_mutex != null);
            lock (this.term_mutex)
            {
                if (this.term_requested)
                {
                    return;
                }
            }

            // receive messages
            while (MctAdapter_ReceiveMessage(out id, out length, out data))
            {
                // convert message
                msg.setID(id);
                msg.setLength(length);
                msg.setData(data);

                // pass message to listeners
                lock (this.m_listeners)
                {
                    foreach (ICANListener listener in this.m_listeners)
                    {
                        listener.handleMessage(msg);
                    }
                }
            }

            // give up CPU for a moment
            Thread.Sleep(1);
            }
        }
Exemple #22
0
        private void sendCommandByte(char a_commandByte)
        {
            //int max_sends = 10; // revert back to 5000!
            int max_sends = 5000; // revert back to 5000!
            CANMessage msg = new CANMessage(0x005, 0, 8);
            //Console.WriteLine("Send command byte");

            ulong cmd = 0x0000000000000000;
            cmd |= (ulong)a_commandByte;
            cmd <<= 8;
            cmd |= (ulong)0xC4;
            cmd |= 0xFFFFFFFFFFFF0000;
            msg.setData(cmd);
            uint nrOfResends = 0;
            while (!m_canDevice.sendMessage(msg))
            {
                if(nrOfResends++ > max_sends)
                    throw new Exception("Couldn't send message"); ;
            }
            if (nrOfResends < max_sends)
            {
                //Console.WriteLine("Sending succeeded");
            }
        }
        /// <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 LAWICEL.CANMsg r_canMsg)
        {
            CANMessage canMessage = new CANMessage();

            string line = string.Empty;

            int readResult = 0;
            int nrOfWait   = 0;

            while (nrOfWait < timeout)
            {
                m_port.Write("\r");
                m_port.Write("P\r");
                bool endofFrames = false;
                while (!endofFrames)
                {
                    Console.WriteLine("reading line");
                    line = m_port.ReadLine();
                    Console.WriteLine("line: " + line + " len: " + line.Length.ToString());
                    if (line[0] == '\x07' || line[0] == '\r' || line[0] == 'A')
                    {
                        endofFrames = true;
                    }
                    else
                    {
                        if (line.Length == 14)
                        {
                            // three bytes identifier
                            r_canMsg     = new LAWICEL.CANMsg();
                            r_canMsg.id  = (uint)Convert.ToInt32(line.Substring(1, 3), 16);
                            r_canMsg.len = (byte)Convert.ToInt32(line.Substring(4, 1), 16);
                            ulong data = 0;
                            // add all the bytes
                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(5, 2), 16) << 7 * 8;
                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(7, 2), 16) << 6 * 8;
                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(9, 2), 16) << 5 * 8;
                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(11, 2), 16) << 4 * 8;
                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(13, 2), 16) << 3 * 8;
                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(15, 2), 16) << 2 * 8;
                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(17, 2), 16) << 1 * 8;
                            data         |= (ulong)(byte)Convert.ToInt32(line.Substring(19, 2), 16);
                            r_canMsg.data = data;
                            canMessage.setID(r_canMsg.id);
                            canMessage.setLength(r_canMsg.len);
                            //canMessage.setTimeStamp(r_canMsg.timestamp);
                            canMessage.setFlags(0);
                            canMessage.setData(r_canMsg.data);
                            if (m_DoLogging)
                            {
                                DumpCanMsg(r_canMsg, false);
                            }
                            return((uint)r_canMsg.id);
                        }
                    }
                }
                Thread.Sleep(1);
                nrOfWait++;
            }
            r_canMsg = new LAWICEL.CANMsg();
            return(0);
        }
Exemple #24
0
        // Stopwatch sw = new Stopwatch();
        // reading data from T5 ECU
        // address should only be a 16 bit address to read SRAM before the bootloader has been loaded
        // address can be a 32 bit address to read FLASH BIN only after the bootloader has been loaded
        private byte[] sendReadCommand(UInt32 address)
        {
            byte[] retData = new byte[6];
            CANMessage msg = new CANMessage(0x005, 0, 8);
            ulong cmd = 0x00000000000000C7;
            //            Console.WriteLine("Send read command");
            cmd |= (ulong)(byte)(address & 0x000000FF) << 4 * 8;
            cmd |= (ulong)(byte)((address & 0x0000FF00) >> 8) << 3 * 8;
            cmd |= (ulong)(byte)((address & 0x00FF0000) >> 2 * 8) << 2 * 8;
            cmd |= (ulong)(byte)((address & 0xFF000000) >> 3 * 8) << 8;
            //cmd |= (ulong)((byte)(address)) << 4 * 8;
            //cmd |= (ulong)((byte)(address >> 8)) << 3*8;
            msg.setData(cmd);
            m_canListener.setupWaitMessage(0x00c);
            //sw.Reset();
            //sw.Start();

            if (!m_canDevice.sendMessage(msg))
            {
                Console.WriteLine("Couldn't send message");
            }
            //sw.Stop();
            //Console.WriteLine("Send took " + sw.ElapsedMilliseconds.ToString() + " ms");
            CANMessage response = new CANMessage();
            //sw.Reset();
            //sw.Start();

            //response = m_canListener.waitForMessage(0x00C, 1000);
            //CANMessage response = new CANMessage();
            response = m_canListener.waitMessage(1000);
            //sw.Stop();
            //Console.WriteLine("Wait took " + sw.ElapsedMilliseconds.ToString() + " ms");
            ulong data = response.getData();
            for (int i = 2; i < 8; i++)
                retData[7 - i] = (byte)(data >> i * 8);

            return retData;
        }
Exemple #25
0
        //-------------------------------------------------------------------------

        /**
         *  Sends a 11 bit CAN data frame.
         *
         *  @param      message     CAN message
         *
         *  @return                 success (true/false)
         */
        public override bool sendMessage(CANMessage message)
        {
            return(MctAdapter_SendMessage(message.getID(), message.getLength(),
                                          message.getData()));
        }
Exemple #26
0
        private string waitNoAck()
        {
            string returnString = "";
            bool timeout = false;
            CANMessage response = new CANMessage();
            m_canListener.setupWaitMessage(0x00c);
            response = m_canListener.waitMessage(2000);
            if (timeout)
            {
                return TIMEOUT;
            }
            if ((byte)response.getData() != 0xC6)
            {
                byte b = (byte)response.getData();
                Console.WriteLine("Error rx data (2): "  + b.ToString("X2"));
                //throw new Exception("Error receiving data (2)");

            }
            if (response.getLength() < 8)
            {
                returnString = "";
                return returnString;
            }
            returnString += (char)((response.getData() >> 16) & 0xFF);
            return returnString;
        }
Exemple #27
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);
Exemple #28
0
        /// <summary>
        /// Sends a C9 command to the bootloader to get offset address and
        /// FLASH chip types
        /// </summary>
        /// <returns></returns>
        public byte[] GetChipTypes()
        {
            byte[] retData = new byte[6];
            CANMessage msg = new CANMessage(0x005, 0, 8);
            //            ulong cmd = 0xC000000000000000;
            ulong cmd = 0x00000000000000C9;
            msg.setData(cmd);

            m_canListener.setupWaitMessage(0x00c);
            if (!m_canDevice.sendMessage(msg))
                throw new Exception("Couldn't send message");

            CANMessage response = new CANMessage();
            response = m_canListener.waitMessage(1000);

            /*
            if (!m_canDevice.sendMessage(msg))
                throw new Exception("Couldn't send message");
            response = new CANMessage();
            response = m_canListener.waitForMessage(0x00C, 1000);*/
            ulong data = response.getData();
            for (int i = 2; i < 8; i++)
                retData[7 - i] = (byte)(data >> i * 8);
            return retData;
            // unit responded 20 01 00 00 04 00
            //                || || |      | - 0x40000 = T5.5
            //                || || - 0x01 = AMD
            //                || - 0x20 = 29F010
        }
Exemple #29
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);
Exemple #30
0
        // sending data from bootloader or flash file
        public byte[] sendBootloaderDataCommand(byte[] data, byte len)
        {
            byte[] retData = new byte[8];
            CANMessage msg = new CANMessage(0x005, 0, 8);
            ulong cmd = 0x0000000000000000;
            //cmd |= (ulong)((byte)(len)) << 7 * 8;
              //  cmd |= (ulong)((byte)(len)) ;
            /*int cnt = 0;
            foreach (byte b in data)
            {
                cmd |= (ulong)((byte)b) << (7-cnt) * 8;
                cnt++;
            }*/
            int cnt = 0;
            foreach (byte b in data)
            {
                //if (cnt > 0)
                {
                    cmd |= (ulong)((byte)b) << (cnt) * 8;
                }
                cnt++;
            }
            //cmd &= 0xFFFFFFFFFFFFFF00;

            //cmd |= (ulong)((byte)(len));
            msg.setData(cmd);
            m_canListener.setupWaitMessage(0x00c);
            if (!m_canDevice.sendMessage(msg))
                throw new Exception("Couldn't send message");
            CANMessage response = new CANMessage();
            response = m_canListener.waitMessage(1000);
            ulong uldata = response.getData();
            for (int i = 0; i < 8; i++)
                retData[7 - i] = (byte)(uldata >> i * 8);
            return retData;
        }
Exemple #31
0
        public byte[] sendBootVectorAddressSRAM(Int32 address)
        {
            byte[] retData = new byte[8];
            CANMessage msg = new CANMessage(0x005, 0, 8);
            /*            ulong cmd = 0xC100000000000000;
            cmd |= (ulong)((byte)(address >> 8)) << 4 * 8;
            cmd |= (ulong)((byte)(address)) << 3 * 8;*/
            ulong cmd = 0x00000000000000C1;
            cmd |= (ulong)(byte)(address & 0x0000FF) << 4 * 8;
            cmd |= (ulong)(byte)((address & 0x00FF00) >> 8) << 3 * 8;
            cmd |= (ulong)(byte)((address & 0xFF0000) >> 2 * 8) << 2 * 8;

            /*            cmd |= (ulong)((byte)(address >> 2 * 8)) << 2 * 8;
            cmd |= (ulong)((byte)(address >> 8)) << 3 * 8;
            cmd |= (ulong)((byte)(address)) << 4 * 8;
            */
            msg.setData(cmd);
            m_canListener.setupWaitMessage(0x00c);
            if (!m_canDevice.sendMessage(msg))
                throw new Exception("Couldn't send message");
            CANMessage response = new CANMessage();
            response = m_canListener.waitMessage(1000);
            ulong data = response.getData();
            for (int i = 0; i < 8; i++)
                retData[7 - i] = (byte)(data >> i * 8);
            return retData;
        }
Exemple #32
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();

               /* Stopwatch
            while (true)
            {
                lock (m_synchObject)
                {
                    if (m_endThread)
                        return;
                }
                readResult = LAWICEL.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 == LAWICEL.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 = LAWICEL.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 == LAWICEL.ERROR_CANUSB_NO_MESSAGE)
                {
                    Thread.Sleep(1); // changed to 0 to see performance impact <GS-13122010>
                }
            }
        }
Exemple #33
0
 /// <summary>
 /// Sends a C3 command to the ECU or bootloader
 /// </summary>
 /// <returns></returns>
 public byte[] sendC3Command()
 {
     byte[] retData = new byte[6];
     CANMessage msg = new CANMessage(0x005, 0, 8);
     //            ulong cmd = 0xC000000000000000;
     ulong cmd = 0x00000000000000C3;
     msg.setData(cmd);
     m_canListener.setupWaitMessage(0x00c);
     if (!m_canDevice.sendMessage(msg))
         throw new Exception("Couldn't send message");
     CANMessage response = new CANMessage();
     response = m_canListener.waitMessage(1000);
     ulong data = response.getData();
     for (int i = 2; i < 8; i++)
         retData[7 - i] = (byte)(data >> i * 8);
     return retData;
     // unit responded B8 89 FF FF 07 00
 }
        // int thrdcnt = 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();

            while (true)
            {

                lock (m_synchObject)
                {
                    if (m_endThread)
                    {
                        m_endThread = false;
                        return;
                    }
                }
                if (m_port.IsOpen)
                {
                    // read the status?
                    string line = string.Empty;
                    m_port.Write("\r");
                    m_port.Write("A\r"); // poll for all pending CAN messages
                    //Console.WriteLine("Polled for frames");
                    Thread.Sleep(0);
                    bool endofFrames = false;
                    while(!endofFrames)
                    {

                        try
                        {
                            if (m_port.BytesToRead > 0)
                            {
                                line = m_port.ReadLine();
                                Console.Write("RX: ");
                                for (int tel = 0; tel < line.Length; tel++)
                                {
                                    byte b = (byte)line[tel];
                                    Console.Write(b.ToString("X2") + " " );
                                }
                                Console.WriteLine("") ;
                                Thread.Sleep(0);
                                if (line.Length > 0)
                                {
                                    if (line[0] == '\x07' || line[0] == '\r' || line[0] == 'A')
                                    {
                                        //Console.WriteLine("End of messages");
                                        endofFrames = true;
                                    }
                                    else
                                    {
                                        //t00C8C6003E0000000000
                                        //Console.WriteLine("line: " + line + " len: " + line.Length.ToString());
                                        if (line.Length == 21)
                                        {
                                            // three bytes identifier
                                            r_canMsg.id = (uint)Convert.ToInt32(line.Substring(1, 3), 16);
                                            r_canMsg.len = (byte)Convert.ToInt32(line.Substring(4, 1), 16);
                                            ulong data = 0;
                                            // add all the bytes
                                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(5, 2), 16);
                                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(7, 2), 16) << 1 * 8;
                                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(9, 2), 16) << 2 * 8;
                                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(11, 2), 16) << 3 * 8;
                                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(13, 2), 16) << 4 * 8;
                                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(15, 2), 16) << 5 * 8;
                                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(17, 2), 16) << 6 * 8;
                                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(19, 2), 16) << 7 * 8;
                                            r_canMsg.data = data;
                                            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);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception E)
                        {
                            //Console.WriteLine("Failed to read frames from CANbus: " + E.Message);
                        }
                        Thread.Sleep(0);

                    }
                }
                Thread.Sleep(1);
            }
        }
Exemple #35
0
 /// <summary>
 /// Sends a free form command to the ECU or bootloader
 /// </summary>
 /// <param name="cmd"></param>
 /// <returns></returns>
 public byte[] sendFreeCommand(ulong cmd)
 {
     byte[] retData = new byte[8];
     CANMessage msg = new CANMessage(0x005, 0, 8);
     msg.setData(cmd);
     m_canListener.setupWaitMessage(0x00c);
     if (!m_canDevice.sendMessage(msg))
         throw new Exception("Couldn't send message");
     CANMessage response = new CANMessage();
     response = m_canListener.waitMessage(10000); // checksum command (C8) may take longer than 1 second, so set it to 10
     ulong data = response.getData();
     for (int i = 0; i < 8; i++)
         retData[7 - i] = (byte)(data >> i * 8);
     return retData;
 }
        /// <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 LAWICEL.CANMsg r_canMsg)
        {
            CANMessage canMessage = new CANMessage();

            string line = string.Empty;
            int readResult = 0;
            int nrOfWait = 0;
            while (nrOfWait < timeout)
            {
                m_port.Write("\r");
                m_port.Write("P\r");
                bool endofFrames = false;
                while (!endofFrames)
                {
                    Console.WriteLine("reading line");
                    line = m_port.ReadLine();
                    Console.WriteLine("line: " + line + " len: " + line.Length.ToString());
                    if (line[0] == '\x07' || line[0] == '\r' || line[0] == 'A')
                    {
                        endofFrames = true;
                    }
                    else
                    {

                        if (line.Length == 14)
                        {
                            // three bytes identifier
                            r_canMsg = new LAWICEL.CANMsg();
                            r_canMsg.id = (uint)Convert.ToInt32(line.Substring(1, 3), 16);
                            r_canMsg.len = (byte)Convert.ToInt32(line.Substring(4, 1), 16);
                            ulong data = 0;
                            // add all the bytes
                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(5, 2), 16) << 7 * 8;
                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(7, 2), 16) << 6 * 8;
                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(9, 2), 16) << 5 * 8;
                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(11, 2), 16) << 4 * 8;
                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(13, 2), 16) << 3 * 8;
                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(15, 2), 16) << 2 * 8;
                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(17, 2), 16) << 1 * 8;
                            data |= (ulong)(byte)Convert.ToInt32(line.Substring(19, 2), 16);
                            r_canMsg.data = data;
                            canMessage.setID(r_canMsg.id);
                            canMessage.setLength(r_canMsg.len);
                            //canMessage.setTimeStamp(0);
                            canMessage.setFlags(0);
                            canMessage.setData(r_canMsg.data);
                            if (m_DoLogging)
                            {
                                DumpCanMsg(r_canMsg, false);
                            }
                            if (r_canMsg.id != a_canID)
                                continue;
                            return (uint)r_canMsg.id;

                        }
                    }

                }
                Thread.Sleep(1);
                nrOfWait++;
            }
            r_canMsg = new LAWICEL.CANMsg();
            return 0;
        }
Exemple #37
0
        private void DumpCanMsg(CANMessage canMsg, bool IsTransmit)
        {
            DateTime dt = DateTime.Now;
            try
            {
                using (StreamWriter sw = new StreamWriter(@"c:\" + dt.Year.ToString("D4") + dt.Month.ToString("D2") + dt.Day.ToString("D2") + "-CanTrace.log", true))
                {
                    if (IsTransmit)
                    {
                        // get the byte transmitted
                        int transmitvalue = (int)(canMsg.getData() & 0x000000000000FF00);
                        transmitvalue /= 256;

                        sw.WriteLine(dt.ToString("dd/MM/yyyy HH:mm:ss") + " TX: id=" + canMsg.getID().ToString("X2") + " len= " + canMsg.getLength().ToString("X8") + " data=" + canMsg.getData().ToString("X16") + " " + canMsg.getFlags().ToString("X2") + "\t ts: " + canMsg.getTimeStamp().ToString("X16") + " flags: " + canMsg.getFlags().ToString("X2"));
                    }
                    else
                    {
                        // get the byte received
                        int receivevalue = (int)(canMsg.getData() & 0x0000000000FF0000);
                        receivevalue /= (256 * 256);
                        sw.WriteLine(dt.ToString("dd/MM/yyyy HH:mm:ss") + " RX: id=" + canMsg.getID().ToString("X2") + " len= " + canMsg.getLength().ToString("X8") + " data=" + canMsg.getData().ToString("X16") + " " + canMsg.getFlags().ToString("X2") + "\t ts: " + canMsg.getTimeStamp().ToString("X16") + " flags: " + canMsg.getFlags().ToString("X2"));
                    }
                }
            }
            catch (Exception E)
            {
                Console.WriteLine("Failed to write to logfile: " + E.Message);
            }
        }
        //-------------------------------------------------------------------------
        /**
        Handles incoming messages.
        */
        private void readMessages()
        {
            CANMessage canMessage = new CANMessage();
            string rxMessage = string.Empty;

            Console.WriteLine("readMessages started");
            while (true)
            {
            lock (m_synchObject)
            {
                if (m_endThread)
                {
                    Console.WriteLine("readMessages ended");
                    return;
                }
            }

            try
            {
                if (m_serialPort.IsOpen)
                {
                    do
                    {
                        rxMessage = m_serialPort.ReadLine();
                        rxMessage = rxMessage.Replace("\r", ""); // remove prompt characters... we don't need that stuff
                        rxMessage = rxMessage.Replace("\n", ""); // remove prompt characters... we don't need that stuff
                    } while (rxMessage.StartsWith("w") == false);

                    uint id = Convert.ToUInt32(rxMessage.Substring(1, 3), 16);
                    canMessage.setID(id);
                    canMessage.setLength(8);
                    canMessage.setData(0x0000000000000000);
                    for (uint i = 0; i < 8; i++)
                        canMessage.setCanData(Convert.ToByte(rxMessage.Substring(5 + (2 * (int)i), 2), 16), i);

                    lock (m_listeners)
                    {
                        //AddToCanTrace("RX: " + canMessage.getID().ToString("X3") + " " + canMessage.getLength().ToString("X1") + " " + canMessage.getData().ToString("X16"));
                        //Console.WriteLine("MSG: " + rxMessage);
                        foreach (ICANListener listener in m_listeners)
                        {
                            listener.handleMessage(canMessage);
                        }
                        //CastInformationEvent(canMessage); // <GS-05042011> re-activated this function
                    }
                    // give up CPU for a moment
                    Thread.Sleep(1);
                }
            }
            catch (Exception)
            {
                Console.WriteLine("MSG: " + rxMessage);
            }
            }
        }
Exemple #39
0
 private void findSynch()
 {
     //System.console.WriteLine("###### Looking for synch ######");
     string str = "";
     char ch;
     bool timeout = false;
     CANMessage ack = new CANMessage(0x006, 0, 2);
     CANMessage response = new CANMessage();
     ack.setData(0x00000000000000C6);
     do
     {
         m_canListener.setupWaitMessage(0x00c);
         if (!m_canDevice.sendMessage(ack))
             throw new Exception("Couldn't send message");
         response = m_canListener.waitMessage(1000);
         if (timeout)
         {
             return;
         }
         if ((byte)response.getData() != 0xC6)
         {
             //Console.WriteLine("Error rx data (3)");
             throw new Exception("Error receiving data (3)");
         }
         ch = (char)((response.getData() >> 16) & 0xFF);
         str += ch;
         if (str.EndsWith(CR + NL + NL + NL))
         {
             return;
         }
         if (str.EndsWith(NL + NL + NL + NL))
             str = sendCommand(CR, 1);
     }
     while (!str.EndsWith("END" + CR + NL));
 }
Exemple #40
0
        //-------------------------------------------------------------------------
        /**
        Waits for any message to be received.

        @param      timeout     timeout
        @param      msg         message

        @return                 message ID if received, 0 otherwise
        */
        private uint wait_message(uint timeout, out CANMessage msg)
        {
            msg = new CANMessage();
            return 0;
               /* msg = new CANMessage();
            Debug.Assert(msg != null);

            int wait_cnt = 0;
            uint id;
            byte length;
            ulong data;
            while (wait_cnt < timeout)
            {
            if (MctAdapter_ReceiveMessage(out id, out length, out data))
            {
                // message received
                msg.setID(id);
                msg.setLength(length);
                msg.setData(data);

                return id;
            }

            // wait a bit
            Thread.Sleep(1);
            ++wait_cnt;
            }

            // nothing was received
            return 0;
            */
        }
Exemple #41
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);