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 / General / March 2006

Tip: Looking for answers? Try searching our database.

javax not working

Thread view: 
tolu45 - 06 Mar 2006 09:52 GMT
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" <tol...@gmail.com>
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.
Thomas Weidenfeller - 06 Mar 2006 10:40 GMT
> 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
Signature

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/



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.