summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLennart Weller <lhw@ring0.de>2012-05-30 11:45:23 +0200
committerLennart Weller <lhw@ring0.de>2012-06-06 09:59:39 +0200
commit11310c037ee90e879ae9c0d1d69a2200030cd085 (patch)
tree3da200c04879136c376a2186ff738644517062db
parent7bc9589466cbb79e42f4deb92f6e91b9d021573c (diff)
downloadserial-barcode-scanner-11310c037ee90e879ae9c0d1d69a2200030cd085.tar.bz2
event based approach
-rw-r--r--.gitignore1
-rw-r--r--device.vala46
-rw-r--r--main.vala10
3 files changed, 52 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore
index 09f0840..488ed8f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
barcode-scanner
+*.swp
diff --git a/device.vala b/device.vala
index b0f16f3..92c7022 100644
--- a/device.vala
+++ b/device.vala
@@ -2,8 +2,11 @@ public class Device {
private Posix.termios newtio;
private Posix.termios restoretio;
public int fd=-1;
+ IOChannel io_read;
public int byterate;
+ public signal void received_barcode(string barcode);
+
public Device(string device, int rate, int bits, int stopbits) {
Posix.speed_t baudrate = Posix.B9600;
@@ -11,7 +14,7 @@ public class Device {
if(fd < 0) {
fd = -1;
- stderr.printf("Could not open device!\n");
+ error("Could not open device!\n");
}
Posix.tcflush(fd, Posix.TCIOFLUSH);
@@ -109,17 +112,57 @@ public class Device {
Posix.tcsetattr(fd, Posix.TCSANOW, newtio);
this.byterate = rate/bits;
+
+ io_read = new IOChannel.unix_new(fd);
+ if(!(io_read.add_watch(IOCondition.IN | IOCondition.HUP, device_read) != 0)) {
+ error("Could not bind IOChannel");
+ }
+ }
+ private bool device_read(IOChannel gio, IOCondition cond) {
+ IOStatus ret;
+ string msg;
+ size_t len, term_char;
+
+ if((cond & IOCondition.HUP) == IOCondition.HUP)
+ stdout.printf("HUP. Do something");
+
+ try {
+ ret = gio.read_line(out msg, out len, out term_char);
+ msg = msg[0:(long)term_char];
+
+ if(msg.has_prefix("USER ") || msg.has_prefix("AMOUNT ")) {
+ if(!check_code39_checksum(msg))
+ received_barcode("SCANNER RETURNED INCORRECT DATA");
+ else {/* remove checksum */
+ msg = msg[0:-2];
+ received_barcode(msg);
+ }
+ }
+
+ }
+ catch(IOChannelError e) {
+ stderr.printf("IOChannel Error: %s", e.message);
+ return false;
+ }
+ catch(ConvertError e) {
+ stderr.printf("Convert Error: %s", e.message);
+ return false;
+ }
+ return true;
}
+#if 0
private ssize_t read(void *buf, size_t count) {
return Posix.read(fd, buf, count);
}
+#endif
private ssize_t write(void *buf, size_t count) {
ssize_t size = Posix.write(fd, buf, count);
return size;
}
+#if 0
public string receive() {
char[] detected = {};
char buf[64];
@@ -157,6 +200,7 @@ public class Device {
}
}
}
+#endif
private bool check_code39_checksum(string data) {
int result = 0;
diff --git a/main.vala b/main.vala
index e584ab2..318653f 100644
--- a/main.vala
+++ b/main.vala
@@ -10,11 +10,13 @@ public static int main(string[] args) {
dev = new Device(args[1], 9600, 8, 1);
db = new Database("shop.db");
- while(true) {
- string message = dev.receive();
- if(interpret((string) message))
+ dev.received_barcode.connect((data) => {
+ if(interpret(data))
dev.blink(10);
- }
+ });
+
+ new MainLoop(null, false).run();
+ return 0;
}
public static bool interpret(string data) {