Quellcode durchsuchen

Listing serial ports in unix working

Thomas Buck vor 12 Jahren
Ursprung
Commit
1ddcb6bff2
3 geänderte Dateien mit 35 neuen und 14 gelöschten Zeilen
  1. 3
    4
      CubeControl/cubeWorker.java
  2. 26
    5
      CubeControl/helper/unixSerial.c
  3. 6
    5
      CubeControl/serialHelper.c

+ 3
- 4
CubeControl/cubeWorker.java Datei anzeigen

377
 
377
 
378
 	/**
378
 	/**
379
 	 * Get the names of all available serial ports.
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
     public String[] getSerialPorts() {
382
     public String[] getSerialPorts() {
383
 		String[] ports = {"Select serial port..."};
383
 		String[] ports = {"Select serial port..."};
386
 			return ports;
386
 			return ports;
387
 		}
387
 		}
388
 		StringTokenizer sT = new StringTokenizer(portLines, "\n");
388
 		StringTokenizer sT = new StringTokenizer(portLines, "\n");
389
-		int size = sT.countTokens() + 1;
389
+		int size = sT.countTokens();
390
 		ports = new String[size];
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
 			ports[i] = sT.nextToken();
392
 			ports[i] = sT.nextToken();
394
 		}
393
 		}
395
 		return ports;
394
 		return ports;

+ 26
- 5
CubeControl/helper/unixSerial.c Datei anzeigen

143
 char** getSerialPorts(void) {
143
 char** getSerialPorts(void) {
144
 	int size;
144
 	int size;
145
 	char** files = namesInDev(&size);
145
 	char** files = namesInDev(&size);
146
-	char** fin = NULL;
146
+	char **fin = NULL, **finish = NULL;
147
 	int i = 0, j = 0, f, g;
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
 	fin = (char **)malloc(size * sizeof(char *));
151
 	fin = (char **)malloc(size * sizeof(char *));
152
-	fin[size - 1] = NULL;
152
+	// Has space for all files in dev!
153
+
153
 	while (files[i] != NULL) {
154
 	while (files[i] != NULL) {
155
+		// Filter for SEARCH and if it is a serial port
154
 		if (strstr(files[i], SEARCH) != NULL) {
156
 		if (strstr(files[i], SEARCH) != NULL) {
157
+			// We have a match
158
+			// printf("JNI: %s matched %s", files[i], SEARCH);
155
 			f = serialOpen(files[i]);
159
 			f = serialOpen(files[i]);
156
-			if (fd != -1) {
160
+			if (f != -1) {
161
+				// printf(" and is a serial port\n");
157
 				fin[j++] = files[i];
162
 				fin[j++] = files[i];
158
 				serialClose();
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
 		i++;
171
 		i++;
162
 	}
172
 	}
163
 	free(files);
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 Datei anzeigen

36
 	char *string = NULL;
36
 	char *string = NULL;
37
 	int length = 0, leng2 = 0, lengthabs = 0;
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
 	// Count how much memory we need for string of all ports, with \n in between
41
 	// Count how much memory we need for string of all ports, with \n in between
42
 	while (ports[length] != NULL) {
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
 		while (ports[length][leng2] != '\0') {
45
 		while (ports[length][leng2] != '\0') {
45
 			leng2++;
46
 			leng2++;
46
 		}
47
 		}
47
-		printf("JNI: Counted %s\n", ports[length]);
48
+		// printf("JNI: Counted %s\n", ports[length]);
48
 		lengthabs += leng2;
49
 		lengthabs += leng2;
49
 		leng2 = 0;
50
 		leng2 = 0;
50
 		length++;
51
 		length++;
51
 	}
52
 	}
52
 	length += lengthabs;
53
 	length += lengthabs;
53
 
54
 
54
-	printf("JNI: Counted serial ports...\n");
55
+	// printf("JNI: Counted serial ports...\n");
55
 
56
 
56
 	string = (char *)malloc((length + 1) * sizeof(char));
57
 	string = (char *)malloc((length + 1) * sizeof(char));
57
 	if (string == NULL) {
58
 	if (string == NULL) {
58
-		printf("JNI: Not enough memory!\n");
59
+		// printf("JNI: Not enough memory!\n");
59
 		return (*env)->NewStringUTF(env, NULL);
60
 		return (*env)->NewStringUTF(env, NULL);
60
 	}
61
 	}
61
 
62
 

Laden…
Abbrechen
Speichern