Ejemplo n.º 1
0
        private bool ReadFileManagedBuffer(
            SafeFileHandle hFile,
            byte[] INBuffer,
            uint nNumberOfBytesToRead,
            ref uint lpNumberOfBytesRead,
            IntPtr lpOverlapped)
        {
            IntPtr num = IntPtr.Zero;

            try
            {
                num = Marshal.AllocHGlobal((int)nNumberOfBytesToRead);
                if (USBM.ReadFile(hFile, num, nNumberOfBytesToRead, ref lpNumberOfBytesRead, lpOverlapped))
                {
                    Marshal.Copy(num, INBuffer, 0, (int)lpNumberOfBytesRead);
                    Marshal.FreeHGlobal(num);
                    return(true);
                }
                Marshal.FreeHGlobal(num);
                return(false);
            }
            catch
            {
                if (num != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(num);
                }
                return(false);
            }
        }
Ejemplo n.º 2
0
 public bool OpenDevice()
 {
     if (this.CheckIfPresentAndGetUSBDevicePath())
     {
         this.WriteHandleToUSBDevice = USBM.CreateFile(this.DevicePath, 1073741824U, 3U, IntPtr.Zero, 3U, 0U, IntPtr.Zero);
         uint lastWin32Error1 = (uint)Marshal.GetLastWin32Error();
         this.ReadHandleToUSBDevice = USBM.CreateFile(this.DevicePath, 2147483648U, 3U, IntPtr.Zero, 3U, 0U, IntPtr.Zero);
         uint lastWin32Error2 = (uint)Marshal.GetLastWin32Error();
         if (lastWin32Error1 == 0U && lastWin32Error2 == 0U)
         {
             this.AttachedState = true;
         }
         else
         {
             this.AttachedState = false;
             if (lastWin32Error1 == 0U)
             {
                 this.WriteHandleToUSBDevice.Close();
             }
             if (lastWin32Error2 == 0U)
             {
                 this.ReadHandleToUSBDevice.Close();
             }
         }
     }
     else
     {
         this.AttachedState = false;
     }
     this.InfoDevice = this.GetInfoForDevice();
     return(this.AttachedState);
 }
