Procházet zdrojové kódy

Fixed windows serial port detection

Thomas Buck před 13 roky
rodič
revize
134118414a
1 změnil soubory, kde provedl 37 přidání a 26 odebrání
  1. 37
    26
      CubeControl/libSerial/winSerial.c

+ 37
- 26
CubeControl/libSerial/winSerial.c Zobrazit soubor

@@ -100,34 +100,45 @@ void serialClose(void) {
100 100
 	hSerial = INVALID_HANDLE_VALUE;
101 101
 }
102 102
 
103
-// Last element has to be NULL
104
-char** getSerialPorts(const char *search) {
105
-	LPTSTR ports;
106
-	DWORD num;
107
-	char **files;
108
-	int i, j = 0, start, k;
109
-
110
-#ifdef UNICODE
111
-	ports = (LPTSTR)malloc(100 * sizeof(WCHAR));
112
-#else
113
-	 ports = (LPTSTR)malloc(100 * sizeof(CHAR));
114
-#endif
115
-
116
-	num = QueryDosDevice(NULL, ports, 100);	
117
-	files = (char **)malloc(num * sizeof(char *));
118
-	
119
-	for (i = 0; i < num; i++) {
120
-		start = j;
121
-		j = 0;
122
-		while (ports[start + j] != '\0') {
123
-			j++;
103
+int availableSerialPorts(char *ports) {
104
+	int i, c = 0;
105
+	char portName[6];
106
+	for (i = 0; i < 20; i++) {
107
+		sprintf(portName, "COM%d", i + 1);
108
+		if (serialOpen(portName) == 0) {
109
+			// success
110
+			ports[i] = 1;
111
+			c++;
112
+			serialClose();
113
+		} else {
114
+			ports[i] = 0;
124 115
 		}
125
-		j++; // \0
126
-		files[i] = (char *)malloc(j * sizeof(char));
127
-		for (k = 0; k < j; k++) {
128
-			files[i][k] = ports[start + k];
116
+	}
117
+	return c;
118
+}
119
+
120
+// Last element has to be NULL
121
+char** getSerialPorts(char *search) {
122
+	int i, num, c = 0, s;
123
+	char ports[20];
124
+	char **portList;
125
+
126
+	num = availableSerialPorts(ports);
127
+	portList = (char **)malloc((num + 1) * sizeof(char *));
128
+	for (i = 0; i < 20; i++) {
129
+		// if ports[n] == 1 -> COMn+1 does exist
130
+		if (ports[i] != 0) {
131
+			if (i < 9) {
132
+				s = 5;
133
+			} else {
134
+				s = 6;
135
+			}
136
+			portList[c] = (char *)malloc(s * sizeof(char));
137
+			sprintf(portList[c], "COM%d", i + 1);
138
+			c++;
129 139
 		}
130 140
 	}
141
+	portList[c] = NULL;
131 142
 
132
-	return files;
143
+	return portList;
133 144
 }

Loading…
Zrušit
Uložit