private void J2534InterfaceItem_Click(object sender, EventArgs e) { ToolStripItem caller = (ToolStripItem)sender; if (Connection != null) { Connection.TryCleanup(); } Connection = new ECUConnection(caller.Tag.ToString(), caller.Text); Connection.ConnectionStateChangeEvent += ConnectionStateChangedHandler; Connection.OpenDevice(); // loadtree should not be necessary if the prior state was disconnected // LoadTree(); }
// FIN = Fahrzeug-Identifizierungs-Nummer // VIN = Vehicle Identification Number public static bool TryReadChassisNumber(ECUConnection connection, out string chassisNumber) { chassisNumber = ""; // we need a existing hardware connection to a J2534 device if (connection is null) { return(false); } if (connection.IsSimulation()) { return(false); } if (connection.ConnectionDevice is null) { // no valid j2534 device initialized yet, can't do anything return(false); } bool connectionWasEstablished = connection.ConnectionProtocol != null; // if the connection is already open, we can use the protocol's vin query int[] baudratesToTest = new int[] { 500000, 800000 }; foreach (int baudrateToTest in baudratesToTest) { // if there is an established connection (i.e. already connected and identified a target, do not reconfigure the connection if (!connectionWasEstablished) { SetupConnectionForBaud(connection, baudrateToTest); } // try KW2C3PE first, since it is a legacy protocol; UDS seems to be somewhat aware, and deliberately avoids known KW2C3PE identifiers if (TryReadKW2C3PEChassisNumber(connection, out string KW2C3PEChassisNumber)) { chassisNumber = KW2C3PEChassisNumber; return(true); } if (TryReadUDSChassisNumber(connection, out string udsChassisNumber)) { chassisNumber = udsChassisNumber; return(true); } // if there's an established connection that failed both UDS/KW2C3PE, the loop is no longer required if (connection.ConnectionProtocol != null) { return(false); } } // if a connection was unavailable, it would have been established earlier on; clean it up if (!connectionWasEstablished) { connection.TryCleanup(); connection = null; } return(false); }