You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Processing与Arduino串口双向通信故障排查及代码修正请求

Processing与Arduino串口通信问题排查与修正

核心问题定位

  • 串口数据包格式未统一:两端没有约定明确的分隔/结束标志,导致Arduino无法正确解析Processing发送的整数
  • serialEvent触发异常:多因串口缓冲区数据不完整,或未设置正确的触发结束符
  • 串口配置不匹配:波特率、数据位等参数两端不一致

修正方案与代码示例

1. 统一通信协议

约定数据包格式:用逗号分隔5个整数,末尾加换行符\n作为结束标志(如10,20,30,40,50\n),确保两端能按行完整读取并解析数据。

2. Processing代码修正

import processing.serial.*;
import controlP5.*;

ControlP5 cp5;
Serial myPort;
Textfield[] inputs = new Textfield[5];
Textarea outputArea;
Button submitBtn;

void setup() {
  size(600, 400);
  cp5 = new ControlP5(this);
  
  // 创建5个整数输入框
  for (int i = 0; i < 5; i++) {
    inputs[i] = cp5.addTextfield("input" + i)
                   .setPosition(50 + i*100, 50)
                   .setSize(80, 30)
                   .setInputFilter(ControlP5.INTEGER)
                   .setValue("0");
  }
  
  // 提交按钮
  submitBtn = cp5.addButton("submit")
                 .setPosition(250, 100)
                 .setSize(100, 30)
                 .setLabel("提交");
  
  // 数据输出区域
  outputArea = cp5.addTextarea("output")
                  .setPosition(50, 150)
                  .setSize(500, 200)
                  .setScrollbarVisible(true);
  
  // 初始化串口:替换为你的Arduino端口(可通过Serial.list()查看)
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
  myPort.bufferUntil('\n'); // 设置换行符为缓冲区触发标志
}

void draw() {
  background(240);
}

// 提交按钮触发逻辑
void submit() {
  int[] values = new int[5];
  for (int i = 0; i < 5; i++) {
    values[i] = int(inputs[i].getText());
  }
  
  // 按协议格式发送数据
  String sendStr = values[0] + "," + values[1] + "," + values[2] + "," + values[3] + "," + values[4] + "\n";
  myPort.write(sendStr);
  outputArea.setText("已发送:" + sendStr);
}

// 串口数据接收逻辑
void serialEvent(Serial myPort) {
  String inString = myPort.readStringUntil('\n');
  if (inString != null) {
    inString = trim(inString);
    outputArea.setText("Arduino返回:" + inString);
  }
}

3. Arduino代码修正

int inputVals[5];
int outputVals[5] = {0, 0, 0, 0, 0};

void setup() {
  Serial.begin(9600);
  // 仅Leonardo/Micro等USB串口设备需要此句,Uno/Nano请注释掉
  // while (!Serial);
}

void loop() {
  if (Serial.available() > 0) {
    String inputStr = Serial.readStringUntil('\n');
    inputStr.trim();
    
    // 拆分字符串为整数数组
    int index = 0;
    char* token = strtok((char*)inputStr.c_str(), ",");
    while (token != NULL && index < 5) {
      inputVals[index] = atoi(token);
      token = strtok(NULL, ",");
      index++;
    }
    
    // 示例处理逻辑:返回输入值的2倍
    for (int i = 0; i < 5; i++) {
      outputVals[i] = inputVals[i] * 2;
    }
    
    // 按协议返回数据
    String outputStr = String(outputVals[0]) + "," + String(outputVals[1]) + "," + String(outputVals[2]) + "," + String(outputVals[3]) + "," + String(outputVals[4]) + "\n";
    Serial.print(outputStr);
  }
}

关键注意事项

  • 确保两端波特率完全一致(示例中为9600)
  • Processing中选择正确的串口端口,可通过Serial.list()打印所有可用端口确认
  • Arduino端若使用Uno/Nano,需注释掉while (!Serial);语句
  • 输入框限制为整数,避免发送非数字字符导致解析失败

内容的提问来源于stack exchange,提问作者Gerzayr Alapizco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 09:13:13