java RXTX send strings

i'm trying to use the RXTX java library to send strings by click listeners in my gui, but i can't edit the code of the RXTX to send strings , where it only responds to input from the console, so how can i edit the code to not only send data that is input from the console?

i'm using the same code here:

http://rxtx.qbang.org/wiki/index.php/Two_way_communcation_with_the_serial_port

i have tried writing this extra function and called it from other classes:

         public void writetoport() {
        String serialMessage = "test";

             try {
        // i have made 'out' as a global variable// 
            this.out.write(serialMessage.getBytes());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

                 }

but i get this error in the console:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at TwoWaySerialComm.writetoport(TwoWaySerialComm.java:121)

java rxtx

i have tried writing this extra function and called it from other classes:

Well, then, the next step is to stop calling functions from other function calls. Break the complex statement up into individual statements that contain ONE function call each. Perhaps a clue will present itself when you know WHICH statement is triggering the exception.

outsorts:
i get this error in the console:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at TwoWaySerialComm.writetoport(TwoWaySerialComm.java:121)



java rxtx

I assume that TwoWaySerialComm.writetoport is the function you posted above. I have to assume that, because you haven’t posted the complete code. One of the values you’re dereferencing at line 121 is null. Of course I have no idea which line that is, because you haven’t posted the complete code. Knowing which line it is, it would be easy enough to add some extra checks to detect when objects are null and tell you. Somebody seeing and understanding the complete code might then be able to work out why the object is null and what the best way is to stop it being null. Unfortunately I can’t do any of that, because you haven’t posted the complete code.

the null pointer exception is in this line in my code:

out.write(serialMessage.getBytes());

and here is the full code of the twowaycomm class:

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;

import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.sun.crypto.provider.RC2Cipher;

public class TwoWaySerialComm
{
     SerialPort serialPort;
    public static OutputStream out;
    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(115200,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                
                InputStream in = serialPort.getInputStream();
                OutputStream out = serialPort.getOutputStream();
                
                (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() {
            String serialMessage = "hello";
        
            
            try {
                             this.out.write(serialMessage.getBytes());
                            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
         
        }
      
    
    
    public static void main ( String[] args )
    {
        try
        {
            (new TwoWaySerialComm()).connect("COM52");
        
        
        }
        catch ( Exception e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

and this is the part in my gui code that i call the writetoport() function

btnNewButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String hello=commands[0];
                TwoWaySerialComm t=new TwoWaySerialComm();
            t.writetoport();

            }
        });

ok i solved my problem with something like this:

if ( commPort instanceof SerialPort )
            {
                SerialPort serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(115200,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                
                InputStream in = serialPort.getInputStream();
                OutputStream out = serialPort.getOutputStream();
                           out1=out; //where out1 is a global variable//
                (new Thread(new SerialReader(in))).start();
             (new Thread(new SerialWriter(out))).start();

            }

then i used this without any problems:

out1.write(serialMessage.getBytes());
out1.flush();

i know this may sound stupid but it works

It looks like you were trying to use a local variable outside the scope where it was created.

I suspect you could have used out1 directly as in "out1 = serialPort.getOutputStream();"

I use Rxtx with my Arduino - but from JRuby which is much easier to understand than Java - especially as Java folks seem unable to avoid any opportunity to make things complex.

...R

Please help !!! Where do I get wrong for sending string

/*
 * 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();
        }
    }
}

Don't waste our time by double posting. I have already replied to your question.

...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