Ver Fonte

Adding code to send commands and receive responses

Baglio Tabifata há 4 anos atrás
pai
commit
5f19590ffc

+ 15 - 0
src/net/hkzlab/dupal/App.java

@@ -1,6 +1,7 @@
 package net.hkzlab.dupal;
 
 import net.hkzlab.dupal.boardio.DuPALManager;
+import net.hkzlab.dupal.dupalproto.DuPALProto;
 
 public class App {
     public static void main(String[] args) throws Exception {
@@ -9,6 +10,20 @@ public class App {
         try {
             System.out.println(dpm.enterRemoteMode());
 
+            for(int idx = 0; idx <= 0xFFFF; idx++) {
+                dpm.writeCommand(DuPALProto.buildWRITECommand(idx));
+                int addr = DuPALProto.handleWRITEResponse(dpm.readResponse());
+
+                if(addr >= 0) {
+                    System.out.println("idx -> " + idx + " written -> " + addr);
+                } else {
+                    System.out.println("Write error!");
+                    break;
+                }
+
+                dpm.writeCommand(DuPALProto.buildREADCommand());
+                System.out.println("Read ... " + DuPALProto.handleREADResponse(dpm.readResponse()));
+            }
         } finally {
             dpm.cleanup();
         }

+ 25 - 0
src/net/hkzlab/dupal/boardio/DuPALManager.java

@@ -2,6 +2,8 @@ package net.hkzlab.dupal.boardio;
 
 import static jssc.SerialPort.*;
 
+import java.nio.charset.StandardCharsets;
+
 import jssc.SerialPort;
 import jssc.SerialPortException;
 import jssc.SerialPortTimeoutException;
@@ -60,6 +62,29 @@ public class DuPALManager {
         }
     }
 
+    public void writeCommand(String command) {
+        if((serport != null) && serport.isOpened()) {
+            try {
+                serport.writeBytes(command.getBytes(StandardCharsets.US_ASCII));
+                try { Thread.sleep(25); } catch(InterruptedException e) {}; // Wait a bit for execution and response
+            } catch (SerialPortException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    public String readResponse() {
+         if((serport != null) && serport.isOpened()) {
+            try {
+                return serport.readString().trim();
+            } catch (SerialPortException e) {
+                e.printStackTrace();
+            }
+        }
+        
+        return null;
+    }
+
     public boolean enterRemoteMode() {
         if (serport != null && serport.isOpened()) {
             resetBoard();

+ 24 - 0
src/net/hkzlab/dupal/dupalproto/DuPALProto.java

@@ -32,6 +32,30 @@ public class DuPALProto {
         return ""+CMD_START+CMD_RESET+CMD_END;
     }
 
+    public static int handleREADResponse(String response) {
+        String[] readRes = parseResponse(response);
+
+        if((readRes == null) || readRes.length != 2 || readRes[0].charAt(0) != CMD_READ) return -1;
+        
+        try {
+            return Integer.parseInt(readRes[1], 16);
+        } catch(NumberFormatException e) {
+            return -1;
+        }
+    }
+
+    public static int handleWRITEResponse(String response) {
+         String[] readRes = parseResponse(response);
+
+        if((readRes == null) || readRes.length != 2 || readRes[0].charAt(0) != CMD_WRITE) return -1;
+        
+        try {
+            return Integer.parseInt(readRes[1], 16);
+        } catch(NumberFormatException e) {
+            return -1;
+        }       
+    }
+
     public static String[] parseResponse(String response) {
         ArrayList<String> respString = new ArrayList<>();