예제 #1
0
        public override bool sendMessage(CANMessage a_message)
        {
            string sendString = "  ";
            /*if (a_message.getID() == 0x11)
            {
                // try to insert length ourselves
                sendString = " ";
                sendString += a_message.getLength().ToString("X2");
            }*/
            Console.WriteLine("send: " + a_message.getID().ToString("X3") + " " + a_message.getData().ToString("X16"));

            if (a_message.getID() != _ECUAddress)
            {
                _ECUAddress = a_message.getID();

                string command = "ATSH" + a_message.getID().ToString("X3");
                /*if (_ECUAddress == 0x11)
                {
                    command = "ATSH 0000" + a_message.getID().ToString("X3");
                }*/
                m_serialPort.Write(command + "\r");    //Set header to 7E0 = ECU
                AddToSerialTrace("SERTX: " + command);
                CastInformationEvent("Switching to ID: " + a_message.getID().ToString("X3"));
                string answer = m_serialPort.ReadTo(">");
                CastInformationEvent(command + " response: " + answer);

            }

            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 = sendString.Trim();
            sendString += "\r";

            if (m_serialPort.IsOpen)
            {

                m_serialPort.Write(sendString);
                AddToSerialTrace("SERTX: " + sendString);

                //Console.WriteLine("TX: " + sendString);
                AddToCanTrace("TX: " + a_message.getID().ToString("X3") + " " + sendString);
            }

            // bitrate = 38400bps -> 4800 bytes per second
            // sending each byte will take 0.2 ms approx
            // bitrate = 115200bps -> 14400 bytes per second
            // sending each byte will take 0,07 ms approx
            Thread.Sleep(2); // sleep length ms

             //07E0 0000000000003E01
            if (a_message.getID() == 0x7E0 && a_message.getCanData(0) == 0x01 && a_message.getCanData(1) == 0x3E)
            {
                //m_serialPort.Write("ATMA\r");
                //AddToSerialTrace("SERTX: ATMA");
            }

            //            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
        }
예제 #2
0
        private bool RequestSecurityAccessCIM(int millisecondsToWaitWithResponse)
        {
            int secondsToWait = millisecondsToWaitWithResponse / 1000;
            ulong cmd = 0x0000000000012702; // request security access
            CANMessage msg = new CANMessage(0x245, 0, 8);
            msg.setData(cmd);
            m_canListener.setupWaitMessage(0x645);
            CastInfoEvent("Requesting security access to CIM", ActivityType.ConvertingFile);
            if (!canUsbDevice.sendMessage(msg))
            {
                Console.WriteLine("Couldn't send message");
            }
            CANMessage response = new CANMessage();
            response = m_canListener.waitMessage(1000);
            //ulong data = response.getData();
            Console.WriteLine("---" + response.getData().ToString("X16"));
            if (response.getCanData(1) == 0x67)
            {
                if (response.getCanData(2) == 0x01)
                {
                    CastInfoEvent("Got seed value from CIM", ActivityType.ConvertingFile);
                    while (secondsToWait > 0)
                    {
                        CastInfoEvent("Waiting for " + secondsToWait.ToString() + " seconds...", ActivityType.UploadingBootloader);
                        Thread.Sleep(1000);
                        SendKeepAlive();
                        secondsToWait--;

                    }
                    byte[] seed = new byte[2];
                    seed[0] = response.getCanData(3);
                    seed[1] = response.getCanData(4);
                    if (seed[0] == 0x00 && seed[1] == 0x00)
                    {
                        return true; // security access was already granted
                    }
                    else
                    {
                        SeedToKey s2k = new SeedToKey();
                        byte[] key = s2k.calculateKeyForCIM(seed);
                        CastInfoEvent("Security access CIM : Key (" + key[0].ToString("X2") + key[1].ToString("X2") + ") calculated from seed (" + seed[0].ToString("X2") + seed[1].ToString("X2") + ")", ActivityType.ConvertingFile);

                        ulong keydata = 0x0000000000022704;
                        ulong key1 = key[1];
                        key1 *= 0x100000000;
                        keydata ^= key1;
                        ulong key2 = key[0];
                        key2 *= 0x1000000;
                        keydata ^= key2;
                        msg = new CANMessage(0x245, 0, 8);
                        msg.setData(keydata);
                        m_canListener.setupWaitMessage(0x645);
                        if (!canUsbDevice.sendMessage(msg))
                        {
                            Console.WriteLine("Couldn't send message");
                        }
                        response = new CANMessage();
                        response = m_canListener.waitMessage(1000);
                        // is it ok or not
                        if (response.getCanData(1) == 0x67 && response.getCanData(2) == 0x02)
                        {
                            CastInfoEvent("Security access to CIM granted", ActivityType.ConvertingFile);
                            return true;
                        }
                    }

                }
                else if (response.getCanData(2) == 0x02)
                {
                    CastInfoEvent("Security access to CIM granted", ActivityType.ConvertingFile);
                    return true;
                }
            }
            else if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0x27)
            {
                Console.WriteLine("Casting error");
                string info = TranslateErrorCode(response.getCanData(3));
                Console.WriteLine("Casting error: " + info);
                CastInfoEvent("Error: " + info, ActivityType.ConvertingFile);
            }
            return false;
        }
