Go Down

Topic: ArduinoBT rxtx Java COMMs (Read 708 times) previous topic - next topic

Lee_G

I've have a simple echo prog. running on the ArduinoBT connecting from WinXP SP2; and it works fine when using the windows hyper-terminal as a client.

From Java using rxtx 2.1.7, however, it works but requires first manually  connecting the serial port service in blueSoleil;  then is very slow to send/receive;
finally it does not disconnect the  serial port service after completion.

I am thinking that since both windows hyper-terminal & the avrdude loader are able to connect/disconnect automatically and are much faster than Java that maybe I am doing something wrong?
As a bluetooth nube - any assistance is appreciated.  

Lee



Java Client Code:



public class EchoTest implements SerialPortEventListener, Runnable {

     private static final int SPEED_19200 = 115200;
     private static CommPortIdentifier portId;
     private static SerialPort             serialPort;
     private static Enumeration<CommPortIdentifier> portList;
     private static String defaultPort = "COM5";
     private OutputStream outputStream = null;
     private InputStream inputStream = null;
   private PrintWriter pw = null;
   
     public static void main(String[] args) {
       boolean portFound = false;
       try {
                 portId = CommPortIdentifier.getPortIdentifier(defaultPort);
                 portFound = true;
           } catch (NoSuchPortException e1) {
                 // TODO Auto-generated catch block
                 e1.printStackTrace();
           }
       

           if (portFound) {
                 EchoTest st = new EchoTest();
                 Thread xThread = new Thread(st);
                 xThread.start();
                 try {
                       xThread.join();
                       st.close();
                 } catch (InterruptedException e) {
                 }
             }
            System.exit(0);
     }


     public void run() {
           try {
                 setup();
                 println("1cha cha cha");
                 println("2cha cha cha");
                 println("3cha cha cha");
           } catch (Throwable e) {
                 System.out.println(e.getMessage());
                 e.printStackTrace();
                 
           }
     }


     public void println(final String s) {
           String s1 = "";
           pw.println(s);
     }

     private void close() {
//            pw.flush();  // flush causes an exception, but does not seem to be necessary
           try {
                 pw.close();
                 outputStream.close();
                 Thread.currentThread().sleep(1000);
                 inputStream.close();
           } catch (Exception e) {
                 e.printStackTrace();
           }
     }

     private void setup() {
           try {
                 serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
           } catch (Exception e) {
                 System.out.println(e.getMessage());
                 e.printStackTrace();
                 }
           
           try {
                 serialPort.setSerialPortParams(SPEED_19200, SerialPort.DATABITS_8,
                                      SerialPort.STOPBITS_1,
                                      SerialPort.PARITY_NONE);
           } catch (UnsupportedCommOperationException e) {
                 System.out.println(e.getMessage());
                 e.printStackTrace();
           }
     
           try {
                 inputStream = serialPort.getInputStream();
           } catch (IOException e) {
                 System.out.println(e.getMessage());
                 e.printStackTrace();            }
           
           try {
                 outputStream = serialPort.getOutputStream();
                  pw = new PrintWriter(outputStream);
                 
           } catch (IOException e) {
                 System.out.println(e.getMessage());
                 e.printStackTrace();
                 }
           
           try {
                 serialPort.addEventListener(this);
           } catch (TooManyListenersException e) {
                 System.out.println(e.getMessage());
                 e.printStackTrace();
                 }
     
           serialPort.notifyOnDataAvailable(true);
     
     }

     public void serialEvent(SerialPortEvent event) {
     
           switch (event.getEventType()) {
           case SerialPortEvent.BI:
           case SerialPortEvent.OE:
           case SerialPortEvent.FE:
           case SerialPortEvent.PE:
           case SerialPortEvent.CD:
           case SerialPortEvent.CTS:
           case SerialPortEvent.DSR:
           case SerialPortEvent.RI:
           case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
                 break;
           case SerialPortEvent.DATA_AVAILABLE:
                 byte[] readBuffer = new byte[20];
                 try {
                    int numBytes = 0;
                    StringBuilder sb = new StringBuilder();
                       while (inputStream.available() > 0) {
                             numBytes = (byte) inputStream.read(readBuffer);
                             sb.append(new String(readBuffer, "ASCII").substring(0, numBytes));
                       }
                       System.out.println("Read: "+sb.toString());
                 } catch (IOException e) {}
                 break;
           }
     }
}





ArduinoBT Code:

void setup()
{
 Serial.begin(115200);
}

void loop()
{
  byte val;
  while (Serial.available()) {
   // read the most recent byte (which will be from 0 to 255)
   val = Serial.read();
   Serial.print(val, BYTE);
 }
}

Lee_G

Update:  I have found two workarounds for the above Java rxtx problem, as follows:

1) Use serproxy as mentioned in another thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1206439823/0

2) Also using a JSR-82 J2SE implementation.  specifically I tried http://code.google.com/p/bluecove/.
If anyone is interested in the latter, I'll post more.

Lee

Smuorfy

could you post more about the second one? thanks

Go Up