你好我的论文项目有这个处理草图,用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); }