【问题标题】:Serial Ports /dev/tty.* are not working when /dev/cu.* ports are accessed firstly当首先访问 /dev/cu.* 端口时,串行端口 /dev/tty.* 不起作用
【发布时间】:2015-11-10 11:35:31
【问题描述】:
我正在尝试创建一个与 Arduino 连接的处理应用程序。
由于我希望自动建立两者之间的连接,这意味着我没有指定端口的名称,但我使用 Serial.list() 来获取可用端口的名称然后使用 for 循环,我将检查哪个正在打印正确的字符串。
问题是,当我首先访问/dev/cu.* 时,所有/dev/tty.* 端口都处于忙碌状态,反之亦然。这很奇怪,我不希望这种情况发生。
【问题讨论】:
标签:
macos
arduino
serial-port
processing
【解决方案1】:
您应该能够使用一个 (/dev/tty.*) 或另一个 (/dev/cu.*),但不能同时使用两个,因为它们可能以不同的方式指向同一个资源。
我建议列出端口,检查端口前缀(再说一遍/dev/tty.*,但不是/dev/cu.*),初始化串行端口,然后退出遍历列出的串行端口的循环:
import processing.serial.*;
Serial arduino;
final int BAUD_RATE = 9600;
void setup(){
String[] ports = Serial.list();
for(int i = 0 ; i < ports.length; i++){//go through each port
if(ports[i].contains("tty.usbmodem")){//find one that looks like an Arduino on OSX
try{
arduino = new Serial(this,ports[i],BAUD_RATE);//initialize the connection
i = ports.length;//exit the loop, break; should also work
println("Arduino connection succesfully initialized");
}catch(Exception e){
System.err.println("Error opening Serial port!\nPlease check USB connection and ensure the port is not already open in another application.");
e.printStackTrace();
}
}
}
if(arduino == null) System.err.println("Serial connection to Arduino failed!");
}