LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-01-2007, 11:04 AM   #1
Vossy
LQ Newbie
 
Registered: Jan 2005
Location: Brisbane, Australia
Distribution: Slackware 12
Posts: 17

Rep: Reputation: 0
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.
 
Old 07-01-2007, 01:51 PM   #2
knobby67
Member
 
Registered: Mar 2006
Posts: 546

Rep: Reputation: 42
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.
 
Old 07-01-2007, 08:26 PM   #3
Vossy
LQ Newbie
 
Registered: Jan 2005
Location: Brisbane, Australia
Distribution: Slackware 12
Posts: 17

Original Poster
Rep: Reputation: 0
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.
 
Old 07-01-2007, 11:50 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.
 
  


Reply

Tags
daemon


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help Monitoring RS232 Port delacr_a Linux - Hardware 1 06-08-2007 11:18 AM
monitoring a port linuxboy69 Linux - General 4 11-18-2003 02:37 PM
Paralell Port Tape Drive-Mounting pickmansmodel Linux - Newbie 1 10-21-2003 01:34 PM
Service Monitoring Daemon (MON) PokerFace Programming 1 12-04-2002 12:31 PM
Linux and paralell port. jalm Linux - Newbie 1 11-29-2001 06:54 AM


All times are GMT -5. The time now is 06:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration