Search code examples
cwindowsparallel-port

Porting a simple c program from linux to windows


I want to write a simple c program to do the following. Open a connection to a parallel port, make pin 2 high, make pin 2 low and close the connection. I am using JNI for this, so my Java source file is as follows.

package meas;

public class Meas {

    public static native boolean open();

    public static native boolean on();

    public static native boolean off();

    public static native boolean close();

}

Note that a Java file should control the parallel port, i.e. decide when it should be high or low. Then, I extracted a c header file using javah.

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class meas_Meas */

#ifndef _Included_meas_Meas
#define _Included_meas_Meas
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     meas_Meas
 * Method:    open
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_meas_Meas_open
  (JNIEnv *, jclass);

/*
 * Class:     meas_Meas
 * Method:    on
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_meas_Meas_on
  (JNIEnv *, jclass);

/*
 * Class:     meas_Meas
 * Method:    off
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_meas_Meas_off
  (JNIEnv *, jclass);

/*
 * Class:     meas_Meas
 * Method:    close
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_meas_Meas_close
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

Then I implemented this for Linux:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/io.h>
#include <sys/types.h>
#include <fcntl.h>
#include <meas_Meas.h>

#define BASEPORT 0x378 /* lp1 */

int tem;

/*
 * Class:     meas_Meas
 * Method:    open
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_meas_Meas_open(JNIEnv *env, jclass clz) {
    //set permissions to access port
    if (ioperm(BASEPORT, 3, 1)) {
        perror("ioperm");
        exit(1);
    }

    tem = fcntl(0, F_GETFL, 0);
    fcntl(0, F_SETFL, (tem | O_ASYNC));
}

/*
 * Class:     meas_Meas
 * Method:    on
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_meas_Meas_on(JNIEnv *env, jclass clz) {
    outb(255, BASEPORT);
}

/*
 * Class:     meas_Meas
 * Method:    off
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_meas_Meas_off(JNIEnv *env, jclass clz) {
    outb(0, BASEPORT);
}

/*
 * Class:     meas_Meas
 * Method:    close
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_meas_Meas_close(JNIEnv *env, jclass clz) {

    fcntl(0, F_SETFL, tem);
    outb(0, BASEPORT);

    //take away permissions to access port
    if (ioperm(BASEPORT, 3, 0)) {
        perror("ioperm");
        exit(1);
    }
}

I am not a C export, so the code above may look weird. But that does not really matter. What matters is that I want to implement this for Windows also. The goal is to get a dll, just like I already have a libMeas.so for Linux. I already have MinGW working and all but the problem is that on Windows you can't use sys/io.h. Searching on google for documentation on how to do this results in tutorials on how to write data on the parallel port. I do not want this, I just want to make pin 2 high or low. My guess is that this should be fairly simple. Can anyone point me in the right direction on how to do this for windows (using the same header file)?


Solution

  • I got it working with a dll obtained from http://logix4u.net/parallel-port/16-inpout32dll-for-windows-982000ntxp. With this dll there also came a c header file with only two functions. Inp32() and Out32(). Which can be used to read and write words directly to the parallel port, quite the same as the deprecated _inp() and _out() functions in conio.h. So it basically means I can directly access the parallel port in user mode.

    Btw. you have to dig a little to find the binaries on logix4u.net; it can be found here: http://logix4u.net/parallel-port/parallel-port/index.php?option=com_content&task=view&id=26&Itemid=1 it seems there also exists a 64 bit version of this dll which supports windows vista and hopefully windows 7.