// --- 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);
+extern int InitSerial(const char *Path, int BaudRate);
// --- Dispense ---
extern int DispenseItem(int User, tItem *Item);
int Coke_InitHandler()
{
printf("connecting to coke machine...\n");
- giCoke_SerialFD = open(gsCoke_SerialPort, O_RDWR | O_NOCTTY | O_NONBLOCK);
+
+ giCoke_SerialFD = InitSerial(gsCoke_SerialPort, 9600);
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;
}
sprintf(tmp, "s%i\n", Item);
write(giCoke_SerialFD, tmp, 2);
+ // Wait a little
+ sleep(250);
+
// Read the response
tmp[0] = '\0';
ret = read(giCoke_SerialFD, tmp, sizeof(tmp)-1);
// Dispense
sprintf(tmp, "d%i\n", Item);
write(giCoke_SerialFD, tmp, 2);
+
+ // Wait a little
+ sleep(250);
// Get status
read(giCoke_SerialFD, tmp, sizeof(tmp)-1);
#include <signal.h>
#include "common.h"
#include <termios.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <fcntl.h>
// === IMPORTS ===
extern void Init_Cokebank(const char *Argument); // cokebank.c
}
// Serial helper
-void InitSerial(int FD, int BaudRate)
+int InitSerial(const char *File, int BaudRate)
{
struct termios info;
int baud;
+ int fd;
+
+
+ fd = open(File, O_RDWR | O_NOCTTY);
+ if( fd == -1 ) return -1;
switch(BaudRate)
{
cfmakeraw(&info); // Sets 8N1
cfsetspeed(&info, baud);
- tcsetattr(FD, TCSANOW, &info);
+ tcsetattr(fd, TCSANOW, &info);
+
+ return fd;
}