ソースを参照

Misc fixes.

Better error output CubeControl. Better debug output cubeEmu. cubeEmu
now supports sending data, but still hangs sometimes or doesn't answer
with OKs, etc.
Thomas Buck 12年前
コミット
79d5b19e65
3個のファイルの変更222行の追加22行の削除
  1. 58
    14
      CubeControl/SerialHelper.java
  2. 154
    2
      HardwareEmulator/main.c
  3. 10
    6
      HardwareEmulator/serial.c

+ 58
- 14
CubeControl/SerialHelper.java ファイルの表示

@@ -62,12 +62,17 @@ public class SerialHelper {
62 62
 		short[] data = new short[1];
63 63
 		data[0] = OK;
64 64
 		if (!writeData(data)) {
65
-			printErrorMessage("Timeout Probing for Cube");
65
+			printErrorMessage("Timeout sending probe for Cube");
66 66
 			return false;
67 67
 		}
68 68
 		data = readData(1);
69 69
 		if ((data == null) || (data[0] != OK)) {
70 70
 			printErrorMessage("No response from cube!");
71
+			if (data == null) {
72
+				System.out.println("Probe was null!");
73
+			} else {
74
+				System.out.println("Probe was " + data[0]);
75
+			}
71 76
 			return false;
72 77
 		}
73 78
 		return true;
@@ -85,26 +90,32 @@ public class SerialHelper {
85 90
 		// Send download command
86 91
 		tmp[0] = 'g';
87 92
 		if (!writeData(tmp)) {
88
-			printErrorMessage("Timout Command");
93
+			printErrorMessage("Timout sending Command");
89 94
 			return null;
90 95
 		}
91 96
 		data = readData(1);
92 97
 		if ((data == null) || (data[0] != OK)) {
93 98
 			printErrorMessage("Response Error");
99
+			if (data == null) {
100
+				System.out.println("Download was null!");
101
+			} else {
102
+				System.out.println("Download was " + data[0]);
103
+			}
94 104
 			return null;
95 105
 		}
96 106
 
97 107
 		// Get animation count
98 108
 		data = readData(1);
99 109
 		if (data == null) {
100
-			printErrorMessage("Response Error");
110
+			printErrorMessage("Response Error.");
111
+			System.out.println("Did not get animation count!");
101 112
 			return null;
102 113
 		} else {
103 114
 			animationCount = data[0];
104 115
 		}
105 116
 		tmp[0] = OK;
106 117
 		if (!writeData(tmp)) {
107
-			printErrorMessage("Timout Response");
118
+			printErrorMessage("Timout acknowledging animation count!");
108 119
 			return null;
109 120
 		}
110 121
 
@@ -118,13 +129,14 @@ public class SerialHelper {
118 129
 			data = readData(1);
119 130
 			if (data == null) {
120 131
 				printErrorMessage("Response Error");
132
+				System.out.println("Did not get frame count!");
121 133
 				return null;
122 134
 			} else {
123 135
 				frameCount = data[0];
124 136
 			}
125 137
 			tmp[0] = OK;
126 138
 			if (!writeData(tmp)) {
127
-				printErrorMessage("Timout Response");
139
+				printErrorMessage("Timout sending response Frame Count.");
128 140
 				return null;
129 141
 			}
130 142
 
@@ -136,13 +148,14 @@ public class SerialHelper {
136 148
 				data = readData(1);
137 149
 				if (data == null) {
138 150
 					printErrorMessage("Response Error");
151
+					System.out.println("Did not get frame duration!");
139 152
 					return null;
140 153
 				} else {
141 154
 					currentFrame.setTime(data[0]);
142 155
 				}
143 156
 				tmp[0] = OK;
144 157
 				if (!writeData(tmp)) {
145
-					printErrorMessage("Timout Response");
158
+					printErrorMessage("Timout sending response Frame Duration");
146 159
 					return null;
147 160
 				}
148 161
 
@@ -150,13 +163,14 @@ public class SerialHelper {
150 163
 				data = readData(64);
151 164
 				if (data == null) {
152 165
 					printErrorMessage("Response Error");
166
+					System.out.println("Did not get frame data!");
153 167
 					return null;
154 168
 				} else {
155 169
 					currentFrame.setData(data);
156 170
 				}
157 171
 				tmp[0] = OK;
158 172
 				if (!writeData(tmp)) {
159
-					printErrorMessage("Timout Response");
173
+					printErrorMessage("Timout sending response Frame Data");
160 174
 					return null;
161 175
 				}
162 176
 
@@ -182,24 +196,34 @@ public class SerialHelper {
182 196
 		// Send upload command
183 197
 		tmp[0] = 's';
184 198
 		if (!writeData(tmp)) {
185
-			printErrorMessage("Timout Command");
199
+			printErrorMessage("Timout sending Command");
186 200
 			return -1;
187 201
 		}
188 202
 		data = readData(1);
189 203
 		if ((data == null) || (data[0] != OK)) {
190
-			printErrorMessage("Response Error");
204
+			printErrorMessage("Response Error Command");
205
+			if (data == null) {
206
+				System.out.println("Response Command was null!");
207
+			} else {
208
+				System.out.println("Response Command was " + data[0]);
209
+			}
191 210
 			return -1;
192 211
 		}
193 212
 
194 213
 		// Send animation count
195 214
 		tmp[0] = (short)worker.size();
196 215
 		if (!writeData(tmp)) {
197
-			printErrorMessage("Timeout numOfAnimations");
216
+			printErrorMessage("Timeout sending numOfAnimations");
198 217
 			return -1;
199 218
 		}
200 219
 		data = readData(1);
201 220
 		if ((data == null) || (data[0] != OK)) {
202 221
 			printErrorMessage("Response Error");
222
+			if (data == null) {
223
+				System.out.println("Response Count was null!");
224
+			} else {
225
+				System.out.println("Response Count was " + data[0]);
226
+			}
203 227
 			return -1;
204 228
 		}
205 229
 
@@ -208,12 +232,17 @@ public class SerialHelper {
208 232
 			// Send frame count
209 233
 			tmp[0] = (short)worker.getAnimation(a).size();
210 234
 			if (!writeData(tmp)) {
211
-				printErrorMessage("Timeout numOfFrames");
235
+				printErrorMessage("Timeout sending numOfFrames");
212 236
 				return -1;
213 237
 			}
214 238
 			data = readData(1);
215 239
 			if ((data == null) || (data[0] != OK)) {
216 240
 				printErrorMessage("Response Error");
241
+				if (data == null) {
242
+				System.out.println("Frame Count was null!");
243
+			} else {
244
+				System.out.println("Frame Count was " + data[0]);
245
+			}
217 246
 				return -1;
218 247
 			}
219 248
 
@@ -222,23 +251,33 @@ public class SerialHelper {
222 251
 				// Frame duration
223 252
 				tmp[0] = worker.getAnimation(a).getFrame(f).getTime();
224 253
 				if (!writeData(tmp)) {
225
-					printErrorMessage("Timeout Frame duration");
254
+					printErrorMessage("Timeout sending Frame duration");
226 255
 					return -1;
227 256
 				}
228 257
 				data = readData(1);
229 258
 				if ((data == null) || (data[0] != OK)) {
230 259
 					printErrorMessage("Response Error");
260
+					if (data == null) {
261
+						System.out.println("Duration was null!");
262
+					} else {
263
+						System.out.println("Duration was " + data[0]);
264
+					}
231 265
 					return -1;
232 266
 				}
233 267
 
234 268
 				// Frame data
235 269
 				if (!writeData(worker.getAnimation(a).getFrame(f).getData())) {
236
-					printErrorMessage("Timeout Frame");
270
+					printErrorMessage("Timeout sending Frame");
237 271
 					return -1;
238 272
 				}
239 273
 				data = readData(1);
240 274
 				if ((data == null) || (data[0] != OK)) {
241 275
 					printErrorMessage("Response Error");
276
+					if (data == null) {
277
+						System.out.println("Datawas null!");
278
+					} else {
279
+						System.out.println("Data was " + data[0]);
280
+					}
242 281
 					return -1;
243 282
 				}
244 283
 			}
@@ -251,12 +290,17 @@ public class SerialHelper {
251 290
 		tmp[2] = OK;
252 291
 		tmp[3] = OK;
253 292
 		if (!writeData(tmp)) {
254
-			printErrorMessage("Timeout Finish");
293
+			printErrorMessage("Timeout sending Finish");
255 294
 			return -1;
256 295
 		}
257 296
 		data = readData(1);
258 297
 		if ((data == null) || (data[0] != OK)) {
259 298
 			printErrorMessage("Response Error");
299
+			if (data == null) {
300
+				System.out.println("Finish was null!");
301
+			} else {
302
+				System.out.println("Finish was " + data[0]);
303
+			}
260 304
 			return -1;
261 305
 		}
262 306
 		return 0;

+ 154
- 2
HardwareEmulator/main.c ファイルの表示

@@ -53,9 +53,11 @@ int main(int argc, char *argv[]) {
53 53
 						printf("Could not write to pseudo terminal\n");
54 54
 						return -1;
55 55
 					}
56
+					printf("OK!\n");
56 57
 					break;
57 58
 
58 59
 				case 'h': case 'H':
60
+					printf("Help\n");
59 61
 					if (serialWriteString("(d)elete, (g)et anims, (s)et anims, (v)ersion\n")) {
60 62
 						printf("Could not write to pseudo terminal\n");
61 63
 						return -1;
@@ -63,6 +65,7 @@ int main(int argc, char *argv[]) {
63 65
 					break;
64 66
 
65 67
 				case 'v': case 'V':
68
+					printf("Version\n");
66 69
 					if (serialWriteString(VERSION)) {
67 70
 						printf("Could not write to pseudo terminal\n");
68 71
 						return -1;
@@ -70,27 +73,34 @@ int main(int argc, char *argv[]) {
70 73
 					break;
71 74
 
72 75
 				case 's': case 'S':
76
+					printf("Recieving... ");
73 77
 					if (recieveFrames()) {
74 78
 						printf("Error while recieving frames!\n");
75 79
 						return -1;
76 80
 					}
81
+					printf("Done!\n");
77 82
 					break;
78 83
 
79 84
 				case 'g': case 'G':
85
+					printf("Sending... ");
80 86
 					if (sendFrames()) {
81 87
 						printf("Error while sending frames!\n");
82 88
 						return -1;
83 89
 					}
90
+					printf("Done!\n");
84 91
 					break;
85 92
 
86 93
 				case 'd': case 'D':
94
+					printf("Deleting... ");
87 95
 					if (deleteFrames()) {
88 96
 						printf("Error while deleting frames!\n");
89 97
 						return -1;
90 98
 					}
99
+					printf("Done!\n");
91 100
 					break;
92 101
 
93 102
 				default:
103
+					printf("Unknown. Error!\n");
94 104
 					c = ERROR;
95 105
 					if (serialWriteTry(&c, 1)) {
96 106
 						printf("Could not write to pseudo terminal\n");
@@ -107,15 +117,157 @@ int main(int argc, char *argv[]) {
107 117
 }
108 118
 
109 119
 int sendFrames() {
110
-
120
+	char c = ERROR;
121
+	printf("Not implemented!\n");
122
+	if (serialWriteTry(&c, 1)) {
123
+		printf("Could not write to pseudo terminal\n");
124
+	}
125
+	return -1;
111 126
 }
112 127
 
113 128
 int recieveFrames() {
129
+	char c;
130
+	ssize_t size;
131
+	int animCount, a, frameCount, f, d;
132
+	char data[65];
133
+
134
+	clearMemory();
135
+
136
+	// First send acknowledge
137
+	c = OK;
138
+	if (serialWriteTry(&c, 1)) {
139
+		printf("Could not write to pseudo terminal\n");
140
+		return -1;
141
+	}
142
+
143
+	printf("AnimationCount");
144
+	while (1) {
145
+		size = serialRead(&c, 1);
146
+		if (size == 1) {
147
+			break;
148
+		} else if (size == -1) {
149
+			printf("Could not read from psuedo terminal!\n");
150
+			return -1;
151
+		}
152
+	}
153
+	printf(" = %d\n", c);
154
+	animCount = c;
155
+
156
+	c = OK;
157
+	if (serialWriteTry(&c, 1)) {
158
+		printf("Could not write to pseudo terminal\n");
159
+		return -1;
160
+	}
161
+
162
+	for (a = 0; a < animCount; a++) {
163
+		printf("FrameCount");
164
+		while (1) {
165
+			size = serialRead(&c, 1);
166
+			if (size == 1) {
167
+				break;
168
+			} else if (size == -1) {
169
+				printf("Could not read from psuedo terminal!\n");
170
+				return -1;
171
+			}
172
+		}
173
+		printf(" = %d\n", c);
174
+		frameCount = c;
175
+
176
+		c = OK;
177
+		if (serialWriteTry(&c, 1)) {
178
+			printf("Could not write to pseudo terminal\n");
179
+			return -1;
180
+		}
181
+
182
+		for (f = 0; f < frameCount; f++) {
183
+			printf("Duration");
184
+			while (1) {
185
+				size = serialRead(&c, 1);
186
+				if (size == 1) {
187
+					break;
188
+				} else if (size == -1) {
189
+					printf("Could not read from psuedo terminal!\n");
190
+					return -1;
191
+				}
192
+			}
193
+			printf(" = %d\n", c);
194
+			data[64] = c;
195
+
196
+			printf("Data...");
197
+			for (d = 0; d < 64; d++) {
198
+				while (1) {
199
+					size = serialRead(&c, 1);
200
+					if (size == 1) {
201
+						break;
202
+					} else if (size == -1) {
203
+						printf("Could not read from psuedo terminal!\n");
204
+						return -1;
205
+					}
206
+				}
207
+				data[d] = c;
208
+			}
209
+			printf(" Done!\n");
210
+
211
+			addFrame(data);
212
+		}
213
+	}
214
+
215
+	while (1) {
216
+		size = serialRead(&c, 1);
217
+		if (size == 1) {
218
+			break;
219
+		} else if (size == -1) {
220
+			printf("Could not read from psuedo terminal!\n");
221
+			return -1;
222
+		}
223
+	}
224
+	while (1) {
225
+		size = serialRead(&c, 1);
226
+		if (size == 1) {
227
+			break;
228
+		} else if (size == -1) {
229
+			printf("Could not read from psuedo terminal!\n");
230
+			return -1;
231
+		}
232
+	}
233
+	while (1) {
234
+		size = serialRead(&c, 1);
235
+		if (size == 1) {
236
+			break;
237
+		} else if (size == -1) {
238
+			printf("Could not read from psuedo terminal!\n");
239
+			return -1;
240
+		}
241
+	}
242
+	while (1) {
243
+		size = serialRead(&c, 1);
244
+		if (size == 1) {
245
+			break;
246
+		} else if (size == -1) {
247
+			printf("Could not read from psuedo terminal!\n");
248
+			return -1;
249
+		}
250
+	}
114 251
 
252
+	printf("Got 4 OKs!\n");
253
+
254
+	c = OK;
255
+	if (serialWriteTry(&c, 1)) {
256
+		printf("Could not write to pseudo terminal\n");
257
+		return -1;
258
+	}
259
+
260
+	return 0;
115 261
 }
116 262
 
117 263
 int deleteFrames() {
118
-
264
+	char c = OK;
265
+	clearMemory();
266
+	if (serialWriteTry(&c, 1)) {
267
+		printf("Could not write to pseudo terminal\n");
268
+		return -1;
269
+	}
270
+	return 0;
119 271
 }
120 272
 
121 273
 int serialWriteString(char *s) {

+ 10
- 6
HardwareEmulator/serial.c ファイルの表示

@@ -1,6 +1,8 @@
1 1
 /*
2
- * POSIX compatible serial port library
3
- * Uses 8 databits, no parity, 1 stop bit, no handshaking
2
+ * POSIX compatible
3
+ * Compatible API to libSerial (SerialHelper)
4
+ * Opens a pseudoterminal, lets you write to it,
5
+ * gives name of slave side.
4 6
  * By: Thomas Buck <taucher.bodensee@gmail.com>
5 7
  * Visit: www.xythobuz.org
6 8
  */
@@ -13,6 +15,7 @@
13 15
 #include <termios.h>
14 16
 #include <dirent.h>
15 17
 #include <errno.h>
18
+#include <string.h>
16 19
 
17 20
 #include "serial.h"
18 21
 
@@ -27,22 +30,23 @@ char *serialOpen() {
27 30
 		close(fd);
28 31
 	}
29 32
 
30
-	fd = posix_openpt(O_RDWR | O_NOCTTY | O_NDELAY);
33
+	fd = posix_openpt(O_RDWR | O_NOCTTY);
31 34
 	if (fd == -1) {
32 35
 		return NULL;
33 36
 	}
34 37
 
35 38
 	// Set rigths
36
-	if (grantpt(fd) != 0) {
39
+	if (grantpt(fd) == -1) {
37 40
 		return NULL;
38 41
 	}
39 42
 
40 43
 	// Unlock slave
41
-	if (unlockpt(fd) != 0) {
44
+	if (unlockpt(fd) == -1) {
42 45
 		return NULL;
43 46
 	}
47
+
48
+	fcntl(fd, F_SETFL, FNDELAY); // read non blocking
44 49
 	
45
-	fcntl(fd, F_SETFL, FNDELAY); // read() isn't blocking'
46 50
 	tcgetattr(fd, &options);
47 51
 	cfsetispeed(&options, BAUD); // Set speed
48 52
 	cfsetospeed(&options, BAUD);

読み込み中…
キャンセル
保存