in web browser serial communication

Applet issues to be considered, because the the Applet security requirements, in ordinary cases, the applet is not allowed to access the resources of the local computer, for example, an applet can not to access the local file system, otherwise, just put on the Internet an applet to the client's C drive delete all the files that have However, the designers of the applet does not suppress all these features, access to local resources through other mechanisms, the serial port as a local resource, you need to use this mechanism, this mechanism is the digital signature. Applet digital signature, you can access to local resources.

It should be noted that the realization of the above ideas is not only achievable ideas, I guess (not practice), the javascript + ActiveX ActiveX instead Applet, can achieve the same effect, I guess, ActiveX and JavaScript scripts language communication, principles, and applets should be similar. In addition, the technical director proposes a third idea: use C to develop a program to read serial on the client side (the reason is very convenient C read serial port), and then through the keyboard Anti true technology, the scanner input analog keyboard input to input effect , due to the relationship between capacity and time has not been selected, but it is a good idea, future time is worth to study.


(C) Java Windows serial programming Profile 
Java IO operation has inherent advantages, Java IO operation is one of the core Java, serial communication as Java IO operation, has been encapsulated very good serial communications using Java, the programmer's job becomes is very simple. 
Java serial communication, can be attributed to the following five process: 
1, serial 
2 OPEN serial 
3, set the serial port 
4, the input (output) stream, stream operations, serial communication 
5, CLOSE serial, serial port operation package Comm.jar this an extension of the java package in the standard JDK does not contain this package require additional downloads, Java serial communication need win32com.dll in the Sun Download comm.jar, will be included in the DLL. 
Serial communication process, the Java code, to achieve the following: 
/ / Step 1: Get the serial port 
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier ("COM1"); 
/ / Step 2: OPEN serial 
SerialPort serialPort = (SerialPort) portId.open ("Serial_Communication", 2000); 
/ / The third step: set the serial port 
serialPort.setSerialPortParams ( 
9600, 
SerialPort.DATABITS_8 
SerialPort.STOPBITS_1 
SerialPort.PARITY_NONE); 
/ / Step 4: Get IO streams flow operation 
InputStream is = serialPort.getInputStream (); 
...... 
/ / Step 5: Close the serial port 
serialPort.close (); 
Java serial communications can find a lot of information in the Internet, this is not the focus of this article you need to know more information in this regard can be obtained in the Internet.


(D) Applet + Javascript achieve details in accordance with the above description, take a look at the details of the implementation: 
Serial communication, the connection is up, send it over, the program does not know when the end, connected to the serial port, serial port input and output streams have been intermittent data transmission, will never end (code in is never InputStream.read () -1), unless artificially interrupt it, otherwise it stays connected. If only the main thread of the Applet serial communication, the operation of the input and output streams never quit, can not be other code never have the chance to run. The best way is to use multi-threaded (multi-threaded Java core strengths, it is now more and more like Java), start the worker thread in the the Applet the main thread, the thread responsible for reading serial information, read the information sent to the Applet properties msg, msg is javascript read away. In addition, JavaScript and applet communication, but the applet can not communicate and javascript (I understand that, do not know there was anything wrong?) Applet whether to read the information, you can not take the initiative to inform the javascript must be javascript every once in a while to see the applet sets a Boolean the variable ready, the javascript regular intervals to see the variable, if true, it means that applet read an input information, javascript can be taken from the information, and Ready Property is false, in order to avoid javascript next view applet again have read the information read out.




According to the detailed description of code to achieve it is easy to take a look at the applet code Applet code contains two Java classes: LocalFileApplet JobThread former Applet main program, which the worker thread.


