Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2006

Tip: Looking for answers? Try searching our database.

Parallel Port Access

Thread view: 
david.brown.0@gmail.com - 11 Feb 2006 03:44 GMT
I'm trying to make a Java program access a parallel port.  Java's comm
API does not provide me with the control I need.  I need to be able to
write to the data and control pins and read the status pins.  Any Java
people know a good solution?  I'm trying to use JNI and create my own
library, but building the library gives me these errors:

ld: warning: cannot find entry symbol _start; defaulting to
0000000008048094
ParallelPort.o: In function `Java_ParallelPort_setAddress':
ParallelPort.cpp:(.text+0x96): undefined reference to `ioperm'
ParallelPort.cpp:(.text+0xaa): undefined reference to `ioperm'

Can anyone help me?  Source follows.

ParallelPort.java
----------
public class ParallelPort {
    static {
        System.loadLibrary("ParallelPort");
    }

    public ParallelPort() {
        setAddress(0x378); // default to lp0
    }

    public ParallelPort(int addr) {
        setAddress(addr);
    }

    public native void setAddress(int addr);

    public native int getAddress();

    public native void sendData(int data);

    public native int readStatus();

    public native void sendControl(int control);
}

==========

ParallelPort.cpp
----------
#include "ParallelPort.h"
#include <jni.h>
#include <stdio.h>

#define extern static
#define REALLY_SLOW_IO
#include <asm/io.h> // port I/O
#undef extern

#include <sys/io.h> // for ioperm

jint DATA;
jint STATUS;
jint CONTROL;

JNIEXPORT void JNICALL Java_ParallelPort_setAddress(JNIEnv *env,
jobject obj,
                           jint addr) {
   DATA = addr;
   STATUS = addr + 1;
   CONTROL = addr +2;

   ioperm(DATA, 3, 1);
   ioperm(0x80, 1, 1);
}

JNIEXPORT jint JNICALL Java_ParallelPort_getAddress(JNIEnv *env,
jobject obj) {
   return DATA;
}

JNIEXPORT void JNICALL Java_ParallelPort_sendData(JNIEnv *env, jobject
obj,
                         jint data) {
   outb(data, DATA);
}

JNIEXPORT jint JNICALL Java_ParallelPort_readStatus(JNIEnv *env,
                           jobject obj) {
   return inb(STATUS);
}

JNIEXPORT void JNICALL Java_ParallelPort_sendControl(JNIEnv *env,
jobject obj,
                            jint control) {
   outb(control, CONTROL);
}

==========

ParallelPort.h (Auto-generated with javah)
----------
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class ParallelPort */

#ifndef _Included_ParallelPort
#define _Included_ParallelPort
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:     ParallelPort
* Method:    setAddress
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_ParallelPort_setAddress
 (JNIEnv *, jobject, jint);

/*
* Class:     ParallelPort
* Method:    getAddress
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_ParallelPort_getAddress
 (JNIEnv *, jobject);

/*
* Class:     ParallelPort
* Method:    sendData
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_ParallelPort_sendData
 (JNIEnv *, jobject, jint);

/*
* Class:     ParallelPort
* Method:    readStatus
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_ParallelPort_readStatus
 (JNIEnv *, jobject);

/*
* Class:     ParallelPort
* Method:    sendControl
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_ParallelPort_sendControl
 (JNIEnv *, jobject, jint);

#ifdef __cplusplus
}
#endif
#endif
me2 - 11 Feb 2006 04:09 GMT
On Fri, 10 Feb 2006 19:44:54 -0800, david.brown.0 wrote:

> I'm trying to make a Java program access a parallel port.  

The rxtx package has parallel port access, as well as others.

www.rxtx.org
Jim Langston - 11 Feb 2006 04:11 GMT
> I'm trying to make a Java program access a parallel port.  Java's comm
> API does not provide me with the control I need.  I need to be able to
> write to the data and control pins and read the status pins.  Any Java
> people know a good solution?  I'm trying to use JNI and create my own
> library, but building the library gives me these errors:

<java error and code snipped>

Umm... why the heck are you asking about a Java issue cross posted to
c.l.c++ ?
Roedy Green - 11 Feb 2006 06:25 GMT
> I need to be able to
>write to the data and control pins and read the status pins.  Any Java
>people know a good solution?

there are some third party libraries . See
http://mindprod.com/jgloss/serialport.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

david.brown.0@gmail.com - 12 Feb 2006 03:27 GMT
RXTX was the only thing that had any form of parallel port access, but
as far as I can see, its just a native library without a Java side
implementation.  I've done some more searching and found a lot that can
access the serial port, but not the parallel port.  I'm trying to drive
an LCD display, so I cannot use the serial port short of buying a
BasicStamp, which is well out of reasonable price range.  Java people
keep trying to find something.  C++ people, help me build a working
parallel port library, in case I can't find anything.
andy@servocomm.freeserve.co.uk - 12 Feb 2006 13:47 GMT
> RXTX was the only thing that had any form of parallel port access, but
> as far as I can see, its just a native library without a Java side
[quoted text clipped - 4 lines]
> keep trying to find something.  C++ people, help me build a working
> parallel port library, in case I can't find anything.

In WinXP and Linux and most modern oses, the parallel port is in
protected memory owned by system and you cant access it directly. You
would need to either use DOS outside windows or Win95/98 3.1. or get a
device driver as mentioned previously. (Look on internet for those.
they are usually available for your preferrred language too). For older
systems(DOS in windows or Win95/98 3.1.) you can get at the hardware.
Methods are often not standard (I think?)  and are actually C not C++.
You will have to study the documentation, for example in VC6.0 you can
choose various options as follows:

#include <conio.h>

const static unsigned short PortA = 0x378;
intval =_inp(Port);
unsigned char val = 1;
_outp(Port,val);

alternatively use assembler eg:

__int16 PrinStat= PrinterStatus; // Port + 1...its a long time ago?
    unsigned char    temp0;
    __asm    {
       mov    dx,PrinStat     /* setup for input from status reg*/
       in    al,dx
       mov    temp0,al
    }

More info on parallel port workings available e.g
http://www.lvr.com/jansfaq.htm

OTOH

More modern practise is to use USB or firewire, even Ethernet etc and
leave printer port alone as its old fashioned cumbersome with all its
wiring and required connectors and EMI dampening hardware and slow.
Some chips are set up to make use of e.g USB very simple. Software
compatible with their hardware is often free. See for example
http://www.ftdichip.com/
Honestly learning about USB or other fast serial comms will be
preferable in the long term. Even modern printers dont use parallel
port;-)

cheers
Andy little
david.brown.0@gmail.com - 13 Feb 2006 02:42 GMT
I'm just trying to control an LCD with 4x5 keypad, which maps perfectly
to the parallel port.  I'm no electrical engineer.  Plus the LCD
requires 4 or 8 bit parallel input (standard Hitachi-type).
Roedy Green - 13 Feb 2006 07:21 GMT
>I'm just trying to control an LCD with 4x5 keypad, which maps perfectly
>to the parallel port.  I'm no electrical engineer.  Plus the LCD
>requires 4 or 8 bit parallel input (standard Hitachi-type).

if it does not talk back to you, perhaps you can drive it as if it
were a very stupid printer with a printer driver that just passes raw
"text" through.

see http://mindprod.com/jgloss/printing.html

Java lets you create PS files and pass them directly to a PS printer,
so there should be some way of creating text files and passing them
unmolested to a dumb printer.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

andy@servocomm.freeserve.co.uk - 13 Feb 2006 09:32 GMT
> I'm just trying to control an LCD with 4x5 keypad, which maps perfectly
> to the parallel port.  I'm no electrical engineer.  Plus the LCD
> requires 4 or 8 bit parallel input (standard Hitachi-type).

Well. This thread is now Beyond off topic for a C++ language newsgoup.
I think you need to download the datasheets from Hitachi for the
particular LCD. I'd be amazed if they dont provide any driver
hardware/software frankly.

cheers
Andy Little
andy@servocomm.freeserve.co.uk - 13 Feb 2006 10:03 GMT
> I'm trying to make a Java program access a parallel port.  Java's comm
> API does not provide me with the control I need.  I need to be able to
[quoted text clipped - 7 lines]
> ParallelPort.cpp:(.text+0x96): undefined reference to `ioperm'
> ParallelPort.cpp:(.text+0xaa): undefined reference to `ioperm'

Actually the following links might explain whats going on. I'm guessing
you are using source code written for Linux or Unix on Windows? ioperm
lives in io.h in Linux, but doesnt exist in that header on Windows. You
could however try running Cygwin in Windows to get Unix like
functionality.

http://www.die.net/doc/linux/man/man2/ioperm.2.html
http://linux.about.com/library/cmd/blcmdl2_ioperm.htm
http://openwince.sourceforge.net/ioperm/

cheers
Andy Little
Gordon Beaton - 13 Feb 2006 10:36 GMT
> ld: warning: cannot find entry symbol _start; defaulting to
> 0000000008048094
> ParallelPort.o: In function `Java_ParallelPort_setAddress':
> ParallelPort.cpp:(.text+0x96): undefined reference to `ioperm'
> ParallelPort.cpp:(.text+0xaa): undefined reference to `ioperm'

