Exemple #1
0
        public HardwareSensors GetTemperaturesInCelsius()
        {
            int i = 0;

            foreach (var hardware in _computer.Hardware)
            {
                hardware.Update(); //use hardware.Name to get CPU model
                if (hardware.HardwareType == HardwareType.CPU)
                {
                    foreach (var sensor in hardware.Sensors)
                    {
                        if (sensor.SensorType == SensorType.Temperature && sensor.Value.HasValue)
                        {
                            Console.WriteLine("{0}, Value={1}, Min Value={2}, Max Value={3}",
                                              sensor.Name, sensor.Value.Value, sensor.Min.Value, sensor.Max.Value);
                            if (sensor.Name.Contains("Package"))
                            {
                                temperature     = Convert.ToInt32(sensor.Value);
                                temperature_min = Convert.ToInt32(sensor.Min);
                                temperature_max = Convert.ToInt32(sensor.Max);
                            }
                        }
                        if (sensor.SensorType == SensorType.Clock && sensor.Value.HasValue)
                        {
                            Console.WriteLine("{0}, Value={1}, Min Value={2}, Max Value={3}",
                                              sensor.Name, sensor.Value.Value, sensor.Min.Value, sensor.Max.Value);
                            if (!sensor.Name.Contains("Bus"))
                            {
                                i++;
                                cpu_clock += (float)sensor.Value;
                            }
                        }
                    }
                }
                if (hardware.HardwareType == HardwareType.RAM)
                {
                    foreach (var sensor in hardware.Sensors)
                    {
                        if (sensor.SensorType == SensorType.Load && sensor.Value.HasValue)
                        {
                            Console.WriteLine("{0}, Value={1}", sensor.Name, sensor.Value.Value);
                            mem_load = Convert.ToInt32(sensor.Value);
                        }
                    }
                }
            }
            cpu_clock = cpu_clock / i / 1000f;
            hs        = new HardwareSensors(temperature, temperature_min, temperature_max, cpu_clock, mem_load);
            return(hs);
        }
        public static void StartTest()
        {
            //string name;
            string message;

            cpuReader = new CpuTemperatureReader();
            Thread readThread = new Thread(Read);

            // Create a new SerialPort object with default settings.
            _serialPort = new SerialPort();

            // Allow the user to set the appropriate properties.
            Console.WriteLine("SetPortName: COM1");
            Console.WriteLine("SetPortBaudRate: 115200");
            Console.WriteLine("SetPortParity: None");
            Console.WriteLine("SetPortDataBits: 8");
            Console.WriteLine("SetPortStopBits: One");
            Console.WriteLine("SetPortHandshake: None");

            /*
             * _serialPort.PortName = SetPortName("COM1");
             * _serialPort.BaudRate = SetPortBaudRate(115200);
             * _serialPort.Parity = SetPortParity(_serialPort.Parity);
             * _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
             * _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
             * _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);
             */
            _serialPort.PortName  = "COM1";
            _serialPort.BaudRate  = 115200;
            _serialPort.Parity    = (Parity)Enum.Parse(typeof(Parity), Parity.None + "", true);
            _serialPort.DataBits  = 8;
            _serialPort.StopBits  = (StopBits)Enum.Parse(typeof(StopBits), StopBits.One + "", true);
            _serialPort.Handshake = (Handshake)Enum.Parse(typeof(Handshake), Handshake.None + "", true);

            //接收中文
            _serialPort.Encoding = System.Text.Encoding.GetEncoding("GB2312");
            // Set the read/write timeouts
            _serialPort.ReadTimeout  = 500;
            _serialPort.WriteTimeout = 500;

            _serialPort.RtsEnable = true;
            _serialPort.DtrEnable = true;

            _serialPort.Open();
            _continue = true;
            readThread.Start();
            start_time = GetComputerStartTime().ToString("F");
            Console.WriteLine("system start time: " + start_time);
            //name = Console.ReadLine();

            //Console.WriteLine("Type QUIT to exit");

            int num = 0;

            while (_continue)
            {
                //message = Console.ReadLine();
                if (_serialPort.IsOpen)
                {
                    if (!update_time)
                    {
                        //Console.WriteLine("start time............." + start_time);
                        hs = cpuReader.GetTemperaturesInCelsius();
                        Thread.Sleep(500);
                        _serialPort.WriteLine(
                            String.Format("<{0}>@{1}", "start_time", start_time));
                        Console.WriteLine(String.Format("CPU Package:{0}℃ Min:{1}℃ Max:{2}℃ CPU Speed:{3}GHz Memory Load:{4}%",
                                                        hs.temperature, hs.temperature_min, hs.temperature_max, hs.cpu_clock.ToString("f2"), hs.mem_load));
                    }
                    else
                    {
                        if (!pause)
                        {
                            current_time = System.DateTime.Now;
                            //message = "连上了,哈哈哈哈哈啊哈哈";
                            hs      = cpuReader.GetTemperaturesInCelsius();
                            message = String.Format("CPU Package:{0}℃ Min:{1}℃ Max:{2}℃ CPU Speed:{3}GHz Memory Load:{4}%",
                                                    hs.temperature, hs.temperature_min, hs.temperature_max, hs.cpu_clock.ToString("f2"), hs.mem_load);
                            num         = message.Length;
                            send_count += num;
                            Thread.Sleep(1000);
                            _serialPort.WriteLine(message);
                            Console.WriteLine(current_time.ToString("HH:mm:ss") + "发送字节:" + send_count.ToString() + "Bytes");
                        }
                    }
                }
            }

            readThread.Join();
            _serialPort.Close();
        }