/// <summary> /// Factory /// </summary> public static MockEcuStream CreateInstance() { MockEcuStream instance = new MockEcuStream(); lastInstance = instance; return(instance); }
/// <summary> /// Open a port, open an OpenPort, or create a mock ECU stream. /// </summary> /// <param name="portName">COM port name, passthru DLL name, or "Mock ECU".</param> /// <param name="baudRate">Baud rate to use for COM port.</param> /// <returns>Stream.</returns> public static Stream GetDataStream( string portName, int baudRate, ref SerialPort port, TraceLine traceLine) { if (portName == SsmUtility.MockEcuDisplayName) { MockEcuStream.Image = new EcuImage2F12785206(); return(MockEcuStream.CreateInstance()); } if (SsmUtility.OpenPort20DisplayName.Equals(portName, StringComparison.OrdinalIgnoreCase)) { portName = SsmUtility.OpenPort20PortName; } if (portName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase)) { PassThruStream passThruStream = PassThruStream.GetInstance(portName); passThruStream.OpenSsmChannel(); return(passThruStream); } Stream stream = null; if (port == null) { traceLine("SsmUtility.GetDataStream: Creating port."); port = new SerialPort(portName, baudRate, Parity.None, 8); port.ReadTimeout = 500; port.WriteTimeout = 500; } if (port.IsOpen) { traceLine("SsmUtility.GetDataStream: Port already open, draining input queue..."); int bytesToRead = 0; while ((bytesToRead = port.BytesToRead) > 0) { Trace.WriteLine("SsmUtility.GetDataStream: " + bytesToRead.ToString(CultureInfo.InvariantCulture) + " bytes in queue, reading..."); byte[] buffer = new byte[bytesToRead]; port.Read(buffer, 0, buffer.Length); traceLine("SsmUtility.GetDataStream: Read completed."); Thread.Sleep(500); } } else { traceLine("SsmUtility.GetDataStream: Port not open."); bool exists = false; foreach (string name in System.IO.Ports.SerialPort.GetPortNames()) { if (string.Compare(name, port.PortName, StringComparison.OrdinalIgnoreCase) == 0) { traceLine("SsmUtility.GetDataStream: Port " + name + " exists."); exists = true; break; } } if (exists) { port.Open(); Trace.WriteLine("SsmUtility.GetDataStream: Port opened."); } else { string message = "Port " + port.PortName + " does not exist."; throw new IOException(message); } } stream = port.BaseStream; Trace.WriteLine("SsmUtility.GetDataStream: Input queue drained, returning stream."); return(stream); }
/// <summary> /// Write a request to the mock ECU /// </summary> public override void Write(byte[] buffer, int offset, int count) { if (buffer[0] != 0x80) { throw new InvalidOperationException("First byte sent to ECU must be 0x80 (command start byte)"); } /* * if (buffer[1] != 0x10) * { * throw new InvalidOperationException("Second byte sent to ECU must be 0x10 (destination = ECU)"); * } * * if (buffer[2] != 0xF0) * { * throw new InvalidOperationException("Third byte sent to ECU must be 0xF0 (source = diagnostic tool)"); * } */ if (MockEcuStream.GetPacketLength(buffer) != buffer.Length - 5) { throw new InvalidOperationException("Fourth byte sent to ECU must be equal to \"buffer_length - 5\""); } if (this.corruptNextResponse) { byte[] data = new byte[100]; for (int i = 0; i < data.Length; i++) { data[i] = (byte)0x55; } this.SetResponse(data); this.corruptNextResponse = false; return; } SsmPacket packet = SsmPacket.ParseRequest(buffer, offset, count); switch (packet.Command) { case SsmCommand.ReadBlockRequest: this.ReadBlock(packet.BlockStart, packet.BlockLength); break; case SsmCommand.ReadAddressesRequest: this.ReadAddresses(packet.Addresses); break; case SsmCommand.WriteBlockRequest: //this.WriteBlock(packet.BlockStart, packet.BlockData); break; case SsmCommand.WriteAddressesRequest: //this.WriteAddresses(packet.Addresses, packet.Values); break; case SsmCommand.EcuInitRequest: this.Handshake(); break; } List <byte> temporaryBuffer = new List <byte>(packet.Data.Length + this.response.Length); temporaryBuffer.AddRange(packet.Data); temporaryBuffer.AddRange(this.response); this.SetResponse(temporaryBuffer.ToArray()); }