Browse Source

Rsad and write serial port

Thomas Buck 12 years ago
parent
commit
fd267e9146
3 changed files with 102 additions and 3 deletions
  1. 54
    1
      CubeControl/Frame.java
  2. 12
    0
      CubeControl/HelperUtility.java
  3. 36
    2
      CubeControl/serialHelper.c

+ 54
- 1
CubeControl/Frame.java View File

@@ -67,7 +67,7 @@ public class Frame extends JFrame implements ListSelectionListener {
67 67
   private JButton load = new JButton();
68 68
   private JButton save = new JButton();
69 69
   private JButton saveAs = new JButton();
70
-  private JComboBox jComboBox1 = new JComboBox();
70
+  public JComboBox jComboBox1 = new JComboBox();
71 71
   private JButton upload = new JButton();
72 72
   private JButton download = new JButton();
73 73
   private JLabel jLabel4 = new JLabel();
@@ -805,8 +805,43 @@ public class Frame extends JFrame implements ListSelectionListener {
805 805
 		l.resetView();
806 806
 	}
807 807
 
808
+	if (s.startsWith("port ")) {
809
+		s = s.substring(5);
810
+		f.jComboBox1.addItem(s);
811
+		f.jComboBox1.setSelectedItem(s);
812
+	}
813
+
814
+	if (s.startsWith("send ")) {
815
+		s = s.substring(5);
816
+		HelperUtility.openPort(((String)f.jComboBox1.getSelectedItem()) + "\n");
817
+		short[] dat = toShortArray(s);
818
+		HelperUtility.writeData(dat, dat.length);
819
+		HelperUtility.closePort();
820
+	}
821
+
822
+	if (s.startsWith("read ")) {
823
+		s = s.substring(5);
824
+		int leng = Integer.parseInt(s);
825
+		HelperUtility.openPort((String)f.jComboBox1.getSelectedItem());
826
+		short[] data = HelperUtility.readData(leng);
827
+		System.out.println(shortToString(data));
828
+		HelperUtility.closePort();
829
+	}
830
+
831
+	if (s.equals("s") || s.equals("scan")) {
832
+		String[] sPorts = f.worker.getSerialPorts();
833
+		f.jComboBox1.removeAllItems();
834
+		for(int i = 0; i < sPorts.length; i++){
835
+			f.jComboBox1.addItem(sPorts[i]);
836
+		}
837
+	}
838
+
808 839
       if (s.equals("h") || (s.equals("help"))) {
809 840
         System.out.println("Commands:");
841
+		System.out.println("\t'port *name*'\t:\tSet serial port to this");
842
+		System.out.println("\t'send *datas*'\t:\tSend data to serial port");
843
+		System.out.println("\t'read *leng*'\t:\tRead data from serial port");
844
+		System.out.println("\t'scan'   / 's'\t:\tScan for serial ports");
810 845
         System.out.println("\t'on'     / '1'\t:\tToggle all LEDs on");
811 846
         System.out.println("\t'off'    / '0'\t:\tToggle all LEDs off");
812 847
         System.out.println("\t'print'  / 'p'\t:\tPrint 3D Translation Matrix Data");
@@ -819,4 +854,22 @@ public class Frame extends JFrame implements ListSelectionListener {
819 854
     }
820 855
   } while (true);
821 856
   }
857
+
858
+  private static short[] toShortArray(String s) {
859
+	  char[] d = s.toCharArray();
860
+	  System.out.println("Length: " + d.length);
861
+	  short[] r = new short[d.length];
862
+	  for (int i = 0; i < d.length; i++) {
863
+		  r[i] = (short)d[i];
864
+	  }
865
+	  return r;
866
+  }
867
+
868
+  private static String shortToString(short[] d) {
869
+	  String s = "";
870
+	  for (int i = 0; i < d.length; i++) {
871
+		  s += String.valueOf((char)d[i]);
872
+	  }
873
+	  return s;
874
+  }
822 875
 }

+ 12
- 0
CubeControl/HelperUtility.java View File

@@ -42,6 +42,18 @@ public class HelperUtility {
42 42
 	public static native String getPorts();
43 43
 
44 44
 	/**
45
+	 * Open Connection to a port
46
+	 * @return TRUE if successful
47
+	 * @param name Port to open
48
+	 */
49
+	public static native boolean openPort(String name);
50
+
51
+	/**
52
+	 * Close Connection to port
53
+	 */
54
+	public static native void closePort();
55
+
56
+	/**
45 57
 	 * Read data from Cube
46 58
 	 * @param length Amount of data to read
47 59
 	 * @return Data read

+ 36
- 2
CubeControl/serialHelper.c View File

@@ -77,9 +77,43 @@ JNIEXPORT jstring JNICALL Java_HelperUtility_getPorts(JNIEnv *env, jclass class)
77 77
 }
78 78
 
79 79
 JNIEXPORT jshortArray JNICALL Java_HelperUtility_readData(JNIEnv *env, jclass class, jint length) {
80
-	return NULL;
80
+	jshortArray arr = (*env)->NewShortArray(env, length);
81
+	int toBeRead = 0, read;
82
+	char *data = (char *)malloc(length * sizeof(char));
83
+
84
+	while (length > 0) {
85
+		read = serialRead(data + toBeRead, length);
86
+		toBeRead += read;
87
+		length -= read;
88
+	}
89
+
90
+	(*env)->SetShortArrayRegion(env, arr, 0, (*env)->GetArrayLength(env, arr), (jshort *)data);
91
+	return arr;
81 92
 }
82 93
 
83 94
 JNIEXPORT void JNICALL Java_HelperUtility_writeData(JNIEnv *env, jclass class, jshortArray data, jint length) {
84
-	return;
95
+	int written = 0;
96
+	int lastIndex = length;
97
+	char *dat = (char *)malloc(length * sizeof(char));
98
+
99
+	while (length > 0) {
100
+		(*env)->GetShortArrayRegion(env, data, written, length, (jshort *)dat);
101
+		written = serialWrite(dat, length);
102
+		length -= written;
103
+	}
104
+}
105
+
106
+JNIEXPORT void JNICALL Java_HelperUtility_closePort(JNIEnv * env, jclass class) {
107
+	serialClose();
108
+}
109
+
110
+JNIEXPORT jboolean JNICALL Java_HelperUtility_openPort(JNIEnv *env, jclass class, jstring name) {
111
+	jboolean isCopy;
112
+	const char *path = (*env)->GetStringUTFChars(env, name, &isCopy);
113
+	int ret = serialOpen((char *)path);
114
+	if (ret == 0) {
115
+		return JNI_TRUE;
116
+	} else {
117
+		return JNI_FALSE;
118
+	}
85 119
 }

Loading…
Cancel
Save