1

I have a Web App, and I'm trying read serial port from client(Browser) using an Applet HTML Page source:

<!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:

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:

<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?

1 Answer 1

1

I saved all file in same directory, so I changed the codebase="" and href="sample-balanca.jnlp". Next step is sign the my applet. It should work well after this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.