Help with JAVA RXTX Connect to COM


hello everyone,

i tried create application can send code via serial port arduino. sadly code doesn't work. have 2 seperate java classes: 1 called "window" manages view , adds sliders , on. other 1 called "serialcom" , shall connect specified serial port in windows. said doesn't work. put code of samples , since i'm quiet new java , rxtx , don't quiet understand wrong it.  can please me out this. heres "serialcom" code:

code: [select]
package controlsurface;

import gnu.io.*;

import java.io.ioexception;
import java.io.inputstream;
import java.io.outputstream;
import java.util.enumeration;
import java.util.toomanylistenersexception;

public class serialcom implements serialporteventlistener{

window window = null;

commportidentifier serialportid;
enumeration enumcomm;
serialport serialport;
outputstream outputstream;
inputstream inputstream;
boolean serialportgeoeffnet = false;
int baudrate = 9600;
int databits = serialport.databits_8;
int stopbits = serialport.stopbits_1;
int parity = serialport.parity_none;
string portname = "com5";
boolean foundport = false;

public void searchforports()
   {
       enumcomm = commportidentifier.getportidentifiers();

       while (enumcomm.hasmoreelements())
       {
           serialportid = (commportidentifier)enumcomm.nextelement();

           //get serial ports
           if (portname.contentequals(serialportid.getname()))
           {
            window.setdebug(portname);
            foundport = true;
break;
           }
       }
   }

public void connect(){
portname = "com5";
//window.setdebug(portname);
if(foundport != true){
//window.setdebug("port not found!\n");
}else{
try {
serialportid = commportidentifier.getportidentifier(portname);
serialport = (serialport) serialportid.open("opening port.", 500);
serialport.setserialportparams(
   baudrate,
   databits,
   stopbits,
   parity);
serialport.setflowcontrolmode(
       serialport.flowcontrol_none);
outputstream = serialport.getoutputstream();
inputstream = serialport.getinputstream();
} catch (nosuchportexception e1) {
// todo auto-generated catch block
window.setdebug(e1.tostring());
} catch (portinuseexception e) {
// todo auto-generated catch block
window.setdebug(e.tostring());
} catch (unsupportedcommoperationexception ex) {
window.setdebug(ex.tostring());;
} catch (ioexception e) {
// todo auto-generated catch block
window.setdebug(e.tostring());
}
}

}
public void disconnect() {
       if (serialport != null) {
           try {
               // close i/o streams.
               outputstream.close();
               inputstream.close();
           } catch (ioexception ex) {
               // don't care
           }
           // close port.
           serialport.close();
           serialport = null;
       }
   }
//possibly unnecessary
public inputstream getserialinputstream() {
       return inputstream;
   }

   public outputstream getserialoutputstream() {
       return outputstream;
   }
   
   public void initlistener()
   {
       try
       {
           serialport.addeventlistener(this);
           serialport.notifyondataavailable(true);
       }
       catch (toomanylistenersexception e)
       {
          window.setdebug(e.tostring());
       }
   }
   public void setled(int r, int g, int b){
    if(serialportgeoeffnet == true){
    byte[] datatosend = {(byte)r,(byte)g,(byte)b,(byte)0x0a};
//remove spurious line endings color bytes serial device doesn't confused
for (int i=0; i<datatosend.length-1; i++){
if (datatosend[i] == 0x0a){
datatosend[i] = 0x0b;
}
}
    try {
outputstream.write(datatosend);
} catch (ioexception e) {
// todo auto-generated catch block
window.setdebug(e.tostring());
}
    }
   }
@override
public void serialevent(serialportevent arg0) {
// todo auto-generated method stub

}

}


thanks

life short wade through java code  ...  :)   (i use jruby).

i don't see code uses serial class.

there couple of common gotchas ...

don't disconnect serial port between uses. leave open time want able communicate arduino.

when serial port opened causes arduino reset , takes arduino few seconds before ready receive or transmit. practice have setup() function in arduino code send short message , have pc program (whatever language) wait until receives before getting down business.

if have no working java code strategy write simple arduino sketch can controlled arduino serial monitor (which proves sketch works) , write java program doing serial monitor.

if feel switching jruby happy share code.

...r


Arduino Forum > Using Arduino > Programming Questions > Help with JAVA RXTX Connect to COM


arduino

Comments

Popular posts from this blog

opencv3, tbb and rasp pi 2 - Raspberry Pi Forums

small ethernet problem - Raspberry Pi Forums

Multithumb configuration params not working? - Joomla! Forum - community, help and support