[Java] Java 实现并行端口打印机打印 (communications API) →→→→→进入此内容的聊天室

来自 , 2019-12-27, 写在 Java, 查看 146 次.
URL http://www.code666.cn/view/19bc9161
  1. import java.io.*;
  2. import java.util.*;
  3. import javax.comm.*;
  4. public class SimpleLPTPort  {
  5.        Enumeration ports;
  6.        CommPortIdentifier portId;
  7.      
  8.        ParallelPort LPTPort;
  9.      
  10.     protected OutputStream out;
  11.    
  12.     String outPrintStr = "When an \n\rinput method \n\ris activated, ";
  13.        public SimpleLPTPort() {
  14.               checkLPTPort();
  15.        }
  16.      
  17.        public void checkLPTPort() {
  18.              
  19.               ports = CommPortIdentifier.getPortIdentifiers();
  20.              
  21.               if (ports == null) {
  22.                      System.out.println("No comm ports found!");
  23.                      System.exit(0);
  24.               }
  25.              
  26.               while (ports.hasMoreElements()) {
  27.                      // Get the specific port
  28.                      portId = (CommPortIdentifier)ports.nextElement();
  29.                    
  30.                      // Is this a parallel port?
  31.                      if (portId.getPortType() == CommPortIdentifier.PORT_PARALLEL) {
  32.                             // Is the port in use?
  33.                             if (portId.isCurrentlyOwned()) {
  34.                                    System.out.println("Detected "
  35.                                                                       + portId.getName()
  36.                                                                       + " in use by "
  37.                                                                       + portId.getCurrentOwner());
  38.                             }
  39.                            
  40.                             // Try to open the port.
  41.                             try {
  42.                                  
  43.                                    try {
  44.                                           //for(int i = 0; i <=2; i++) {
  45.                                                  LPTPort = (ParallelPort)portId.open("ParallelPort", 2000);
  46.                                                
  47.                                                  System.out.println(LPTPort.getName() + " a LPT port is opened.");
  48.                                                
  49.                                                  if (LPTPort == null) {
  50.                                                         System.out.println("Error opening LPT port "
  51.                                                         + LPTPort.getName());
  52.                                                  }
  53.                                                
  54.                                                  // Get the output stream                                          
  55.                                                  try
  56.                                                  {
  57.                                                         out = LPTPort.getOutputStream();
  58.                                                  }
  59.                                                
  60.                                                  catch (IOException e)
  61.                                                  {
  62.                                                         System.out.println("Cannot open output stream");
  63.                                                  }
  64.                                                
  65.                                                  out.write(outPrintStr.getBytes());
  66.                                                  System.out.println(outPrintStr.getBytes());
  67.                                                  LPTPort.close();
  68.                                                  System.out.println(LPTPort.getName() + " is closing.");
  69.                                           //}
  70.                                           System.out.println("Output stream complete.");
  71.                                           //LPTPort.close();
  72.                                           System.exit(0);
  73.                                          
  74.                                    } catch (IOException ex) {
  75.                                           System.out.println("Output stream fail.");
  76.                                           System.out.println(LPTPort.getName()
  77.                                                                              + ": Cannot write to output stream");
  78.                                           LPTPort.close();
  79.                                           System.exit(0);
  80.                                    }
  81.                            
  82.                             } catch (PortInUseException e) {
  83.                                    System.out.println("Queueing open for "
  84.                                                                      + portId.getName()
  85.                                                                      + ": port in use by "
  86.                                                                     + e.currentOwner);
  87.                             }
  88.                      }
  89.               }
  90.              
  91.        }
  92.      
  93.        public static void main(String[] args) {
  94.               new SimpleLPTPort();
  95.        }
  96.      
  97. }

回复 "Java 实现并行端口打印机打印 (communications API)"

这儿你可以回复上面这条便签

captcha