Python+PyQt5+C Hw信息检测

UI主界面:

Python+PyQt5+C Hw信息检测_第1张图片

C源代码:

/* I/O port access
 * Copyright (C) 2009 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

//#include 

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 

static int op = 0;		/* Operation, 0 = read, 1 = write. */
static int size = 1;		/* Operation size in bytes. */
static int rcode = 0;		/* If set, inX exits with return value. */
static int rhex = 0;		/* If set, print result in hex. */

static struct option long_options[] = {
  { "code",  0, 0, 'c' },
  { "read",  0, 0, 'r' },
  { "size",  1, 0, 's' },
  { "write", 0, 0, 'w' },
  { "hex",   0, 0, 'x' },
  { NULL,    0, 0, 0   },
};
static const char *options = "crs:w";

static void
usage ()
{
  printf ("\n\
Usage:\n\
  inb|inw|inl [--options] address\n\
  outb|outw|outl [--options] address data\n\
Options:\n\
--code    Exit with status code instead of printing value (inX only).\n\
--hex     Print hex instead of decimal (inX only).\n\
--read    Perform a read (in) operation.\n\
--write   Perform a write (out) operation.\n\
--size N  Set size to N bytes where N = 1, 2 or 4.\n\
\n\
For detailed information, see the manual page inb(1).\n\
");
}

static unsigned get_int_or_die (const char *);

int
main (int argc, char *argv[])
{
  int c, option_index;
  char *p;
  unsigned addr = 0;
  unsigned data = 0;

  /* Find out how the program was invoked. */
  p = strrchr (argv[0], '/');
  if (p == NULL) p = argv[0]; else p++;

  if (strncasecmp (p, "inb", 3) == 0) {
    op = 0; size = 1;
  } else if (strncasecmp (p, "outb", 4) == 0) {
    op = 1; size = 1;
  } else if (strncasecmp (p, "inw", 3) == 0) {
    op = 0; size = 2;
  } else if (strncasecmp (p, "outw", 4) == 0) {
    op = 1; size = 2;
  } else if (strncasecmp (p, "inl", 3) == 0) {
    op = 0; size = 4;
  } else if (strncasecmp (p, "outl", 4) == 0) {
    op = 1; size = 4;
  }

  /* Parse command line arguments. */
  while (1) {
    option_index = 0;
    c = getopt_long (argc, argv, options, long_options, &option_index);
    if (c == -1) break;

    switch (c) {
    case 'c':
      rcode = 1;
      break;

    case 'r':
      op = 0;
      break;

    case 's':
      size = get_int_or_die (optarg);
      if (!(size == 1 || size == 2 || size == 4)) {
	fprintf (stderr, "%s: size can only be 1, 2 or 4.\n", optarg);
	exit (1);
      }
      break;

    case 'w':
      op = 1;
      break;

    case 'x':
      rhex = 1;
      break;

    case 0:
      fprintf (stderr, "internal error in getopt_long\n");
      exit (1);

    default:
      usage ();
      exit (1);
    }
  }

  /* Parse the address (for read/write) and data (for writes). */
  if (optind >= argc) {
  missing:
    fprintf (stderr, "%s: missing parameter, see --help or man page\n",
	     argv[0]);
    exit (1);
  }

  addr = get_int_or_die (argv[optind++]);

  if (op == 1) {
    if (optind >= argc) goto missing;
    data = get_int_or_die (argv[optind++]);
  }

  if (optind != argc) {
    fprintf (stderr,
	     "%s: extra parameters on command line, see --help or man page\n",
	     argv[0]);
    exit (1);
  }

  /* Raise our privilege level. */
  if (iopl (3) == -1) {
    fprintf (stderr, "iopl failed: You may need to run as root or give the process the CAP_SYS_RAWIO\ncapability. On non-x86 architectures, this operation probably isn't possible.\n");
    perror ("iopl");
    exit (1);
  }

  /* Perform the operation. */
  switch (op) {
  case 0:
    switch (size) {
    case 1: data = inb (addr); break;
    case 2: data = inw (addr); break;
    case 4: data = inl (addr); break;
    }
    break;
  case 1:
    switch (size) {
    case 1: outb (data, addr); break;
    case 2: outw (data, addr); break;
    case 4: outl (data, addr); break;
    }
    break;
  }

  if (op == 0) {
    if (rcode == 0) {
      if (rhex == 0)
	printf ("%d\n", data);
      else
	printf ("%x\n", data);
    } else
      exit (data);
  }
  exit (0);
}

