要使用applet往串口写数据,applet程序如下:
private SerialPort serialPort;
private static OutputStream os;
public void initCom(){
Enumeration ports = CommPortIdentifier.getPortIdentifiers();// 查询电脑所有端口
CommPortIdentifier portId = null;
while(ports.hasMoreElements()){
portId = (CommPortIdentifier)ports.nextElement();
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { //串口
if(portId.getName().equalsIgnoreCase("COM1")) {
break;
}
}
}
try{
serialPort = (SerialPort)portId.open("PosPrint", 2000);
//设置串口初始化参数,依次是波特率 数据位 停止位和校验
System.out.println("serialPort:"+serialPort);
serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, 
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
}catch(PortInUseException e) {
System.out.println("COM端口正被使用!");
e.printStackTrace();
}catch(UnsupportedCommOperationException e) {
System.out.println("串行口初始化异常!");
e.printStackTrace();
}
try{
os = serialPort.getOutputStream();
}catch(IOException e) {
System.out.println("获取输出流失败!");
e.printStackTrace();
}
}
程序在serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8,  SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);这里报
java.lang.NullPointerException
at gnu.io.RXTXPort.setSerialPortParams(RXTXPort.java:178)
at com.chq.printpos.PrintSerialPortApplet.initCom(PrintSerialPortApplet.java:97)
at com.chq.printpos.PrintSerialPortApplet.start(PrintSerialPortApplet.java:186)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
异常:java.lang.NullPointerException打印serialPort是有值非空的,为//./COM1,也没有RXTXPort.java的最新程序。
同样程序不用web的applet调用直接main运行就不报错,
求教各位大神是什么原因?多谢,给分。java 串口通信Applet