Browse Source

No high cpu usage while idling in Emulator.

Emulator reads now blocking.
Thomas Buck 12 years ago
parent
commit
9b0b6d5874
3 changed files with 20 additions and 10 deletions
  1. 14
    8
      CubeControl/SerialHelper.java
  2. 5
    1
      HardwareEmulator/main.c
  3. 1
    1
      HardwareEmulator/serial.c

+ 14
- 8
CubeControl/SerialHelper.java View File

@@ -38,6 +38,10 @@ public class SerialHelper {
38 38
 	private final short OK = 0x42;
39 39
 	private final short ERROR = 0x23;
40 40
 
41
+	// Timeout in milliseconds per byte
42
+	private final int transmitTimeout = 1000;
43
+	private final int recieveTimeout = 1000;
44
+
41 45
 	private Frame frame;
42 46
 
43 47
 	/**
@@ -325,9 +329,7 @@ public class SerialHelper {
325 329
 		SerialWriteThread t = new SerialWriteThread(data);
326 330
 		t.start();
327 331
 		while (!t.dataWasSent()) {
328
-			if ((new Date()).getTime() >= (startdate + (data.length * 1000))) {
329
-				// More than (length * 1000) milliseconds went by
330
-				// => 1 second per byte
332
+			if ((new Date()).getTime() >= (startdate + (data.length * transmitTimeout))) {
331 333
 				return false;
332 334
 			}
333 335
 		}
@@ -337,16 +339,20 @@ public class SerialHelper {
337 339
 	private short[] readData(int length) {
338 340
 		// return data read or null if timeout
339 341
 		long startdate = (new Date()).getTime();
340
-
342
+		short[] result;
341 343
 		SerialReadThread t = new SerialReadThread(length);
342 344
 		t.start();
343 345
 		while (!t.dataIsReady()) {
344
-			if ((new Date()).getTime() >= (startdate + (length * 1000))) {
345
-				// More than (length * 1000) milliseconds went by
346
-				// => 1 second per byte
346
+			if ((new Date()).getTime() >= (startdate + (length * recieveTimeout))) {
347 347
 				return null;
348 348
 			}
349 349
 		}
350
-		return t.getSerialData();
350
+		result = t.getSerialData();
351
+		if (result == null) {
352
+			System.out.println("Data read was null!");
353
+		} else if (result.length == 0) {
354
+			System.out.println("No data read!");
355
+		}
356
+		return result;
351 357
 	}
352 358
 }

+ 5
- 1
HardwareEmulator/main.c View File

@@ -24,6 +24,8 @@ void intHandler(int dummy);
24 24
 
25 25
 volatile int keepRunning = 1;
26 26
 
27
+#define printf if(keepRunning)printf
28
+
27 29
 int main(int argc, char *argv[]) {
28 30
 	char c;
29 31
 	ssize_t size;
@@ -449,6 +451,8 @@ int serialWriteTry(char *data, size_t length) {
449 451
 }
450 452
 
451 453
 void intHandler(int dummy) {
452
-	keepRunning = 0;
453 454
 	printf("\nExiting...\n");
455
+	keepRunning = 0;
456
+	serialClose();
457
+	exit(0);
454 458
 }

+ 1
- 1
HardwareEmulator/serial.c View File

@@ -45,7 +45,7 @@ char *serialOpen() {
45 45
 		return NULL;
46 46
 	}
47 47
 
48
-	fcntl(fd, F_SETFL, FNDELAY); // read non blocking
48
+	// fcntl(fd, F_SETFL, FNDELAY); // read non blocking
49 49
 	
50 50
 	tcgetattr(fd, &options);
51 51
 	cfsetispeed(&options, BAUD); // Set speed

Loading…
Cancel
Save