快乐每一天 发表于 2020-7-19 08:16:27

求助:自定义通信协议不懂,求大神帮忙

import processing.serial.*;
Serial port;
public static final char HEADER = 'h';
public static final short LF = 10;
float vol;
float offset = 100;
float scaleVal = 35;
float angleInc = PI/60;
float angle = 0;
float dx = 0;
float under;
float high = 200;
//String[] com;
//String[] data;

void setup()
{
//println(Serial.list());
//com = Serial.list();
port = new Serial(this,"com3",9600);
port.bufferUntil('\n');
size(600,200);
noStroke();
frameRate(60);
}

void draw()
{
scaleVal = map(vol,under,high,0,90);
print(scaleVal);
background(204);
fill(255,0,0);
rect(20,20,20,20);
fill(0);
for (int posx=0;posx<=width;posx++)
{
    float posy = offset+sin(angle-dx)*scaleVal;
    rect(posx,posy,1,1);
    angle+=angleInc;
}
dx+=0.05;
angle = 0;
}

void serialEvent(Serial port)
{
String message = port.readStringUntil(LF);
if (message != null)
{
    //print(message);
    String [] data = message.split(",");
    //println(data.charAt(0));
    if (data.charAt(0) == HEADER)
    {
      print("hello");
      if(data.length >1)
      {
      vol = Integer.parseInt(data);
      print(vol);
      if (high < vol)
      {
          vol = high;
      }
      if (under > vol)
      {
          vol = under;
      }
      }
    }
}
}

void mouseClicked()
{
if (mouseX<40&&mouseX>20)
{
    if (mouseY<40&&mouseY>20)
    {
      under = vol;
    }
}
}

碰到的问题:h是自定义的起始位置,那为什么接收到的数据会有h呢?
页: [1]
查看完整版本: 求助:自定义通信协议不懂,求大神帮忙