Java RXTX send string

Hi to all. I read almost everything that is on this forum but nothing woks. Receiving string is no problem. Working as charm but sending not possible ! Please help if anyone can provide me with solution...

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package twowaycomm;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class TwoWaySerialComm
{
   private OutputStream out1 ;
    
    public TwoWaySerialComm()
    {
        super();
    }
    
    void connect ( String portName ) throws Exception
    {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        if ( portIdentifier.isCurrentlyOwned() )
        {
            System.out.println("Error: Port is currently in use");
        }
        else
        {
            CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
            
            if ( commPort instanceof SerialPort )
            {
                SerialPort serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                
                InputStream in = serialPort.getInputStream();
                OutputStream out = serialPort.getOutputStream();
                out1 = out;
                (new Thread(new SerialReader(in))).start();
                (new Thread(new SerialWriter(out))).start();

            }
            else
            {
                System.out.println("Error: Only serial ports are handled by this example.");
            }
        }     
    }
    
    /** */
    public static class SerialReader implements Runnable 
    {
        InputStream in;
        
        public SerialReader ( InputStream in )
        {
            this.in = in;
        }
        
        public void run ()
        {
            byte[] buffer = new byte[1024];
            int len = -1;
            try
            {
                while ( ( len = this.in.read(buffer)) > -1 )
                {
                    System.out.print(new String(buffer,0,len));
                }
            }
            catch ( IOException e )
            {
                e.printStackTrace();
            }            
        }
    }

    /** */
    public static class SerialWriter implements Runnable 
    {
        OutputStream out;
        public SerialWriter ( OutputStream out )
        {
            this.out = out;
        }
        public void run ()
        {
            try
            {                
                int c = 0;
                while ( ( c = System.in.read()) > -1 )
                {
                    this.out.write(c);
                }                
            }
            catch ( IOException e )
            {
                e.printStackTrace();
            }            
        }
    }
    
    public void writetoport() {
       try {   
           String send="1";
            out1.write(send.getBytes());
            out1.flush();
        } catch (IOException e) {    
            e.printStackTrace();     
        }

    }




    
    public static void main ( String[] args )
    {
        try
        {
            (new TwoWaySerialComm()).connect("COM3");
            
            TwoWaySerialComm ttt= new TwoWaySerialComm();
            ttt.writetoport();


        }
        catch ( Exception e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

simenesky: Please help if anyone can provide me with solution...

Use Python? Python - Arduino demo

It might be worth trying the newer JSSC library although I had no problem with either RxTx or JSSC when using JRuby - but that was about 2 years ago and I have forgotten most of the detail.

I am never comfortable trying to understand Java code. Is your program allowing time for the Arduino to reset when it opens the Serial port. I usually get my Arduino to send a message from setup() and have my PC programs wait until they get that before sending anything.

...R

Can't you read!. This forum deals wirh C/C++ running on Arduinos (and only the AVR versions of them)

So why are you posting java questions here?

Mark

holmes4: Can't you read!. This forum deals wirh C/C++ running on Arduinos (and only the AVR versions of them)

So why are you posting java questions here? Mark

A bit harsh don't you think! The OP has a problem communicating between Java and his Arduino. It may be a Java problem but it also may be a C problem.

simenesky: Receiving string is no problem. Working as charm but sending not possible ! Please help if anyone can provide me with solution...

It is not obvious to me how you are testing your Java. There is nothing obviously wrong with your Java but without the associated Arduino code it is difficult to help.

There is one obvious thing wrong with the Java. You are using two different instances of TwoWaySerialComm. You open the connection with the first then try to write to the second!

Change

        (new TwoWaySerialComm()).connect("COM3");
            
            TwoWaySerialComm ttt= new TwoWaySerialComm();
            ttt.writetoport();

to

           TwoWaySerialComm ttt= new TwoWaySerialComm();
           ttt.connect("COM3");
            ttt.writetoport();

You should still post your Arduino test code if you want any further help.