2

下午好,我正在使用 switch 构造,但我不知道如何正确编写 case 运算符之后的其他值。

在DisplayData代码中,在switch构造中,我给字段分配了一个命令号,例如:Vin = cp5.addTextlabel,这样以后通过com口连接时,通过发送这个命令“60”来显示或更改数据",123 在 Vin = cp5.addTextlabel 字段中。

仅适用于 (minutess = cp5.addButton) - 10, (Vin = cp5.addTextlabel) - 60, (inputPULI = cp5.addNumberbox) - 40, (inputNapryzenieKV = cp5.addNumberbox) - 70

主要错误如下(函数“setText(String)”未定义。见截图。

如何为变量修复此问题并正确编写?

图像按钮按钮;

单选按钮 CheckBoxuvum; 文档

复选框 P4;文档

查看图形用户界面文件!

一个任务:

ImageButton(按钮) - 20,目标是通过终端或微控制器发送命令来获得窗口中的图片变化。

P4 = cp5.addCheckBox - 30 CHECK,目标是通过终端或微控制器发送命令来改变窗口中复选框的打开和关闭状态。

CheckBoxuvum = cp5.addRadioButton - 50 +/-,目标是通过终端或微控制器发送命令来改变窗口中复选框的打开和关闭 (+/-) 状态。

在 Displaydata 代码中,我用一个问题对此进行了标记。

将来,我计划通过 com 端口从终端发送命令,以接收这些值的窗口中数据状态的变化。

错误截图: 在此处输入图像描述

我将不得不上传所有测试项目文件,以便您进行测试。

Com_Interface1:

import processing.serial.*;
import controlP5.*;
ControlP5 cp5;
DropdownList serialPortsList;

Serial serialPort;
final int BAUD_RATE = 115200;

char parity = 'N';
int dataBits = 8;
float stopBits = 1.0;

public void setup() {
  size(700, 420, JAVA2D);
  surface.setTitle("TEST");
  surface.setResizable(false);
  setupUI();
  smooth();
  frameRate(30);
  writeOutgioing[lengthmas-1]=1; 
  String[] portNames = Serial.list();
  //serialPort.clear(); // Why does not it work?
  for (int i = 0; i < portNames.length; i++) serialPortsList.addItem(portNames[i], i);
}
public void toplug (int theValue) { // Start button on click sends a commad 1.
  println("Button click events: "+theValue);
  strata =!strata;
  if (!strata) {
    connection.setLabel("Пуск");
  } else {
    connection.setLabel("СТОП");
    serialports((int)serialPortsList.getValue());
    writeOutgioing[0]=1;
    writeOut();
  }
}
public void serialports(int theValue) {
  try {
    serialPort = new Serial(this, Serial.list()[theValue], BAUD_RATE, parity, dataBits, stopBits);
    serialPort.bufferUntil('\n');

    println("COM connected: "+ Serial.list()[theValue] );
    /*Send.unlock();
     connection.unlock();*/    //locking buttons in applications if not connected via rs-232.
  }
  catch(Exception e) {
    System.err.println("Error opening serial port" + Serial.list()[theValue]);
    e.printStackTrace();
  }
}

显示数据:

    void Displaydata() {

  switch(readIncome[0]) {
  case 10:
    minutess.setCaptionLabel(readIncome[1]+" Мин1.");
    min1=readIncome[1];
    break;
  case 20:
    // What to write?
    // label(readIncome[1]+" Мин."); // ImageButton
    // min=readIncome[1];
    break;
  case 30:
    // What to write?
    // P4.setText("std2"+readIncome[1] ); // CheckBox
    break;
  case 40:
    inputPULI.setLabel("Bullet - "+readIncome[1] );
    break;
  case 70:
    inputNapryzenieKV.setLabel("Voltage - "+readIncome[1] );
    break;
  case 60:
    Vin.setText("Voltage K.V - "+readIncome[1] );
    break;
  case 50:
    // What to write?
    // CheckBoxuvum.setText("UV/UM - "+readIncome[1] ); // RadioButton
    break;
  default:
    println("DisplayData(): no case selected.");
    break; // technically not necessary, but I like my switches tidy
  }
}

图形用户界面:

int min1 = 0;
int PUL;
float NapryzenieKV;
boolean strata=false;

Button minutess;
ImageButton button; 
Numberbox inputPULI; 
Numberbox inputNapryzenieKV; 
RadioButton CheckBoxuvum; 
CheckBox P4; 
Textlabel Vin; 
Button connection; 
Button Send; 

public void setupUI()
{
  cp5 = new ControlP5(this);
  PFont fontn = createFont("Times New Roman", 18);
  PFont p = createFont("Times New Roman", 18);
  ControlFont font=new
    ControlFont(p);
  cp5.setFont(font);

  connection = cp5.addButton("toplug")
    .setCaptionLabel("ПУСК")
    .setPosition(387, 30)
    .setSize(150, 30);

  serialPortsList = cp5.addDropdownList("Порт")
    .setPosition(130, 30)
    .setSize(150, 200)
    .setItemHeight(30)
    .setBarHeight(30);

  minutess = cp5.addButton("minutesss")
    .setCaptionLabel("ВЫКЛ")
    .setPosition(145, 100)
    .setSize(90, 25);

  Send = cp5.addButton("toapply")
    .setCaptionLabel("Apply")
    .setPosition(510, 370)
    .setSize(150, 30);

  Vin = cp5.addTextlabel("naprazhenie kondencatora")
    .setText("Voltage K.V")
    .setFont(p)
    .setColor(color(#00ffff))
    .setPosition(45, 320);

  CheckBoxuvum = cp5.addRadioButton("UV/UM")
    .setPosition(155, 360)
    .setSize(15, 15)
    .setColorActive(color(255))
    .setItemsPerRow(2)
    .setSpacingColumn(85)
    .addItem("+", 1)
    .addItem("-", 2);

  P4 = cp5.addCheckBox("std2")
    .setPosition(150, 190)
    .setSize(15, 15)
    .setItemsPerRow(1)
    .setSpacingColumn(30)
    .setSpacingRow(20)
    .addItem("Check", 2);

  inputPULI = cp5.addNumberbox("PUL")
    .setLabel("Bullet")
    .setPosition(150, 220)
    .setSize(80, 30)
    .setColorValue(0xffffff00)
    .setFont(p)
    .setScrollSensitivity(1.1)
    .setDirection(Controller.HORIZONTAL)
    .setRange(1, 199)
    .setValue(3);
  Label labelinputPULI = inputPULI.getCaptionLabel();
  labelinputPULI.setFont(font);
  labelinputPULI.setColor(color(#00ffff));
  labelinputPULI.toUpperCase(false);
  labelinputPULI.setText("Пуль");
  labelinputPULI.align(ControlP5.LEFT_OUTSIDE, CENTER);
  labelinputPULI.getStyle().setPaddingLeft(-55);

  inputNapryzenieKV = cp5.addNumberbox("NapryzenieKV")
    .setLabel("Voltage")
    .setPosition(150, 270)
    .setSize(80, 30)
    .setColorValue(0xffffff00)
    .setFont(p)
    .setScrollSensitivity(1.1)
    .setMin(25)
    .setMax(99)
    .setMultiplier(0.01)
    .setDirection(Controller.HORIZONTAL)
    .setValue(25);
  Label labelinputNapryzenieKV = inputNapryzenieKV.getCaptionLabel();
  labelinputNapryzenieKV.setFont(font);
  labelinputNapryzenieKV.setColor(color(#00ffff));
  labelinputNapryzenieKV.toUpperCase(false);
  labelinputNapryzenieKV.setText("Напряжение");
  labelinputNapryzenieKV.align(ControlP5.LEFT_OUTSIDE, CENTER);
  labelinputNapryzenieKV.getStyle().setPaddingLeft(-45);

  textFont(fontn);
  {
    // button dimensions
    int w = 99;
    int h = 25;
    // test with generated images
    button = new ImageButton(140, 140, w, h, 
      new PImage[]{
      getImage(w, h, color(192, 0, 32 * 2)), // off
      getImage(w, h, color(0, 0, 32 * 3)), // 10
      getImage(w, h, color(0, 0, 32 * 4)), // 20
      getImage(w, h, color(0, 0, 32 * 5)), // 30
      getImage(w, h, color(0, 0, 32 * 6)), // 40
      getImage(w, h, color(0, 0, 32 * 7)), // 50
      getImage(w, h, color(0, 0, 32 * 8)), // 60
      });
  }
}
void mousePressed() {
  button.mousePressed(mouseX, mouseY);
  println(button.min);
}
// test images to represent loaded state images
PImage getImage(int w, int h, int c) {
  PImage img = createImage(w, h, RGB);
  java.util.Arrays.fill(img.pixels, c);
  img.updatePixels();
  return img;
}

// make a custom image button class
class ImageButton {
  // minutes is the data it stores
  int min = 0;
  // images for each state
  PImage[] stateImages;
  // which image to display
  int stateIndex;
  // position
  int x, y;
  // dimensions: width , height
  int w, h;
  // text to display
  String label = "ВЫКЛ";

  ImageButton(int x, int y, int w, int h, PImage[] stateImages) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.stateImages = stateImages;
  }
  void mousePressed(int mx, int my) {
    // check the cursor is within the button bounds
    boolean isOver = ((mx >= x && mx <= x + w) && // check horizontal
      (my >= y && my <= y + h) ); // check vertical

    if (isOver) {

      min += 10;
      stateIndex++;

      if (min>60) {
        min = 0; 
        stateIndex = 0;
        label = "ВЫКЛ";
      } else {
        label = (str(min) + "Мин");
      }
    }
  }
  void draw() {
    // if the images and index are valid
    if (stateImages != null && stateIndex < stateImages.length) {
      image(stateImages[stateIndex], x, y, w, h);
    } else {
      println("error displaying button state image");
      println("stateImages: ");
      printArray(stateImages);
      println("stateIndex: " + stateIndex);
    }
    // display text
    text(label, x + 17, y + h - 8);
  }
}
public void minutesss() {

  min1 += 10;
  if (min1>60) {
    min1 = 0; 
    minutess.setCaptionLabel("ВЫКЛ");
  } else { 
    minutess.setCaptionLabel(str(min1)+" Мин1");
  }
}
void controlEvent(ControlEvent theEvent) {
  if (theEvent.isFrom(CheckBoxuvum)) {
    //myColorBackground = 0;
    print("got an event from "+CheckBoxuvum.getName()+"\t\n");
    // checkbox uses arrayValue to store the state of 
    // individual checkbox-items. usage:
    println(CheckBoxuvum.getArrayValue());
    int col = 0;
    for (int i=0; i<CheckBoxuvum.getArrayValue().length; i++) {
      int n = (int)CheckBoxuvum.getArrayValue()[i];
      print(n);
      if (n==1) {
        //myColorBackground += CheckBoxuvum.getItem(i).internalValue();
      }
    }
    println();
  }

  if (theEvent.isGroup()) {
    // check if the Event was triggered from a ControlGroup
    println("event from group : "+theEvent.getGroup().getValue()+" from "+theEvent.getGroup());
  } else if (theEvent.isController()) {
    println("event from controller : "+theEvent.getController().getValue()+" from "+theEvent.getController());
  }
}

协议:

int lengthmas = 7;
int RC = -128; // -128 separating byte в java.
int[] readIncome = new int[lengthmas];
int[] writeOutgioing = new int[lengthmas];
String incomingData= null;
String outgoingData=null;

void readInc() {
  while ( serialPort.available() > 0) {
    incomingData = serialPort.readStringUntil(RC);
    readIncome = int(split (incomingData, ',' )); // the received data is comma separated.
  }
}

void  writeOut() {
  outgoingData=str(writeOutgioing[0])+str(writeOutgioing[1])+str(writeOutgioing[2])+str(writeOutgioing[3])+str(writeOutgioing[4])+str(writeOutgioing[5])+str(writeOutgioing[6]); // sending data as a string.
  serialPort.write(outgoingData);
}

/*Protocol:
 Protocol composition - an array of seven elements, 0 - command, 1-4 data, 5 - remainder of packets for transmission,  6 (-128) separating byte.
 The controller responds by sending the same command and data.
 Example: 10(Command),235,2353,564,5(packages left),77777(CRC),-128(RC)
 Commands:
 Modifier:
 10 - minutess = cp5.addButton
 20 - ImageButton button
 30 - CheckBox P4
 40 - Numberbox inputPULI
 70 - Numberbox inputNapryzenieKV
 60 - Textlabel Vin
 50 - RadioButton CheckBoxuvum
 */

发送数据:

public void toapply()
{
}

环形:

void draw() {
  background(50);
  button.draw();
}
{
  if (strata) {
    readInc();
    //readIncome[]
    Displaydata();
  }
}
4

1 回答 1

1

问题是default:

开关是这样运行的:它找到与键对应的 case 并从这一点开始执行代码,直到遇到break;. 这个default:案例是一个包罗万象的:它会喜欢一个永远为真的条件。

为了说明,我喜欢这个例子:当用户输入一个数字时,它会在控制台上打印从那个点到 12 月的所有月份的名称。

int month = 5;

switch (month) {
    case 1:  println("January");
    case 2:  println("February");
    case 3:  println("March");
    case 4:  println("April");
    case 5:  println("May");
    case 6:  println("June");
    case 7:  println("July");
    case 8:  println("August");
    case 9:  println("September");
    case 10: println("October");
    case 11: println("November");
    case 12: println("December");
             break;
    default: println("Error: no valid month selected.");
             break;
}

注意default最后的情况。那是因为因为它总是“真”并且因为开关只会选择一个案例然后运行直到一个break语句,如果我把它放在其他任何地方,它总是会在读取时被选中。通过像这样修改示例来查看:

int month = 5;

switch (month) {
    default: println("Whatever, I'm not printing month anymore!");
             break; // no case after this point will ever be read
    case 1:  println("January");
    case 2:  println("February");
    case 3:  println("March");
    case 4:  println("April");
    case 5:  println("May");
    case 6:  println("June");
    case 7:  println("July");
    case 8:  println("August");
    case 9:  println("September");
    case 10: println("October");
    case 11: println("November");
    case 12: println("December");
             break;
}

这正是您的程序中发生的事情。在DisplayData()中,一开始就有一个default案例覆盖了所有其他案例:

void Displaydata() {

  switch(readIncome[0]) {
  case 10:
  default: // <--- HERE THIS IS ALWAYS SELECTED
    minutess.setCaptionLabel(readIncome[1]+" Мин.");
    min=readIncome[1];
    break; // <--- EVERYTHING AFTER THIS IS IGNORED

您可以使用正确的语法重新编写,如下所示:

void Displaydata() {
  switch(readIncome[0]) {
  case 10:
    minutess.setCaptionLabel(readIncome[1]+" Мин.");
    min=readIncome[1];
    break;
  case 20:
    button.label(readIncome[1]+" Мин."); //ImageButton
    min=readIncome[1];
    break;
  case 30:
    if(readIncome[1]==1)P4.setText("on");
    if(readIncome[1]==0)P4.setText("off");// CheckBox
    break;
  case 40:
    inputPULI.setText("Bullet - "+readIncome[1] ); //Numberbox int
    break;
  case 70:
    inputNapryzenieKV.setText("Voltage - "+readIncome[1] ); //Numberbox float
    break;
  case 60:
    Vin.setText("Voltage K.V - "+readIncome[1] );
    break;
  case 50:
    if(readIncome[1]==1)CheckBoxuvum.setText("+"); //RadioButton
    if(readIncome[1]==0)CheckBoxuvum.setText("-");
    break;
  default:
    println("DisplayData(): no case selected.");
    break; // technically not necessary, but I like my switches tidy
  }
}

玩得开心!

于 2020-11-08T12:15:01.043 回答