Exemplo n.º 1
0
        private void SendReceive(
            byte device,
            byte command,
            bool pad,
            byte[] payloadBytes,
            StringBuilder builder)
        {
            SsmPacket probe = SsmPacket.CreateArbitrary(device, command, pad, payloadBytes);

            builder.AppendFormat(
                "Sending {0:X2} to {1:X2} with payload of {2} bytes:",
                command,
                device,
                payloadBytes.Length);
            builder.AppendLine();
            builder.AppendLine(MainForm.BytesToString(payloadBytes));
            builder.AppendLine();

            byte[] requestBuffer = probe.Data;
            this.stream.Write(requestBuffer, 0, requestBuffer.Length);
            System.Threading.Thread.Sleep(250);

            byte[] responseBuffer = new byte[2000];
            int    bytesRead      = this.stream.Read(responseBuffer, 0, responseBuffer.Length);

            builder.AppendFormat(
                "Received {0} bytes total, {1} after subtracting echo, {2} payload bytes.",
                bytesRead,
                bytesRead - probe.Data.Length,
                bytesRead - (probe.Data.Length + 6));
            builder.AppendLine();

            SsmPacket response = SsmPacket.ParseResponse(responseBuffer, 0, bytesRead);

            builder.AppendLine("Response packet:");
            builder.AppendLine(BytesToString(response.Data));
            builder.AppendLine();
            builder.AppendLine("Response payload:");
            builder.AppendFormat("Header: {0}", response.Data[0].ToHex());
            builder.AppendLine();
            builder.AppendFormat("Dest: {0}", response.Data[1].ToHex());
            builder.AppendLine();
            builder.AppendFormat("Source: {0}", response.Data[2].ToHex());
            builder.AppendLine();
            builder.AppendFormat("DataSize: {0}", response.Data[3].ToHex());
            builder.AppendLine();
            builder.AppendFormat("Command: {0}", response.Data[4].ToHex());
            builder.AppendLine();
            builder.AppendFormat("Checksum: {0}", response.Data[response.Data.Length - 1].ToHex());
            builder.AppendLine();

            byte[] payload = new byte[response.Data.Length - 6];
            for (int i = 5; i < response.Data.Length - 1; i++)
            {
                payload[i - 5] = response.Data[i];
            }

            builder.AppendFormat("Payload: {0}", BytesToString(payload));
        }
Exemplo n.º 2
0
        static void ReadMultipleTest(Stream ecuStream, SerialPort port)
        {
            SsmPacket ecuInitRequest = SsmPacket.CreateEcuIdentifierRequest();

            byte[] buffer = ecuInitRequest.Data;
            ecuStream.Write(buffer, 0, buffer.Length);

            Thread.Sleep(100);

            byte[]    receiveBuffer   = new byte[1000];
            int       expectedLength  = 68;
            int       receiveLength   = ecuStream.Read(receiveBuffer, 0, receiveBuffer.Length);
            SsmPacket ecuInitResponse = SsmPacket.ParseResponse(receiveBuffer, 0, receiveLength);

            // TPS and IPW
            UInt32[] addresses = new UInt32[] { 0x29, 0x20 };

            for (int i = 0; i < 1000 && !Console.KeyAvailable; i++)
            {
                SsmPacket readRequest = SsmPacket.CreateMultipleReadRequest(addresses);
                buffer = readRequest.Data;
                ecuStream.Write(buffer, 0, buffer.Length);

                Thread.Sleep(100);
                expectedLength = 21;
                receiveLength  = ecuStream.Read(receiveBuffer, 0, expectedLength);
                if (!Check("ReceiveLength", receiveLength, expectedLength))
                {
                    if (port != null)
                    {
                        port.DiscardInBuffer();
                        port.DiscardOutBuffer();
                    }
                    continue;
                }

                SsmPacket readResponse = SsmPacket.ParseResponse(receiveBuffer, 0, receiveLength);
                if (!Check("CommandID", readResponse.Command, SsmCommand.ReadAddressesResponse))
                {
                    if (port != null)
                    {
                        port.DiscardInBuffer();
                        port.DiscardOutBuffer();
                    }
                    continue;
                }
                Console.WriteLine(readResponse.Values[0] + " " + readResponse.Values[1]);
            }
        }