端口目前由Unknown Windows Application拥有 - 条形码读取问题

时间:2014-06-13 06:54:23

标签: serial-port barcode barcode-scanner

我已经使用Device Argox条形码阅读器(java中的型号为As-8250)将程序编写到阅读器条形码中。 我正在使用javax.com jar进行串行通信。 这是我写的从端口号COM13读取的程序。

我还将我的Argox Utility的默认端口号设置为COM13。

package scanhandler;


import java.awt.AWTException;
import java.awt.Robot;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Enumeration;
import java.util.Properties;
import java.util.TooManyListenersException;

import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;

public class ScanHandler implements Runnable, SerialPortEventListener {

    private static CommPortIdentifier   myCommPortIdentifier;
    private static Enumeration          portList;
    private static String               TimeStamp;
    private static String               driverClass;
    private static String               connectionString;
    private static String               comPort;    

    private Connection                  myConnection;
    private InputStream                 myInputStream;
    private Robot                       myRobot;
    private SerialPort                  mySerialPort;
    private Thread                      myThread;


    public ScanHandler() {

        // open serial port
        try {
            TimeStamp = new java.util.Date().toString();
            mySerialPort = (SerialPort) myCommPortIdentifier.open("ComControl", 2000);
            //System.out.println(TimeStamp + ": " + myCommPortIdentifier.getName() + " opened for scanner input");
        } catch (PortInUseException e) {
            e.printStackTrace();
        }

        // get serial input stream
        try {
            myInputStream = mySerialPort.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // add an event listener on the port
        try {
            mySerialPort.addEventListener(this);
        } catch (TooManyListenersException e) {
            e.printStackTrace();
        }
        mySerialPort.notifyOnDataAvailable(true);

        // set up the serial port properties
        try {
            mySerialPort.setSerialPortParams(9600,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);
            mySerialPort.setDTR(false);
            mySerialPort.setRTS(false);

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

        // make a robot to pass keyboard data
        try {
            myRobot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }

        // create the thread
        myThread = new Thread(this);
        myThread.start();
    }

    public void run() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {}
    }

    // on scan
    public void serialEvent(SerialPortEvent event) {

        if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {

            StringBuilder myStringBuilder = new StringBuilder();
            int c;
            try {

                // append the scanned data onto a string builder
                while ((c = myInputStream.read()) != 10){
                   if (c != 13)  myStringBuilder.append((char) c);
                }               

                // send to keyboard buffer if it the barcode doesn't start with '5'
                if (myStringBuilder.charAt(0) != '5') {

                    for (int i = 0; i < myStringBuilder.length(); i++) {
                        myRobot.keyPress((int) myStringBuilder.charAt(i));
                        myRobot.keyRelease((int) myStringBuilder.charAt(i));
                    }

                // here's the scanned barcode as a variable!
                } else {
                    TimeStamp = new java.util.Date().toString();
                    System.out.println(TimeStamp + ": scanned input received:" + myStringBuilder.toString());                    
                }

                // close the input stream
                myInputStream.close();

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

    public static void main(String[] args) {

        // read ScanHandler properties
        Properties myProperties = new Properties();
        // myProperties.load(new FileInputStream("config.properties"));
        comPort             = "COM13";              

        try {

            // get our pre-defined COM port
            myCommPortIdentifier = CommPortIdentifier.getPortIdentifier(comPort);
            ScanHandler reader = new ScanHandler();

        } catch (Exception e) {
            TimeStamp = new java.util.Date().toString();
            System.out.println(TimeStamp + ": " + comPort + " " + myCommPortIdentifier);
            System.out.println(TimeStamp + ": msg1 - " + e);
        }
    };    
}

- &GT;这个程序正在侦听端口COM13。并在输入流中打印字符串(由条形码阅读器扫描)。

- &gt; ==========================问题在这里=============== ======== 当我首先插入Usb并通过Device实用程序强制它使用端口COM13。它完美地工作。 之后,当我运行我的Java程序来侦听同一端口时,它会抛出异常

javax.comm.PortInUseException: Port currently owned by Unknown Windows Application
    at javax.comm.CommPortIdentifier.open(CommPortIdentifier.java:337)
    at scanhandler.ScanHandler.<init>(ScanHandler.java:41)
    at scanhandler.ScanHandler.main(ScanHandler.java:141)
Fri Jun 13 12:20:05 IST 2014: COM13 javax.comm.CommPortIdentifier@d1a2f1
Fri Jun 13 12:20:05 IST 2014: msg1 - java.lang.NullPointerException

所以如果我的proram被拒绝听那个端口,我怎么能读取扫描的条形码呢。

任何帮助都会得到赞赏。

1 个答案:

答案 0 :(得分:0)

以下将显示当前使用特定端口的应用程序的PID:

C:\Users\user>netstat -ano | findstr ":5984"

输出看起来像:

  TCP    127.0.0.1:5984         0.0.0.0:0              LISTENING       2036
  TCP    127.0.0.1:5984         127.0.0.1:55706        ESTABLISHED     2036
  TCP    127.0.0.1:5984         127.0.0.1:55710        ESTABLISHED     2036
  TCP    127.0.0.1:5984         127.0.0.1:55711        ESTABLISHED     2036
  TCP    127.0.0.1:5984         127.0.0.1:55712        ESTABLISHED     2036
  TCP    127.0.0.1:5984         127.0.0.1:55713        ESTABLISHED     2036
  TCP    127.0.0.1:5984         127.0.0.1:55714        ESTABLISHED     2036
  TCP    127.0.0.1:55706        127.0.0.1:5984         ESTABLISHED     4716
  TCP    127.0.0.1:55710        127.0.0.1:5984         ESTABLISHED     4716
  TCP    127.0.0.1:55711        127.0.0.1:5984         ESTABLISHED     4716
  TCP    127.0.0.1:55712        127.0.0.1:5984         ESTABLISHED     4716
  TCP    127.0.0.1:55713        127.0.0.1:5984         ESTABLISHED     4716
  TCP    127.0.0.1:55714        127.0.0.1:5984         ESTABLISHED     4716

然后,您可以使用tasklist命令或任务管理器查找名称:

C:\Users\user>tasklist | findstr "2036"
erl.exe                       2036 Services                   0     15,604 K