From 7f04cbfac6057805880b7f448b944692b51d07e6 Mon Sep 17 00:00:00 2001 From: josh Date: Sat, 6 Feb 2010 04:37:54 +0000 Subject: [PATCH] added files git-svn-id: svn://anubis/misc/parport-write@230 bd8a9e45-a331-0410-811e-c64571078777 --- Makefile | 7 +++++ parport-write.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 Makefile create mode 100644 parport-write.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..dcc45d3 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ + +TARGET := parport-write + +all: $(TARGET) + +$(TARGET): $(TARGET).c + gcc -o $@ $< diff --git a/parport-write.c b/parport-write.c new file mode 100644 index 0000000..aefd71c --- /dev/null +++ b/parport-write.c @@ -0,0 +1,78 @@ + +#include +#include +#include +#include +#include +#include +#include +#include + +char getVal(int argc, char * argv[]) +{ + int val = 0xFF; + if (argc >= 2) + { + if (strlen(argv[1]) >= 2 && strncmp(argv[1], "0x", 2) == 0) + { + sscanf(argv[1] + 2, "%x", &val); + } + else + { + sscanf(argv[1], "%d", &val); + } + } + else + { + printf("Value not specified, using 0x%x\n", val); + } + return val; +} + +int main(int argc, char * argv[]) +{ + int fd; + int result; + char val = getVal(argc, argv); + + fd = open("/dev/parport0", O_RDWR); + + if (fd == -1) + { + perror("Could not open parallel port"); + return 1; + } + + printf("Claiming parallel port\n"); + if (ioctl(fd, PPCLAIM, NULL)) + { + perror("Could not claim the parallel port!"); + close(fd); + return 2; + } + + int mode = IEEE1284_MODE_BYTE; + if (ioctl(fd, PPSETMODE, &mode)) + { + perror("Could not set mode"); + ioctl(fd, PPRELEASE); + close(fd); + return 3; + } + + int dir = 0x00; + if (ioctl(fd, PPDATADIR, &dir)) + { + perror("Could not set parallel port direction"); + ioctl(fd, PPRELEASE); + close(fd); + return 4; + } + + ioctl(fd, PPWDATA, &val); + + printf("Releasing parallel port\n"); + ioctl(fd, PPRELEASE); + close(fd); + return 0; +}