예제 #3
0
        /// <summary>
        /// Send a KWP request.
        /// </summary>
        /// <param name="a_request">A KWP request.</param>
        /// <param name="r_reply">A KWP reply.</param>
        /// <returns>The status of the request.</returns>
        public RequestResult sendRequest(KWPRequest a_request, out KWPReply r_reply)
        {
            CANMessage msg = new CANMessage(0x7E0, 0, 8);
            uint row = nrOfRowsToSend(a_request.getData());

            m_kwpCanListener.setupWaitMessage(0x7E8);

            // Send one or several request messages.
            for (; row > 0; row--)
            {
                msg.setData(createCanMessage(a_request.getData(), row - 1));
                if (!m_canDevice.sendMessage(msg))
                {
                    r_reply = new KWPReply();
                    return RequestResult.ErrorSending;
                }
            }

            msg = m_kwpCanListener.waitMessage(timeoutPeriod);
             //         msg = m_kwpCanListener.waitForMessage(0x7E8, timeoutPeriod);

            // Receive one or several replys and send an ack for each reply.
            if (msg.getID() == 0x7E8)
            {
                uint nrOfRows = (uint)(msg.getCanData(0) & 0x3F)+ 1;
                row = 0;
                if (nrOfRows == 0)
                    throw new Exception("Wrong nr of rows");
                //Assume that no KWP reply contains more than 0x200 bytes
                byte[] reply = new byte[0x200];
                reply = collectReply(reply, msg.getData(), row);
                sendAck(nrOfRows - 1);
                nrOfRows--;

                m_kwpCanListener.setupWaitMessage(0x7E8);

                while (nrOfRows > 0)
                {
            //                    msg = m_kwpCanListener.waitForMessage(0x7E8, timeoutPeriod);
                    msg = m_kwpCanListener.waitMessage(timeoutPeriod);
                    if (msg.getID() == 0x7E8)
                    {
                        row++;
                        reply = collectReply(reply, msg.getData(), row);
                        sendAck(nrOfRows - 1);
                        nrOfRows--;
                    }
                    else
                    {
                        r_reply = new KWPReply();
                        return RequestResult.Timeout;
                    }

                }
                r_reply = new KWPReply(reply, a_request.getNrOfPID());
                return RequestResult.NoError;
            }
            else
            {
                r_reply = new KWPReply();
                return RequestResult.Timeout;
            }
        }
예제 #4
0
        public float GetE85Percentage()
        {
            float retval = 0;
            GetDiagnosticDataIdentifier();

            CastInfoEvent("Request 0xAA", ActivityType.ConvertingFile);
            CANMessage msg = new CANMessage(0x7E0, 0, 3);//<GS-18052011> ELM327 support requires the length byte
            ulong cmd = 0x000000007A01AA03;// <dpid=7A> <level=sendOneResponse> <service=AA> <length>
            msg.setData(cmd);
            m_canListener.setupWaitMessage(0x5E8);
            if (!canUsbDevice.sendMessage(msg))
            {
                Console.WriteLine("Couldn't send message");
            }
            CANMessage response = new CANMessage();
            response = m_canListener.waitMessage(1000);
            // 7A 00 52 04 00 16 DC 00
            // <dpid><6 datavalues>
            if (response.getCanData(0) == 0x7A)
            {
                retval = Convert.ToInt32(response.getCanData(2));
            }
            // Negative Response 0x7F Service <nrsi> <service> <returncode>
            // Bug: this is never handled because its sent with id=0x7E8
            else if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0xAA)
            {
                string info = TranslateErrorCode(response.getCanData(3));
                CastInfoEvent("Error: " + info, ActivityType.ConvertingFile);
            }
            return retval;
        }
예제 #5
0
        private bool RequestSecurityAccess011(int millisecondsToWaitWithResponse)
        {
            int secondsToWait = millisecondsToWaitWithResponse / 1000;
            ulong cmd = 0x0000000000FD2702; // request security access
            if (_securityLevel == AccessLevel.AccessLevel01)
            {
                cmd = 0x0000000000012702; // request security access
            }
            else if (_securityLevel == AccessLevel.AccessLevelFB)
            {
                cmd = 0x0000000000FB2702; // request security access
            }
            CANMessage msg = new CANMessage(0x11, 0, 3); //<GS-18052011> ELM327 support requires the length byte
            msg.setData(cmd);
            m_canListener.setupWaitMessage(0x311);
            CastInfoEvent("Requesting security access", ActivityType.ConvertingFile);
            if (!canUsbDevice.sendMessage(msg))
            {
                Console.WriteLine("Couldn't send message");
            }
            CANMessage response = new CANMessage();
            response = m_canListener.waitMessage(1000);
            //ulong data = response.getData();
            Console.WriteLine("---" + response.getData().ToString("X16"));
            if (response.getCanData(1) == 0x67)
            {
                if (response.getCanData(2) == 0xFD || response.getCanData(2) == 0xFB || response.getCanData(2) == 0x01)
                {
                    CastInfoEvent("Got seed value from ECU", ActivityType.ConvertingFile);

                    while (secondsToWait > 0)
                    {
                        CastInfoEvent("Waiting for " + secondsToWait.ToString() + " seconds...", ActivityType.UploadingBootloader);
                        Thread.Sleep(1000);
                        SendKeepAlive();
                        secondsToWait--;

                    }

                    byte[] seed = new byte[2];
                    seed[0] = response.getCanData(3);
                    seed[1] = response.getCanData(4);
                    if (seed[0] == 0x00 && seed[1] == 0x00)
                    {
                        return true; // security access was already granted
                    }
                    else
                    {
                        SeedToKey s2k = new SeedToKey();
                        byte[] key = s2k.calculateKey(seed, _securityLevel);
                        CastInfoEvent("Security access : Key (" + key[0].ToString("X2") + key[1].ToString("X2") + ") calculated from seed (" + seed[0].ToString("X2") + seed[1].ToString("X2") + ")", ActivityType.ConvertingFile);

                        ulong keydata = 0x0000000000FE2704;
                        if (_securityLevel == AccessLevel.AccessLevel01)
                        {
                            keydata = 0x0000000000022704;
                        }
                        else if (_securityLevel == AccessLevel.AccessLevelFB)
                        {
                            keydata = 0x0000000000FC2704;
                        }
                        ulong key1 = key[1];
                        key1 *= 0x100000000;
                        keydata ^= key1;
                        ulong key2 = key[0];
                        key2 *= 0x1000000;
                        keydata ^= key2;
                        msg = new CANMessage(0x11, 0, 5);//<GS-18052011> ELM327 support requires the length byte
                        msg.setData(keydata);
                        m_canListener.setupWaitMessage(0x311);
                        if (!canUsbDevice.sendMessage(msg))
                        {
                            Console.WriteLine("Couldn't send message");
                        }
                        response = new CANMessage();
                        response = m_canListener.waitMessage(1000);
                        // is it ok or not
                        if (response.getCanData(1) == 0x67 && (response.getCanData(2) == 0xFE || response.getCanData(2) == 0xFC || response.getCanData(2) == 0x02))
                        {
                            CastInfoEvent("Security access granted", ActivityType.ConvertingFile);
                            return true;
                        }
                    }

                }
                else if (response.getCanData(2) == 0xFE || response.getCanData(2) == 0x02)
                {
                    CastInfoEvent("Security access granted", ActivityType.ConvertingFile);
                    return true;
                }
            }
            else if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0x27)
            {
                Console.WriteLine("Casting error");
                string info = TranslateErrorCode(response.getCanData(3));
                Console.WriteLine("Casting error: " + info);
                CastInfoEvent("Error: " + info, ActivityType.ConvertingFile);
            }
            return false;
        }
