import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
public class SerialConnection {
CommPortIdentifier portIdentifier;
OutputStream portOut;
InputStream portIn;
SerialPort serialPort;
String port;
private final Object stopReadMutex = new Object();
private final Object stopWriteMutex = new Object();
public boolean stopRead = false;
public boolean stopWrite = false;
public SerialConnection(String portName){
this(portName, 2400, SerialPort.DATABITS_7, SerialPort.STOPBITS_1, SerialPort.PARITY_ODD);
}
public SerialConnection(String portName, int baudRate, int databits, int stopbits, int parity){
port = portName;
try {
portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
} catch (NoSuchPortException e) {
e.printStackTrace();
}
if ( portIdentifier.isCurrentlyOwned() )
{
System.out.println("Error: Port is currently in use");
}
else
{
CommPort commPort = null;
try {
commPort = portIdentifier.open(this.getClass().getName(),2000);
} catch (PortInUseException e) {
e.printStackTrace();
}
if ( commPort instanceof SerialPort )
{
serialPort = (SerialPort) commPort;
serialPort.setDTR(true);
serialPort.setRTS(true);
InputStream in = null;
OutputStream out = null;
try {
serialPort.setSerialPortParams(baudRate,databits,stopbits,parity);
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
(new Thread(new SerialReader(in))).start();
(new Thread(new SerialWriter(out))).start();
portIn = in;
portOut = out;
}
else
{
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}
public void write(String s){
try {
System.out.println(s);
s = s+"\r\n";
portOut.write(s.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
/** */
public class SerialReader implements Runnable
{
InputStream in;
public SerialReader ( InputStream in )
{
synchronized(stopReadMutex)
{stopRead = false;}
this.in = in;
}
public void run ()
{
byte[] buffer = new byte[1024];
int len = -1;
try
{
while ( ( len = this.in.read(buffer)) > -1 )
{
if(stopRead){
System.out.println("StoppedReading");
break;
}
System.out.print(new String(buffer,0,len));
}
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
/** */
public class SerialWriter implements Runnable
{
OutputStream out;
public SerialWriter ( OutputStream out )
{
this.out = out;
synchronized(stopWriteMutex)
{stopWrite = false;}
}
public void run ()
{
while(true){
if(stopWrite){
System.out.println("Stopped writing");
break;
}
}
}
}
static void listPorts()
{
@SuppressWarnings("unchecked")
java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
while ( portEnum.hasMoreElements() )
{
CommPortIdentifier portIdentifier = portEnum.nextElement();
System.out.println(portIdentifier.getName() + " - " + getPortTypeName(portIdentifier.getPortType()) + " - " + portIdentifier.isCurrentlyOwned());
}
}
static String[] portsArray()
{
@SuppressWarnings("unchecked")
java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
ArrayList<String> portsAL = new ArrayList<String>();
while ( portEnum.hasMoreElements() )
{
CommPortIdentifier portIdentifier = portEnum.nextElement();
portsAL.add(portIdentifier.getName() + " - " + getPortTypeName(portIdentifier.getPortType()));
}
return portsAL.toArray(new String[0]);
}
static String getPortTypeName ( int portType )
{
switch ( portType )
{
case CommPortIdentifier.PORT_I2C:
return "I2C";
case CommPortIdentifier.PORT_PARALLEL:
return "Parallel";
case CommPortIdentifier.PORT_RAW:
return "Raw";
case CommPortIdentifier.PORT_RS485:
return "RS485";
case CommPortIdentifier.PORT_SERIAL:
return "Serial";
default:
return "unknown type";
}
}
public void disconnect(){
try {
synchronized(stopReadMutex)
{stopRead = true;}
synchronized(stopWriteMutex)
{stopWrite = true;}
portOut.close();
portIn.close();
serialPort.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}