bool Connect() { try { if (!LoadJ2534()) { MessageBox.Show("No J2534 USB cables appear to be connected to the PC.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return(false); } _comm = new UDSFord(_passThru); _comm.ConnectISO15765(); return(true); } catch (J2534Exception ex) { if (ex.Error == J2534Err.ERR_DEVICE_NOT_CONNECTED || ex.Error == J2534Err.ERR_FAILED) { MessageBox.Show("J2534 USB cable is not attached.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } else if (ex.Error == J2534Err.ERR_TIMEOUT || ex.Error == J2534Err.ERR_BUFFER_EMPTY) { MessageBox.Show("A J2534 USB cable was detected however there was no CANBUS response. Check the device is connected to a vehicle with the ignition on.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } } catch (OBDException ex) { MessageBox.Show("A J2534 USB cable was detected however there was no response from the CANBUS. Check the device is connected to a vehicle with the ignition on.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } catch (Exception e) { MessageBox.Show("An unknown error occured whilst attempting to make an ISO15765 connection: " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } return(false); }
void SetVoltage(bool off = false) { try { if (!Connect()) { return; } UpdateLog("Connected"); if (_comm == null) { _comm = new UDSFord(_passThru); } if (off) { UpdateLog("setProgrammingVoltage(PinNumber.PIN_13, OFF)"); float programmingVoltage = _comm.PassThruSetProgrammingVoltage(PinNumber.PIN_13, PinVoltage.VOLTAGE_OFF); UpdateLog("Voltage = : " + programmingVoltage); _toggle = false; } else { uint mvolts = 0; if (!UInt32.TryParse(textBoxVolts.Text, out mvolts)) { return; } UpdateLog("setProgrammingVoltage(PinNumber.PIN_13 " + mvolts + " mV"); float programmingVoltage = _comm.PassThruSetProgrammingVoltage(PinNumber.PIN_13, (PinVoltage)mvolts); UpdateLog("Voltage = " + programmingVoltage + "V"); _toggle = true; } } catch (OBDException obdEx) { MessageBox.Show("Error setting voltage due to OBD error: " + obdEx.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } catch (UDSException udsEx) { MessageBox.Show("Error setting voltage due to UDS error: " + udsEx.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } catch (J2534Exception j2534Ex) { MessageBox.Show("Error setting voltage due to J2534 error: " + j2534Ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } catch (Exception ex) { MessageBox.Show("Unknown error setting voltage: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } finally { Disconnect(); UpdateLog("Disconnect"); } }
private void ChangeDevice_Click(object sender, EventArgs e) { Disconnect(); if (_passThru != null) { _passThru.FreeLibrary(); } _passThru = null; _comm = null; }
bool LoadJ2534() { if (_passThru == null) { _passThru = new J2534Extended(); } if (_passThru.IsLoaded) { return(true); } J2534Device j2534Device; // Find all of the installed J2534 passthru devices List <J2534Device> availableJ2534Devices = J2534Detect.ListDevices(); if (availableJ2534Devices.Count == 0) { MessageBox.Show("Could not find any installed J2534 devices in the Windows registry, have you installed the device drivers for your cable?"); _passThru.FreeLibrary(); return(false); } if (autoDetectCheckBox.Checked) { foreach (var lib in availableJ2534Devices) { if (Path.GetFileName(lib.FunctionLibrary).Contains("J2534DotNet.Logger.dll")) { continue; } try { j2534Device = new J2534Device(); if (!_passThru.LoadLibrary(lib)) { j2534Device = null; continue; } _comm = new UDSFord(_passThru); _comm.Connect(); //if we get this far then we have successfully connected _comm.Disconnect(); return(true); } catch { j2534Device = null; continue; } } _passThru.FreeLibrary(); return(false); } if (checkBoxLogJ2534.Checked) { j2534Device = new J2534Device(); j2534Device.FunctionLibrary = System.IO.Directory.GetCurrentDirectory() + "\\" + "J2534DotNet.Logger.dll"; Thread.Sleep(10); var loaded = _passThru.LoadLibrary(j2534Device); if (!loaded) { _passThru.FreeLibrary(); } return(loaded); } //If there is only one DLL to choose from then load it if (availableJ2534Devices.Count == 1) { return(_passThru.LoadLibrary(availableJ2534Devices[0])); } else { var sd = new SelectDevice(); if (sd.ShowDialog() == DialogResult.OK) { j2534Device = sd.Device; var loaded = _passThru.LoadLibrary(j2534Device); if (!loaded) { _passThru.FreeLibrary(); } return(loaded); } } return(false); }