如何使用applet通过rxtxComm.jar将数据发送到串口on ubuntu

时间:2013-02-20 11:25:08

标签: java linux applet policy rxtx

我编写程序使用applet通过rxtxComm.jar访问串口,我更改了java.policy,这样applet就可以访问没有签名的串口。当程序运行时 Windows,它是有效的。但是在ubuntu上有一些execptions( java.lang.reflect.InvocationTargetException和“npobject上的错误调用方法”)。为了解决这个问题,我花了很多次谷歌,我无法解决。是否有人有同样的问题?这是关于这个程序的代码。

print.html

function print() {
    var zplText = $("#zplText").val();
    document.printApplet.print(zplText);
}



<textarea rows="5" cols="6" id="zplText"></textarea>
<a href="#" onClick="print()">print</a>
<applet id="printApplet" alt="" codebase=.. code="zpl/ZplPrint.class">
    <PARAM NAME="archive" VALUE="../lib/RXTXcomm.jar">
</applet>

ZplPrint.java

public class ZplPrint extends Applet {
static String zpl;
static CommPortIdentifier portId;
static CommPort commPort;
static SerialPort serialPort;
@SuppressWarnings("rawtypes")
static Enumeration portList;
static OutputStream out;

public void print(String zplText) {
    zpl = zplText;
    if (zpl == null) {
        return;
    }
    byte[] zplByte = zpl.getBytes();

    portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements()) {
        portId = (CommPortIdentifier) portList.nextElement();
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {

            try {

                commPort = portId.open(portId.getName(), 2000);
                serialPort = (SerialPort) commPort;

                serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
                        SerialPort.PARITY_NONE);
                out = serialPort.getOutputStream();
                out.write(zplByte);
                commPort.close();
            } catch (PortInUseException e) {
                e.printStackTrace();
            } catch (UnsupportedCommOperationException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NullPointerException e) {
                e.printStackTrace();
            }

            System.out.println("Find CommPort: " + portId.getName());
        }
    }

}

的java.policy

  • 权限java.lang.RuntimePermission“loadLibrary.rxtxSerial”;
  • 权限java.io.FilePermission“$ {java.home} $ {/} lib $ {/} \ textme {/} x86 $ {/} rxtxSerial.dll”,“read”;
  • 权限java.util.PropertyPermission“gnu.io.log.mode”,“read”;
  • 权限java.util.PropertyPermission“gnu.io.SerialPorts”,“写,读”;
  • permission java.util.PropertyPermission“gnu.io.rxtx.SerialPorts”,“read,write”;
  • 权限java.util.PropertyPermission“gnu.io.ParallelPorts”,“read”;
  • 权限java.util.PropertyPermission“gnu.io.rxtx.ParallelPorts”,“read”;
  • 权限java.lang.RuntimePermission“modifyThreadGroup”;
  • 权限java.lang.RuntimePermission“modifyThread”;

当ubuntu为:

时,第一行和第二行与ubuntu不同
  • 权限java.lang.RuntimePermission“loadLibrary.librxtxSerial”;
  • permission java.io.FilePermission“$ {java.home} $ {/} lib $ {/} amd64 $ {/} librxtxSerial.so”,“read”;

1 个答案:

答案 0 :(得分:1)

我已经解决了自己。

   permission java.lang.RuntimePermission "loadLibrary.rxtxSerial";

   permission java.io.FilePermission "${java.home}${/}lib${/}ext${/}amd64${/}librxtxSerial.so", "read";

   permission java.io.FilePermission "${java.home}${/}lib${/}ext${/}librxtxSerial.so", "read";

   permission java.io.FilePermission "${/}dev", "read";

   permission java.util.PropertyPermission "gnu.io.log.mode", "read";

   permission java.util.PropertyPermission "gnu.io.SerialPorts", "write,read";

   permission java.util.PropertyPermission "gnu.io.rxtx.SerialPorts", "read,write";

   permission java.util.PropertyPermission "gnu.io.ParallelPorts", "read";

   permission java.util.PropertyPermission "gnu.io.rxtx.ParallelPorts", "read";

   permission java.lang.RuntimePermission "modifyThreadGroup";

   permission java.lang.RuntimePermission "modifyThread";