Processing向串口发送数据
复制代码
1 import processing.serial.*;
2 Serial port;
3 String message;
4 void setup() {
5 message = “c”;
6 port = new Serial(this, “COM3”, 9600);
7 }
8
9 void draw() {
10 port.write(message);
11 }
复制代码
arduino连接的是com3,执行程序后Arduino板载的RX灯一直亮起,说明有数据在不停地发送过去。关闭程序后,RX灯熄灭。
2.3Arduino串口编程
通过头文件HardwareSerial.h中定义一个HardwareSerial类的对象serial,然后直接使用类的成员函数来实现的。
Serial.begin() 用于设置串口的比特率,除以8可得到每秒传输的字节数
Serial.end() 停止串口通信
Serial.available() 用来判断串口是否收到数据,该函数返回值为int型,不带参数
示例:从串口输出“The char I havr received:" 字符
复制代码
1 int c = 0;
2
3 void setup()
4 {
5 Serial.begin(9600);
6 }
7
8 void loop()
9 {
10 if (Serial.available()){
11 c = Serial.read();
12 Serial.print(“The char I have received:”);
13 Serial.println(c, DEC);
14 }
15 delay(200);
16 }
复制代码
打开Arduino界面的串口监视器,输入任意字符,单片机收到会返回该字符的ASCII码。一次输入多个字符,会返回多个ASCII码。
2.4 Process与Arduino通信编程
实例一:在Processing界面上画一个矩形,当用鼠标单击矩形内的时候,Arduino板载的LED灯点亮,单击矩形外的时候,Arduino板载的LED熄灭。
processing代码
复制代码
1 import processing.serial.*;
2 Serial port;
3
4 void setup() {
5 port = new Serial(this, “COM3”, 9600);
6 size(300, 300);
7 }
8
9 void draw() {
10 rect(100, 100, 50, 50);
11 }
12
13 void mouseClicked() {
14 if ((mouseX >= 100) & (mouseX <= 150) & (mouseY >= 100) & (mouseY <= 150)) {
15 println(“LED turn ON!”);
16 port.write(“a”);
17 } else {
18 println(“LED turn OFF!”);
19 port.write(“b”);
20 }
21 }
复制代码
Arduino代码
复制代码
int c = 0;
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
}
void loop() {
if(Serial.available()) {
c = Serial.read();
if (c == 97)
digitalWrite(13, HIGH);
else
digitalWrite(13, LOW);
}
}
复制代码
实例二:将一个开关连接到Arduino上的+引脚,信号线连接在D8,长按开关,Processing上的圆形变成红色;松开开关,Processing上的圆变成绿色。初始为绿色。
processing代码:
复制代码
1 import processing.serial.*;
2 Serial myPort;
3
4 void setup() {
5 size(300, 300);
6 fill(0, 255, 0);
7 ellipse(100, 100, 100, 100);
8 myPort = new Serial(this, “COM3”, 9600);
9 }
10
11 void draw() {
12 while (myPort.available() > 0) {
13 char inByte = myPort.readChar();
14 println(inByte);
15 switch (inByte) {
16 case ‘a’: fill(0, 255, 0);
17 ellipse(100, 100, 100, 100);
18 break;
19 case ‘b’: fill(255, 0, 0);
20 ellipse(100, 100, 100, 100);
21 break;
22 default: break;
23 }
24 }
25 }
复制代码
Arduino代码:
复制代码
1 boolean button;
2
3 void setup() {
4 button = false;
5 pinMode (8, INPUT);
6 Serial.begin(9600);
7 }
8
9 void loop() {
10 button = digitalRead(8);
11 if (button) {
12 Serial.write(“a”);
13 } else {
14 Serial.write(“b”);
15 }
16 }
17
复制代码
实例三:在Processing里画一个矩形,当在矩形区域单击时,向Arduino发送字母a,Arduino收到后点亮板载的LED灯,同时向Processing发送”The light trun ON"; Processing收到后显示在屏幕的左上角。当单击矩形框外时,Processing向Arduino发送字母b,Arduino板载的LED灯熄灭,与此同时Arduino向Processing发送“The light turn OFF", Processing收到这串字符后显示在屏幕的左上角。
Processing代码:
复制代码
1 import processing.serial.*;
2 Serial myPort;
3 int lf = 10;
4 String myString = null;
5
6 void setup() {
7 size (300, 300);
8 background (125);
9 myPort = new Serial (this, “COM3”, 9600);
10 myPort.clear();
11 }
12
13 void draw() {
14 rect(100, 100, 50, 50);
15 while (myPort.available() > 0) {
16 myString = myPort.readStringUntil(lf);
17 if (myString != null) {
18 background(125);
19 text (myString, 10, 30);
20 }
21 }
22 }
23
24 void mouseClicked() {
25 if ((mouseX >= 100) & (mouseX <= 150) & (mouseY >= 100) & (mouseY <= 150)) {
26 myPort.write(“a”);
27 } else {
28 myPort.write(“b”);
29 }
30 }
复制代码
Arduino代码
复制代码
int c = 0;
void setup() {
Serial.begin(9600);
pinMode (13, OUTPUT);
digitalWrite(13, LOW);
}
void loop() {
if (Serial.available()) {
c = Serial.read();
if (c == 97) {
digitalWrite(13, HIGH);
Serial.println(“The light turn ON”);
} else {
digitalWrite(13, LOW);
Serial.println(“The light turn OFF”);
}
}
}
复制代码