Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using RXTX

807600Nov 27 2007 — edited Nov 27 2007
Hello,

I'm trying to write a simple program that will use RXTX to open a serial port on com4, read from it and write to it. I must be doing something wrong because this doesn't seem to be working - I sort of hodge-podged this together from an example that was on the rxtx website. Basically I call the connect method, that seems to work (no exceptions atleast), I call writeSerial, and though I don't know for sure if this works it doesn't throw any exceptions, but then I try calling readSerial and never get anything. Can anyone tell me why this isn't working? I know that my serial device responds fine when communicated to - I've checked it on a serial terminal. Also, what is considered the end of the input stream?
    /********************************************
     * name: readSerial
     * description: reads from serial port until \0?
     * arguments: none
     * returns: read bytes from serial port 
     *******************************************/
    private byte [] readSerial() throws IOException{
        byte [] buffer = new byte [128];
        
        //read until end of stream. whats considered the end?
        int len = -1;
        while ( ( len = serialIn.read(buffer)) > -1 );

        return(buffer);
    }
    
    /********************************************
     * name: writeSerial
     * description: writes to serial port until
     * arguments: String to write
     * returns: none
     *******************************************/
    private void writeSerial(String data) throws IOException{
        byte [] buffer = data.getBytes();
        serialOut.write(buffer);
    }
    
    /********************************************
     * name: connect
     * description: opens serial port.
     * arguments: Com port name
     * returns: none
     *******************************************/
    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);
                
                serialIn = serialPort.getInputStream();
                serialOut = serialPort.getOutputStream();
                
            }
            else
            {
                System.out.println("Error: Only serial ports are handled by this example.");
            }
        }     
    }
    

    /********************************************
     * name: closeSerial
     * description: close serial port.
     * arguments: none
     * returns: none
     *******************************************/
    private void closeSerial() throws IOException{
        serialIn.close();
        serialOut.close();
    }

    //some globals 
    private InputStream serialIn;
    private OutputStream serialOut;
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 25 2007
Added on Nov 27 2007
4 comments
360 views