java—如何在接收数据时以图形方式更改数字?

w80xi6nr  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(434)

下午好,我正在使用stm32蓝丸usb接口,开发环境“iar”。问题是,当我通过应用程序连接时,箭头后面的数字“3”并没有变为数字“50”,而是相反的显示在下面!见图1.2。我是按照协议工作的。
你如何做到这一点?我正在使用controlp5库和cp5.addnumberbox。


“iar”中的数据。

int Bullet = 50;  // variable for result
sprintf((char *)str, "(,%d,-Ђ",Bullet);  // sending a command with data to stm32.

在displaydata选项卡的代码中,行“case 40”是符号(该命令也可以使用虚拟com端口通过终端发送)。命令:(,50,!图3。

我得把整个小项目布置好让你看看。
通信接口1:

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

Serial serialPort;
final int BAUD_RATE = 9600;

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

public void setup() {
  background(50);
  size(700, 420, P3D);
  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("Пуск");
    serialPort.dispose();
    Vin.setText("Voltage K.V - ");
    inputPULI.setLabel("Bullet");
  } 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 controlEvent(ControlEvent event){
 println(event.getController().getName(),"changed value to",event.getValue(),"inputPULI = ",PUL,"inputNapryzenieKV = ",NapryzenieKV,"CheckBoxuvum= ",
 UV/UM,"P4 = ",std2,);
 }*/

显示数据:

void Displaydata() {

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

图形用户界面:

int PUL;
float NapryzenieKV;
boolean strata=false;

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);

  PImage[] imgs = {loadImage("button101.png"), loadImage("button102.png"), loadImage("button103.png")};
  Send = cp5.addButton("toapply")
    //.setCaptionLabel("Apply")
    //.setPosition(510, 370)
    //.setSize(150, 30);
    .setPosition(590, 330)
    .setImages(imgs)
    .updateSize();
  //.lock()

  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(220, 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("Bullet");
  labelinputPULI.align(ControlP5.LEFT_OUTSIDE, CENTER);
  labelinputPULI.getStyle().setPaddingLeft(-25);

  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(555, 230, w, h, 
      new PImage[]{
      loadImage("0.png"), // off
      loadImage("1.png"), // 10
      loadImage("2.png"), // 20
      loadImage("3.png"), // 30
      loadImage("4.png"), // 40
      loadImage("5.png"), // 50
      loadImage("6.png"), // 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);
  }
}
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 = 0x21; // -128 separating byte в java
int[] writeOutgioing = new int[lengthmas];
String incomingData= null;
String outgoingData=null;
String[] testRead = new String[lengthmas];
int x;

void readInc() {
  while ( serialPort.available() > 0) 
  {
    incomingData = serialPort.readStringUntil(RC);
    testRead = split (incomingData, ',' );
    if (testRead != null)
    {
      x = (int) testRead[0].charAt(0);
      Displaydata();
    }
  }
}

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);
}

发送数据:

public void toapply()
{
}

回路:

void draw() 
{

  background(50);
  button.draw();

  if (strata) 
  {
    readInc();
  }
}

代码:

inputPULI = cp5.addNumberbox("PUL")
    .setLabel("Bullet")
    .setPosition(220, 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("Bullet");
  labelinputPULI.align(ControlP5.LEFT_OUTSIDE, CENTER);
  labelinputPULI.getStyle().setPaddingLeft(-25);
55ooxyrt

55ooxyrt1#

我自己想出来的,我不得不写setlabel->setvaluelabel

相关问题