Code follows LocalFileApplet.java: 
------------------------------------------- LocalFileApplet for java ---- -------------------------------------------------- -------- 
The import the javax.comm *; 
import java.applet.Applet; 
import java.awt. *; 
import java.io. *; 
import java.util.Enumeration; 
/ ** 
* User: YRW - 
* Date: 2006-8-17 <br> 
* Time: 8:55:40 <br> 
* / 
public class LocalFileApplet extends Applet { 
static { 
System.setSecurityManager (null); 
String drivername = "com.sun.comm.Win32Driver"; 
try { 
CommDriver driver = (CommDriver) Class.forName (drivername). NewInstance (); 
driver.initialize (); 
} Catch (InstantiationException e) { 
e.printStackTrace (); / / To change body of catch statement use File | Settings | File Templates. 
} Catch (IllegalAccessException e) { 
e.printStackTrace (); / / To change body of catch statement use File | Settings | File Templates. 
} Catch (ClassNotFoundException e) { 
e.printStackTrace (); / / To change body of catch statement use File | Settings | File Templates. 



private String msg = ""; 
private boolean ready; 
CommPortIdentifier portId; 
SerialPort serialPort; 
InputStream is; 
public CommPortIdentifier getPortId () { 
return portId; 

public void setPortId (CommPortIdentifier portId) { 
this.portId = portId; 

the public Serialport getSerialPort () { 
return serialPort; 

the public void setSerialPort (SerialPort SerialPort) { 
this.serialPort = SerialPort; 

public InputStream getIs () { 
return is; 

the public void SETIS (InputStream is) { 
this.is = is; 

the public boolean the isReady () { 
return ready; 

the public void setReady (boolean ready) { 
this.ready ready; 

public String getMsg () { 
return Msg; 

the public void setMsg (String msg) { 
this.msg = msg; 
}

public void paint (Graphics g) { 

the public void initPort () { 
try { 
portId = CommPortIdentifier.getPortIdentifier ("COM1"); 
serialPort = (SerialPort) portId.open ("Serial_Communication", 2000); 
serialPort.setSerialPortParams (9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); 
the is = serialPort.getInputStream (); 
} Catch (NoSuchPortException, e) { 
e.printStackTrace (); / / To change body of catch statement use File | Settings | File Templates. 
} Catch (PortInUseException, e) { 
e.printStackTrace (); / / To change body of catch statement use File | Settings | File Templates. 
} Catch (UnsupportedCommOperationException, e) { 
e.printStackTrace (); / / To change body of catch statement use File | Settings | File Templates. 
} Catch (IOException e) { 
e.printStackTrace (); / / To change body of catch statement use File | Settings | File Templates. 




the public void cleanPort () { 
try { 
is.close (); 
} Catch (IOException e) { 
e.printStackTrace (); / / To change body of catch statement use File | Settings | File Templates. 

serialPort.close (); 
}

public void init () { 
initPort (); 
JobThread job = new JobThread (this); 
Thread t = new Thread (job); 
t.start (); 
this.setBounds (0,0,0,0); 
this.repaint (); 
}

public void start () { 
}

public void stop () { 
cleanPort (); 
}

the private void showPorts (Graphics g) { 
g.drawString ("scan on the local computer serial port:", 10,10); 
java.util.List ports = listPorts (); 
if (ports.size () == 0) { 
g.drawString ("on the local computer did not find the serial port:", 10,30); 
} Else { 
int y = 50; 
g.drawString ("found" + ports.size (serial ports) + ":", 10, 30) on the local computer; 
for (int i = 0; i <ports.size (); i + +) { 
String port = (of String) ports.get (i); 
g.drawString (port, 10, y); 
y + = 20; 


}

Private the java.util.List listports () { 
the java.util.List ports = new java.util.ArrayList (); 
Enumeration en = CommPortIdentifier.getPortIdentifiers (); 
CommPortIdentifier portId; 
the while (en.hasMoreElements ()) { 
portId = (CommPortIdentifier) ??en.nextElement (); 
if (portId.getPortType () == CommPortIdentifier.PORT_SERIAL) { 
ports.add (portId.getName ()); 


return ports; 

}

-------------------------------------- LocalFileApplet. Java --------- -----------------------------------------------


This class, there are two test methods: listports, and showPorts is used to scan the serial port on the local computer, the project did not use them. This class, it is noteworthy 2: the the static code snippet, System.setSecurityManager (null); makes a digitally signed applet can smoothly access local resources, CommDriver driver = (CommDriver) Class.forName (drivername). newInstance (); manually load the serial driver, eliminating the need to go client load the driver the configuration file javax.comm.properties may cause routing problem. 

Code JobThread.java:

-------------------------------- JobThread. Java --------------- ---------------------------------------------

import java.io.InputStream; 
import java.io.IOException;

/ ** 
* User: YRW - 
* Date: 2006-8-17 <br> 
* Time: 15:12:42 <br> 
* / 
public class JobThread implements Runnable { 
The private LocalFileApplet applet; 
public JobThread (LocalFileApplet applet) { 
this.applet applet; 
}

public void run () { 
InputStream is = applet.getIs (); 
byte [] buffer = new byte [1024]; 
try { 
while (true) { 
int ch; 
int index = 0; 
while ((the ch = Is.read ())! = -1) { 
System.out.println (ch); 
if (ch == 10) { 
if (buffer [index-1] == 13) 
break; 

buffer [index + +] = (byte) ch; 

byte [] bytes = New byte [index-1]; 
for (int i = 0; i <bytes.length; i + +) { 
bytes [i] = buffer [i]; 

String msg = new String (bytes); 
applet.setMsg (msg); 
applet.setReady (true); 
System.out.println (msg); 
applet.repaint (); 

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


} --------------------------------------------- JobThread. Java- -------------------------------------------------- ---------

Even simpler JobThread.java, almost nothing can describe the scanner for each scan will be sent in the last carriage return line feed (ASCII code 1310), so we decided by a carriage return line feed end of a scan.


Script code: applet.html:

--------------------------------------------- Applet.html - -------------------------------------------------- --------


<! ------------------------------------------------ ------------------------------------------> 
<! - 
Serial port scanner to scan Applet program, this code calls the programmer can not be changed! 
-> 
<OBJECT Classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" id="pdf417reader" width="0" height="0"> 
<PARAM NAME="java_code" VALUE="LocalFileApplet"> 
<PARAM NAME="java_codebase" VALUE="."> 
<PARAM NAME="java_type" VALUE="application/x-java-applet;version=1.4"> 
<PARAM NAME="ARCHIVE" VALUE="LocalFileApplet.jar"> 
<PARAM NAME="scriptable" VALUE="true"> 
</ OBJECT> 
<script type="text/javascript"> 
function process () { 
var ready = pdf417reader.isReady (); 
if (ready) { 
pdf417reader.setReady (false); 
var msg = pdf417reader.getMsg (); 
htmlDoJob (msg); 

setTimeout ('process ()', 35); 

process (); 
</ Script> 
<! - 
The above code is the serial scanner scans Applet program, call the programmer can not be changed! 
->

<! ------------------------------------------------ ------------------------------------------>

<textarea name="msgId" cols=70 rows=20> </ textarea> 
<script> 
/ ** 
* This function is js processing function, the function is called automatically when the scanner reads the information, msg information 
* This function is done by calling the programmers 
* /

function htmlDoJob (msg) {

document.all.msgId.value = document.all.msgId.value + "\ n" + msg;


</ Script>

--------------------------- Applet. Html -------------------- ----------------------------------------

Script code is also very simple to note here, applet must be digitally signed, digitally signed applets can not use the <call the applet in such a way, to be called through ActiveX, such as the above code <object ... <the applet call and object invocation conversion tool to automatically convert, if necessary, to get to the bottom of access to relevant information.

(5) Applet digital signature production steps


Applet access to local computer resources need to be digitally signed, in the above applet program, you need to used comm.jar package, this package should be digitally signed, so you can unzip the comm.jar to list files and directories ( MANIFEST.MF META-INF) removed, and write your own Java class labeled jar package, digitally signed, and released to the user.


The steps are as follows:

Step 1: Generate jar package, the command: 
jar cvf LocalFileApplet.jar *. *

Step 2: jar package file to create a keystore and the keys. Keystore will be used to store keys (private keys) and public key authentication, alias alias command: 
keytool-genkey-keystore LocalFileApplet.keystore-alias LocalFileApplet

This command generates a the named LocalFileApplet.keystore keystore file, then the command, the system will ask you a lot of questions, such as your company name, your address, you have to set passwords, etc., by their own casual write. The password will be in the third and fourth steps manipulation and other information available to the client user to know the digital signature is provided. 

The third step: jar package signature, the command: 
jarsigner-keystore LocalFileApplet.keystore LocalFileApplet.jar LocalFileApplet 

Step 4: Export certification documents, the command: 
keytool-export-keystore LocalFileApplet.keystore-alias LocalFileApplet-file LocalFileApplet.cer


This step will generate LocalFileApplet.cer file released, the jar file and applet.html file in the same directory can.

About applet digital signature, you can refer to the following address, and a description of these addresses may be described in this paper are some differences:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值