Serial port 处理:如何写入串行端口? boolean squareVisible=true; int x=50; int y=50; int w=100; int h=100; 输入处理。串行。*; 串口; int-val; 无效设置(){ 大小(200200); 仰泳(); 填充(255,0,0); rect(x,y,w,h); 端口=新的串行端口(此端口为9600); } 作废提款(){ 背景(255); 如果(可见){ 填充(40、80、90); }否则{ 填充(255,0,0); } rect(x,y,w,h);//画一个正方形 } void mousePressed(){ 如果((mouseX>x)和&(mouseXy)和(鼠标x)和&(mouseXy)和(鼠标x)和&(mouseXy)和(鼠标x)和&(mouseXy)和(鼠标x)和&(mouseX

Serial port 处理:如何写入串行端口? boolean squareVisible=true; int x=50; int y=50; int w=100; int h=100; 输入处理。串行。*; 串口; int-val; 无效设置(){ 大小(200200); 仰泳(); 填充(255,0,0); rect(x,y,w,h); 端口=新的串行端口(此端口为9600); } 作废提款(){ 背景(255); 如果(可见){ 填充(40、80、90); }否则{ 填充(255,0,0); } rect(x,y,w,h);//画一个正方形 } void mousePressed(){ 如果((mouseX>x)和&(mouseXy)和(鼠标x)和&(mouseXy)和(鼠标x)和&(mouseXy)和(鼠标x)和&(mouseXy)和(鼠标x)和&(mouseX,serial-port,processing,Serial Port,Processing,我在处理中完全是个傻瓜。这是一个简单的切换开关,我试图在切换完成后写入串口。我试图将其与arduino集成,但我似乎无法让它读取来自串行端口的任何信息。我做的每个交换机都有不同的方式写入串行端口吗?我有什么问题吗?提前感谢…我发现了一个问题:port=new Serial(这是9600)应该是port=new Serial(这是Serial.list()[0],9600)。您缺少构造函数中的一个(重要)参数。始终检查处理控制台中的错误(请在代码下方显示),尤其是当代码不起作用时:) 我将从Pro

我在处理中完全是个傻瓜。这是一个简单的切换开关,我试图在切换完成后写入串口。我试图将其与arduino集成,但我似乎无法让它读取来自串行端口的任何信息。我做的每个交换机都有不同的方式写入串行端口吗?我有什么问题吗?提前感谢…

我发现了一个问题:
port=new Serial(这是9600)
应该是
port=new Serial(这是Serial.list()[0],9600)。您缺少构造函数中的一个(重要)参数。始终检查处理控制台中的错误(请在代码下方显示),尤其是当代码不起作用时:)

我将从Processing附带的SimpleWrite示例开始,让您首先了解Processing/Arduino之间的通信是如何工作的,然后继续使用从项目中获得的知识

基本设置如下: 在处理过程中,在setup()中初始化串行实例,在draw中,使用串行的write()方法发送值。 在Arduino中,在setup()中初始化Serial(Serial.begin(yourBaudRate)),在loop()中检查是否有可用的数据和值。 在处理和Arduino中使用相同的波特率是非常重要的,否则您将无法识别传输的大部分数据

此外,您不需要发送字符串,还可以发送整数、字节等。 如果要显示这些,请不要忘记添加类型作为Serial.print()或Serial.println()的第二个参数(例如Serial.println(myByte,BYTE);或Serial.println(myInt,DEC))

我已经在Arduino中设置了一个非常基本的草图,当你的方块被切换时,LED会闪烁一次 别做别的事。此外,输入数据打印在串行监视器中: int=0//这将存储串行数据的值

void setup(){
  pinMode(13,OUTPUT);//add an LED on PIN 13 for kicks
  Serial.begin(9600);//init Serial library (make sure Processing is sending data at the same baud rate)
}
void loop(){
  if(Serial.available() > 0){//look for Serial data 
    incoming = Serial.read();//read and store teh value
    Serial.print(incoming,DEC);//print it to the Serial monitor, change DEC to the type of variable you're using
    if(incoming == 1){//if it's a 1 blink once
      digitalWrite(13,HIGH);
      delay(500);
      digitalWrite(13,LOW);    
      delay(500);
    } 
  }
}
我已经调整了一下你正在处理的草图:

boolean squareVisible = true;
int x = 50;
int y = 50;
int w = 100;
int h = 100;
import processing.serial.*;
Serial port;
int val;

void setup() {
    size(200, 200);
    noStroke();
    fill(255, 0, 0);
    rect(x, y, w, h);

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

void draw() {
    background(255);
    if (squareVisible) {
        fill(40, 80, 90);
    } else {
        fill(255, 0, 0);
    }
    rect(x, y, w, h); // Draw a square
}


void mousePressed() {
    if (((mouseX > x) && (mouseX < x + w) &&
            (mouseY > y) && (mouseY < y + h))) {
        // if mouse clicked inside square
        squareVisible = !squareVisible;  // toggle square visibility
        if(squareVisible) port.write(0);
        else              port.write(1);
    }
}
/*
void mouseMoved() {
    if (((mouseX > x) && (mouseX < x + w) &&
            (mouseY > y) && (mouseY < y + h))) {
        port.write(2);                
    }
}*/
boolean squareVisible=true;
int x=50;
int y=50;
int w=100;
int h=100;
输入处理。串行。*;
串口;
int-val;
无效设置(){
大小(200200);
仰泳();
填充(255,0,0);
rect(x,y,w,h);
字符串portName=Serial.list()[0];
端口=新的串行端口(此端口名为9600);
}
作废提款(){
背景(255);
如果(可见){
填充(40、80、90);
}否则{
填充(255,0,0);
}
rect(x,y,w,h);//画一个正方形
}
void mousePressed(){
如果((mouseX>x)和&(mouseXy)和(鼠标x)和&(mouseXy)和(鼠标

祝你好运

我发现了一个问题:
port=new Serial(这是9600)
应该是
port=new Serial(这是Serial.list()[0],9600)。您缺少构造函数中的一个(重要)参数。始终检查处理控制台中的错误(请在代码下方显示),尤其是当代码不起作用时:)

我将从Processing附带的SimpleWrite示例开始,让您首先了解Processing/Arduino之间的通信是如何工作的,然后继续使用从项目中获得的知识

基本设置如下: 在处理过程中,在setup()中初始化串行实例,在draw中,使用串行的write()方法发送值。 在Arduino中,在setup()中初始化Serial(Serial.begin(yourBaudRate)),在loop()中检查是否有可用的数据和值。 在处理和Arduino中使用相同的波特率是非常重要的,否则您将无法识别传输的大部分数据

此外,您不需要发送字符串,还可以发送整数、字节等。 如果要显示这些,请不要忘记添加类型作为Serial.print()或Serial.println()的第二个参数(例如Serial.println(myByte,BYTE);或Serial.println(myInt,DEC))

我已经在Arduino中设置了一个非常基本的草图,当你的方块被切换时,LED会闪烁一次 别做别的事。此外,输入数据打印在串行监视器中: int=0//这将存储串行数据的值

void setup(){
  pinMode(13,OUTPUT);//add an LED on PIN 13 for kicks
  Serial.begin(9600);//init Serial library (make sure Processing is sending data at the same baud rate)
}
void loop(){
  if(Serial.available() > 0){//look for Serial data 
    incoming = Serial.read();//read and store teh value
    Serial.print(incoming,DEC);//print it to the Serial monitor, change DEC to the type of variable you're using
    if(incoming == 1){//if it's a 1 blink once
      digitalWrite(13,HIGH);
      delay(500);
      digitalWrite(13,LOW);    
      delay(500);
    } 
  }
}
我已经调整了一下你正在处理的草图:

boolean squareVisible = true;
int x = 50;
int y = 50;
int w = 100;
int h = 100;
import processing.serial.*;
Serial port;
int val;

void setup() {
    size(200, 200);
    noStroke();
    fill(255, 0, 0);
    rect(x, y, w, h);

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

void draw() {
    background(255);
    if (squareVisible) {
        fill(40, 80, 90);
    } else {
        fill(255, 0, 0);
    }
    rect(x, y, w, h); // Draw a square
}


void mousePressed() {
    if (((mouseX > x) && (mouseX < x + w) &&
            (mouseY > y) && (mouseY < y + h))) {
        // if mouse clicked inside square
        squareVisible = !squareVisible;  // toggle square visibility
        if(squareVisible) port.write(0);
        else              port.write(1);
    }
}
/*
void mouseMoved() {
    if (((mouseX > x) && (mouseX < x + w) &&
            (mouseY > y) && (mouseY < y + h))) {
        port.write(2);                
    }
}*/
boolean squareVisible=true;
int x=50;
int y=50;
int w=100;
int h=100;
输入处理。串行。*;
串口;
int-val;
无效设置(){
大小(200200);
仰泳();
填充(255,0,0);
rect(x,y,w,h);
字符串portName=Serial.list()[0];
端口=新的串行端口(此端口名为9600);
}
作废提款(){
背景(255);
如果(可见){
填充(40、80、90);
}否则{
填充(255,0,0);
}
rect(x,y,w,h);//画一个正方形
}
void mousePressed(){
如果((mouseX>x)和&(mouseX