Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-01-2007, 10:04 AM   #1
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.

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

void initDaemon() {
        pid_t pid, sid;

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

        /* OPEN logs here */

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

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

        /* 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[]) {
        /* Start Big Loop */
        ioperm(LPT1, 3, 1);
        int value;
        while (1) {
                value = inb(0x379);
        /* End Big Loop */
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, 12:51 PM   #2
Registered: Mar 2006
Posts: 612

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, 07:26 PM   #3
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.

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

int main(int argc, char* argv[])
        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, 10:50 PM   #4
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

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



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 10:18 AM
monitoring a port linuxboy69 Linux - General 4 11-18-2003 01:37 PM
Paralell Port Tape Drive-Mounting pickmansmodel Linux - Newbie 1 10-21-2003 12:34 PM
Service Monitoring Daemon (MON) PokerFace Programming 1 12-04-2002 11:31 AM
Linux and paralell port. jalm Linux - Newbie 1 11-29-2001 05:54 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:03 AM.

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