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
 	private final short OK = 0x42;
38
 	private final short OK = 0x42;
39
 	private final short ERROR = 0x23;
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
 	private Frame frame;
45
 	private Frame frame;
42
 
46
 
43
 	/**
47
 	/**
325
 		SerialWriteThread t = new SerialWriteThread(data);
329
 		SerialWriteThread t = new SerialWriteThread(data);
326
 		t.start();
330
 		t.start();
327
 		while (!t.dataWasSent()) {
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
 				return false;
333
 				return false;
332
 			}
334
 			}
333
 		}
335
 		}
337
 	private short[] readData(int length) {
339
 	private short[] readData(int length) {
338
 		// return data read or null if timeout
340
 		// return data read or null if timeout
339
 		long startdate = (new Date()).getTime();
341
 		long startdate = (new Date()).getTime();
340
-
342
+		short[] result;
341
 		SerialReadThread t = new SerialReadThread(length);
343
 		SerialReadThread t = new SerialReadThread(length);
342
 		t.start();
344
 		t.start();
343
 		while (!t.dataIsReady()) {
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
 				return null;
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
 
24
 
25
 volatile int keepRunning = 1;
25
 volatile int keepRunning = 1;
26
 
26
 
27
+#define printf if(keepRunning)printf
28
+
27
 int main(int argc, char *argv[]) {
29
 int main(int argc, char *argv[]) {
28
 	char c;
30
 	char c;
29
 	ssize_t size;
31
 	ssize_t size;
449
 }
451
 }
450
 
452
 
451
 void intHandler(int dummy) {
453
 void intHandler(int dummy) {
452
-	keepRunning = 0;
453
 	printf("\nExiting...\n");
454
 	printf("\nExiting...\n");
455
+	keepRunning = 0;
456
+	serialClose();
457
+	exit(0);
454
 }
458
 }

+ 1
- 1
HardwareEmulator/serial.c View File

45
 		return NULL;
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
 	tcgetattr(fd, &options);
50
 	tcgetattr(fd, &options);
51
 	cfsetispeed(&options, BAUD); // Set speed
51
 	cfsetispeed(&options, BAUD); // Set speed

Loading…
Cancel
Save