简体   繁体   中英

using jssc and java applet to communicate to Serial with JS but browser freezes

Im trying to talk to the serial port with JS using a java applet. Im not too experienced with java but i managed to get jssc running just fine when not in a applet or using js. and Ive also been able to talk between an applet and JS passing functions and variables. but I cannot understand why my browers freezes everytime i try to load the serialCom.class applet that contains the jssc information

here is my java

import jssc.SerialPort;
import jssc.SerialPortException;
import java.applet.*;
import java.awt.*;



public class serialCom extends Applet{


SerialPort serialPort;


public void connect(){
    serialPort = new SerialPort("/dev/pts/2");
    try {
        serialPort.openPort();
        serialPort.setParams(9600, 8, 1, 0);
        serialPort.writeBytes("Hello Again".getBytes());
        serialPort.closePort();
        }
    catch (SerialPortException ex){
        System.out.print(ex);
        }
}
public void disconnect(){
    //SerialPort serialPort = new SerialPort("/dev/pts/1");
    try {
        //serialPort.openPort();
        //serialPort.setParams(9600, 8, 1, 0);
        //serialPort.writeBytes("Hello Again".getBytes());
        serialPort.closePort();
        }
    catch (SerialPortException ex){
        System.out.print(ex);
        }
}
public void writeSerial(){
     //SerialPort serialPort = new SerialPort("/dev/pts/1");
    try {
        //serialPort.openPort();
        //serialPort.setParams(9600, 8, 1, 0);
        serialPort.writeBytes("Hello Again".getBytes());
        //serialPort.closePort();
        }
    catch (SerialPortException ex){
        System.out.print(ex);
        }
}





}

and here is my html

<!DOCTYPE html>
<html>
<head>

<title>
 Will It Work
</title>
</head>
<body>
<div>
<button id="test" onclick="connectTest();">Connect</button>
<button id="test" onclick="writeTest();">Write</button>
<button id="test" onclick="readTest();">Read</button>
<button id="test" onclick="disconnectTest();">Disconnect</button>
<applet code="serialCom.class" id="myApplet" width="0" height="0"></applet>
 <script>
var connectTest = function(){
alert("Im Connected");
myApplet.connect();

}
alert("Connect");
var disconnectTest = function(){
alert("Im Disconnected");
myApplet.disconnect();
}
alert("Disconnect");
var writeTest = function(){
alert("Im Writing");
myApplet,writeSerial();
}
alert("Write");
var readTest = function(){
}
alert("Read");
</script>-->
</div>
</body>
</html>

I'm quite new to all this java applet programming, but its seems like we are trying to do something similar. I have no idea what exactly is wrong with your code but I found jss-bridge which helped me a lot on achieving serial port communication trough javascript/java-applet.

you can download the full code using svn

svn checkout http://java-simple-serial-connector.googlecode.com/svn/trunk/ java-simple-serial-connector-read-only

inside the js_terminal folder you will find an index.html that simulates a terminal on your browser to communicate with the serial ports. it works amazingly well. you will also probably need to lower your java security setting or self-sign the applet.

I hope it helps!

pd: I know it isn't an answer, but i can't comment yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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