OpenHardwareMonitor不向我显示温度传感器

时间:2019-04-02 15:57:25

标签: c# arduino openhardwaremonitor

问题是我写了这个程序来将PC数据发送到arduino:

using System.IO.Ports;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
using OpenHardwareMonitor.Hardware;

namespace ArduinoDetector
{
    class Program
    {
        private static PerformanceCounter cpu;
        private static PerformanceCounter ram;
        private static SerialPort port;

        private static float totalRam;

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        private class MEMORYSTATUSEX
        {
            public uint dwLength;
            public uint dwMemoryLoad;
            public ulong ullTotalPhys;
            public ulong ullAvailPhys;
            public ulong ullTotalPageFile;
            public ulong ullAvailPageFile;
            public ulong ullTotalVirtual;
            public ulong ullAvailVirtual;
            public ulong ullAvailExtendedVirtual;

            public MEMORYSTATUSEX()
            {
                dwLength = (uint)Marshal.SizeOf(this);
            }
        }

        private class Visitor : IVisitor
        {
            public void VisitComputer(IComputer computer)
            {
                computer.Traverse(this);
            }

            public void VisitHardware(IHardware hardware)
            {
                hardware.Update();
                foreach(IHardware sh in hardware.SubHardware)
                {
                    sh.Update();
                    sh.Accept(this);
                }
            }

            public void VisitSensor(ISensor sensor) {
                sensor.Accept(this);
            }

            public void VisitParameter(IParameter parameter) { }
        }

        [return : MarshalAs(UnmanagedType.Bool)]
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer);

        private static void GetTotalMemory()
        {
            MEMORYSTATUSEX m = new MEMORYSTATUSEX();
            ulong phys = 0, virt = 0, swap = 0;
            if(GlobalMemoryStatusEx(m))
            {
                phys = m.ullTotalPhys;
                virt = m.ullTotalVirtual;
                swap = m.ullTotalPageFile;
            } else
            {
                Console.WriteLine("Reading memory data failed");
                Thread.Sleep(new TimeSpan(0, 0, 5));
                Environment.Exit(0);
            }
            totalRam = ((phys + virt + swap) / 1024f) / 1024f;
            Console.WriteLine("Reading memory data went successfully");
        }

        private static int Percentage(float mem)
        {
            int free = (int)Math.Ceiling(((mem / totalRam) * 100));
            int taken = 100 - free;
            return taken;
        }

        private static int GetTemperatureOfType(HardwareType t)
        {
            Visitor v = new Visitor();
            Computer pc = new Computer();
            pc.Open();
            pc.CPUEnabled = true;
            pc.GPUEnabled = true;
            pc.Accept(v);
            IHardware[] hardware = pc.Hardware;
            float r = -1;
            for(int i = 0; i < hardware.Length; i++)
            {
                var h = hardware[i];
                if(h.HardwareType == t)
                {
                    for(int j = 0; j < h.Sensors.Length; j++)
                    {
                        var s = h.Sensors[i];
                        Console.WriteLine(s.SensorType.ToString());
                        if (s.SensorType == SensorType.Temperature)
                        {
                            r = s.Value.GetValueOrDefault(-1);
                        }
                    }
                }
            }
            pc.Close();
            if(r != -1)
            {
                return (int)Math.Floor(r);
            } else
            {
                throw new Exception("Temperature of component " + t.ToString() + " is null");
            }
        }

        static void Main(string[] args)
        {
            cpu = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
            ram = new PerformanceCounter("Memory", "Available MBytes");
            cpu.NextValue();
            GetTotalMemory();
            //Uncomment next line for production code
            //TODO: Arduino port autodetection
            //Comment next line for production code
            var pn = "COM1";
            port = new SerialPort(pn, 9600, Parity.None, 8, StopBits.One);
            port.Handshake = Handshake.None;
            port.Open();
            port.Write(new byte[] { 0xFF }, 0, 1);
            while(true)
            {
                try
                {
                    var cpusage = (byte)Math.Ceiling(cpu.NextValue());
                    var ramusage = (byte)Percentage(ram.NextValue());
                    var cputemp = GetTemperatureOfType(HardwareType.CPU);
                    var gputemp = GetTemperatureOfType(HardwareType.GpuAti);
                } catch(Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                break;
            }
            Console.ReadLine();
        }
    }
}

如您所见,我添加了以下行来调试传感器的类型:

Console.WriteLine(s.SensorType.ToString());

但是,它给出了以下内容: No temperature sensors 那么,有什么问题呢?我以这样的方式编写清单,即强制应用 始终以管理员身份进行调整。相同的结果。但是OHM给了我result。 有什么问题吗?

0 个答案:

没有答案
相关问题