예제 #6
0
        public void setECUparameterVIN(string vin)
        {
            // 62 DPID + 01 sendOneResponse + $AA ReadDataByPacketIdentifier
            CANMessage msg62 = new CANMessage(0x7E0, 0, 3); //<GS-18052011> ELM327 support requires the length byte
            msg62.setData(0x000000006201AA03);
            m_canListener.setupWaitMessage(0x5E8);
            CastInfoEvent("Wait for response 5E8 62 00 00", ActivityType.ConvertingFile);
            if (!canUsbDevice.sendMessage(msg62))
            {
                Console.WriteLine("Couldn't send message");
            }
            CANMessage response62 = new CANMessage();
            response62 = m_canListener.waitMessage(1000);
            Console.WriteLine("---" + response62.getData().ToString("X16"));
            //05E8	62	00	00	02	A7	01	7F	01
            if (response62.getCanData(0) == 0x62)
            {
                if (response62.getCanData(1) == 0x00)
                {
                    if (response62.getCanData(2) == 0x00)
                    {
                        CastInfoEvent("Got response 5E8 62 00 00", ActivityType.ConvertingFile);
                    }
                }
            }

            if (GetManufacturersEnableCounter() == 0x00)
                CastInfoEvent("GetManufacturersEnableCounter == 0x00", ActivityType.ConvertingFile);

            CastInfoEvent("ECM EOL Parameter Settings-part1", ActivityType.ConvertingFile);
            // 02 DPID + 01 sendOneResponse + $AA ReadDataByPacketIdentifier
            CANMessage msg = new CANMessage(0x7E0, 0, 3); //<GS-18052011> ELM327 support requires the length byte
            msg.setData(0x000000000201AA03);
            m_canListener.setupWaitMessage(0x5E8);
            CastInfoEvent("Wait for response 5E8 02 02", ActivityType.ConvertingFile);
            if (!canUsbDevice.sendMessage(msg))
            {
                Console.WriteLine("Couldn't send message");
            }
            CANMessage response = new CANMessage();
            response = m_canListener.waitMessage(1000);
            Console.WriteLine("---" + response.getData().ToString("X16"));
            //05E8	02	02	A0	42	80	A0	00	00
            if (response.getCanData(0) == 0x02)
            {
                if (response.getCanData(1) == 0x02)
                {
                    CastInfoEvent("Got response 5E8 02 02", ActivityType.ConvertingFile);
                }
            }

            if(ProgramVIN(vin))
                CastInfoEvent("ProgramVIN true", ActivityType.ConvertingFile);

            Thread.Sleep(200);

            //RequestSecurityAccess(2000);
            //CastInfoEvent("End programming?", ActivityType.ConvertingFile);
            //SendCommandNoResponse(0x7E0, 0x0000000000012702); // 01 SPSrequestSeed + $27 SecurityAccess

            //CastInfoEvent("Unidentified, security access again?", ActivityType.ConvertingFile);
            //SendCommandNoResponse(0x7E0, 0x000000EFA4022704); // EFA4 securitykey + 02 SPSsendKey + $27 SecurityAccess

            //SendCommandNoResponse(0x7E0, 0x0000000000901A02);
            string newVIN = GetVehicleVIN();
            CastInfoEvent("New VIN: " + newVIN, ActivityType.ConvertingFile);
        }
