Porting a simple c program from linux to windows
Posted: Tue Jul 09, 2019 8:06 am
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;
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.
Then I implemented this for Linux:
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 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.
package meas;
Code: Select all
public class Meas {
public static native boolean open();
public static native boolean on();
public static native boolean off();
public static native boolean close();
}
Code: Select all
/* 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
Code: Select all
#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);
}
}