Browse Source

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 years ago
parent
commit
79d5b19e65
3 changed files with 222 additions and 22 deletions
  1. 58
    14
      CubeControl/SerialHelper.java
  2. 154
    2
      HardwareEmulator/main.c
  3. 10
    6
      HardwareEmulator/serial.c

+ 58
- 14
CubeControl/SerialHelper.java View File

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

+ 154
- 2
HardwareEmulator/main.c View File

53
 						printf("Could not write to pseudo terminal\n");
53
 						printf("Could not write to pseudo terminal\n");
54
 						return -1;
54
 						return -1;
55
 					}
55
 					}
56
+					printf("OK!\n");
56
 					break;
57
 					break;
57
 
58
 
58
 				case 'h': case 'H':
59
 				case 'h': case 'H':
60
+					printf("Help\n");
59
 					if (serialWriteString("(d)elete, (g)et anims, (s)et anims, (v)ersion\n")) {
61
 					if (serialWriteString("(d)elete, (g)et anims, (s)et anims, (v)ersion\n")) {
60
 						printf("Could not write to pseudo terminal\n");
62
 						printf("Could not write to pseudo terminal\n");
61
 						return -1;
63
 						return -1;
63
 					break;
65
 					break;
64
 
66
 
65
 				case 'v': case 'V':
67
 				case 'v': case 'V':
68
+					printf("Version\n");
66
 					if (serialWriteString(VERSION)) {
69
 					if (serialWriteString(VERSION)) {
67
 						printf("Could not write to pseudo terminal\n");
70
 						printf("Could not write to pseudo terminal\n");
68
 						return -1;
71
 						return -1;
70
 					break;
73
 					break;
71
 
74
 
72
 				case 's': case 'S':
75
 				case 's': case 'S':
76
+					printf("Recieving... ");
73
 					if (recieveFrames()) {
77
 					if (recieveFrames()) {
74
 						printf("Error while recieving frames!\n");
78
 						printf("Error while recieving frames!\n");
75
 						return -1;
79
 						return -1;
76
 					}
80
 					}
81
+					printf("Done!\n");
77
 					break;
82
 					break;
78
 
83
 
79
 				case 'g': case 'G':
84
 				case 'g': case 'G':
85
+					printf("Sending... ");
80
 					if (sendFrames()) {
86
 					if (sendFrames()) {
81
 						printf("Error while sending frames!\n");
87
 						printf("Error while sending frames!\n");
82
 						return -1;
88
 						return -1;
83
 					}
89
 					}
90
+					printf("Done!\n");
84
 					break;
91
 					break;
85
 
92
 
86
 				case 'd': case 'D':
93
 				case 'd': case 'D':
94
+					printf("Deleting... ");
87
 					if (deleteFrames()) {
95
 					if (deleteFrames()) {
88
 						printf("Error while deleting frames!\n");
96
 						printf("Error while deleting frames!\n");
89
 						return -1;
97
 						return -1;
90
 					}
98
 					}
99
+					printf("Done!\n");
91
 					break;
100
 					break;
92
 
101
 
93
 				default:
102
 				default:
103
+					printf("Unknown. Error!\n");
94
 					c = ERROR;
104
 					c = ERROR;
95
 					if (serialWriteTry(&c, 1)) {
105
 					if (serialWriteTry(&c, 1)) {
96
 						printf("Could not write to pseudo terminal\n");
106
 						printf("Could not write to pseudo terminal\n");
107
 }
117
 }
108
 
118
 
109
 int sendFrames() {
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
 int recieveFrames() {
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
 int deleteFrames() {
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
 int serialWriteString(char *s) {
273
 int serialWriteString(char *s) {

+ 10
- 6
HardwareEmulator/serial.c View File

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
  * By: Thomas Buck <taucher.bodensee@gmail.com>
6
  * By: Thomas Buck <taucher.bodensee@gmail.com>
5
  * Visit: www.xythobuz.org
7
  * Visit: www.xythobuz.org
6
  */
8
  */
13
 #include <termios.h>
15
 #include <termios.h>
14
 #include <dirent.h>
16
 #include <dirent.h>
15
 #include <errno.h>
17
 #include <errno.h>
18
+#include <string.h>
16
 
19
 
17
 #include "serial.h"
20
 #include "serial.h"
18
 
21
 
27
 		close(fd);
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
 	if (fd == -1) {
34
 	if (fd == -1) {
32
 		return NULL;
35
 		return NULL;
33
 	}
36
 	}
34
 
37
 
35
 	// Set rigths
38
 	// Set rigths
36
-	if (grantpt(fd) != 0) {
39
+	if (grantpt(fd) == -1) {
37
 		return NULL;
40
 		return NULL;
38
 	}
41
 	}
39
 
42
 
40
 	// Unlock slave
43
 	// Unlock slave
41
-	if (unlockpt(fd) != 0) {
44
+	if (unlockpt(fd) == -1) {
42
 		return NULL;
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
 	tcgetattr(fd, &options);
50
 	tcgetattr(fd, &options);
47
 	cfsetispeed(&options, BAUD); // Set speed
51
 	cfsetispeed(&options, BAUD); // Set speed
48
 	cfsetospeed(&options, BAUD);
52
 	cfsetospeed(&options, BAUD);

Loading…
Cancel
Save