I'm trying consume a Java Applet to send bytes for my serial port in a ASP.NET page.
In cshtml i call my applet with javascript
<pre><script type="text/javascript">
var attributes = {
code: 'Principal.class', archive: 'Applet.jar', width: 325, height: 325
};
var parameters = { fontSize: 16 };
var version = '1.7';
deployJava.runApplet(attributes, parameters, version);
</script></pre>
Running in Internet Explorer i have this error :
java.lang.NullPointerException at Principal.(Principal.java:99) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at java.lang.Class.newInstance(Unknown Source) at sun.applet.AppletPanel.createApplet(Unknown Source) at sun.plugin.AppletViewer.createApplet(Unknown Source) at sun.applet.AppletPanel.runLoader(Unknown Source) at sun.applet.AppletPanel.run(Unknown Source) at java.lang.Thread.run(Unknown Source)
My applet:
private static final long serialVersionUID = 1L;
static CommPortIdentifier portId1;
static CommPortIdentifier portId2;
InputStream inputStream;
OutputStream outputStream;
SerialPort serialPort1, serialPort2;
protected String divertCode = "10";
static String TimeStamp;
@Override
public void serialEvent(SerialPortEvent arg0) {}
public static void main (String[] args){
try {
portId1 = CommPortIdentifier.getPortIdentifier("COM11");
//portId2 = CommPortIdentifier.getPortIdentifier("COM9");
Principal reader = new Principal();
} catch (NoSuchPortException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
public Principal(){
try {
TimeStamp = new java.util.Date().toString();
serialPort1 = (SerialPort) portId1.open("Principal",2000);
// System.out.println(TimeStamp+":"+portId1.getName()+"porta aberta");
//serialPort2 = (SerialPort) portId2.open("Principal",2000);
//System.out.println(TimeStamp+":"+portId2.getName()+"porta aberta");
try {
serialPort1.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort1.setDTR(false);
serialPort1.setRTS(false);
try {
outputStream = serialPort1.getOutputStream();
String texto = "Testando envio \n";
byte[] envio = texto.getBytes();
outputStream.write(envio);
} catch (IOException e) {
e.printStackTrace();
}
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
}
} catch (PortInUseException e) {
}
}
My url already listed for trusted in java and my applet was self-authenticated Someone can help me?
The code works perfectly like a Java application. The problem was when I use the same code like a applet in browser. To works THE SAME CODE I just need load the driver before open the port. In java application I don't need.
If some one else has the same question (why the code works in java application and don't works like applet) the answer it's simple. Add in your code before getportIdentifier:
String driverName = "com.sun.comm.Win32Driver";
javax.comm.CommDriver driver = null;
driver = (javax.comm.CommDriver) Class.forName(driverName).newInstance();
driver.initialize();
In java application don't need do that, but like applet you have to do this. So, after you copy the files (javax.comm.proprieties etc..) and signed your applet, this code works (i just summarize the last code)
CommPortIdentifier portId;
String defaultPort = "COM1";
String driverName = "com.sun.comm.Win32Driver";
javax.comm.CommDriver driver = null;
try
{
driver = (javax.comm.CommDriver) Class.forName(driverName).newInstance();
driver.initialize();
portId1 = CommPortIdentifier.getPortIdentifier("COM1");
serialPort1 = (SerialPort) portId1.open("Principal", 2000);
serialPort1.setSerialPortParams(115200,SerialPort.DATABITS_8, SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
outputStream = serialPort1.getOutputStream();
String texto = "Testando envio \n";
byte[] envio = texto.getBytes();
outputStream.write(envio);
}
catch (Exception e) {System.err.println (e);}