79 lines
1.5 KiB
C
79 lines
1.5 KiB
C
|
|
#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;
|
|
}
|