LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Paralell port monitoring daemon? (https://www.linuxquestions.org/questions/programming-9/paralell-port-monitoring-daemon-565812/)

Vossy 07-01-2007 10:04 AM

Paralell port monitoring daemon?
 
Hey. I'm working on a project at the moment where I'm trying to use a bank of switches to interface with my computer and perform tasks like volume up etc. I reckon a daemon process would be the way to do this, but unfortunately I'm reasonably inexperienced with this sort of programming. I did a few google searches and found some neat tutorials, and this is the code I've written so far.

Code:

lots of #include statements here...
#define LPT1 0x378

void initDaemon() {
        pid_t pid, sid;

        /* Fork off the parent process */
        pid = fork();
        if (pid < 0) {
                exit(EXIT_FAILURE);
        }
        /* If we get a good PID, then we can exit the parent process */
        if (pid > 0 ) {
                exit(EXIT_SUCCESS);
        }
        /* Change the file mode mask */
        umask(0);

        /* OPEN logs here */

        /* Create a new SID for the child process */
        sid = setsid();
        if (sid < 0) {
                /* Log failures */
                exit(EXIT_FAILURE);
        }

        /* Change the current working directory */
        if ((chdir("/")) < 0 ) {
                /* Log any failure here */
                exit(EXIT_FAILURE);
        }

        /* Close out the standard file descriptors and disable terminal output */
//      close(STDIN_FILENO);
//      close(STDOUT_FILENO);
//      close(STDERR_FILENO);
}


int main(int argc, char* argv[]) {
        initDaemon();
        /* Start Big Loop */
        ioperm(LPT1, 3, 1);
        int value;
        while (1) {
                value = inb(0x379);
                printf("%i",value);
                sleep(30);
        }
        /* End Big Loop */
        exit(EXIT_SUCCESS);
}

The problem is, it doesn't seem to do anything. I don't get any output at all (I've commented out the close() statements). In an ideal world, I'd like it to call a function when the input ports go high.

As I mentioned previously, I was planning to use it as sort of external media control, and I have command line interface with the media programs I'm using.

knobby67 07-01-2007 12:51 PM

Two very quick things if you've read tutorials have you ran any of there demo's? To see if they work? Paralell ports can be a nightmare on modern machines, with all sorts of virtual ones. Has your version of Linux detected a port?
Finally can you use USB? It might take longer, but will be far better in the long run.

Vossy 07-01-2007 07:26 PM

I haven't got my hardware interface working yet, so I don't really have a way of testing it.

I did find a library that should make it easier called PARAPIN. It can be compiled as a c library or a kernel module. At the moment, I'm using the C library version, because I know nothing about kernel programming. I've written a small implementation of the program below. The problem is, when I run the new program I've written it eats up the CPU.

Code:

#include <parapin.h>
#include "daemon.h"

int main(int argc, char* argv[])
{
        initDaemon();
        if(pin_init_user(LPT1))
        {
                exit(EXIT_FAILURE);
        }
        while (pin_is_set(LP_PIN11)){
//              printf("Pin 11 is %s\n", pin_is_set(LP_PIN11) ? "on" : "off");
        }

}

NB I've left out all the daemon initialisation code here. It's in the function initDaemon()

I understand that I'm basically running an infinite loop here, but is there any way to avoid it? I *think* that the kernel module version of PARAPIN might work better here, cause it can handle interrupts, but I don't really understand what they are, or how to compile a kernel module, or pretty much most of the help file...

With regards to the USB idea, it would be easier to code, but far harder to actually implement with respect to the hardware. As I see it, I would need some sort of IC to emit a series of pulses and decode them with the software because the USB protocol works on a series interface rather than parallel. With parallel port interfacing basically all you need to do it short two terminals together.

theNbomr 07-01-2007 10:50 PM

Unless your external hardware uses the one interrupt-capable input (ACK, if I recall correctly), you have no alternative but to poll the input port. Your use of ioperm() requires your program to run as root. You can avoid this by using a kernel module to read the hardware for you. Use libieee1284 to call the kernel appropriately. It can also do a good job of locating the available parallel ports for you and report where they are mapped in IO space. It shouldn't take too much to test the input pins: left floating, they are likely to go to the high rail, and a simple switch (read piece(s) of wire) to ground will pull the pin low. You can also access the hardware with with the kernel's blessing through the /dev/port interface, but this still requires root access.
For testing/debugging, it seems to me that you need to separate the two issues of hardware functionality, and daemonizing your application. Neither seems to depend on the other, and as such, should be debugged separately.
--- rod.


All times are GMT -5. The time now is 06:04 AM.