예제 #7
0
        // How to read DTC codes
        //A7 A6    First DTC character
        //-- --    -------------------
        // 0  0    P - Powertrain
        // 0  1    C - Chassis
        // 1  0    B - Body
        // 1  1    U - Network
        //A5 A4    Second DTC character
        //-- --    --------------------
        // 0  0    0
        // 0  1    1
        // 1  0    2
        // 1  1    3
        //A3 A2 A1 A0    Third/Fourth/Fifth DTC characters
        //-- -- -- --    -------------------
        // 0  0  0  0    0
        // 0  0  0  1    1
        // 0  0  1  0    2
        // 0  0  1  1    3
        // 0  1  0  0    4
        // 0  1  0  1    5
        // 0  1  1  0    6
        // 0  1  1  1    7
        // 1  0  0  0    8
        // 1  0  0  1    9
        // 1  0  1  0    A
        // 1  0  1  1    B
        // 1  1  0  0    C
        // 1  1  0  1    D
        // 1  1  1  0    E
        // 1  1  1  1    F
        // Example
        // E1 03 ->
        // 1110 0001 0000 0011
        // 11=U
        //   10=2
        //      0001=1
        //           0000=0
        //                0011=3
        //----------------------
        // U2103
        private static string GetDtcDescription(CANMessage responseDTC)
        {
            int firstDtcNum = (0xC0 & Convert.ToInt32(responseDTC.getCanData(1))) >> 6;
            char firstDtcChar = '-';
            if (firstDtcNum == 0)
            {
                firstDtcChar = 'P';
            }
            else if (firstDtcNum == 1)
            {
                firstDtcChar = 'C';
            }
            else if (firstDtcNum == 2)
            {
                firstDtcChar = 'B';
            }
            else if (firstDtcNum == 3)
            {
                firstDtcChar = 'U';
            }
            int secondDtcNum = (0x30 & Convert.ToInt32(responseDTC.getCanData(1))) >> 4;

            int thirdDtcNum = (0x0F & Convert.ToInt32(responseDTC.getCanData(1)));

            int forthDtcNum = (0xF0 & Convert.ToInt32(responseDTC.getCanData(2))) >> 4;

            int fifthDtcNum = (0x0F & Convert.ToInt32(responseDTC.getCanData(2)));

            return "DTC: " + firstDtcChar + secondDtcNum.ToString("d") + thirdDtcNum.ToString("X") + forthDtcNum.ToString("X") + fifthDtcNum.ToString("X");
        }
