【问题标题】:Processing not communicating with Arduino uno and console is spouting garbage处理不与 Arduino uno 和控制台通信正在喷垃圾
【发布时间】:2018-07-24 07:49:29
【问题描述】:

我正在制作一个小型 G4P GUI 作为 arduino 通信的测试。

Control Panel

所以它只是基本的,当按下 X 按钮时将 Y 发送到控制台,并且 arduino 对 Y 执行任何操作,但处理控制台只是充满了垃圾。

the trash

Arduino wiring

这是arduino的代码:

    int rLED = 7;
    int gLED = 8;
    int R = 11;
    int G = 12;
    int B = 13;

    void setup() {
    pinMode(rLED, OUTPUT);
    pinMode(gLED, OUTPUT);
    Serial.begin(9600);
    }

    void loop() {

    if (Serial.available ( ) > 0) {

      char state = Serial.read ( );
    if(state == '1')
    { 
    digitalWrite (rLED, HIGH); 
    }  
    if(state == '2')
    { 
    digitalWrite (rLED, LOW); 
    }  
    if(state == '3')
    { 
    digitalWrite (gLED, HIGH); 
    }  
    if(state == '4')
    { 
    digitalWrite (gLED, LOW); 
    }  
    }
    }

这里是处理代码:

    import processing.serial.*;

    // Need G4P library
    import g4p_controls.*;
      Serial myPort;

    public void setup(){
      size(480, 320, JAVA2D);
      createGUI();
      customGUI();
      // Place your setup code here
      myPort = new Serial(this, "COM4", 9600);
    }

    public void draw(){
      background(230);

    }
    public void handleButtonEvents(GButton button, GEvent event) {
      if (button == rLED && event == GEvent.CLICKED) {
          myPort.write ( '1' ) ;
          delay(100);
          myPort.write ( '2' ) ;
      }
      if (button == button1 && event == GEvent.CLICKED) {
          myPort.write ( '3' ) ;
          delay(100);
          myPort.write ( '4' ) ;
      }
    }

    // Use this method to add additional statements
    // to customise the GUI controls
    public void customGUI(){

}

这是 GUI 的代码(处理):

    public void Red_Blink(GButton source, GEvent event) { //_CODE_:rLED:951221:
      println("rLED - GButton >> GEvent." + event + " @ " + millis());
    } //_CODE_:rLED:951221:

    public void Green_Blink(GButton source, GEvent event) { //_CODE_:button1:443069:
      println("button1 - GButton >> GEvent." + event + " @ " + millis());
    } //_CODE_:button1:443069:

    public void Red_int(GCustomSlider source, GEvent event) { //_CODE_:Red:702179:
      println("custom_slider1 - GCustomSlider >> GEvent." + event + " @ " + millis());
    } //_CODE_:Red:702179:

    public void Green_int(GCustomSlider source, GEvent event) { //_CODE_:Green:645891:
      println("custom_slider2 - GCustomSlider >> GEvent." + event + " @ " + millis());
    } //_CODE_:Green:645891:

    public void Blue_int(GCustomSlider source, GEvent event) { //_CODE_:Blue:372420:
      println("Blue - GCustomSlider >> GEvent." + event + " @ " + millis());
    } //_CODE_:Blue:372420:



    // Create all the GUI controls. 
    // autogenerated do not edit
    public void createGUI(){
      G4P.messagesEnabled(false);
      G4P.setGlobalColorScheme(GCScheme.GOLD_SCHEME);
      G4P.setCursor(ARROW);
      surface.setTitle("Controll panel ");
      tittle = new GLabel(this, 180, 10, 150, 30);
      tittle.setTextAlign(GAlign.CENTER, GAlign.MIDDLE);
      tittle.setText("Controll arduino");
      tittle.setTextBold();
      tittle.setLocalColorScheme(GCScheme.BLUE_SCHEME);
      tittle.setOpaque(false);
      rLED = new GButton(this, 60, 80, 80, 30);
      rLED.setText("blink red LED");
      rLED.setLocalColorScheme(GCScheme.RED_SCHEME);
      rLED.addEventHandler(this, "Red_Blink");
      button1 = new GButton(this, 150, 80, 80, 30);
      button1.setText("blink greed LED");
      button1.setLocalColorScheme(GCScheme.GREEN_SCHEME);
      button1.addEventHandler(this, "Green_Blink");
      Red = new GCustomSlider(this, 20, 140, 440, 40, "grey_blue");
      Red.setShowValue(true);
      Red.setLimits(150, 0, 255);
      Red.setNumberFormat(G4P.INTEGER, 0);
      Red.setLocalColorScheme(GCScheme.RED_SCHEME);
      Red.setOpaque(false);
      Red.addEventHandler(this, "Red_int");
      Green = new GCustomSlider(this, 18, 200, 440, 40, "grey_blue");
      Green.setShowValue(true);
      Green.setLimits(50, 0, 255);
      Green.setNumberFormat(G4P.INTEGER, 0);
      Green.setLocalColorScheme(GCScheme.GREEN_SCHEME);
      Green.setOpaque(false);
      Green.addEventHandler(this, "Green_int");
      label1 = new GLabel(this, 200, 130, 80, 20);
      label1.setTextAlign(GAlign.CENTER, GAlign.MIDDLE);
      label1.setText("Red");
      label1.setLocalColorScheme(GCScheme.BLUE_SCHEME);
      label1.setOpaque(false);
      label2 = new GLabel(this, 200, 190, 80, 20);
      label2.setTextAlign(GAlign.CENTER, GAlign.MIDDLE);
      label2.setText("Green");
      label2.setLocalColorScheme(GCScheme.BLUE_SCHEME);
      label2.setOpaque(false);
      Blue = new GCustomSlider(this, 20, 260, 440, 40, "grey_blue");
      Blue.setShowValue(true);
      Blue.setLimits(150, 0, 255);
      Blue.setNumberFormat(G4P.INTEGER, 0);
      Blue.setLocalColorScheme(GCScheme.BLUE_SCHEME);
      Blue.setOpaque(false);
      Blue.addEventHandler(this, "Blue_int");
      label3 = new GLabel(this, 200, 250, 80, 20);
      label3.setTextAlign(GAlign.CENTER, GAlign.MIDDLE);
      label3.setText("Blue");
      label3.setLocalColorScheme(GCScheme.BLUE_SCHEME);
      label3.setOpaque(false);
    }

    // Variable declarations 
    // autogenerated do not edit
    GLabel tittle; 
    GButton rLED; 
    GButton button1; 
    GCustomSlider Red; 
    GCustomSlider Green; 
    GLabel label1; 
    GLabel label2; 
    GCustomSlider Blue; 
    GLabel label3; 

【问题讨论】:

    标签: process arduino console processing arduino-uno


    【解决方案1】:

    也许我来晚了,但问题似乎出在 setup() 函数中的处理代码中。 试试这个

        String portName = Serial.list()[0];
        myPort = new Serial(this, portName, 9600);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      相关资源
      最近更新 更多