static unsigned
get_int_or_die (const char *str)
{
  char *endp;
  unsigned r;

  errno = 0;
  r = strtoul (str, &endp, 0);
  if ((errno == ERANGE && r == ULONG_MAX)
      || (errno != 0 && r == 0)) {
    perror (str);
    exit (1);
  }

  if (str == endp) {
    fprintf (stderr, "expecting a number, but found an empty string\n");
    exit (1);
  }
  if (*endp != '\0') {
    fprintf (stderr, "%s: trailing garbage after number\n", str);
    exit (1);
  }

  return r;
}

配置文件:

[HW_UOS_X86_FAN]
R_FAN_TAC1_READING=0x0D
R_FAN_TAC1_EXT_READING=0x18
R_FAN_TAC2_READING=0x0E
R_FAN_TAC2_EXT_READING=0x19
R_FAN_TAC3_READING=0x0F
R_FAN_TAC3_EXT_READING=0x1A
R_FAN_TAC4_LBS_READING=0x80
R_FAN_TAC4_MSB_READING=0x81
R_FAN5_READING_LSB=0x82
R_FAN5_READING_MSB=0x83

[HW_UOS_X86_TMPIN]
R_TMPIN1_READING=0x29
R_TMPIN2_READING=0x2A
R_TMPIN3_READING=0x2B

[HW_UOS_X86_Voltage]
R_VIN0_READING=0x20
R_VIN1_READING=0x21
R_VIN2_READING=0x22
R_VIN3_READING=0x23
R_VIN4_READING=0x24
R_VIN5_READING=0x25
R_VIN6_READING=0x26
R_VBAT_READING=0x28

[HW_UOS_X86_TestConfig]
CPUFAN=1000~2000
SYSFAN=1300~2000
CPUTMP=25~40
VIN_CPU=1.11~1.2
VIN_DIMM=1.20~1.330
VIN_12=12~12.7
VIN_5V=5~5.08
VIN_3_3V=3.3~3.5
VBAT=1.8~2.2

[HW_ARM_TestConfig]
CPUFAN=1300~3000
CPUTMP1=25~40
CPUTMP2=25~40

源代码:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'HWWinFrm.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QTimer, pyqtSlot, Qt
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel
#pip install opencv-python PyQt5
from PyQt5 import *
from PyQt5.Qt import *
from PyQt5 import QtWidgets
from PyQt5 import QtGui
from PyQt5 import QtCore
import logging
import os
import json
import configparser
from PyQt5.QtCore import pyqtSignal
import sys
import subprocess
import inspect
from subprocess import Popen,PIPE
from colorama import Fore,Style
import re
#pip install colorama

