serial port - java not seeing connected device COM4 -


java not seeing arudino on com4 code seems working

package tester; import java.io.bufferedreader; import java.io.inputstreamreader; import java.io.outputstream; import gnu.io.commportidentifier;  import gnu.io.serialport; import gnu.io.serialportevent;  import gnu.io.serialporteventlistener;  import java.util.enumeration; public class serialtest implements serialporteventlistener { serialport serialport;     /** port we're going use. */ private static final string port_names[] = {          "/dev/tty.usbserial-a9007ux1", // mac os x                     "/dev/ttyacm0", // raspberry pi         "/dev/ttyusb0", // linux         "com4", // windows }; /** * bufferedreader fed inputstreamreader  * converting bytes characters  * making displayed results codepage independent */ private bufferedreader input; /** output stream port */ private outputstream output; /** milliseconds block while waiting port open */ private static final int time_out = 2000; /** default bits per second com port. */ private static final int data_rate = 9600;  public void initialize() {             // next line raspberry pi ,              // gets while loop , suggested here       suggested http://www.raspberrypi.org/phpbb3/viewtopic.php?f=81&t=32186             system.setproperty("gnu.io.rxtx.serialports", "/dev/ttyacm0");      commportidentifier portid = null;     enumeration portenum = commportidentifier.getportidentifiers();      //first, find instance of serial port set in port_names.     while (portenum.hasmoreelements()) {         commportidentifier currportid = (commportidentifier) portenum.nextelement();         (string portname : port_names) {             if (currportid.getname().equals(portname)) {                 portid = currportid;                 break;             }         }     }     if (portid == null) {         system.out.println("could not find com port.");         return;     }      try {         // open serial port, , use class name appname.         serialport = (serialport) portid.open(this.getclass().getname(),                 time_out);          // set port parameters         serialport.setserialportparams(data_rate,                 serialport.databits_8,                 serialport.stopbits_1,                 serialport.parity_none);          // open streams         input = new bufferedreader(new inputstreamreader(serialport.getinputstream()));         output = serialport.getoutputstream();          // add event listeners         serialport.addeventlistener(this);         serialport.notifyondataavailable(true);     } catch (exception e) {         system.err.println(e.tostring());     } }  /**  * should called when stop using port.  * prevent port locking on platforms linux.  */ public synchronized void close() {     if (serialport != null) {         serialport.removeeventlistener();         serialport.close();     } }  /**  * handle event on serial port. read data , print it.  */ public synchronized void serialevent(serialportevent oevent) {     if (oevent.geteventtype() == serialportevent.data_available) {         try {             string inputline=input.readline();             system.out.println(inputline);         } catch (exception e) {             system.err.println(e.tostring());         }     }     // ignore other eventtypes, should consider other ones. }  public static void main(string[] args) throws exception {     serialtest main = new serialtest();     main.initialize();     thread t=new thread() {         public void run() {             //the following line keep app alive 1000 seconds,             //waiting events occur , responding them (printing incoming messages console).             try {thread.sleep(1000000);} catch (interruptedexception ie) {}         }     };     t.start();     system.out.println("started"); }} 

seems working in console is

warning:  rxtx version mismatch jar version = rxtx-2.2pre1 native lib version = rxtx-2.2pre2 not find com port. started 

i looked in jar version being off rxtx asked in there stable release

why assuming "com4" on windows ? maybe try com port (from http://www.programcreek.com/java-api-examples/index.php?api=gnu.io.commportidentifier):

public static list<string> getserialports(){   list<string> vp=new arraylist<string>();   enumeration portlist=commportidentifier.getportidentifiers();   while (portlist.hasmoreelements()) {     commportidentifier portid=(commportidentifier)portlist.nextelement();     if (portid.getporttype() == commportidentifier.port_serial) {       vp.add(portid.getname());     }   }   return vp; } 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -