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