Browse Source

Probing for cube working...

Thomas Buck 12 years ago
parent
commit
91d328c955
3 changed files with 41 additions and 10 deletions
  1. 13
    7
      CubeControl/Frame.java
  2. 20
    0
      CubeControl/SerialHelper.java
  3. 8
    3
      CubeControl/cubeWorker.java

+ 13
- 7
CubeControl/Frame.java View File

@@ -896,26 +896,32 @@ public class Frame extends JFrame implements ListSelectionListener {
896 896
 		}
897 897
 	}
898 898
 
899
+	// We get detailed error messages from the SerialHelper...
899 900
 	public void upload_ActionPerformed(ActionEvent evt) {
900 901
 		if (jComboBox1.getSelectedItem().equals("Select serial port...")) {
901
-			errorMessage("No serial port selected...");
902
+			// errorMessage("No serial port selected...");
902 903
 		} else {
903
-			if (worker
904
-					.probeCubeConnected((String) jComboBox1.getSelectedItem())) {
904
+			if (worker.probeCubeConnected((String) jComboBox1.getSelectedItem())) {
905 905
 				if (worker.uploadState((String) jComboBox1.getSelectedItem()) != 0) {
906
-					errorMessage("Could not upload data!");
906
+					// errorMessage("Could not upload data!");
907 907
 				}
908 908
 			} else {
909
-				errorMessage("Cube does not respond...");
909
+				// errorMessage("Cube does not respond...");
910 910
 			}
911 911
 		}
912 912
 	}
913 913
 
914 914
 	public void download_ActionPerformed(ActionEvent evt) {
915 915
 		if (jComboBox1.getSelectedItem().equals("Select serial port...")) {
916
-			errorMessage("No serial port selected...");
916
+			// errorMessage("No serial port selected...");
917 917
 		} else {
918
-			worker.downloadState((String) jComboBox1.getSelectedItem());
918
+			if (worker.probeCubeConnected((String) jComboBox1.getSelectedItem())) {
919
+				if (worker.downloadState((String) jComboBox1.getSelectedItem()) != 0) {
920
+					// errorMessage("Could not download data!");
921
+				}
922
+			} else {
923
+				// errorMessage("Cube does not respond...");
924
+			}
919 925
 		}
920 926
 	}
921 927
 

+ 20
- 0
CubeControl/SerialHelper.java View File

@@ -48,12 +48,32 @@ public class SerialHelper {
48 48
 	 */
49 49
 	public SerialHelper(String serialPort, Frame frame) throws Exception {
50 50
 		if (HelperUtility.openPort(serialPort) == false) {
51
+			printErrorMessage("Could not open serial port \"" + serialPort + "\"");
51 52
 			throw new Exception("Could not open serial port \"" + serialPort + "\"");
52 53
 		}
53 54
 		this.frame = frame;
54 55
 	}
55 56
 
56 57
 	/**
58
+	 * Poll to check if the cube is there...
59
+	 * @return TRUE if cube is connected.
60
+	 */
61
+	public boolean probeForCube() {
62
+		short[] data = new short[1];
63
+		data[0] = OK;
64
+		if (!writeData(data)) {
65
+			printErrorMessage("Timeout Probing for Cube");
66
+			return false;
67
+		}
68
+		data = readData(1);
69
+		if ((data == null) || (data[0] != OK)) {
70
+			printErrorMessage("No response from cube!");
71
+			return false;
72
+		}
73
+		return true;
74
+	}
75
+
76
+	/**
57 77
 	 * Recieve all animations saved in cube.
58 78
 	 * @return A cubeWorker populated with the new data or null.
59 79
 	 */

+ 8
- 3
CubeControl/cubeWorker.java View File

@@ -426,7 +426,6 @@ public class cubeWorker {
426 426
 			sh.closeSerialPort();
427 427
 			return ret;
428 428
 		} catch (Exception e) {
429
-			System.out.println("Serial Exception: " + e.getMessage());
430 429
 			return -1;
431 430
 		}
432 431
 	}
@@ -450,7 +449,6 @@ public class cubeWorker {
450 449
 				return 0;
451 450
 			}
452 451
 		} catch (Exception e) {
453
-			System.out.println("Serial Exception: " + e.getMessage());
454 452
 			return -1;
455 453
 		}
456 454
 	}
@@ -470,7 +468,14 @@ public class cubeWorker {
470 468
 	 * @param port Name of serial port
471 469
 	 */
472 470
 	public boolean probeCubeConnected(String port) {
473
-		return false;
471
+		try {
472
+			SerialHelper sh = new SerialHelper(port, parentFrame);
473
+			boolean response = sh.probeForCube();
474
+			sh.closeSerialPort();
475
+			return response;
476
+		} catch (Exception e) {
477
+			return false;
478
+		}
474 479
 	}
475 480
 
476 481
 	/**

Loading…
Cancel
Save