Hi everyone,

I am trying to write some bits to the parallel port using the java communications port and all is okay except that i need some clarification on something.

The parallel port is connected to circuit board that needs nine inputs(or better known as nine bits)
Now this is what i am doing to write the values to the parallel port

import javax.comm.*;
import java.io.*;
 
public class PortTest 
{
ParallelPort port;
int outputByte = 256;  //The byte value is 100000000

public void outputBits() 
{

try 
{
port.getOutputStream().write(outputByte);
} 

catch(Exception e) 
{ 
e.printStackTrace();
}
  
}

public static void main(String args[])
{
PortTest p1 = new PortTest();
p1.outputBits;
}

}

Now here is my problem, the output stream only seems to output to the first eights bits but does not output the last bit which is a zero. Now i know that a byte is eight bits but if i use a byte array it still does not work.

Basically my question is that i need to output nine bits but i seem to be able to only output eight bits and would really appreciate some guidance from anyone on this topic.

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West

Dani AI

Generated

— the behaviour you saw is exactly what pointed out: Java’s OutputStream writes a single byte (the eight low‑order bits) so passing 256 (0x100) leaves the data register with 0x00. (docs.oracle.com)

Hardware note: a PC parallel port exposes eight data outputs (D0–D7) and a handful of control/status lines that can be reused as extra I/O. Those control pins (STROBE, AUTOFEED, INIT, SELIN) are commonly available but some are inverted and driven via the control register rather than the data register — so they can act as a “9th bit” but must be handled separately and with the inversion/handshaking in mind. (manualmachine.com)

Practical options

  • If you only need one extra bit, drive D0–D7 with write(byte) and use one control pin for bit‑9. That requires writing the control register (account for inversion) at base+2 (or via whatever API your Comm implementation exposes). The Java Communications API does expose a ParallelPort abstraction in some implementations, but concrete control-line access varies by provider; many developers use a small native helper instead. (flylib.com)
  • On modern Windows it’s common to call a tiny native driver/DLL (for example InpOut32/InpOutx64) or use JNI/JNA to call Out32/Inp32 so you can write the data port and the control port directly. That approach gives deterministic pin toggles but needs the driver installed and admin rights. (highrez.co.uk)
  • If driver hassles are unwanted, convert to a USB‑GPIO/microcontroller solution (Arduino, FTDI in bit‑bang/MPSSE mode) and talk to that from Java — simpler and more portable for new machines. (manuals.plus)

Quick troubleshooting checklist: verify the LPT base address (e.g. 0x378), test outputs with a scope or LED, remember control lines may be active‑low (inverted), and avoid USB→DB25 “printer” cables (they usually do not expose raw pins). If you need, implement a tiny native test that writes one byte to the data port and a separate write to the control port to confirm the ninth line can be toggled.

Recommended Answers

All 2 Replies

Hi Richard,

Unfortunately I don't believe this is possible. You cannot output 9 bits to the Parallel Port using java.io.OutputStream.write(int b)

The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored

As you say, even using the overloaded byte[] method won't work. As a byte is 8 bits by its very nature.

If this circuit board is expecting a 9 bit binary value then I think you may be out of luck using Java. Maybe you could try using one of the classes which extend OutputStream. You might even have to write this portion of the code in C as a library and call it from Java. But don't forget that Java stores its numeric values in 2's Compliment - something to keep in mind.

Kate

Hi everyone,

Kate i think you are right on this one and i may have to find another way even maybe using JNI

Thank You

Richard West

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.