Kód: Vybrat vše
import processing.serial.*;
Serial port;
StringList usb = new StringList();
String OS[] = {"WINDOWS", "LINUX"};
String Chip_id[] = {"PL2303", "CH340G", "CP2102"};
byte Os, ID, Port, pomi;
int delka, delka_pom;
boolean ready = false;
String chyba, Idvendor, Usb, props;
boolean device_detected = false;
String Text ;
String[] port_list;
String[] Id;
String detected_port = "";
//*************************************
void setup() {
ID =0;
size(500, 60);
fill(0);
text("USB INFO:", 0, 10);
}
//*************************************
void draw() { // telo programu
USB_informace();
delay(500);
}
//*************************************
void mousePressed() { // UKONCENI PORGRAMU
if (mouseButton == RIGHT) {
exit();
}
}
//**************************************
void USB_informace() {
usb = new StringList();
usb.append(Serial.list());
background(200);
text("USB INFO:", 0, 10);
if (usb.hasValue("/dev/ttyS0")) {
Os = 1;
} else if (usb.hasValue("/COM")) {
Os = 0;
}
//***************************************
if (Os == 1) { // linux USB
delka = usb.size();
if (usb.size() > 1) {
for (byte i=0; i < usb.size(); i++) {
//Map<String, String>
props = (Serial.getProperties(usb.get(i))).toString();
Idvendor = trim(props);
Id = match(Idvendor, "idVendor=1a86");
if (Id != null) {
ID = 1;
pomi =i;
} else {
Id = match(Idvendor, "idVendor=067b");
if (Id != null) {
ID = 0;
pomi =i;
} else {
Id = match(Idvendor, "idVendor=10c4");
if (Id != null) {
ID = 2;
pomi =i;
} else {
ID = -1;
pomi =0;
}
}
}
}
if (ID != -1) {
Usb = usb.get(pomi);
Text=("OS: "+OS[Os]+" Port: "+Usb+" Chip com: "+ Chip_id[ID]);
text(Text, 0, 21);
} else {
Text=("OS: "+ OS[Os]+ " Nezapojeno do USB.");
text(Text, 0, 21);
}
//PL2303 idProduct=2303 idVendor=067b
//CH340g idProduct=7523 idVendor=1a86
//CP2102 idProduct=ea60 idVendor=10c4
}
}
//***************************************
else if (Os == 0) { // Windows USB
if (delka_pom == 0) {
delka = Serial.list().length;
delka_pom = delka;
} else {
delka = Serial.list().length;
if (delka_pom == delka) {
delka_pom = delka;
device_detected = false;
}
}
port_list = new String[delka];
for (int i = 0; i < delka; i++) {
port_list[i] = Serial.list()[i];
}
// see if Arduino or serial device was plugged in
if ((Serial.list().length > delka_pom) && !device_detected) {
device_detected = true;
// determine which port the device was plugge into
if (delka == 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 (byte j = 0; j < delka; j++) {
if (Serial.list()[i].equals(port_list[j])) {
Port =j;
break;
}
}
}
}
}
if (Port == (delka - 1)) {// vypis COMu
detected_port = Serial.list()[Port];
}
if (!device_detected) {
Text=("OS: "+ OS[Os]+ " Nove nezapojeno do USB.");
text(Text, 0, 21);
} else {
Text=("OS: "+ OS[Os]+ " Port: "+ detected_port);
text(Text, 0, 21);
}
}
}