| 1 |
8 |
gajos |
package webexponentiator.serialcomm;
|
| 2 |
|
|
|
| 3 |
|
|
import com.fazecast.jSerialComm.SerialPort;
|
| 4 |
|
|
import com.fazecast.jSerialComm.SerialPortDataListener;
|
| 5 |
|
|
import com.fazecast.jSerialComm.SerialPortEvent;
|
| 6 |
|
|
|
| 7 |
|
|
import java.io.InputStream;
|
| 8 |
|
|
import java.io.OutputStream;
|
| 9 |
|
|
import java.math.BigInteger;
|
| 10 |
|
|
import java.util.HashMap;
|
| 11 |
|
|
|
| 12 |
|
|
import org.apache.commons.lang3.ArrayUtils;
|
| 13 |
|
|
import org.eclipse.swt.graphics.Color;
|
| 14 |
|
|
|
| 15 |
|
|
public class Communication implements SerialPortDataListener {
|
| 16 |
|
|
|
| 17 |
|
|
//passed from main GUI
|
| 18 |
|
|
Window window = null;
|
| 19 |
|
|
String selectedPort = "COM3";
|
| 20 |
|
|
// just a boolean flag that i use for enabling
|
| 21 |
|
|
// and disabling buttons depending on whether the program
|
| 22 |
|
|
// is connected to a serial port or not
|
| 23 |
|
|
private boolean bConnected = false;
|
| 24 |
|
|
|
| 25 |
|
|
// the timeout value for connecting with the port
|
| 26 |
|
|
final static int TIMEOUT = 2000;
|
| 27 |
|
|
|
| 28 |
|
|
// for containing the ports that will be found
|
| 29 |
|
|
private SerialPort[] ports = null;
|
| 30 |
|
|
// map the port names to CommPortIdentifiers
|
| 31 |
|
|
private HashMap<String, SerialPort> portMap = new HashMap<String, SerialPort>();
|
| 32 |
|
|
|
| 33 |
|
|
// this is the object that contains the opened port
|
| 34 |
|
|
private SerialPort serialPort = null;
|
| 35 |
|
|
|
| 36 |
|
|
// input and output streams for sending and receiving data
|
| 37 |
|
|
private InputStream input = null;
|
| 38 |
|
|
private OutputStream output = null;
|
| 39 |
|
|
|
| 40 |
|
|
public Communication(Window window) {
|
| 41 |
|
|
this.window = window;
|
| 42 |
|
|
}
|
| 43 |
|
|
|
| 44 |
|
|
// a string for recording what goes on in the program
|
| 45 |
|
|
// this string is written to the GUI
|
| 46 |
|
|
String logText = "";
|
| 47 |
|
|
|
| 48 |
|
|
// search for all the serial ports
|
| 49 |
|
|
// pre style="font-size: 11px;": none
|
| 50 |
|
|
// post: adds all the found ports to a combo box on the GUI
|
| 51 |
|
|
public void searchForPorts() {
|
| 52 |
|
|
|
| 53 |
|
|
ports = SerialPort.getCommPorts();
|
| 54 |
|
|
|
| 55 |
|
|
for (SerialPort port:ports) {
|
| 56 |
|
|
String portName = port.getDescriptivePortName();
|
| 57 |
|
|
if (portName.contains("COM3")) {
|
| 58 |
|
|
selectedPort = portName;
|
| 59 |
|
|
}
|
| 60 |
|
|
// get only serial ports
|
| 61 |
|
|
window.combo.add(portName);
|
| 62 |
|
|
portMap.put(portName, port);
|
| 63 |
|
|
}
|
| 64 |
|
|
}
|
| 65 |
|
|
|
| 66 |
|
|
// connect to the selected port in the combo box
|
| 67 |
|
|
// pre style="font-size: 11px;": ports are already found by using the
|
| 68 |
|
|
// searchForPorts
|
| 69 |
|
|
// method
|
| 70 |
|
|
// post: the connected comm port is stored in commPort, otherwise,
|
| 71 |
|
|
// an exception is generated
|
| 72 |
|
|
public void connect() {
|
| 73 |
|
|
if (window.combo.getSelectionIndex() >= 0) {
|
| 74 |
|
|
selectedPort = (String) window.combo.getItem(window.combo.getSelectionIndex());
|
| 75 |
|
|
serialPort = (SerialPort) portMap.get(selectedPort);
|
| 76 |
|
|
serialPort.setBaudRate(115200);
|
| 77 |
|
|
serialPort.setNumDataBits(8);
|
| 78 |
|
|
serialPort.setNumStopBits(1);
|
| 79 |
|
|
serialPort.setParity(SerialPort.ODD_PARITY);
|
| 80 |
|
|
|
| 81 |
|
|
// the method below returns an object of type CommPort
|
| 82 |
|
|
setConnected(serialPort.openPort());
|
| 83 |
|
|
if (getConnected()) {
|
| 84 |
|
|
// logging
|
| 85 |
|
|
logText = selectedPort + " opened successfully.";
|
| 86 |
|
|
window.text.setForeground(new Color(window.shlPcExponentiator.getDisplay(), 0, 0, 0));
|
| 87 |
|
|
window.appendText(logText + "\n");
|
| 88 |
|
|
window.toggleControls();
|
| 89 |
|
|
} else {
|
| 90 |
|
|
logText = selectedPort + " was not opened";
|
| 91 |
|
|
window.text.setForeground(new Color(window.shlPcExponentiator.getDisplay(), 255, 0, 0));
|
| 92 |
|
|
window.appendText(logText + "\n");
|
| 93 |
|
|
}
|
| 94 |
|
|
}
|
| 95 |
|
|
}
|
| 96 |
|
|
// open the input and output streams
|
| 97 |
|
|
// pre style="font-size: 11px;": an open port
|
| 98 |
|
|
// post: initialized input and output streams for use to communicate data
|
| 99 |
|
|
public boolean initIOStream() {
|
| 100 |
|
|
// return value for whether opening the streams is successful or not
|
| 101 |
|
|
boolean successful = false;
|
| 102 |
|
|
|
| 103 |
|
|
input = serialPort.getInputStream();
|
| 104 |
|
|
output = serialPort.getOutputStream();
|
| 105 |
|
|
|
| 106 |
|
|
successful = true;
|
| 107 |
|
|
return successful;
|
| 108 |
|
|
}
|
| 109 |
|
|
|
| 110 |
|
|
// starts the event listener that knows whenever data is available to be
|
| 111 |
|
|
// read
|
| 112 |
|
|
// pre style="font-size: 11px;": an open serial port
|
| 113 |
|
|
// post: an event listener for the serial port that knows when data is
|
| 114 |
|
|
// received
|
| 115 |
|
|
public void initListener() {
|
| 116 |
|
|
serialPort.addDataListener(this);
|
| 117 |
|
|
}
|
| 118 |
|
|
|
| 119 |
|
|
//disconnect the serial port
|
| 120 |
|
|
//pre style="font-size: 11px;": an open serial port
|
| 121 |
|
|
//post: closed serial port
|
| 122 |
|
|
public void disconnect()
|
| 123 |
|
|
{
|
| 124 |
|
|
//close the serial port
|
| 125 |
|
|
try {
|
| 126 |
|
|
serialPort.removeDataListener();
|
| 127 |
|
|
input.close();
|
| 128 |
|
|
output.close();
|
| 129 |
|
|
serialPort.closePort();
|
| 130 |
|
|
setConnected(false);
|
| 131 |
|
|
window.toggleControls();
|
| 132 |
|
|
|
| 133 |
|
|
logText = "Disconnected.";
|
| 134 |
|
|
window.text.setForeground(new Color(window.shlPcExponentiator.getDisplay(), 255, 0, 0));
|
| 135 |
|
|
window.appendText(logText + "\n");
|
| 136 |
|
|
|
| 137 |
|
|
logText = "Disconnected.";
|
| 138 |
|
|
} catch (Exception e) {
|
| 139 |
|
|
logText = "Failed to close " + serialPort.getDescriptivePortName()
|
| 140 |
|
|
+ "(" + e.toString() + ")";
|
| 141 |
|
|
window.text.setForeground(new Color(window.shlPcExponentiator.getDisplay(), 255, 0, 0));
|
| 142 |
|
|
window.appendText(logText + "\n");
|
| 143 |
|
|
}
|
| 144 |
|
|
}
|
| 145 |
|
|
|
| 146 |
|
|
|
| 147 |
|
|
@Override
|
| 148 |
|
|
public int getListeningEvents() {
|
| 149 |
|
|
return SerialPort.LISTENING_EVENT_DATA_AVAILABLE;
|
| 150 |
|
|
}
|
| 151 |
|
|
|
| 152 |
|
|
//what happens when data is received
|
| 153 |
|
|
//pre style="font-size: 11px;": serial event is triggered
|
| 154 |
|
|
//post: processing on the data it reads
|
| 155 |
|
|
@Override
|
| 156 |
|
|
public void serialEvent(SerialPortEvent evt) {
|
| 157 |
|
|
if (evt.getEventType() != SerialPort.LISTENING_EVENT_DATA_AVAILABLE) {
|
| 158 |
|
|
try {
|
| 159 |
|
|
byte[] buffer = new byte[10];
|
| 160 |
|
|
int n = input.read(buffer);
|
| 161 |
|
|
if (n > 0) {
|
| 162 |
|
|
if (n == 1) {
|
| 163 |
|
|
BigInteger command = new BigInteger(new byte[] { 0,
|
| 164 |
|
|
buffer[0] });
|
| 165 |
|
|
final String s = "Command = " + command.toString(16)
|
| 166 |
|
|
+ "\n";
|
| 167 |
|
|
//TODO Something for retrieve data
|
| 168 |
|
|
//window.appendText(s);
|
| 169 |
|
|
} else {
|
| 170 |
|
|
ArrayUtils.reverse(buffer);
|
| 171 |
|
|
BigInteger data = new BigInteger(buffer);
|
| 172 |
|
|
// buffer = ArrayUtils.subarray(buffer, 0, buffer.length - 2);
|
| 173 |
|
|
// buffer = ArrayUtils.add(buffer, (byte)0);
|
| 174 |
|
|
// ArrayUtils.reverse(buffer);
|
| 175 |
|
|
// BigInteger data = new BigInteger(buffer);
|
| 176 |
|
|
// window.appendText(data.toString(10) + "\n");
|
| 177 |
|
|
}
|
| 178 |
|
|
window.appendText("\n");
|
| 179 |
|
|
}
|
| 180 |
|
|
} catch (Exception e) {
|
| 181 |
|
|
logText = "Failed to read data. (" + e.toString() + ")";
|
| 182 |
|
|
window.text.setForeground(new Color(window.shlPcExponentiator.getDisplay(), 255, 0, 0));
|
| 183 |
|
|
window.appendText(logText + "\n");
|
| 184 |
|
|
}
|
| 185 |
|
|
}
|
| 186 |
|
|
}
|
| 187 |
|
|
|
| 188 |
|
|
public String readData() {
|
| 189 |
|
|
try {
|
| 190 |
|
|
byte[] buffer = new byte[65];
|
| 191 |
|
|
int n = input.read(buffer);
|
| 192 |
|
|
if (n > 0) {
|
| 193 |
|
|
if (n == 1) {
|
| 194 |
|
|
BigInteger command = new BigInteger(new byte[] { 0,
|
| 195 |
|
|
buffer[0] });
|
| 196 |
|
|
String s = "Command = " + command.toString(16);
|
| 197 |
|
|
return s;
|
| 198 |
|
|
} else {
|
| 199 |
|
|
buffer = ArrayUtils.subarray(buffer, 0, buffer.length - 1);
|
| 200 |
|
|
buffer = ArrayUtils.add(buffer, (byte)0);
|
| 201 |
|
|
ArrayUtils.reverse(buffer);
|
| 202 |
|
|
BigInteger data = new BigInteger(buffer);
|
| 203 |
|
|
return data.toString(16);
|
| 204 |
|
|
}
|
| 205 |
|
|
}
|
| 206 |
|
|
} catch (Exception e) {
|
| 207 |
|
|
window.appendText("Failed to read data. (" + e.toString() + ")");
|
| 208 |
|
|
}
|
| 209 |
|
|
return null;
|
| 210 |
|
|
}
|
| 211 |
|
|
|
| 212 |
|
|
//method that can be called to send data
|
| 213 |
|
|
//pre style="font-size: 11px;": open serial port
|
| 214 |
|
|
//post: data sent to the other device
|
| 215 |
|
|
public void writeData(String str)
|
| 216 |
|
|
{
|
| 217 |
|
|
try
|
| 218 |
|
|
{
|
| 219 |
|
|
BigInteger bi = new BigInteger(str, 2);
|
| 220 |
|
|
byte [] b = bi.toByteArray();
|
| 221 |
|
|
if (b.length == 2) {
|
| 222 |
|
|
output.write(b[1]);
|
| 223 |
|
|
} else {
|
| 224 |
|
|
output.write(b);
|
| 225 |
|
|
}
|
| 226 |
|
|
Thread.sleep(50);
|
| 227 |
|
|
//ArrayUtils.reverse(b);
|
| 228 |
|
|
/*String [] data = str.split(" ");
|
| 229 |
|
|
byte [] b = new byte [data.length];
|
| 230 |
|
|
for (int i = 0; i < data.length; i++) {
|
| 231 |
|
|
Byte toSend = Byte.decode(data[i]);
|
| 232 |
|
|
b[i] = toSend;
|
| 233 |
|
|
|
| 234 |
|
|
}
|
| 235 |
|
|
ArrayUtils.reverse(b);
|
| 236 |
|
|
for (int i = 0; i < b.length; i++) {
|
| 237 |
|
|
output.write(b[i]);
|
| 238 |
|
|
Thread.sleep(1);
|
| 239 |
|
|
}*/
|
| 240 |
|
|
//b = Arrays.copyOfRange(b, 1, b.length);
|
| 241 |
|
|
//ArrayUtils.reverse(b);
|
| 242 |
|
|
//for (int i = 0; i < b.length; i++) {
|
| 243 |
|
|
// output.write(b[i]);
|
| 244 |
|
|
// Thread.sleep(10);
|
| 245 |
|
|
//}
|
| 246 |
|
|
//output.flush();
|
| 247 |
|
|
}
|
| 248 |
|
|
catch (Exception e)
|
| 249 |
|
|
{
|
| 250 |
|
|
logText = "Failed to write data. (" + e.toString() + ")";
|
| 251 |
|
|
window.text.setForeground(new Color(window.shlPcExponentiator.getDisplay(), 255, 0, 0));
|
| 252 |
|
|
window.appendText(logText + "\n");
|
| 253 |
|
|
}
|
| 254 |
|
|
}
|
| 255 |
|
|
|
| 256 |
|
|
final public boolean getConnected()
|
| 257 |
|
|
{
|
| 258 |
|
|
return bConnected;
|
| 259 |
|
|
}
|
| 260 |
|
|
|
| 261 |
|
|
public void setConnected(boolean bConnected)
|
| 262 |
|
|
{
|
| 263 |
|
|
this.bConnected = bConnected;
|
| 264 |
|
|
}
|
| 265 |
|
|
|
| 266 |
|
|
|
| 267 |
|
|
}
|