1

我想编写一个简单的 c 程序来执行以下操作。打开与并行端口的连接,使引脚 2 为高电平,使引脚 2 为低电平并关闭连接。我为此使用 JNI,所以我的 Java 源文件如下。

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();

}

请注意,Java 文件应控制并行端口,即决定何时应为高或低。然后,我使用 javah 提取了 ac 头文件。

/* 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

然后我为 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);
    }
}

我不是 C 出口,所以上面的代码可能看起来很奇怪。但这并不重要。重要的是我也想为 Windows 实现这个。目标是获得一个 dll,就像我已经有一个用于 Linux 的 libMeas.so 一样。我已经让 MinGW 工作了,但问题是在 Windows 上你不能使用 sys/io.h。在 google 上搜索有关如何执行此操作的文档会得到有关如何在并行端口上写入数据的教程。我不想要这个,我只想让引脚 2 高或低。我的猜测是这应该相当简单。谁能指出我如何为 Windows 执行此操作(使用相同的头文件)的正确方向?

4

3 回答 3

2

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

于 2012-05-21T09:17:58.137 回答
0

我使用从http://logix4u.net/parallel-port/16-inpout32dll-for-windows-982000ntxp获得的 dll 来工作。有了这个 dll,还有一个只有两个函数的 ac 头文件。Inp32() 和 Out32()。可用于直接向并行端口读写字,与 conio.h 中已弃用的 _inp() 和 _out() 函数完全相同。所以这基本上意味着我可以在用户模式下直接访问并行端口。

顺便提一句。您必须深入挖掘才能在 logix4u.net 上找到二进制文件;它可以在这里找到:http ://logix4u.net/parallel-port/parallel-port/index.php?option=com_content&task=view&id=26&Itemid= 1 似乎也存在这个 dll 的 64 位版本,它支持 Windows vista 和希望是 windows 7。

于 2012-05-22T08:14:58.803 回答
0

这有一个到并行端口的 JNI 接口 http://www.hytherion.com/beattidp/comput/pport.htm

于 2012-05-21T21:29:20.473 回答