class HW_UOS_X86(QtCore.QThread):
    test_result_signal = QtCore.pyqtSignal(tuple)  # 新增信号,传递一个包含设备路径和测试结果的元组
    finished_signal = QtCore.pyqtSignal()  # 新增信号,表示线程完成

    def __init__(self,parent=None):
        super(HW_UOS_X86, self).__init__(parent)
        self.is_running=True

    def run(self):
        self.config = configparser.ConfigParser()  # 创建对象
        self.FANControl = []  # 风扇控制器
        #self.SYSFANControl = []  # 风扇控制器
        self.TEMPINControl = []  # 温度控制器
        self.VINControl = []  # 电压控制器
        self.TestArgs = []  # 测试参数

        # 读取的参数
        self.r_FanValues = []  # 读取的风扇值
        self.r_TempValues = []  # 读取温度值
        self.r_VinValues = []  # 读取电压值
        self.testResult=True#测试结果
        self.parent().hwTestInfo=''#测试信息
        self.testStandardArgs = ''  # 测试准标参数

        # 读取配置
        self.config.read('./Conf/config.conf', encoding='utf-8')  # 读取配置文件,如果配置文件不存在创建

        # 读取风扇配置寄存器信息
        self.FANControl.append(self.config.get('HW_UOS_X86_FAN', 'R_FAN_TAC1_READING'))
        self.FANControl.append(self.config.get('HW_UOS_X86_FAN', 'R_FAN_TAC1_EXT_READING'))
        self.FANControl.append(self.config.get('HW_UOS_X86_FAN', 'R_FAN_TAC2_READING'))
        self.FANControl.append(self.config.get('HW_UOS_X86_FAN', 'R_FAN_TAC2_EXT_READING'))
        self.FANControl.append(self.config.get('HW_UOS_X86_FAN', 'R_FAN_TAC3_READING'))
        self.FANControl.append(self.config.get('HW_UOS_X86_FAN', 'R_FAN_TAC3_EXT_READING'))
        self.FANControl.append(self.config.get('HW_UOS_X86_FAN', 'R_FAN_TAC4_LBS_READING'))
        self.FANControl.append(self.config.get('HW_UOS_X86_FAN', 'R_FAN_TAC4_MSB_READING'))
        self.FANControl.append(self.config.get('HW_UOS_X86_FAN', 'R_FAN5_READING_LSB'))
        self.FANControl.append(self.config.get('HW_UOS_X86_FAN', 'R_FAN5_READING_MSB'))

        # 读取温度配置寄存器信息
        self.TEMPINControl.append(self.config.get('HW_UOS_X86_TMPIN', 'R_TMPIN1_READING'))

        # 读取电压配置寄存器信息
        self.VINControl.append(self.config.get('HW_UOS_X86_Voltage', 'R_VIN0_READING'))
        self.VINControl.append(self.config.get('HW_UOS_X86_Voltage', 'R_VIN1_READING'))
        self.VINControl.append(self.config.get('HW_UOS_X86_Voltage', 'R_VIN2_READING'))
        self.VINControl.append(self.config.get('HW_UOS_X86_Voltage', 'R_VIN3_READING'))
        self.VINControl.append(self.config.get('HW_UOS_X86_Voltage', 'R_VIN4_READING'))
        self.VINControl.append(self.config.get('HW_UOS_X86_Voltage', 'R_VIN5_READING'))
        self.VINControl.append(self.config.get('HW_UOS_X86_Voltage', 'R_VIN6_READING'))
        self.VINControl.append(self.config.get('HW_UOS_X86_Voltage', 'R_VBAT_READING'))

        # 读取的hw信息配置
        self.HwConfig_CPUFAN = []  # Hw配置CPU风扇信息
        self.HwConfig_CPUFAN.append(self.config.get('HW_UOS_X86_TestConfig', 'CPUFAN'))
        self.cpufan=self.HwConfig_CPUFAN[0].split('~')#cpu风扇转速参数

        self.HwConfig_SYSFAN=[]# HW配置系统风扇信息
        self.HwConfig_SYSFAN.append(self.config.get('HW_UOS_X86_TestConfig', 'SYSFAN'))
        self.sysfan = self.HwConfig_SYSFAN[0].split('~')  # cpu风扇转速参数

        # 读取温度信息配置
        self.HwConfig_Tmp = []  # Hw配置温度信息
        self.HwConfig_Tmp.append(self.config.get('HW_UOS_X86_TestConfig', 'CPUTMP'))
        self.cputmp=self.HwConfig_Tmp[0].split('~')#cpu温度参数

        # 读取电压信息配置
        self.HwConfig_Vin = []  # Hw配置电压信息
        self.HwConfig_Vin.append(self.config.get('HW_UOS_X86_TestConfig', 'VIN_CPU'))
        self.vincpu=self.HwConfig_Vin[0].split('~')
        self.HwConfig_Vin.append(self.config.get('HW_UOS_X86_TestConfig', 'VIN_DIMM'))
        self.vindimm=self.HwConfig_Vin[1].split('~')
        self.HwConfig_Vin.append(self.config.get('HW_UOS_X86_TestConfig', 'VIN_12'))
        self.vin_12=self.HwConfig_Vin[2].split('~')
        self.HwConfig_Vin.append(self.config.get('HW_UOS_X86_TestConfig', 'VIN_5V'))
        self.vin_5v=self.HwConfig_Vin[3].split('~')
        self.HwConfig_Vin.append(self.config.get('HW_UOS_X86_TestConfig', 'VIN_3_3V'))
        self.vin_3_3v=self.HwConfig_Vin[4].split('~')
        self.HwConfig_Vin.append(self.config.get('HW_UOS_X86_TestConfig', 'VBAT'))
        self.vbat=self.HwConfig_Vin[5].split('~')

        # 读取测试参数并比较
        self.ReadFanVlaues('R_FAN_TAC1',self.FANControl[0],self.FANControl[1],self.cpufan)
        #self.ReadFanVlaues('R_FAN_TAC2',self.FANControl[2],self.FANControl[3],self.cputmp)

        # 读取温度参数
        # print('Template_1:',self.TEMPINControl[0])
        self.ReadCpuTemplate('R_TMPIN1', self.TEMPINControl[0],self.cputmp)  # 读取TEMP1寄存器温度

        # 读取电压参数
        self.ReadVINvalues('VIN_CPU', self.VINControl[0],self.vincpu)  # 读取VIN_CPU
        self.ReadVINvalues('VIN_DIMM', self.VINControl[1],self.vindimm)  # 读取VINO寄存器电压
        self.ReadVINvalues('VIN_12', self.VINControl[2],self.vin_12)  # 读取VIN_12V寄存器电压
        self.ReadVINvalues('VIN_5V', self.VINControl[3],self.vin_5v)  # 读取VIN_5V寄存器电压
        self.ReadVINvalues('VIN_3_3V', self.VINControl[4],self.vin_3_3v)  # 读取VIN_3_3V寄存器电压
        self.ReadVINvalues('VBAT', self.VINControl[5],self.vbat)  # 读取VBAT寄存器电压
        #self.ReadVINvalues('R_VIN6', self.VINControl[6],)  # 读取VINO寄存器电压
        #self.ReadVINvalues('R_VBAT', self.VINControl[7],)  # 读取VINO寄存器电压


        #更新风扇信息
        for fanvalues in self.r_FanValues:#CPU风扇信息
            isPass = False
            #faninfo.append(fanvalues['itemName'])
            #faninfo.append(fanvalues['readValues'])
            #faninfo.append(fanvalues['testReuslt'])
            faninfo = [fanvalues['itemName'], fanvalues['readValues'], fanvalues['testReuslt']]
            if fanvalues['testReuslt']=='PASS':
                isPass=True
            else:
                self.testResult=False
            self.parent().TableAddItem(faninfo,isPass)
            self.parent().hwTestInfo+=f"{fanvalues['itemName']},{fanvalues['readValues']},{fanvalues['testReuslt']}|"

        #更新CPU温度信息
        for tempvalues in self.r_TempValues:#CPU温度信息
            isPass=False
            #tempinfo.append(tempvalues['itemName'])
            #tempinfo.append(tempvalues['readValues'])
            #tempinfo.append(tempvalues['testReuslt'])
            tempinfo = [tempvalues['itemName'],tempvalues['readValues'],tempvalues['testReuslt']]
            if tempvalues['testReuslt']=='PASS':
                isPass=True
            else:
                self.testResult=False
            self.parent().TableAddItem(tempinfo,isPass)
            self.parent().hwTestInfo += f"{tempvalues['itemName']},{tempvalues['readValues']},{tempvalues['testReuslt']}|"

        #更新各项电压信息
        for vim in self.r_VinValues:#电压信息
            isPass=False
            #viminfo.append(vim['itemName'])
            #viminfo.append(vim['readValues'])
            #viminfo.append(vim['testReuslt'])
            viminfo = [vim['itemName'],vim['readValues'],vim['testReuslt']]
            if vim['testReuslt']=='PASS':
                isPass=True
            else:
                self.testResult=False
            self.parent().TableAddItem(viminfo,isPass)
            self.parent().hwTestInfo += f"{vim['itemName']},{vim['readValues']},{vim['testReuslt']}|"
        self.parent().hwTestInfo=self.parent().hwTestInfo[:-1]
        self.finished_signal.emit()  # 线程完成时发出信号
        self.stop()
        self.parent().hwTestPass = self.testResult#回传测试结果

    def stop(self):
        self.is_running = False # 或者使用更安全的停止逻辑

    #读取电压信息
    def ReadVINvalues(self,itemName,sendsorData,argsvalues):
        try:
            vinvalue=0.0000
            #进行pnp mode
            os.system('./Hw_UOS_X86/port --write 0x2E 0x87')
            os.system('./Hw_UOS_X86/port --write 0x2E 0X01')
            os.system('./Hw_UOS_X86/port --write 0x2E 0X55')
            os.system('./Hw_UOS_X86/port --write 0x2E 0X55')

            #读取电压寄存器
            os.system('./Hw_UOS_X86/port --write 0x295 ' + sendsorData)  #传值
            redtep_l = Popen(['./Hw_UOS_X86/port --read 0x296'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
            # 获取子进程的输出
            stdout, stderr = redtep_l.communicate()
            #将输出转换成为字符串
            vin=stdout.decode("utf-8").strip()#获取的值
            if itemName=='VIN_12':#12V电压
                vinvalue=float((int(vin)/2)*12)/100+1
            elif itemName=='VIN_5V':#5V电压
                vinvalue=float((int(vin)/2)*5)/100+0.4
            elif itemName=='VIN_3_3V':#3.3V电压
                vinvalue=float((int(vin)/20)*33)/100+0.3
            else:
                vinvalue=(float(vin)*10.9)/1000
            result=round(vinvalue,4)

            if float(result)>float(argsvalues[0]) and float(result)float(argsvalues[0]) and float(tmp)float(self.cpufan[0]) and float(fanvalues)float(self.cpufanArgs[0]) and float(fanvalues)float(self.cputmp1_Args[0]) and float(CpuTemplate_ts1)float(self.cputmp2_Args[0]) and float(CpuTemplate_ts2)

你可能感兴趣的:(python,qt,c语言)