예제 #8
0
        public byte[] RequestECUInfo0101(uint _pid)
        {
            byte[] retval = new byte[2];
            byte[] rx_buffer = new byte[128];
            int rx_pnt = 0;

            if (canUsbDevice.isOpen())
            {
                ulong cmd = 0x0000000000001A02;
                ulong lpid = _pid;
                //lpid *= 256;
                //lpid *= 256;
                lpid *= 256;
                lpid *= 256;
                cmd ^= lpid;
                //SendMessage(data);  // software version
                CANMessage msg = new CANMessage(0x11, 0, 3); //<GS-18052011> support for ELM requires length byte
                msg.setData(cmd);
                m_canListener.setupWaitMessage(0x7E8);
                if (!canUsbDevice.sendMessage(msg))
                {
                    Console.WriteLine("Couldn't send message");
                }

                int msgcnt = 0;
                bool _success = false;
                CANMessage response = new CANMessage();
                ulong data = 0;
                while (!_success && msgcnt < 2)
                {
                    response = new CANMessage();
                    response = m_canListener.waitMessage(1000);
                    data = response.getData();
                    if (response.getCanData(1) != 0x7E) _success = true;
                    msgcnt++;
                }

                //CANMessage response = new CANMessage();
                //response = m_canListener.waitMessage(1000);
                //ulong data = response.getData();
                if (response.getCanData(1) == 0x5A)
                {
                    // only one frame in this repsonse

                    for (uint fi = 3; fi < 8; fi++) rx_buffer[rx_pnt++] = response.getCanData(fi);
                    retval = new byte[rx_pnt];
                    for (int i = 0; i < rx_pnt; i++) retval[i] = rx_buffer[i];
                }
                else if (response.getCanData(2) == 0x5A)
                {
                    SendMessage(0x7E0, 0x0000000000000030);
                    byte len = response.getCanData(1);
                    int m_nrFrameToReceive = ((len - 4) / 8);
                    if ((len - 4) % 8 > 0) m_nrFrameToReceive++;
                    int lenthisFrame = len;
                    if (lenthisFrame > 4) lenthisFrame = 4;
                    for (uint fi = 4; fi < 4 + lenthisFrame; fi++) rx_buffer[rx_pnt++] = response.getCanData(fi);
                    // wait for more records now

                    while (m_nrFrameToReceive > 0)
                    {
                        m_canListener.setupWaitMessage(0x7E8);
                        response = new CANMessage();
                        response = m_canListener.waitMessage(1000);
                        if (response.getCanData(1) != 0x7E)
                        {
                            m_nrFrameToReceive--;
                            data = response.getData();
                            // add the bytes to the receive buffer
                            for (uint fi = 1; fi < 8; fi++)
                            {
                                if (rx_pnt < rx_buffer.Length) // prevent overrun
                                {
                                    rx_buffer[rx_pnt++] = response.getCanData(fi);
                                }
                            }
                        }
                    }
                    retval = new byte[rx_pnt];
                    for (int i = 0; i < rx_pnt; i++) retval[i] = rx_buffer[i];

                }
                else if (response.getCanData(1) == 0x7F && response.getCanData(1) == 0x27)
                {
                    string info = TranslateErrorCode(response.getCanData(3));
                    CastInfoEvent("Error: " + info, ActivityType.ConvertingFile);
                }
            }
            Thread.Sleep(5);

            return retval;
        }
