import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.util.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
public class SerialCommunication implements SerialPortEventListener
{
InputStream input;
OutputStream output;
CommPort commPort;
byte buffer[] = new byte[32768];
int bufferIndex;
int bufferLast;
public int[] dataIn = new int[20];
public byte[] dataOut = new byte [90];
int counter = 0;
int rate;
int parity;
int databits;
int stopbits;
boolean monitor = true;
MessageConsumer consumer;
boolean Reply1Received = false;
boolean Reply2Received = false;
boolean Reply3Received = false;
boolean Reply4Received = false;
boolean Reply5Received = false;
boolean _allowed = false;
byte[] _key = new byte[2];
int _verifyCount = 0;
int _serialCount = 0;
byte[] _inBuffer = new byte[6];
int _checkSum = 0;
byte InByte1 = 0;
byte InByte2 = 0;
byte InByte3 = 0;
byte InByte4 = 0;
byte[] reply1 = new byte[4];
byte[] reply2 = new byte[4];
byte[] reply3 = new byte[4];
byte[] reply4 = new byte[4];
byte[] reply5 = new byte[4];
public SerialCommunication()
{
super();
}
void connect ( String portName ) throws Exception
{ _key[0] = 1;
_key[1] = -2;
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if ( portIdentifier.isCurrentlyOwned() )
{
System.out.println("Error: Port is currently in use");
}
else
{
commPort = portIdentifier.open(this.getClass().getName(),1000);
if ( commPort instanceof SerialPort )
{
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(115200,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
input = serialPort.getInputStream();
output = serialPort.getOutputStream();
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
}
else
{
System.out.println("Error: Only serial ports are handled");
}
}
}
public void addListener(MessageConsumer consumer) {
this.consumer = consumer;
}
public void dispose(){
try{
if (input != null) input.close();
if (output != null) output.close();
}
catch
(Exception e){e.printStackTrace();
}
try{
if (commPort != null) commPort.close();
}
catch (Exception e)
{e.printStackTrace();
}
}
synchronized public void serialEvent(SerialPortEvent serialEvent) {
if (serialEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
while (input.available() > 0) {
synchronized (buffer) {
if (bufferLast == buffer.length) {
byte temp[] = new byte[bufferLast << 1];
System.arraycopy(buffer, 0, temp, 0, bufferLast);
buffer = temp;
}
int inByte = input.read();
feed(inByte);
//System.out.println(inByte); // uncomment for testing
if (this.consumer != null)
this.consumer.message("" + (char) input.read());
}
}
} catch (IOException e) {
System.out.println("no!");
}
catch (Exception e) {
}
}}
public int read() {
if (bufferIndex == bufferLast) return -1;
synchronized (buffer) {
int outgoing = buffer[bufferIndex++] & 0xff;
if (bufferIndex == bufferLast) { // rewind
bufferIndex = 0;
bufferLast = 0;
}
return outgoing;
}
}
public char readChar() {
if (bufferIndex == bufferLast) return (char)(-1);
return (char) read();
}
public void write(int what) {
try {
output.write(what & 0xff);
output.flush();
} catch (Exception e) { // null pointer or serial port dead
//errorMessage("write", e);
}
}
public void write(byte bytes[]) {
try {
output.write(bytes);
output.flush();
} catch (Exception 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();
System.exit(-1);
}
}
}*/
public List<String> getAvailablePorts() {
List<String> list = new ArrayList<String>();
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
list.add(portId.getName());
}
}
return list;
}
void feed(int _inByte){
if(_allowed == false){
if(_inByte == _key[_verifyCount]){
_verifyCount++;
if(_verifyCount > 1){_allowed = true;_verifyCount = 0;}
}else{_verifyCount = 0;}}
else{_inBuffer[_serialCount] = (byte)_inByte;
if(_serialCount < 5){
_checkSum += _inByte; }
_serialCount++;
if(_serialCount > 5){
if(_inBuffer[5] == _checkSum){_Process(_inBuffer);}
else{_BadSerialEvent();}
_allowed = false;
_checkSum = 0;
_serialCount = 0;}}
}
void _Process(byte[] _input ){
InByte1 = _input[1];
InByte2 = _input[2];
InByte3 = _input[3];
InByte4 = _input[4];
switch (_input[0]){
case 0:
_SaveReply1(InByte1,InByte2,InByte3,InByte4);
Reply1Received = true;
break;
case 1:
_SaveReply2(InByte1,InByte2,InByte3,InByte4);
Reply2Received = true;
break;
case 2:
_SaveReply3(InByte1,InByte2,InByte3,InByte4);
Reply3Received = true;
break;
case 3:
_SaveReply4(InByte1,InByte2,InByte3,InByte4);
Reply4Received = true;
break;
case 4:
_SaveReply5(InByte1,InByte2,InByte3,InByte4);
Reply5Received = true;
break;
}
}
void _BadSerialEvent(){
}
public void SendFunction1(int byte1, int byte2, int byte3, int byte4){
//create _checksum
_checkSum = byte1 + byte2 + byte3 + byte4;
write(_key);
write(0);
write((byte)byte1);
write((byte)byte2);
write((byte)byte3);
write((byte)byte4);
write((byte)_checkSum);
//System.out.println((byte)_checkSum);
}
public void SendFunction2(int byte1, int byte2, int byte3, int byte4){
_checkSum = byte1 + byte2 + byte3 + byte4;
write(_key);
write(1);
write((byte)byte1);
write((byte)byte2);
write((byte)byte3);
write((byte)byte4);
write(_checkSum);
}
public void SendFunction3(int byte1, int byte2, int byte3, int byte4){
_checkSum = byte1 + byte2 + byte3 + byte4;
write(_key);
write(2);
write((byte)byte1);
write((byte)byte2);
write((byte)byte3);
write((byte)byte4);
write(_checkSum);
}
public void SendFunction4(int byte1, int byte2, int byte3, int byte4){
_checkSum = byte1 + byte2 + byte3 + byte4;
write(_key);
write(3);
write((byte)byte1);
write((byte)byte2);
write((byte)byte3);
write((byte)byte4);
write(_checkSum);
}
public void SendFunction5(int byte1, int byte2, int byte3, int byte4){
_checkSum = byte1 + byte2 + byte3 + byte4;
write(_key);
write(4);
write((byte)byte1);
write((byte)byte2);
write((byte)byte3);
write((byte)byte4);
write(_checkSum);
}
void _SaveReply1(byte byte1, byte byte2, byte byte3, byte byte4){
reply1[0] = byte1;
reply1[1] = byte2;
reply1[2] = byte3;
reply1[3] = byte4;
}
void _SaveReply2(byte byte1, byte byte2, byte byte3, byte byte4){
reply2[0] = byte1;
reply2[1] = byte2;
reply2[2] = byte3;
reply2[3] = byte4;
}
void _SaveReply3(byte byte1, byte byte2, byte byte3, byte byte4){
reply3[0] = byte1;
reply3[1] = byte2;
reply3[2] = byte3;
reply3[3] = byte4;
}
void _SaveReply4(byte byte1, byte byte2, byte byte3, byte byte4){
reply4[0] = byte1;
reply4[1] = byte2;
reply4[2] = byte3;
reply4[3] = byte4;
}
void _SaveReply5(byte byte1, byte byte2, byte byte3, byte byte4){
reply5[0] = byte1;
reply5[1] = byte2;
reply5[2] = byte3;
reply5[3] = byte4;
}
public void AskDataSet1(int byte1, int byte2, int byte3, int byte4){
//create _checksum
_checkSum = byte1 + byte2 + byte3 + byte4;
write(_key);
write(5);
write((byte)byte1);
write((byte)byte2);
write((byte)byte3);
write((byte)byte4);
write((byte)_checkSum);
Reply1Received = false;
//System.out.println((byte)_checkSum);
}
public void AskDataSet2(int byte1, int byte2, int byte3, int byte4){
//create _checksum
_checkSum = byte1 + byte2 + byte3 + byte4;
write(_key);
write(6);
write((byte)byte1);
write((byte)byte2);
write((byte)byte3);
write((byte)byte4);
write((byte)_checkSum);
Reply2Received = false;
//System.out.println((byte)_checkSum);
}
public void AskDataSet3(int byte1, int byte2, int byte3, int byte4){
//create _checksum
_checkSum = byte1 + byte2 + byte3 + byte4;
write(_key);
write(7);
write((byte)byte1);
write((byte)byte2);
write((byte)byte3);
write((byte)byte4);
write((byte)_checkSum);
Reply3Received = false;
//System.out.println((byte)_checkSum);
}
public void AskDataSet4(int byte1, int byte2, int byte3, int byte4){
//create _checksum
_checkSum = byte1 + byte2 + byte3 + byte4;
write(_key);
write(8);
write((byte)byte1);
write((byte)byte2);
write((byte)byte3);
write((byte)byte4);
write((byte)_checkSum);
Reply4Received = false;
//System.out.println((byte)_checkSum);
}
public void AskDataSet5(int byte1, int byte2, int byte3, int byte4){
//create _checksum
_checkSum = byte1 + byte2 + byte3 + byte4;
write(_key);
write(9);
write((byte)byte1);
write((byte)byte2);
write((byte)byte3);
write((byte)byte4);
write((byte)_checkSum);
Reply5Received = false;
//System.out.println((byte)_checkSum);
}
}