您好,我的论文项目有这个处理草图,可以使用 Emotiv Epoc 向左或向右移动伺服(实际上我想发送击键 r 或 l)。
第一个目标是自动识别端口。我收到一个打开的串行端口繁忙错误。
打开串行端口“comx”时出错。(港口繁忙)
我认为串行端口在循环内是打开的,所以当我尝试打开它时(ser_port = new Serial(this,detected_port,9600);
第 73-74 行)它让我出错。
有什么办法可以关闭再打开吗?(实际上我想要一个检查哪个端口有 Arduino,然后连接到该端口的草图)任何帮助将不胜感激
import processing.serial.*; Serial ser_port; // for
serial port PFont fnt; // for font int
num_ports; boolean device_detected = false; String[] port_list;
String detected_port = ""; void setup() {
size(400, 200); // size of application window
background(0); // black background
fnt = createFont("Arial", 16, true); // font displayed in window
println(Serial.list());
// get the number of detected serial ports
num_ports = Serial.list().length;
// save the current list of serial ports
port_list = new String[num_ports];
for (int i = 0; i < num_ports; i++) {
port_list[i] = Serial.list()[i];
} } void draw() {
background(0);
// display instructions to user
textFont(fnt, 14);
text("1. Arduino or serial device must be unplugged.", 20, 30);
text(" (unplug device and restart this application if not)", 20, 50);
text("2. Plug the Arduino or serial device into a USB port.", 20, 80);
// see if Arduino or serial device was plugged in
if ((Serial.list().length > num_ports) && !device_detected) {
device_detected = true;
// determine which port the device was plugge into
boolean str_match = false;
if (num_ports == 0) {
detected_port = Serial.list()[0];
}
else {
// go through the current port list
for (int i = 0; i < Serial.list().length; i++) {
// go through the saved port list
for (int j = 0; j < num_ports; j++) {
if (Serial.list()[i].equals(port_list[j])) {
break;
}
if (j == (num_ports - 1)) {
str_match = true;
detected_port = Serial.list()[i];
}
}
}
}
}
// calculate and display serial port name
if (device_detected) {
text("Device detected:", 20, 110);
textFont(fnt, 18);
text(detected_port, 20, 150);
textFont(fnt, 14);
text("3. Now think Right or Left in this Window",20,190);
} }
char valToWrite = 'r' & 'l'; //a value to send as a single byte
void keyPressed(){
// This is the line I'm getting the error!
ser_port = new Serial(this,detected_port,9600);
ser_port.bufferUntil(10);
valToWrite = key;
ser_port.write(valToWrite); }
void keyReleased(){
valToWrite = 'r' & 'l';
ser_port.write(valToWrite); }