Permalink
Browse files

Added code and tests for Windows Serial.

Refactored test files
  • Loading branch information...
1 parent ea9558e commit d83a98cfd6bcb510511adc0e455c075673239e0e adamo committed Sep 10, 2016
@@ -200,7 +200,7 @@ public void writeBytesToFile(String file, byte[] data) throws FileNotFoundExcept
* @throws IOException
*/
public byte[] readBytesFromFile(String file) throws FileNotFoundException, IOException {
-
+
return Files.readAllBytes(Paths.get(file));
}
@@ -6,9 +6,6 @@
package CASUAL.communicationstools.serial_interface;
-import java.io.InputStream;
-import java.io.OutputStream;
-
/**
*
* @author adamoutler
@@ -17,5 +14,6 @@
public String[] getComPorts();
public boolean checkPortStatus(String port);
public boolean sendDataToPort(String port, String data, String expectedValue);
+ public byte[] sendBinaryData(String port, byte[] data, byte[] expectedValue);
public String sendData(String port, String data);
}
@@ -3,29 +3,28 @@
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
-
package CASUAL.communicationstools.serial_interface;
-
+import CASUAL.Log;
import CASUAL.communicationstools.serial_interface.posix.FileFilter;
import CASUAL.communicationstools.serial_interface.posix.PosixSerialConnectivity;
import java.io.IOException;
-import java.util.logging.Level;
-import java.util.logging.Logger;
/**
*
* @author adamoutler
*/
public class LinuxSerial implements InterfaceSerialPort {
- String pathname="/dev/";
- String fileContains="ttyUSB";
+
+ String pathname = "/dev/";
+ String fileContains = "ttyUSB";
+
@Override
public String[] getComPorts() {
- String[] ports=new FileFilter().selectFilesInPathBasedOnName(pathname, fileContains);
- for (int i=0; i<ports.length; i++){
- if (!ports[i].startsWith(pathname)){
- ports[i]=pathname+ports[i];
+ String[] ports = new FileFilter().selectFilesInPathBasedOnName(pathname, fileContains);
+ for (int i = 0; i < ports.length; i++) {
+ if (!ports[i].startsWith(pathname)) {
+ ports[i] = pathname + ports[i];
}
}
return ports;
@@ -36,31 +35,37 @@ public boolean checkPortStatus(String port) {
try {
return new PosixSerialConnectivity().verifyConnectivity(port);
} catch (IOException | InterruptedException ex) {
- Logger.getLogger(LinuxSerial.class.getName()).log(Level.SEVERE, null, ex);
+ Log.errorHandler(ex);
+
}
return false;
}
@Override
- public boolean sendDataToPort(String port, String data, String expectedValue) {
+ public boolean sendDataToPort(String port, String data, String expectedValue) {
try {
return (new PosixSerialConnectivity().sendCommandToSerial(port, data).contains(expectedValue));
} catch (IOException | InterruptedException ex) {
- Logger.getLogger(LinuxSerial.class.getName()).log(Level.SEVERE, null, ex);
+ Log.errorHandler(ex);
}
return false;
}
@Override
public String sendData(String port, String data) {
try {
- String s=new PosixSerialConnectivity().sendCommandToSerial(port, data);
+ String s = new PosixSerialConnectivity().sendCommandToSerial(port, data);
return s;
} catch (IOException | InterruptedException ex) {
- Logger.getLogger(LinuxSerial.class.getName()).log(Level.SEVERE, null, ex);
+ Log.errorHandler(ex);
}
return null;
}
-
+ @Override
+ public byte[] sendBinaryData(String port, byte[] data, byte[] expectedValue) {
+ throw new UnsupportedOperationException("Linux binary in Windows Serial is not yet supported");
+
+ }
+
}
@@ -5,13 +5,9 @@
*/
package CASUAL.communicationstools.serial_interface;
-import CASUAL.CASUALSessionData;
import CASUAL.language.Command;
import CASUAL.Log;
-
import CASUAL.OSTools;
-import javafx.application.Platform;
-import javafx.scene.control.ProgressBar;
/**
*
@@ -63,6 +59,11 @@ public InterfaceSerialPort getInterface() {
return selectedInterface.getComPorts();
}
+ @Override
+ public byte[] sendBinaryData(String port, byte[] data, byte[] expectedValue) {
+ return selectedInterface.sendBinaryData(port, data, expectedValue);
+ }
+
@Override
public boolean checkPortStatus(String port) {
return selectedInterface.checkPortStatus(port);
@@ -77,86 +78,12 @@ public boolean sendDataToPort(String port, String data, String expectedValue) {
public String sendData(String port, String data) {
return selectedInterface.sendData(port, data);
}
- final class StatusOject{
- boolean complete = true;
- final Object lock=new Object();
- }
- public void sendScriptByLineToSerialPort(final String port, final String data) {
- final StatusOject c=new StatusOject();
- Thread t;
- t = new Thread(new Runnable(){
- public void run(){
- Thread.currentThread().setName("Sending Script by line");
- String[] script = data.split("\n");
- float progressMax = script.length;
- float progress = 0;
- Log.level2Information("Starting");
-
- for (String line : script) {
- if (!line.contains(":::")) continue;
- String[] sendexpect = line.split(":::");
- if (sendexpect.length==3){
- Log.LiveUpdate(sendexpect[2]);
- }
- if (!sendLineUntilMaxRepeatsExceeded(port, sendexpect[0], sendexpect[1])) {
- Log.level2Information("Critical Error while processing information");
- c.complete=false;
- break;
- }
- Log.LiveUpdate("OK");
- progress++;
- }
- Log.level2Information("Job complete. All sequences closed.");
- synchronized (c.lock){
- c.lock.notifyAll();
- }
- }
- });
- t.start();
- }
+ final class StatusOject {
- public String parseUARTCommand(String port, Command cmd){
-
- String line=cmd.get();
- String[] sendExpect = line.split(":::");
-
-
-
- for (int i=0; i<MAXREPEATS;i++){
- if (sendExpect.length==3){
- if (i>0){
- Log.progress("RETRY");
- }
- Log.progress(sendExpect[2]);
- }
- String s;
- s=this.sendData( port, sendExpect[0]);
- cmd.setReturn(false, s);
-
-
- if ((sendExpect.length>1 && cmd.getReturn().contains(sendExpect[1])) ||sendExpect.length==1){
- cmd.setReturn(true,cmd.getReturn());
- break;
- } else {
- cmd.setReturn(false,cmd.getReturn());
- break;
- }
- }
-
-
- if (!cmd.getReturnPassedOrFailed()) {
- Log.level2Information("Critical Error while processing information");
- cmd.set("$ERROR!!!! COULD NOT COMPLETE"+cmd);
- cmd.setReturn(false, "");
- Log.progress("FAIL");
-
- }
-
- return cmd.getReturn();
+ boolean complete = true;
+ final Object lock = new Object();
}
-
-
private boolean sendLineUntilMaxRepeatsExceeded(String port, String data, String expected) {
for (int i = 0; i < MAXREPEATS; i++) {
@@ -168,4 +95,14 @@ private boolean sendLineUntilMaxRepeatsExceeded(String port, String data, String
return false;
}
+ public static byte[] hexStringToByteArray(String s) {
+ s = s.replace(" ", "");
+ int len = s.length();
+ byte[] data = new byte[len / 2];
+ for (int i = 0; i < len; i += 2) {
+ data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ + Character.digit(s.charAt(i + 1), 16));
+ }
+ return data;
+ }
}
Oops, something went wrong.

0 comments on commit d83a98c

Please sign in to comment.