예제 #9
0
        public string RequestCIMInfo(uint _pid)
        {
            string retval = string.Empty;
            byte[] rx_buffer = new byte[128];
            int rx_pnt = 0;

            if (canUsbDevice.isOpen())
            {
                // pid=2
                // 17-12-2012 17:15:51.239 - TX: 0245 0000000000021A02
                // 17-12-2012 17:15:51.298 - RX: 0645 00000000311A7F03
                // pid=3
                // 17-12-2012 17:16:41.190 - TX: 0245 0000000000031A02
                // 17-12-2012 17:16:41.238 - RX: 0645 00000000311A7F03

                ulong cmd = 0x0000000000001A02;
                ulong lpid = _pid;
                lpid *= 256;
                lpid *= 256;
                cmd ^= lpid;
                //SendMessage(data);  // software version
                CANMessage msg = new CANMessage(0x245, 0, 8);
                msg.setData(cmd);
                m_canListener.setupWaitMessage(0x645);
                if (!canUsbDevice.sendMessage(msg))
                {
                    Console.WriteLine("Couldn't send message");
                }

                int msgcnt = 0;
                bool _success = false;
                CANMessage response = new CANMessage();
                ulong data = 0;
                while (!_success && msgcnt < 2)
                {
                    response = new CANMessage();
                    response = m_canListener.waitMessage(1000);
                    data = response.getData();
                    if (response.getCanData(1) != 0x7E) _success = true;
                    msgcnt++;
                }

                //CANMessage response = new CANMessage();
                //response = m_canListener.waitMessage(1000);
                //ulong data = response.getData();
                if (response.getCanData(1) == 0x5A)
                {
                    // only one frame in this repsonse

                    for (uint fi = 3; fi < 8; fi++) rx_buffer[rx_pnt++] = response.getCanData(fi);
                    retval = Encoding.ASCII.GetString(rx_buffer, 0, rx_pnt);
                }
                else if (response.getCanData(2) == 0x5A)
                {
                    SendMessage(0x245, 0x0000000000000030);
                    byte len = response.getCanData(1);
                    int m_nrFrameToReceive = ((len - 4) / 8);
                    if ((len - 4) % 8 > 0) m_nrFrameToReceive++;
                    int lenthisFrame = len;
                    if (lenthisFrame > 4) lenthisFrame = 4;
                    for (uint fi = 4; fi < 4 + lenthisFrame; fi++) rx_buffer[rx_pnt++] = response.getCanData(fi);
                    // wait for more records now

                    while (m_nrFrameToReceive > 0)
                    {
                        m_canListener.setupWaitMessage(0x645);
                        response = new CANMessage();
                        response = m_canListener.waitMessage(1000);
                        if (response.getCanData(1) != 0x7E)
                        {
                            m_nrFrameToReceive--;
                            data = response.getData();
                            // add the bytes to the receive buffer
                            for (uint fi = 1; fi < 8; fi++)
                            {
                                if (rx_pnt < rx_buffer.Length) // prevent overrun
                                {
                                    rx_buffer[rx_pnt++] = response.getCanData(fi);
                                }
                            }
                        }
                    }
                    retval = Encoding.ASCII.GetString(rx_buffer, 0, rx_pnt);
                }
                else if (response.getCanData(1) == 0x7F && response.getCanData(1) == 0x27)
                {
                    string info = TranslateErrorCode(response.getCanData(3));
                    CastInfoEvent("Error: " + info, ActivityType.ConvertingFile);
                }
            }
            Thread.Sleep(25);

            return retval;
        }
예제 #10
0
        public string[] readDTCCodesCIM()
        {
            // test code
            //ulong c = 0x0000006F00070181;//81 01 07 00 6F 00 00 00
            //ulong c = 0x000000FD00220181; //81 01 22 00 FD 00 00 00
            //CANMessage test = new CANMessage();
            //test.setData(c);
            //AddToCanTrace(GetDtcDescription(test));

            // send message to read DTC
            StartSession10();

            List<string> list = new List<string>();

            ulong cmd = 0x000000001281A903; // 245 03 A9 81 12 00 00 00 00

            CANMessage msg = new CANMessage(0x245, 0, 8);//<GS-18052011> ELM327 support requires the length byte
            msg.setData(cmd);
            m_canListener.setupWaitMessage(0x545);
            if (!canUsbDevice.sendMessage(msg))
            {
                Console.WriteLine("Couldn't send message");
            }

            CANMessage response = new CANMessage();
            ulong data = 0;
            // Wait for response
            // 545 03 7F A9 78 00 00 00 00
            response = m_canListener.waitMessage(1000);
            data = response.getData();

            if (response.getCanData(1) == 0x7F && response.getCanData(2) == 0xA9 && response.getCanData(3) == 0x78)
            {
                // Now wait for all DTCs
                m_canListener.setupWaitMessage(0x545);

                bool more_errors = true;
                while (more_errors)
                {
                    CANMessage responseDTC = new CANMessage();
                    responseDTC = m_canListener.waitMessage(1000);

                    // Read until response:   No more errors, status == 0xFF
                    int dtcStatus = Convert.ToInt32(responseDTC.getCanData(4));
                    if (dtcStatus == 0xFF)
                    {
                        more_errors = false;
                        list.Add("0xFF No more errors!");
                    }
                    else if (dtcStatus == 0x97)
                    {
                        more_errors = false;
                        list.Add("0x17 No more errors!");
                    }
                    else
                    {
                        string dtcDescription = GetDtcDescription(responseDTC);
                        list.Add(dtcDescription);
                    }
                }
            }

            Send0120();

            return list.ToArray();
        }
예제 #11
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;
        }
예제 #12
0
        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
        }