added files

git-svn-id: svn://anubis/misc/parport-write@230 bd8a9e45-a331-0410-811e-c64571078777
This commit is contained in:
josh 2010-02-06 04:37:54 +00:00
parent 2addf4e52a
commit 7f04cbfac6
2 changed files with 85 additions and 0 deletions

7
Makefile Normal file
View File

@ -0,0 +1,7 @@
TARGET := parport-write
all: $(TARGET)
$(TARGET): $(TARGET).c
gcc -o $@ $<

78
parport-write.c Normal file
View File

@ -0,0 +1,78 @@
#include <stdio.h>
#include <sys/ioctl.h>
#include <linux/parport.h>
#include <linux/ppdev.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
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;
}