Browse Source

Listing serial ports in unix working

Thomas Buck 12 years ago
parent
commit
1ddcb6bff2
3 changed files with 35 additions and 14 deletions
  1. 3
    4
      CubeControl/cubeWorker.java
  2. 26
    5
      CubeControl/helper/unixSerial.c
  3. 6
    5
      CubeControl/serialHelper.c

+ 3
- 4
CubeControl/cubeWorker.java View File

@@ -377,7 +377,7 @@ public class cubeWorker {
377 377
 
378 378
 	/**
379 379
 	 * Get the names of all available serial ports.
380
-	 * @return Array of port names. First entry is always "Select serial port..."
380
+	 * @return Array of port names. First entry is "Select serial port..." if no others
381 381
 	 */
382 382
     public String[] getSerialPorts() {
383 383
 		String[] ports = {"Select serial port..."};
@@ -386,10 +386,9 @@ public class cubeWorker {
386 386
 			return ports;
387 387
 		}
388 388
 		StringTokenizer sT = new StringTokenizer(portLines, "\n");
389
-		int size = sT.countTokens() + 1;
389
+		int size = sT.countTokens();
390 390
 		ports = new String[size];
391
-		ports[0] = "Select serial port...";
392
-		for (int i = 1; i < size; i++) {
391
+		for (int i = 0; i < size; i++) {
393 392
 			ports[i] = sT.nextToken();
394 393
 		}
395 394
 		return ports;

+ 26
- 5
CubeControl/helper/unixSerial.c View File

@@ -143,24 +143,45 @@ char** namesInDev(int *siz) {
143 143
 char** getSerialPorts(void) {
144 144
 	int size;
145 145
 	char** files = namesInDev(&size);
146
-	char** fin = NULL;
146
+	char **fin = NULL, **finish = NULL;
147 147
 	int i = 0, j = 0, f, g;
148 148
 
149
-	printf("JNI: Got files in /dev/\n");
149
+	// printf("JNI: Got files in /dev (%d)\n", size);
150 150
 
151 151
 	fin = (char **)malloc(size * sizeof(char *));
152
-	fin[size - 1] = NULL;
152
+	// Has space for all files in dev!
153
+
153 154
 	while (files[i] != NULL) {
155
+		// Filter for SEARCH and if it is a serial port
154 156
 		if (strstr(files[i], SEARCH) != NULL) {
157
+			// We have a match
158
+			// printf("JNI: %s matched %s", files[i], SEARCH);
155 159
 			f = serialOpen(files[i]);
156
-			if (fd != -1) {
160
+			if (f != -1) {
161
+				// printf(" and is a serial port\n");
157 162
 				fin[j++] = files[i];
158 163
 				serialClose();
164
+			} else {
165
+				// printf(" and is not a serial port\n");
166
+				free(files[i]);
159 167
 			}
168
+		} else {
169
+			free(files[i]);
160 170
 		}
161 171
 		i++;
162 172
 	}
163 173
 	free(files);
164 174
 
165
-	return fin;
175
+	// printf("JNI: Found %d serial ports\n", j);
176
+
177
+	// Copy in memory with matching size, NULL at end
178
+	finish = (char **)malloc((j + 1) * sizeof(char *));
179
+	finish[j] = NULL; 
180
+	for (i = 0; i < j; i++) {
181
+		finish[i] = fin[i];
182
+	}
183
+
184
+	free(fin);
185
+
186
+	return finish;
166 187
 }

+ 6
- 5
CubeControl/serialHelper.c View File

@@ -36,26 +36,27 @@ JNIEXPORT jstring JNICALL Java_HelperUtility_getPorts(JNIEnv *env, jclass class)
36 36
 	char *string = NULL;
37 37
 	int length = 0, leng2 = 0, lengthabs = 0;
38 38
 
39
-	printf("JNI: Got serial ports...\n");
39
+	// printf("JNI: Got serial ports...\n");
40 40
 
41 41
 	// Count how much memory we need for string of all ports, with \n in between
42 42
 	while (ports[length] != NULL) {
43
-		printf("JNI: Startin count...\n");
43
+		// printf("JNI: Starting count... (%d at %p)\n", length, (void *)ports[length]);
44
+
44 45
 		while (ports[length][leng2] != '\0') {
45 46
 			leng2++;
46 47
 		}
47
-		printf("JNI: Counted %s\n", ports[length]);
48
+		// printf("JNI: Counted %s\n", ports[length]);
48 49
 		lengthabs += leng2;
49 50
 		leng2 = 0;
50 51
 		length++;
51 52
 	}
52 53
 	length += lengthabs;
53 54
 
54
-	printf("JNI: Counted serial ports...\n");
55
+	// printf("JNI: Counted serial ports...\n");
55 56
 
56 57
 	string = (char *)malloc((length + 1) * sizeof(char));
57 58
 	if (string == NULL) {
58
-		printf("JNI: Not enough memory!\n");
59
+		// printf("JNI: Not enough memory!\n");
59 60
 		return (*env)->NewStringUTF(env, NULL);
60 61
 	}
61 62
 

Loading…
Cancel
Save