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; +}