From b0565c2479db84eab54539133f4d1dd5bd8fd0f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergiusz=20Baza=C5=84ski?= Date: Tue, 1 May 2012 06:16:34 +0000 Subject: [PATCH] Barcode code. --- terminal/barcode.c | 105 ++++++++++++++++++++++++++++++++++++++++ terminal/barcode.h | 7 +++ terminal/test-barcode.c | 28 +++++++++++ 3 files changed, 140 insertions(+) create mode 100644 terminal/barcode.c create mode 100644 terminal/barcode.h create mode 100644 terminal/test-barcode.c diff --git a/terminal/barcode.c b/terminal/barcode.c new file mode 100644 index 0000000..fe57668 --- /dev/null +++ b/terminal/barcode.c @@ -0,0 +1,105 @@ +#include +#include +#include +#include +#include +#include +#include + +int g_BarcodeFD; + +int barcode_initialize(char *Device) +{ + g_BarcodeFD = open(Device, O_RDWR | O_NONBLOCK); + if (g_BarcodeFD == -1) + { + printf("Failed to open barcode reader TTY.\n"); + return 1; + } + + struct termios Config; + + if (!isatty(g_BarcodeFD)) + { + printf("Barcode TTY is not a TTY!\n"); + return 1; + } + + if (tcgetattr(g_BarcodeFD, &Config)) + { + printf("Could not get barcode TTY config.\n"); + return 1; + } + + memset(&Config, 0, sizeof(Config)); + Config.c_iflag = 0; + Config.c_oflag = 0; + Config.c_lflag = 0; + Config.c_cflag = CS8|CREAD|CLOCAL; + Config.c_cc[VMIN] = 1; + Config.c_cc[VTIME] = 0; + + if (cfsetispeed(&Config, B9600) < 0 || cfsetospeed(&Config, B9600) < 0) + { + printf("Could not set barcode TTY baudrate!\n"); + return 1; + } + + if (tcsetattr(g_BarcodeFD, TCSAFLUSH, &Config) < 0) + { + printf("Could not set barcode TTY attributes!\n"); + return 1; + } + + return 0; +} + +int _async_read_timeout(int FD, char *DataOut, int NumBytes, int Timeout) +{ + fd_set Read, Write, Special; + FD_ZERO(&Read); + FD_ZERO(&Write); + FD_ZERO(&Special); + + FD_SET(FD, &Read); + + struct timeval TTimeout; + memset(&TTimeout, 0, sizeof(TTimeout)); + TTimeout.tv_sec = Timeout; + + int ResultFD = select(FD + 1, &Read, &Write, &Special, &TTimeout); + if (ResultFD == -1) + { + return -1; + } + + return read(FD, DataOut, NumBytes); +} + +int barcode_read(char *BarcodeOut) +{ + int BytesRead = 0; + + // read the first byte with a long timeout + BytesRead += _async_read_timeout(g_BarcodeFD, BarcodeOut + BytesRead, 14, 20); + if (BytesRead == -1) + { + printf("Timeout on reading first barcode bytes.\n"); + return 1; + } + + while (BytesRead < 14) + { + int Result = _async_read_timeout(g_BarcodeFD, BarcodeOut + BytesRead, 14 - BytesRead, 1); + if (Result == -1) + { + printf("Timeout on reading following bytecode bytes.\n"); + return 1; + } + BytesRead += Result; + } + BarcodeOut[13] = 0; + + return 0; +} + diff --git a/terminal/barcode.h b/terminal/barcode.h new file mode 100644 index 0000000..05fa7c5 --- /dev/null +++ b/terminal/barcode.h @@ -0,0 +1,7 @@ +#ifndef __BARCODE_H__ +#define __BARCODE_H__ + +int barcode_initialize(char *Device); +int barcode_read(char *BarcodeOut); + +#endif diff --git a/terminal/test-barcode.c b/terminal/test-barcode.c new file mode 100644 index 0000000..30cc670 --- /dev/null +++ b/terminal/test-barcode.c @@ -0,0 +1,28 @@ +#include + +#include "barcode.h" + +int main() +{ + if (barcode_initialize("/dev/ttyUSB0")) + { + printf("Could not initialize barcode.\n"); + return 1; + } + + char Barcode[14]; + if (barcode_read(Barcode)) + { + printf("Could not read barcode.\n"); + return 1; + } + printf("barcode: %s\n", Barcode); + if (barcode_read(Barcode)) + { + printf("Could not read barcode.\n"); + return 1; + } + printf("barcode: %s\n", Barcode); + + return 0; +}