Dear All
I finally managed to get all the equipment together and connected all the stuff mentioned in this blog.
On my laptop using Java I am running the following program:
Code: Select all
package twowayserialcomm;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import static javax.management.Query.gt;
public class TwoWaySerialComm {
void connect( String portName ) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier
.getPortIdentifier( portName );
if( portIdentifier.isCurrentlyOwned() ) {
System.out.println( "Error: Port is currently in use" );
} else {
int timeout = 2000;
CommPort commPort = portIdentifier.open( this.getClass().getName(), timeout );
if( commPort instanceof SerialPort ) {
SerialPort serialPort = ( SerialPort )commPort;
serialPort.setSerialPortParams( 57600,
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;
}
@Override
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 ) {
}
}
}
public static void main( String[] args ) {
try {
( new TwoWaySerialComm() ).connect( "/dev/ttyUSB0" );
} catch( Exception e ) {
}
}
}
And I am getting the following error in the CMD:
Code: Select all
WARNING: RXTX Version mismatch
Jar version = RXTX-2.2pre1
native lib Version = RXTX-2.2pre2
BUILD SUCCESSFUL (total time: 2 seconds)
Could anyone let me know what needs to be done to solve this issue?
Thanks
Mat