|
@@ -5,89 +5,56 @@ import static java.lang.Thread.sleep;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.io.OutputStream;
|
|
|
-import java.util.HashMap;
|
|
|
|
|
|
import com.fazecast.jSerialComm.SerialPort;
|
|
|
|
|
|
public class Communicator {
|
|
|
|
|
|
- // for containing the ports that will be found
|
|
|
- private SerialPort[] ports;
|
|
|
- // map the port names to CommPortIdentifiers
|
|
|
- private HashMap<String, SerialPort> portMap = new HashMap<>();
|
|
|
- // this is the object that contains the opened port
|
|
|
private SerialPort serialPort = null;
|
|
|
|
|
|
- // input and output streams for sending and receiving data
|
|
|
private InputStream input = null;
|
|
|
private OutputStream output = null;
|
|
|
|
|
|
- /**
|
|
|
- *
|
|
|
- * This method searches for COM ports on the system and saves their
|
|
|
- * Identifier in the hashmap portMap with their name as key.
|
|
|
- *
|
|
|
- */
|
|
|
- public void searchForPorts() {
|
|
|
- ports = SerialPort.getCommPorts();
|
|
|
-
|
|
|
- for (SerialPort p : ports) {
|
|
|
- portMap.put(p.getSystemPortName(), p);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // connect to the selected port in the combo box
|
|
|
- // pre: ports are already found by using the searchForPorts method
|
|
|
- // post: the connected comm port is stored in commPort, otherwise,
|
|
|
- // an exception is generated
|
|
|
-
|
|
|
/**
|
|
|
* This method opens the COM port with port parameters: Baudrate: 4800;
|
|
|
* databits: 8; Stopbit: 1; parity: none;
|
|
|
*
|
|
|
* @param selectedPort
|
|
|
*/
|
|
|
- public void connect(String selectedPort) {
|
|
|
+ public void connect(SerialPort selectedPort) {
|
|
|
try {
|
|
|
- serialPort = portMap.get(selectedPort);
|
|
|
+ serialPort = selectedPort;
|
|
|
|
|
|
+ // Open connection
|
|
|
serialPort.setBaudRate(4800);
|
|
|
serialPort.setNumDataBits(8);
|
|
|
serialPort.setNumStopBits(SerialPort.ONE_STOP_BIT);
|
|
|
serialPort.setParity(SerialPort.NO_PARITY);
|
|
|
serialPort.openPort();
|
|
|
- initIOStream();
|
|
|
|
|
|
- // logging
|
|
|
- System.out.println(selectedPort + " opened successfully.");
|
|
|
+ input = serialPort.getInputStream();
|
|
|
+ output = serialPort.getOutputStream();
|
|
|
+
|
|
|
+ // Test connection (allow a few failures)
|
|
|
+ for (int i = 0; i < 8; i++) {
|
|
|
+ if (ping()) {
|
|
|
+ System.out.println(selectedPort.getSystemPortName() + " opened successfully.");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ System.err.println("Device did not respond correctly to ping.");
|
|
|
} catch (Exception e) {
|
|
|
- System.out.println("Failed to open " + selectedPort + "(" + e.toString() + ")");
|
|
|
+ System.out.println("Failed to open " + selectedPort.getSystemPortName() + ".");
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * This method initializes the serial IO stream, after the init is complete
|
|
|
- * the method sends an initialize command to the GRID+ controller
|
|
|
+ * Sends test data to the device to check availability.
|
|
|
*
|
|
|
- * @return successful Boolean value which indicates whether the method was
|
|
|
- * completed successfully
|
|
|
+ * @return If the ping was successful (the device responded correctly)
|
|
|
*/
|
|
|
- public void initIOStream() {
|
|
|
- input = serialPort.getInputStream();
|
|
|
- output = serialPort.getOutputStream();
|
|
|
-
|
|
|
- knock();
|
|
|
- if (!ping())
|
|
|
- System.err.println("Device did not respond to ping.");
|
|
|
- }
|
|
|
-
|
|
|
- private void knock() {
|
|
|
- for (int i = 0; i < 8; i++) {
|
|
|
- ping();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
private boolean ping() {
|
|
|
byte[] buffer = writeData(new byte[] { (byte) 0xc0 });
|
|
|
return buffer[0] == 0x21;
|
|
@@ -96,7 +63,6 @@ public class Communicator {
|
|
|
/**
|
|
|
* This method disconnects the serial communication by first closing the
|
|
|
* serialPort and then closing the IOStreams
|
|
|
- *
|
|
|
*/
|
|
|
public void disconnect() {
|
|
|
if (serialPort == null || !serialPort.isOpen())
|
|
@@ -107,23 +73,17 @@ public class Communicator {
|
|
|
serialPort.closePort();
|
|
|
System.out.println("Disconnected.");
|
|
|
} catch (IOException e) {
|
|
|
- System.err.println("Failed to close " + serialPort.getSystemPortName() + "(" + e.toString() + ")");
|
|
|
+ System.err.println("Failed to close " + serialPort.getSystemPortName() + ".");
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * This method reads data from the input stream and puts it in a buffer
|
|
|
- * after the data is read the method waits 50 msec to make sure a new
|
|
|
- * command doesn't follow up to quickly This promotes stability in the
|
|
|
- * program.
|
|
|
- *
|
|
|
- * @return
|
|
|
- *
|
|
|
+ * Reads a buffer of data from the device.
|
|
|
*
|
|
|
+ * @return The data buffer
|
|
|
*/
|
|
|
- private byte[] serialRead() throws InterruptedException {
|
|
|
- // This prevents commands from being sent too soon
|
|
|
- sleep(50);
|
|
|
+ private byte[] serialRead() {
|
|
|
try {
|
|
|
if (input.available() > 0) {
|
|
|
byte[] buffer = new byte[input.available()];
|
|
@@ -142,25 +102,21 @@ public class Communicator {
|
|
|
* This method sends a byte array command over the serial communication
|
|
|
* afterwards the method calls the read method
|
|
|
*
|
|
|
- * @param command
|
|
|
- * an array of bytes as a command
|
|
|
+ * @param command an array of bytes as a command
|
|
|
+ * @return The response data from the device
|
|
|
*/
|
|
|
public byte[] writeData(byte[] command) {
|
|
|
try {
|
|
|
+ sleep(50);
|
|
|
output.write(command);
|
|
|
sleep(50);
|
|
|
return serialRead();
|
|
|
- } catch (InterruptedException | IOException e) {
|
|
|
+ } catch (IOException e) {
|
|
|
System.out.println("Failed to write data. (" + e.toString() + ")");
|
|
|
- return new byte[32];
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ // Just ignore
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @return the portMap
|
|
|
- */
|
|
|
- public HashMap<String, SerialPort> getPortMap() {
|
|
|
- return portMap;
|
|
|
+ return new byte[32];
|
|
|
}
|
|
|
|
|
|
public boolean isConnected() {
|