Ejemplo n.º 3
0
        public float GetMeasuredValue()
        {
            byte[] lpBuffer = new byte[65];
            byte[] INBuffer = new byte[65];
            uint   lpNumberOfBytesWritten = 0;
            uint   lpNumberOfBytesRead    = 0;

            try
            {
                if (this.AttachedState)
                {
                    lpBuffer[0] = (byte)0;
                    lpBuffer[1] = (byte)55;
                    for (uint index = 2; index < 65U; ++index)
                    {
                        lpBuffer[index] = byte.MaxValue;
                    }
                    if (USBM.WriteFile(this.WriteHandleToUSBDevice, lpBuffer, 65U, ref lpNumberOfBytesWritten, IntPtr.Zero))
                    {
                        INBuffer[0] = (byte)0;
                        if (this.ReadFileManagedBuffer(this.ReadHandleToUSBDevice, INBuffer, 65U, ref lpNumberOfBytesRead, IntPtr.Zero))
                        {
                            if (INBuffer[1] == (byte)55)
                            {
                                if (this.InfoDevice.UnitVersion == 5)
                                {
                                    if (this.InfoDevice.Subtype == 1)
                                    {
                                        this.ADC = ((int)INBuffer[2] << 16) + ((int)INBuffer[3] << 8) + (int)INBuffer[4];
                                        if ((this.ADC & this.signbit) != 0)
                                        {
                                            this.ADC -= 16777216;
                                        }
                                        this.ADC       = (int)((double)this.ADC * this.InfoDevice.CalibValue);
                                        this.ADCValue  = (float)this.ADC;
                                        this.ADCValue /= 1000f;
                                        this.ADCValue  = (float)Math.Round((double)this.ADCValue, 3);
                                        return(this.ADCValue);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
            }

            return(0.0f);
        }
Ejemplo n.º 4
0
        internal USBM.MyDeviceInfo GetInfoForDevice()
        {
            byte[] lpBuffer = new byte[65];
            byte[] INBuffer = new byte[65];
            uint   lpNumberOfBytesWritten = 0;
            uint   lpNumberOfBytesRead    = 0;
            uint   num1 = 0;
            uint   num2 = 0;
            uint   num3 = 0;
            uint   num4 = 0;
            uint   num5 = 0;

            USBM.MyDeviceInfo myDeviceInfo = new USBM.MyDeviceInfo();
            try
            {
                if (this.AttachedState)
                {
                    lpBuffer[0] = (byte)0;
                    lpBuffer[1] = byte.MaxValue;
                    lpBuffer[2] = (byte)55;
                    for (uint index = 3; index < 65U; ++index)
                    {
                        lpBuffer[index] = byte.MaxValue;
                    }
                    if (USBM.WriteFile(this.WriteHandleToUSBDevice, lpBuffer, 65U, ref lpNumberOfBytesWritten, IntPtr.Zero))
                    {
                        INBuffer[0] = (byte)0;
                        if (this.ReadFileManagedBuffer(this.ReadHandleToUSBDevice, INBuffer, 65U, ref lpNumberOfBytesRead, IntPtr.Zero))
                        {
                            num1 = ((uint)INBuffer[9] << 8) + (uint)INBuffer[8];
                            num2 = ((uint)INBuffer[9] << 8) + (uint)INBuffer[8];
                            num3 = ((uint)INBuffer[17] << 8) + (uint)INBuffer[16];
                            num4 = ((uint)INBuffer[19] << 8) + (uint)INBuffer[18];
                            num5 = ((uint)INBuffer[21] << 8) + (uint)INBuffer[20];
                        }
                    }
                    myDeviceInfo.CalibValue  = (double)num1 / 10000.0;
                    myDeviceInfo.CalibValue1 = 1.0 + ((double)num2 - 30000.0) * 1E-05;
                    myDeviceInfo.CalibValue2 = 1.0 + ((double)num3 - 30000.0) * 1E-05;
                    myDeviceInfo.CalibValue3 = 1.0 + ((double)num4 - 30000.0) * 1E-05;
                    myDeviceInfo.CalibValue4 = 1.0 + ((double)num5 - 30000.0) * 1E-05;
                    myDeviceInfo.Subtype     = (int)INBuffer[7];
                    myDeviceInfo.UnitVersion = (int)INBuffer[6];
                }
            }
            catch (Exception ex)
            {
            }
            return(myDeviceInfo);
        }
Ejemplo n.º 5
0
        private static void ObserverThread()
        {
            try
            {
                var device = new USBM();

                if (!device.OpenDevice())
                {
                    throw new Exception("Could not open device.");
                }

                while (true)
                {
                    var volts = device.GetMeasuredValue();

                    if (volts == 0.0f)
                    {
                        throw new Exception("Value read from device was zero. This indicates the device may not be working. Restarting to try to recover.");
                    }

                    var ppmPerVolt = 197.5f; // Experimentally determined - looks perfectly linear.
                    var ppm        = (int)(volts * ppmPerVolt);

                    Measurements.Inc();
                    Volts.Set(volts);
                    Ppm.Set(ppm);

                    Console.WriteLine($"{volts:#00.000} V\t\t{ppm} PPM");

                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }
            }
            catch (Exception ex)
            {
                // Oh no! This is fatal error.
                Console.WriteLine(ex.ToString());
                Console.WriteLine("Will restart after sleeping for a while.");
                Thread.Sleep(TimeSpan.FromSeconds(30));
                Process.GetCurrentProcess().Kill();
            }
        }
Ejemplo n.º 6
0
 private bool CheckIfPresentAndGetUSBDevicePath()
 {
     try
     {
         IntPtr zero1 = IntPtr.Zero;
         USBM.SP_DEVICE_INTERFACE_DATA        DeviceInterfaceData = new USBM.SP_DEVICE_INTERFACE_DATA();
         USBM.SP_DEVICE_INTERFACE_DETAIL_DATA interfaceDetailData = new USBM.SP_DEVICE_INTERFACE_DETAIL_DATA();
         USBM.SP_DEVINFO_DATA spDevinfoData = new USBM.SP_DEVINFO_DATA();
         uint   MemberIndex         = 0;
         uint   PropertyRegDataType = 0;
         uint   RequiredSize1       = 0;
         uint   RequiredSize2       = 0;
         uint   RequiredSize3       = 0;
         IntPtr zero2     = IntPtr.Zero;
         uint   num1      = 0;
         string str       = "Vid_04d8&Pid_fc39";
         IntPtr classDevs = USBM.SetupDiGetClassDevs(ref this.InterfaceClassGuid, IntPtr.Zero, IntPtr.Zero, 18U);
         if (!(classDevs != IntPtr.Zero))
         {
             return(false);
         }
         do
         {
             DeviceInterfaceData.cbSize = (uint)Marshal.SizeOf((object)DeviceInterfaceData);
             if (USBM.SetupDiEnumDeviceInterfaces(classDevs, IntPtr.Zero, ref this.InterfaceClassGuid, MemberIndex, ref DeviceInterfaceData))
             {
                 if (Marshal.GetLastWin32Error() == 259)
                 {
                     USBM.SetupDiDestroyDeviceInfoList(classDevs);
                     return(false);
                 }
                 spDevinfoData.cbSize = (uint)Marshal.SizeOf((object)spDevinfoData);
                 USBM.SetupDiEnumDeviceInfo(classDevs, MemberIndex, ref spDevinfoData);
                 USBM.SetupDiGetDeviceRegistryProperty(classDevs, ref spDevinfoData, 1U, ref PropertyRegDataType, IntPtr.Zero, 0U, ref RequiredSize1);
                 IntPtr num2 = Marshal.AllocHGlobal((int)RequiredSize1);
                 USBM.SetupDiGetDeviceRegistryProperty(classDevs, ref spDevinfoData, 1U, ref PropertyRegDataType, num2, RequiredSize1, ref RequiredSize2);
                 string stringUni = Marshal.PtrToStringUni(num2);
                 Marshal.FreeHGlobal(num2);
                 string lowerInvariant = stringUni.ToLowerInvariant();
                 str = str.ToLowerInvariant();
                 if (lowerInvariant.Contains(str))
                 {
                     // Just... it just is. I don't get it.
                     interfaceDetailData.cbSize = 8;
                     USBM.SetupDiGetDeviceInterfaceDetail(classDevs, ref DeviceInterfaceData, IntPtr.Zero, 0U, ref RequiredSize3, IntPtr.Zero);
                     IntPtr zero3          = IntPtr.Zero;
                     IntPtr detailDataReal = Marshal.AllocHGlobal((int)RequiredSize3);
                     Marshal.StructureToPtr(interfaceDetailData, detailDataReal, false);
                     if (USBM.SetupDiGetDeviceInterfaceDetail(classDevs, ref DeviceInterfaceData, detailDataReal, RequiredSize3, IntPtr.Zero, IntPtr.Zero))
                     {
                         // Starts right after the 4 byte size.
                         this.DevicePath = Marshal.PtrToStringUni(new IntPtr(detailDataReal.ToInt64() + 4));
                         USBM.SetupDiDestroyDeviceInfoList(classDevs);
                         Marshal.FreeHGlobal(detailDataReal);
                         return(true);
                     }
                     Marshal.GetLastWin32Error();
                     USBM.SetupDiDestroyDeviceInfoList(classDevs);
                     Marshal.FreeHGlobal(detailDataReal);
                     return(false);
                 }
                 ++MemberIndex;
                 ++num1;
             }
             else
             {
                 uint lastWin32Error = (uint)Marshal.GetLastWin32Error();
                 USBM.SetupDiDestroyDeviceInfoList(classDevs);
                 return(false);
             }
         }while (num1 != 10000000U);
         return(false);
     }
     catch
     {
         return(false);
     }
 }