It appears as though you are trying to compile your library code as if
it were a complete program. You need to compile it as a shared
library, perhaps something like this:

 g++ -shared -fPIC ParallelPort.cpp -o libParallelPort.so

(you will likely need to add some -I paths to the above as well)

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Roedy Green - 13 Feb 2006 11:04 GMT
>I'm trying to make a Java program access a parallel port.  Java's comm
>API does not provide me with the control I need.  I need to be able to
>write to the data and control pins and read the status pins.  Any Java
>people know a good solution?  I'm trying to use JNI and create my own
>library, but building the library gives me these errors:

Are you trying to do this on Windows or Linux. If Windows:

If you were writing your JNI in C++ called mouse.cpp then:

* must have previously generated the mouse.h file with
* javah.
* you must have
* e:\program files\java\jdk1.5.0_06\include\win32
* and e:\program files\java\jdk1.5.0_06\include\win32
* in tools | options | directories | include
* For project as a whole:
* In project | settings | general | no MFC
* In project | settings | link | output filename | should end in DLL

Look at any of my JNI projects for inspiration. All have well
commented source. including
http://mindprod.com/products1.html#FILETIMES
http://mindprod.com/products1.html#MOUSE
http://mindprod.com/products1.html#PENTIUM
http://mindprod.com/products1.html#SETCLOCK

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

andy@servocomm.freeserve.co.uk - 13 Feb 2006 11:55 GMT
> >I'm trying to make a Java program access a parallel port.  Java's comm
> >API does not provide me with the control I need.  I need to be able to
[quoted text clipped - 3 lines]
>
> Are you trying to do this on Windows or Linux. If Windows:

Contrary to  what i said before, I  now reckon he's using gcc in Linux
and getting a linker error because linker (ld) cant find definition of
ioperm.

FWIW ;-)

regards
Andy Little


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.