import javax.comm.*;
import java.util.*;
import java.io.*;
public class SerialConnection implements SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
static String comName;
static int baudRate, parityValue, stopBitValue;
SerialPort serialPort;
public SerialConnection() {
this.portList = CommPortIdentifier.getPortIdentifiers();
}
public static void setSerialParams( String comValue,
String baudValue,
String parity,
String stopBits){
comName = comValue;
baudRate = Integer.parseInt(baudValue);
if(Integer.parseInt(stopBits) == 1)
stopBitValue = SerialPort.STOPBITS_1;
else
stopBitValue = SerialPort.STOPBITS_2;
if(parity.equalsIgnoreCase("none"))
parityValue = SerialPort.PARITY_NONE;
else if(parity.equalsIgnoreCase("odd"))
parityValue = SerialPort.PARITY_ODD;
else
parityValue = SerialPort.PARITY_EVEN;
}
public boolean openPort() {
try {
this.portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(comName))
serialPort = (SerialPort)portId.open("BlueConsole", 2000);
}
}
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
serialPort.setSerialPortParams( baudRate,
SerialPort.DATABITS_8,
stopBitValue,
parityValue);
return true;
} catch (Exception e) {
return false;
}
}
public void closePort() {
serialPort.close();
}
public void sendCommand(ArrayList hexValueList) {
try {
OutputStream outputStream = serialPort.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(outputStream);
byte []sendData = new byte[hexValueList.size()];
for (int i = 0; i < hexValueList.size(); i++){
Integer in = (Integer)hexValueList.get(i);
sendData[i] = (byte)in.intValue();
}
outputStream.write(sendData, 0, sendData.length);
osw.close();
outputStream.close();
} catch (IOException e) {System.out.println("ioe");}
}
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
this.readInput();
}
}
private void readInput() {
int text = 0;
try {
String recieved = "[Received]:\t";
int[] readBuffer = new int[50];
InputStream inputStream = serialPort.getInputStream();
InputStreamReader isr = new InputStreamReader(inputStream);
BufferedReader in = new BufferedReader(isr );
for (int i = 0; i < readBuffer.length; i++){
readBuffer[i] = isr.read();
MonitorBuffer.processbyte((short) readBuffer[i]);
recieved += optimizeInput(readBuffer[i]);
if(Integer.toHexString(readBuffer[i]).equalsIgnoreCase("FD")){
break;
}
}
BlueConsoleFrame.appendLog(recieved + "\n");
inputStream.close();
isr.close();
in.close();
}
catch (IOException e)
{
System.out.println("IO Exception");
}
}
public static String optimizeInput(int i) {
String a = "bla";
if(i < 0x10) {
switch(i) {
case 0x00: a = new String("0" + Integer.toHexString(i).toUpperCase()) + " ";
break;
case 0x01: a = new String("0" + Integer.toHexString(i).toUpperCase()) + " ";
break;
case 0x02: a = new String("0" + Integer.toHexString(i).toUpperCase()) + " ";
break;
case 0x03: a = new String("0" + Integer.toHexString(i).toUpperCase()) + " ";
break;
case 0x04: a = new String("0" + Integer.toHexString(i).toUpperCase()) + " ";
break;
case 0x05: a = new String("0" + Integer.toHexString(i).toUpperCase()) + " ";
break;
case 0x06: a = new String("0" + Integer.toHexString(i).toUpperCase()) + " ";
break;
case 0x07: a = new String("0" + Integer.toHexString(i).toUpperCase()) + " ";
break;
case 0x08: a = new String("0" + Integer.toHexString(i).toUpperCase()) + " ";
break;
case 0x09: a = new String("0" + Integer.toHexString(i).toUpperCase()) + " ";
break;
case 0x0a: a = new String("0" + Integer.toHexString(i).toUpperCase()) + " ";
break;
case 0x0b: a = new String("0" + Integer.toHexString(i).toUpperCase()) + " ";
break;
case 0x0c: a = new String("0" + Integer.toHexString(i).toUpperCase()) + " ";
break;
case 0x0d: a = new String("0" + Integer.toHexString(i).toUpperCase()) + " ";
break;
case 0x0e: a = new String("0" + Integer.toHexString(i).toUpperCase()) + " ";
break;
case 0x0f: a = new String("0" + Integer.toHexString(i).toUpperCase()) + " ";
break;
}
}
else
a = Integer.toHexString(i).toUpperCase() + " ";
return(a);
}
}