javax not working

T

tolu45

Hi everyone,
I am making a simple code to connect to the parallel port in order to
print from my application to my printer. I am able to locate the port,
It tells me that the port is not in use but when i try to open the
port, it gives me an error saying that an unknown windors application
is using the port. Here is my code:

package systemInterface;


import PortLister.PrintThread;
import javax.comm.*;
import java.util.*;
import java.io.*;


/**
*
* @author Adesola
*/
public class PortWatcher implements CommPortOwnershipListener{


/**
* Creates a new instance of PortWatcher
*/


Vector portList;
String portName;
public PortWatcher(){


}


/**
* @param args the command line arguments
*/
public CommPortIdentifier getFreeParallelPort() {
CommPortIdentifier com;
CommPortIdentifier freePort = null;
int portNumber = 1;
try{


com =
CommPortIdentifier.getPortIdentifier("LPT"+portNumber);
if (com.isCurrentlyOwned()==false){
freePort = com;
freePort.addPortOwnershipListener(this);
System.out.println("free port at"+ freePort);
//portList.add(com);
}
else{
System.out.println("no free port at" + freePort);
portNumber++;
}


}
catch(NoSuchPortException nspe){
System.out.println(" No such port error:" + nspe);
}
return freePort;
}


public void openPort(){
CommPortIdentifier thePort = getFreeParallelPort();
System.out.println(thePort.getCurrentOwner());


try{


CommPort openPort = thePort.open("portOpener", 20);
ParallelPort pPort = (ParallelPort)openPort;
pPort.setMode(0);
if (pPort == null)
{
System.out.println("Error opening port " + thePort.getName());
}


PrintThread input = new PrintThread(System.in,
pPort.getOutputStream());
PrintThread output = new
PrintThread(pPort.getInputStream(), System.out);
input.start();
output.start();
//openPort.close();
}
catch(PortInUseException piue){
String Owner = thePort.getCurrentOwner();
System.out.println("port in use error: ");
System.out.println( piue);
}
catch(Exception ex){
System.out.println(ex);
}
}


public void ownershipChange(int type){
switch(type){
case CommPortOwnershipListener.PORT_OWNED:
System.out.println("port has become unavailable");


break;
case CommPortOwnershipListener.PORT_UNOWNED:
System.out.println("port has become available");
break;
case CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED:
System.out.println("An application has requested this
port");
break;
default:


System.out.println("Unknown port ownership event,
type"+type);
}


}



}


regards,
Noah

Reply




2 From: tolu45 - view profile
Date: Mon, Mar 6 2006 10:48 am
Email: "tolu45" <[email protected]>
Groups: comp.lang.java.programmer
Not yet ratedRating:
show options


Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Remove | Report Abuse | Find messages by this author


Hi everyone,
I am new to java, and i seem to be running to some trouble comunicating

with my parallel port. I was able to find the parrallel port but when i

tried to open it tells me that the port is being owned by an unknown
windows appliction. Below is my code:

package systemInterface;


import PortLister.PrintThread;
import javax.comm.*;
import java.util.*;
import java.io.*;


/**
*
* @author Adesola
*/
public class PortWatcher implements CommPortOwnershipListener{


/**
* Creates a new instance of PortWatcher
*/


Vector portList;
String portName;
public PortWatcher(){


}


/**
* @param args the command line arguments
*/
public CommPortIdentifier getFreeParallelPort() {
CommPortIdentifier com;
CommPortIdentifier freePort = null;
int portNumber = 1;
try{


com =
CommPortIdentifier.getPortIdentifier("LPT"+portNumber);
if (com.isCurrentlyOwned()==false){
freePort = com;
freePort.addPortOwnershipListener(this);
System.out.println("free port at"+ freePort);
//portList.add(com);
}
else{
System.out.println("no free port at" + freePort);
portNumber++;
}


}
catch(NoSuchPortException nspe){
System.out.println(" No such port error:" + nspe);
}
return freePort;
}


public void openPort(){
CommPortIdentifier thePort = getFreeParallelPort();
System.out.println(thePort.getCurrentOwner());


try{


CommPort openPort = thePort.open("portOpener", 20);
ParallelPort pPort = (ParallelPort)openPort;
pPort.setMode(0);
if (pPort == null)
{
System.out.println("Error opening port " + thePort.getName());
}


PrintThread input = new PrintThread(System.in,
pPort.getOutputStream());
PrintThread output = new
PrintThread(pPort.getInputStream(), System.out);
input.start();
output.start();
//openPort.close();
}
catch(PortInUseException piue){
String Owner = thePort.getCurrentOwner();
System.out.println("port in use error: ");
System.out.println( piue);
}
catch(Exception ex){
System.out.println(ex);
}
}


public void ownershipChange(int type){
switch(type){
case CommPortOwnershipListener.PORT_OWNED:
System.out.println("port has become unavailable");


break;
case CommPortOwnershipListener.PORT_UNOWNED:
System.out.println("port has become available");
break;
case CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED:
System.out.println("An application has requested this
port");
break;
default:


System.out.println("Unknown port ownership event,
type"+type);
}


}



}


below is the there for the input/output stream

package systemInterface;
import java.io.*;


/**
*
* @author Adesola
*/
public class PrintThread extends Thread {
InputStream theInput;
OutputStream theOutput;
/** Creates a new instance of PrintThread */
public PrintThread(InputStream in) {
this(in, System.out);
}
public PrintThread(OutputStream out) {
this(System.in,out);
}
public PrintThread(InputStream in, OutputStream out) {
theInput = in;
theOutput = out;
}


public void run () {
try{
byte[] buffer = new byte[256];
while(true){
int byteRead = theInput.read(buffer);
if (byteRead==-1) break;
theOutput.write(buffer, 0, byteRead);
}
}
catch(IOException ioe){
System.out.println(ioe);
}
}



}


Thanks for your help.
 
T

Thomas Weidenfeller

tolu45 said:
Hi everyone,
I am making a simple code to connect to the parallel port in order to
print from my application to my printer. I am able to locate the port,

Posting the same question three times within minutes will not give you
better answers. And using a meaningless subject line will also not serve
your purpose.

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top