From: John Hodge Date: Sat, 4 Dec 2010 12:50:27 +0000 (+0800) Subject: Fixing serial handling X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=dfa4eb21fc9e7cc2bfa912581d38aaee04f3b990;p=tpg%2Fopendispense2.git Fixing serial handling --- diff --git a/RunServerTest b/RunServerTest index 27d943b..9966176 100755 --- a/RunServerTest +++ b/RunServerTest @@ -1,6 +1,7 @@ #!/bin/sh -ARGS="--itemsfile items.cfg -p 11020 --cokeport /dev/ttyUSB0" +ARGS="--itemsfile items.cfg -p 11020" +ARGS=$ARGS" --cokeport /dev/ttyUSB0" if [ "x$1" != "x" ]; then LD_LIBRARY_PATH=. gdb --args ./dispsrv $ARGS diff --git a/src/server/common.h b/src/server/common.h index 932c006..9f2c3b3 100644 --- a/src/server/common.h +++ b/src/server/common.h @@ -71,6 +71,7 @@ extern int giDebugLevel; // --- Helpers -- extern void CompileRegex(regex_t *Regex, const char *Pattern, int Flags); extern int RunRegex(regex_t *regex, const char *string, int nMatches, regmatch_t *matches, const char *errorMessage); +extern void InitSerial(int FD, int BaudRate); // --- Dispense --- extern int DispenseItem(int User, tItem *Item); diff --git a/src/server/handler_coke.c b/src/server/handler_coke.c index ce6ff1f..53cbd0f 100644 --- a/src/server/handler_coke.c +++ b/src/server/handler_coke.c @@ -37,10 +37,13 @@ regex_t gCoke_StatusRegex; int Coke_InitHandler() { printf("connecting to coke machine...\n"); - giCoke_SerialFD = open(gsCoke_SerialPort, O_RDWR); + giCoke_SerialFD = open(gsCoke_SerialPort, O_RDWR | O_NOCTTY | O_NONBLOCK); if( giCoke_SerialFD == -1 ) { fprintf(stderr, "ERROR: Unable to open coke serial port ('%s')\n", gsCoke_SerialPort); } + + InitSerial(giCoke_SerialFD, 9600); + CompileRegex(&gCoke_StatusRegex, "^slot\\s+(\\d)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED); return 0; } diff --git a/src/server/handler_snack.c b/src/server/handler_snack.c index 50a0d53..c1100cb 100644 --- a/src/server/handler_snack.c +++ b/src/server/handler_snack.c @@ -37,7 +37,13 @@ regex_t gSnack_ResponseRegex; // == CODE === int Snack_InitHandler() { - giSnack_SerialFD = open(gsSnack_SerialPort, O_RDWR); + giSnack_SerialFD = open(gsSnack_SerialPort, O_RDWR | O_NOCTTY | O_NONBLOCK); + if( giSnack_SerialFD == -1 ) { + fprintf(stderr, "ERROR: Unable to open snack serial port ('%s')\n", gsSnack_SerialPort); + } + + InitSerial(giSnack_SerialFD, 9600); + regcomp(&gSnack_ResponseRegex, "^(\\d\\d\\d)(.*)$", REG_EXTENDED); return 0; } diff --git a/src/server/main.c b/src/server/main.c index ac6dd27..a903f75 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -12,6 +12,7 @@ #include #include #include "common.h" +#include // === IMPORTS === extern void Init_Cokebank(const char *Argument); // cokebank.c @@ -118,4 +119,22 @@ void CompileRegex(regex_t *regex, const char *pattern, int flags) } } +// Serial helper +void InitSerial(int FD, int BaudRate) +{ + struct termios info; + int baud; + + switch(BaudRate) + { + case 9600: baud = B9600; break; + default: return ; + } + + cfmakeraw(&info); // Sets 8N1 + cfsetspeed(&info, baud); + + tcsetattr(FD, TCSANOW, &info); +} +