javax not working

Discussion in 'Java' started by tolu45, Mar 6, 2006.

  1. tolu45

    tolu45 Guest

    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" <>
    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.
     
    tolu45, Mar 6, 2006
    #1
    1. Advertising

  2. tolu45 wrote:
    > 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
    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
     
    Thomas Weidenfeller, Mar 6, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rui Pacheco
    Replies:
    2
    Views:
    3,964
    Rui Pacheco
    Oct 17, 2003
  2. Replies:
    1
    Views:
    802
    Esmond Pitt
    Mar 27, 2005
  3. lizard
    Replies:
    0
    Views:
    1,799
    lizard
    Jan 30, 2006
  4. Andrew Thompson

    javax.servlet and javax.servlet.http

    Andrew Thompson, Apr 24, 2007, in forum: Java
    Replies:
    1
    Views:
    705
    newbie_at_tomcat
    Apr 25, 2007
  5. Lew
    Replies:
    1
    Views:
    631
    newbie_at_tomcat
    Apr 25, 2007
Loading...

Share This Page