static private void ConnectAllDevices(J2534Library DLL) { //If the DLL successfully executes GetNextCarDAQ_RESET() if (DLL.GetNextCarDAQ_RESET().IsOK) { GetNextCarDAQResults TargetDrewtechDevice = DLL.GetNextCarDAQ(); while (TargetDrewtechDevice.Exists) { J2534Device ThisDevice = DLL.ConstructDevice(TargetDrewtechDevice); if (ThisDevice.IsConnected) //This should always succeed. { //To avoid populating the list with duplicate devices due to disconnection //Remove any unconnected devices that are attached to this library. PhysicalDevices.RemoveAll(Listed => Listed.DeviceName == ThisDevice.DeviceName); PhysicalDevices.Add(ThisDevice); } TargetDrewtechDevice = DLL.GetNextCarDAQ(); } } //If its not a drewtech library, then attempt to connect a device else { J2534Device ThisDevice = DLL.ConstructDevice(); if (ThisDevice.IsConnected) { //To avoid populating the list with duplicate devices due to disconnection //Remove any unconnected devices that are attached to this library. PhysicalDevices.RemoveAll(Listed => (Listed.Library == ThisDevice.Library) && !Listed.IsConnected); PhysicalDevices.Add(ThisDevice); } } }
internal J2534Device(J2534Library Library, GetNextCarDAQResults CarDAQ) { this.Library = Library; this.DeviceName = CarDAQ.Name; this.DrewtechVersion = CarDAQ.Version; this.DrewtechAddress = CarDAQ.Address; ConnectToDevice(DeviceName); }
internal GetNextCarDAQResults GetNextCarDAQ() { J2534Status Status = new J2534Status(); IntPtr pName = Marshal.AllocHGlobal(4); IntPtr pAddr = Marshal.AllocHGlobal(4); IntPtr pVer = Marshal.AllocHGlobal(4); lock (API_LOCK) { Status.Code = API.GetNextCarDAQ(pName, pVer, pAddr); if (Status.Code == J2534ERR.FUNCTION_NOT_ASSIGNED || Marshal.ReadIntPtr(pName) == IntPtr.Zero) { Marshal.FreeHGlobal(pName); Marshal.FreeHGlobal(pVer); Marshal.FreeHGlobal(pAddr); return(new GetNextCarDAQResults() { Exists = false }); } else if (Status.IsNotOK) { Status.Description = GetLastError(); Marshal.FreeHGlobal(pName); Marshal.FreeHGlobal(pVer); Marshal.FreeHGlobal(pAddr); throw new J2534Exception(Status); } byte[] b = new byte[3]; Marshal.Copy(pVer, b, 0, 3); GetNextCarDAQResults Result = new GetNextCarDAQResults() { Exists = true, Name = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(pName)), Version = string.Format("{2}.{1}.{0}", b[0], b[1], b[2]), Address = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(pAddr)) }; Marshal.FreeHGlobal(pName); Marshal.FreeHGlobal(pAddr); Marshal.FreeHGlobal(pVer); return(Result); } }
internal J2534Device ConstructDevice(GetNextCarDAQResults CarDAQ) { return(new J2534.J2534Device(this, CarDAQ)); }