Vydělávej až 160.000 Kč měsíčně! Akreditované rekvalifikační kurzy s garancí práce od 0 Kč. Více informací.
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.
Avatar
KlikBill
Člen
Avatar
KlikBill:26.10.2017 15:21

Zdravím všechny. Jestli někdo zná PROCESSING programovací nástroj tak jsem sestavil prográmek na idetifikaci USB pod WINDOWS tak pod LINUXEM.
Tu je :

import processing.serial.*;
Serial port;
StringList usb = new StringList();
String OS[] = {"WINDOWS", "LINUX"};
String Chip_id[] = {"PL2303", "CH340G", "CP2102"};
byte Os, ID, Port;
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 (int 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) {
          props = (Serial.getProperties(usb.get(i))).toString();
          Idvendor = trim(props);
          Id = match(Idvendor, "idVendor=067b");
          ID = 0;
        }
        if (Id == null) {
          props = (Serial.getProperties(usb.get(i))).toString();
          Idvendor = trim(props);
          Id = match(Idvendor, "idVendor=10c4");
          ID = 2;
        } else {
          ID = 1;
        }
        if (Id != null) {
          Usb = usb.get(i);
          Text=("OS: "+OS[Os]+" Port: "+Usb+" Chip com: "+ Chip_id[ID]);
          text(Text, 0, 21);
          break;
        } 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);
    }
  }
}

Tu je mustr na vložení do programu:

import processing.serial.*;
Serial port;
//*************** promene pro kounikaci skrz USB port*************
int rychlost = 57600;
StringList usb = new StringList();
String OS[] = {"WINDOWS", "LINUX"};
String Chip_id[] = {"PL2303", "CH340G", "CP2102"};
byte Os, ID, Port;
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 = "";
//****************** ostatni *********************

/*****************************************/
void setup() {
  SetComPort();
  if (!ready) {
    exit(); // ukonci program kdyz neni USB
  }
}
//**************************************************
void draw() { // telo programu

}
//***************************************
void SetComPort() { // pripoijeni na USB
  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 (int 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) {
          props = (Serial.getProperties(usb.get(i))).toString();
          Idvendor = trim(props);
          Id = match(Idvendor, "idVendor=067b");
          ID = 0;
        }
        if (Id == null) {
          props = (Serial.getProperties(usb.get(i))).toString();
          Idvendor = trim(props);
          Id = match(Idvendor, "idVendor=10c4");
          ID = 2;
        } else {
          ID = 1;
        }
        if (Id != null) {
          Usb = usb.get(i);
          Text=("OS: "+OS[Os]+" Port: "+Usb+" Chip com: "+ Chip_id[ID]);
          text(Text, 0, 21);
          break;
        } else {
          Text=("OS: "+ OS[Os]+ " Nezapojeno do USB.");
          text(Text, 0, 21);
        }
      }
      port = new Serial(this, Usb, rychlost);
      ready = true;
      //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);
      port = new Serial(this, detected_port, rychlost);
      ready = true;
    }
  }
}
 
Odpovědět
26.10.2017 15:21
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 1 zpráv z 1.