Commit 7aba2f30 authored by Hugo Hörnquist's avatar Hugo Hörnquist

Add much better serial code.

parent 26b4c55c
.PHONY: all clean
CC=gcc
CFLAGS=-Wall -fPIC -pedantic
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
%: %.o
$(CC) -o $@ $<
libcom.so: cash_drawer.o
$(CC) -shared -o $@ $<
libcom.a: cash_drawer.o
ar rcs $@ $<
all: libcom.so libcom.a
#include "cash_drawer.h"
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdio.h>
#include <errno.h>
int port_setup (const char* path, speed_t speed) {
int fd = open(path, O_RDWR | O_NOCTTY | O_SYNC);
if (fd == -1) return fd;
struct termios term;
cfmakeraw(&term);
cfsetspeed(&term, speed);
tcsetattr(fd, 0, &term);
return fd;
}
int port_close (int fd) {
return 0;
}
int port_write (int fd, char data) {
tcflush(fd, TCIOFLUSH);
ssize_t s;
char buf[1] = { data };
s = write(fd, &buf, 1);
if (s != 1) return 1;
s = read(fd, &buf, 1);
if (s != 1) return 2;
if (buf[0] != '1') return 3;
s = read(fd, &buf, 1);
if (s != 1) return 4;
if (buf[0] != '2') return 5;
return 0;
}
#ifndef CASH_DRAWER_H
#define CASH_DRAWER_H
#include <termios.h>
#include <unistd.h>
int port_setup (const char* path, speed_t speed);
int port_write (int fd, char data);
int port_close (int fd);
#endif // CASH_DRAWER_H
#include <stdio.h>
#include <errno.h>
#include "cash_drawer.h"
#define COM "/dev/serial/by-id/usb-Arduino__www.arduino.cc__0043_553383436393514032C0-if00"
#define SYN 0x16
int main(void) {
int fd = port_setup(COM, B9600);
if (fd == -1) printf("error opening connection (%i)\n", errno);
port_write(fd, SYN);
port_close(fd);
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment