Skip to content

Instantly share code, notes, and snippets.

@shinnn
Last active December 10, 2015 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinnn/4358353 to your computer and use it in GitHub Desktop.
Save shinnn/4358353 to your computer and use it in GitHub Desktop.
[for Mac OS X / Linux] unixUsbSerial() and unixUsbSerials(), functions to open USB serial ports easily in Processing
// Return single USB port's name
String unixUsbSerial(String device){
return unixUsbSerials(device, 1)[0];
}
// Return multiple USB ports' names
String[] unixUsbSerials(String device, int... num){
/* Parameter "device" can be "cu" or "tty".
* num[0] can be a number of ports required.
*/
String[][] deviceSortArr, USBSortArr;
deviceSortArr = arrayMatchOrNot(Serial.list(), device + ".");
if(deviceSortArr[0].length > 0){
USBSortArr = arrayMatchOrNot(deviceSortArr[0], "usb");
}else{
println("No " + device + ".* devices are available.");
return null; // Error
}
println(
"\n" + (USBSortArr[0].length > 0 ? USBSortArr[0].length : "No")
+ " " + device + ". USB Serial Port"
+ ((USBSortArr[0].length > 1 || USBSortArr[0].length == 0) ? "s are":" is")
+ " available."
);
if(USBSortArr[0].length == 0){
println("Opening from the others temporarily...");
}
int openNum = 1;
if(num.length == 1){
openNum = num[0];
}else if(USBSortArr[0].length > 0){
openNum = USBSortArr[0].length;
}
return openResult(USBSortArr, openNum);
}
/* ==========================================================================
* You don't need to call functions below.
* ==========================================================================
*/
String[][] arrayMatchOrNot(String[] strArr, String searchStr){
String[][] result = {
{}, // Array of the matched elements
{} // Array of the rest
};
for(String elm : strArr){
String[] m = match(elm, ".*" + searchStr + ".*");
if(m != null){
result[0] = append(result[0], m[0]);
}else{
result[1] = append(result[1], elm);
}
}
return result;
}
String[] openResult(String[][] arr, int num){
String[] openPort = {};
// Print the matched elements
for(int i=0; i < arr[0].length; i++){
print("[USB] " + arr[0][i]);
if(i < num){
print("\t<- OPEN");
openPort = append(openPort, arr[0][i]);
}
println();
}
// Print the rest
for(int i=0; i < arr[1].length; i++){
print("[---] " + arr[1][i]);
if(arr[0].length == 0 && i < num){
print("\t<- OPEN\t");
openPort = append(openPort, arr[1][i]);
}
println();
}
return openPort;
}
// Example1 : Open single USB port
import processing.serial.*;
Serial Port;
void setup(){
String portName = unixUsbSerial("cu");
Port = new Serial(this, portName, 9600);
}
void draw(){
if(Port.available() > 0){
String inBuffer = Port.readStringUntil(10); //10 is Linefeed in ASCII
if(inBuffer != null){
print(inBuffer);
}
}
}
// Example2 : Open all USB ports
import processing.serial.*;
Serial[] Port;
void setup(){
String[] portName = unixUsbSerials("cu");
Port = new Serial[portName.length];
for(int i=0; i < portName.length; i++){
Port[i] = new Serial(this, portName[i], 9600);
}
}
void draw(){
for(int i=0; i < Port.length;){
if(Port[i].available() > 0){
String inBuffer = Port[i].readStringUntil(10); //10 is Linefeed in ASCII
if(inBuffer != null){
print("[Port " + (++i) + "] " + inBuffer);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment