python脚本变慢,几秒钟后几乎停止

时间:2019-10-12 14:14:29

标签: python python-3.x arduino

我正在尝试使用arduino进行资源监控,并且它在几乎停止运行之前已经运行了将近20秒。 当速度变慢时,两次更新之间大约需要5秒钟。

我尝试用psuti l注释掉所有内容,并赋予它永久性的价值。 并尝试过使用GPUtil

这是python代码

import serial.tools.list_ports
import serial
import psutil
import GPUtil
import time
import serial

ports = list(serial.tools.list_ports.comports())
baud = 9600
for p in ports:
    if "Arduino" in p[1]:
        port = p[0]

ser=serial.Serial(port, baud, timeout=1)
try:
    while True:
        cpuUsage = psutil.cpu_percent()
        ramUsage = psutil.virtual_memory()
        cpuUsage = str(cpuUsage)


        GPUs = GPUtil.getGPUs()
        gpuUsage = GPUs[0].load
        gpuUsage = str(gpuUsage)
        gpuUsage = gpuUsage[2:]

        ramUsage = str(ramUsage.percent)

        toSend = cpuUsage + "," + gpuUsage + ","+ ramUsage
        print (toSend)


        ser.write(toSend.encode())
        #print("20.5".encode())




        #line = ser.readline()[:-2]
        #line.decode()
        #print ("Read : " , line);
        time.sleep(0.1)
except:
    print ("error")

这是Arduino代码

#include <FastLED.h>
#define NUM_LEDS 15
#define DATA_PIN 6
float Cpu = 40;
float Gpu = 99;
float Ram = 60;
String Cpu_Read;
String Gpu_Read;
String Ram_Read;
int RXLED = 17;
String StrNumbers = "";
int numbers;
int Received = 0;
int Separrator = 0;
String Text = "";
CRGB leds[NUM_LEDS];
void setup() {
  // put your setup code here, to run once:
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(5);

  Serial.begin(115200);
  pinMode(LED_BUILTIN_TX,INPUT);
  pinMode(LED_BUILTIN_RX,INPUT);
}
void loop() {
  // put your main code here, to run repeatedly:
  char inByte = ' ';
  StrNumbers = "";
  Text = "";
  Separrator = 0;
  Cpu_Read = "";
  Gpu_Read = "";
  Ram_Read = "";
  Received = 0;
  pinMode(LED_BUILTIN_TX,INPUT);
  while(Serial.available() > 0){ // only send data back if data has been sent
    pinMode(LED_BUILTIN_TX,OUTPUT);
    inByte = Serial.read(); // read the incoming data
    if (inByte == ','){
      Separrator += 1;
    }
    else if (Separrator == 0){
      Cpu_Read +=  (char) inByte;
    }
    else if (Separrator == 1){
      Gpu_Read +=  (char) inByte;
    }
    else if (Separrator == 2){
      Ram_Read +=  (char) inByte;
      Serial.print("Ram : ");
      Serial.println(Ram_Read);
    }
    else{
      Serial.println("Error");
    }
    /*
    inByte = Serial.read(); // read the incoming data
    if (isDigit(inByte)) {  // tests if inByte is a digit
      StrNumbers += (char) inByte;
      Serial.println(inByte); // send the data back in a new line so that it is not all one long line
      //Serial.println(numbers); // send the data back in a new line so that it is not all one long line
    }
    else if (inByte == ".")
    {
      abortLoop = 1;
    }
    else{
      Text +=(char) inByte;
    } 
    */
    Received = 1;
  }
  if (Received == 1){
    Cpu = Cpu_Read.toInt();
    Gpu = Gpu_Read.toInt();
    Ram = Ram_Read.toInt();
    UpdateMonitor(Cpu ,Gpu ,Ram);

  }
  Text.trim();
  if (StrNumbers != ""){
    numbers = StrNumbers.toInt();
    Serial.println(numbers);
    if (numbers > 100) numbers = 100;
    UpdateMonitor(Cpu,numbers,Ram);
  }
  if(Text!= ""){
    //Serial.println(Text); // send the data back in a new line so that it is not all one long line

  }
  if (Text == "ResourceMonitor"){
    Serial.println("Yes");
  }
  else if (Text != ""){
    Serial.println(Text);
    numbers = Text.toInt();
    if (numbers > 100) numbers = 100;
    UpdateMonitor(Cpu, numbers, Ram);
  }



}
void UpdateMonitor(int cpu,int gpu, int ram){
  int Cpu_Usage = map(cpu, 0, 100, 0, 5);
  int Gpu_Usage = map(gpu, 0, 100, 0, 5);
  int Ram_Usage = map(ram, 0, 100, 0, 5);
  FastLED.clear();
  for(int led = 0; led < Ram_Usage; led++) {
    leds[led] = CRGB::Blue;
  }
  for(int led = 0; led < Gpu_Usage; led++) {
    leds[9 - led] = CRGB::Green;
  }
  for(int led = 0; led < Cpu_Usage; led++) {
    leds[led+10] = CRGB::Red;
  }
  FastLED.show();
}

1 个答案:

答案 0 :(得分:0)

现在已修复,不确定它是什么修复程序,但它至少可以工作:) 比一切企图停止的要好

相关问题