Пример #1
0
 void Disconnect()
 {
     if (_comm != null)
     {
         _comm.Disconnect();
     }
 }
Пример #2
0
        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);
        }