diff options
author | Sebastian Reichel <sre@ring0.de> | 2012-04-28 18:59:33 +0200 |
---|---|---|
committer | Sebastian Reichel <sre@ring0.de> | 2012-04-28 18:59:33 +0200 |
commit | 697d92d00cc020cadd00d2c67ac77f6109eeb16e (patch) | |
tree | c416d54d217b62aeb546c6286b1b4b4634d68fea | |
download | serial-barcode-scanner-697d92d00cc020cadd00d2c67ac77f6109eeb16e.tar.bz2 |
initial code import
-rw-r--r-- | Makefile | 12 | ||||
-rw-r--r-- | main.vala | 30 | ||||
-rw-r--r-- | serial.vala | 111 | ||||
-rw-r--r-- | web.vala | 23 |
4 files changed, 176 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..14f49d1 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +PREFIX=/usr/local + +barcode-scanner: main.vala serial.vala web.vala + valac --output $@ --pkg posix --pkg linux --pkg libsoup-2.4 $^ + +install: barcode-scanner + install -m755 barcode-scanner $(DESTDIR)$(PREFIX)/bin/barcode-scanner + +clean: + rm -f barcode-scanner + +.PHONY: clean install diff --git a/main.vala b/main.vala new file mode 100644 index 0000000..bd71163 --- /dev/null +++ b/main.vala @@ -0,0 +1,30 @@ +public static int main(string[] args) { + if(args.length < 2) { + stderr.printf("%s <device>\n", args[0]); + return 1; + } + + var s = new Serial(args[1], 19200, 7, 1); + + char[] detected = {}; + + while(true) { + uint8 buf[64]; + int size = (int) Posix.read(s.fd, buf, 64); + + for(int i = 0; i < size; i++) + if(buf[i] != '\r' && buf[i] != '\n') { + detected += (char) buf[i]; + } else { + if(detected.length > 0) { + detected += '\0'; + interpret(detected); + } + detected = {}; + } + } +} + +public static void interpret(char[] data) { + stdout.printf("%s\n", (string) data); +} diff --git a/serial.vala b/serial.vala new file mode 100644 index 0000000..480d73c --- /dev/null +++ b/serial.vala @@ -0,0 +1,111 @@ +public class Serial { + private Posix.termios newtio; + private Posix.termios restoretio; + public int fd=-1; + + public signal void new_data(uchar[] data); + + public Serial(string device, int rate, int bits, int stopbits) { + Posix.speed_t baudrate = Posix.B9600; + + fd = Posix.open(device, Posix.O_RDONLY | Posix.O_NONBLOCK); + + if(fd < 0) { + fd = -1; + stderr.printf("Could not open device!\n"); + } + + Posix.tcflush(fd, Posix.TCIOFLUSH); + + Posix.tcgetattr(fd, out restoretio); + + /* apply settings */ + switch(rate) { + case 300: + baudrate = Posix.B300; + break; + case 600: + baudrate = Posix.B600; + break; + case 1200: + baudrate = Posix.B1200; + break; + case 2400: + baudrate = Posix.B2400; + break; + case 4800: + baudrate = Posix.B4800; + break; + case 9600: + baudrate = Posix.B9600; + break; + case 19200: + baudrate = Posix.B19200; + break; + case 38400: + baudrate = Posix.B38400; + break; + case 57600: + baudrate = Posix.B57600; + break; + case 115200: + baudrate = Posix.B115200; + break; + case 230400: + baudrate = Posix.B230400; + break; + default: + /* not supported */ + break; + } + + Posix.cfsetospeed(ref newtio, baudrate); + Posix.cfsetispeed(ref newtio, baudrate); + + switch(bits) { + case 5: + newtio.c_cflag = (newtio.c_cflag & ~Posix.CSIZE) | Posix.CS5; + break; + case 6: + newtio.c_cflag = (newtio.c_cflag & ~Posix.CSIZE) | Posix.CS6; + break; + case 7: + newtio.c_cflag = (newtio.c_cflag & ~Posix.CSIZE) | Posix.CS7; + break; + case 8: + default: + newtio.c_cflag = (newtio.c_cflag & ~Posix.CSIZE) | Posix.CS8; + break; + } + + newtio.c_cflag |= Posix.CLOCAL | Posix.CREAD; + + newtio.c_cflag &= ~(Posix.PARENB | Posix.PARODD); + + /* TODO: parity */ + + newtio.c_cflag &= ~Linux.Termios.CRTSCTS; + + if(stopbits == 2) + newtio.c_cflag |= Posix.CSTOPB; + else + newtio.c_cflag &= ~Posix.CSTOPB; + + newtio.c_iflag = Posix.IGNBRK; + + newtio.c_lflag = 0; + newtio.c_oflag = 0; + + newtio.c_cc[Posix.VTIME]=1; + newtio.c_cc[Posix.VMIN]=1; + + newtio.c_lflag &= ~(Posix.ECHONL|Posix.NOFLSH); + + int mcs=0; + Posix.ioctl(fd, Linux.Termios.TIOCMGET, out mcs); + mcs |= Linux.Termios.TIOCM_RTS; + Posix.ioctl(fd, Linux.Termios.TIOCMSET, out mcs); + + Posix.tcsetattr(fd, Posix.TCSANOW, newtio); + } +} diff --git a/web.vala b/web.vala new file mode 100644 index 0000000..860e607 --- /dev/null +++ b/web.vala @@ -0,0 +1,23 @@ +public class Web { + private Soup.SessionAsync session; + private static string server = "https://shop.kreativitaet-trifft-technik.de"; + + public Web() { + session = new Soup.SessionAsync (); + } + + public void login() { + var message = new Soup.Message ("GET", server+"/login"); + session.send_message (message); + } + + public void logout() { + var message = new Soup.Message ("GET", server+"/logout"); + session.send_message (message); + } + + public void add(string article) { + var message = new Soup.Message ("GET", server+"/buy"); + session.send_message (message); + } +} |