简体   繁体   English

使用Applet + Javascript从客户端(浏览器)读取串行端口

[英]Read serial port from client(Browser) using Applet + Javascript

I have a Web App, and I'm trying read serial port from client(Browser) using an Applet HTML Page source: 我有一个Web应用程序,正在尝试使用Applet HTML页面源从客户端(浏览器)读取串行端口:

<!DOCTYPE html>
<html>
<head>
<script src="http://www.java.com/js/deployJava.js"></script>
<script type="text/javascript">
    var attributes = {
        code : 'SampleBalanca.class',
        archive : 'sampleBalanca.jar',
        width : 100,
        height : 100,
        id : 'SampleBalanca'
    };
    var parameters = {
        jnlp_href : 'sample-balanca.jnlp'
    };
    deployJava.runApplet(attributes, parameters, '1.7');

    function peso() {
        alert(document.SampleBalanca.peso());
    }
</script>
</head>
<body>
    <input type="button" name="listbutton" value="Show" onclick="peso();" />
</body>
</html>

The source from sampleBalanca.jar is: sampleBalanca.jar的来源是:

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.applet.Applet;

public class SampleBalanca extends Applet {

    private static final long serialVersionUID = 1L;
    SampleBalanca.SerialThread thread;
    SerialPort serialPort;
    String value;

    public String peso() {

        try {
            CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM1");
            serialPort = (SerialPort) portIdentifier.open("ListPortClass", 1);
            serialPort.setSerialPortParams(4800, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        } catch (Exception e) {
            e.printStackTrace();
        }

        this.thread = new SampleBalanca.SerialThread();
        this.thread.start();
        return this.value;
    }

    class SerialThread extends Thread {
        public void run() {

            try {
                CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM1");
                if (portIdentifier.isCurrentlyOwned())
                    System.out.println("Port in use!");

                else {

                    serialPort.getOutputStream().write(5);

                    // serialPort.getOutputStream().flush();

                    Thread.sleep(50);
                    serialPort.getInputStream().read();

                    byte mBytesIn[] = new byte[5];
                    // int n = serialPort.getInputStream().read(mBytesIn);
                    value = new String(mBytesIn);
                    serialPort.close();
                }
            } catch (Exception ex) {
                System.out.println("Exception : " + ex.getMessage());
            }
        }
    }
}

My sample-balanca.jnlp file is: 我的sample-balanca.jnlp文件是:

<jnlp spec="1.0+" codebase="path\\to\\project\\src\\main\\webapp\\applet\\" href="path\\to\\project\\src\\main\\webapp\\applet\\sample-balanca.jnlp">
        <information>
        <title>sampleBalanca</title>
            <vendor>Lad</vendor>
    </information>
    <resources>
                    <j2se version="1.7" href="http://java.sun.com/products/autodl/j2se" />
                    <jar href="sampleBalanca.jar" main="true" />
    </resources>
    <security>
             <all-permissions />
    </security>
    <applet-desc name="Sample Balanca Applet" main-class="SampleBalanca" width="100" height="100"></applet-desc>
</jnlp>

When I open the applet from browser I get this error: 当我从浏览器打开小程序时,出现此错误:

java.lang.NullPointerException
    at sun.plugin2.applet.JNLP2Manager.getAppInfo(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Does anybody know where is the mistake? 有人知道错误在哪里吗?

I saved all file in same directory, so I changed the codebase="" and href="sample-balanca.jnlp" . 我将所有文件保存在同一目录中,因此更改了codebase=""href="sample-balanca.jnlp" Next step is sign the my applet. 下一步是签署我的小程序。 It should work well after this. 此后它应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM