Reputation: 1077
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)?
Upvotes: 1
Views: 1761
Reputation: 1077
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.
Upvotes: 0
Reputation: 694
This has a JNI interface to parallel port http://www.hytherion.com/beattidp/comput/pport.htm
Upvotes: 0
Reputation: 14467
To compile the C code on windows you should add the #ifdefs around all of the specifics (outb, fcntl).
Use the _inp/_outp intrinsics to access the ports directly. See MSDN for these.
http://msdn.microsoft.com/en-us/library/y7ae61bc(v=vs.80).aspx
To get the .dll file using MinGW (gcc/win32) just use the "-shared" command line switch.
gcc -o libMeas.dll -shared source.c <your_libraries_for_win32>
i586-mingw32msvc-gcc also works on linux (the cross-compilation)
There are also issues with the x64 version.
Looks like you have to use the driver for that kind of stuff, because the _inp/_outp are accessible in the DDK (Driver DevKit), not "out-of-the-box".
Have a look here http://www.highrez.co.uk/downloads/inpout32/default.htm for the 32/64 bit drivers.
There's a whole thread about reading the parallel ports in managed environment (.NET in that case, but JNI would do also, I guess): http://www.vbforums.com/showthread.php?t=643913
Upvotes: 2