大神!来帮我!
```package com.z.remotecheck.util;
import java.applet.Applet;
import java.awt.HeadlessException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.TooManyListenersException;
import javax.comm.CommDriver;
import javax.comm.CommPortIdentifier;
import javax.comm.NoSuchPortException;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;
import com.z.remotecheck.util.Readstr.Read;
public class test extends Applet {
static Map map = new HashMap();
//private static DefaultSet ds = new DefaultSet();
private static final String LIB_PATH_SUFFIX = "system32";
private static final String DLL_FILE = "win32com.dll";
public test() throws HeadlessException {
super();
}
public void init() {
String driverName = "com.sun.comm.Win32Driver";
CommDriver driver = null;
System.out.println(".....初始化.....");
addwin32();
System.loadLibrary("win32com");
try {
driver = (CommDriver) Class.forName(driverName).newInstance();
driver.initialize();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void start() {
System.out.println("....start....");
CommPortIdentifier portId;
Enumeration portList;
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier)portList.nextElement();
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
if(portId.getName().equals("COM1")||portId.getName().equals("COM2")
||portId.getName().equals("COM3")||portId.getName().equals("COM4")||portId.getName().equals("COM5")
||portId.getName().equals("COM6")||portId.getName().equals("COM7")||portId.getName().equals("COM8")
||portId.getName().equals("COM9")||portId.getName().equals("COM10")||portId.getName().equals("COM11")
||portId.getName().equals("COM12")||portId.getName().equals("COM13")||portId.getName().equals("COM14")){
System.out.println(portId.getName() + "开启");
try {
Read reader = new Read(portId);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
break;
}
}
public void destrory() {
}
public void addwin32() {
try {
// 获取加载库时搜索的路径列表
String dirs = System.getProperty("java.library.path");
String[] libs = dirs.split(";");
String libPath = "";
for (String lib : libs) {
if (lib.toLowerCase().endsWith(LIB_PATH_SUFFIX)) {
libPath = lib;
break;
}
}
File dll = new File(libPath, DLL_FILE);
if (!dll.exists()) {
URL url = new URL(super.getCodeBase() + DLL_FILE);
InputStream is = url.openConnection().getInputStream();
FileOutputStream fos = new FileOutputStream(dll);
byte[] buf = new byte[256]; // 读取缓存
int len = 0;
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.flush();
fos.close();
is.close();
System.out.println("创建文件完成[" + dll + "].");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public class Read implements Runnable, SerialPortEventListener {
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
public Read(CommPortIdentifier portId) throws InterruptedException {
try {
serialPort = (SerialPort) portId.open("MyReader", 2000);
// portId.open("串口所有者名称", 超时等待时间);
} catch (PortInUseException e) {
// 如果端口被占用就抛出这个异常
e.printStackTrace();
}
try {
// outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
// 从COM9获取数据
} catch (IOException e) {
}
try {
serialPort.addEventListener(this);
// 添加监听器
} catch (TooManyListenersException e) {
}
serialPort.notifyOnDataAvailable(true);
/* 侦听到串口有数据,触发串口事件 */
try {
serialPort.setSerialPortParams(9600,// 波特率
SerialPort.DATABITS_8,// 数据位数
SerialPort.STOPBITS_1,// 停止位
SerialPort.PARITY_NONE);// 校验
} catch (UnsupportedCommOperationException e) {
}
readThread = new Thread(this);
readThread.start();
// 启动线程
}
@Override
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20];
/*
* String write = new String("111"); try {
* outputStream.write(write.getBytes());
* //System.out.println(write.getBytes()); } catch (IOException
* e1) { e1.printStackTrace(); }
*/
try {
while (inputStream.available() > 0) {
int n = 0;
int numBytes = inputStream.read(readBuffer);
// System.out.println("numBytes" + numBytes);
//
String emid = "";
for (int i = 0; i < numBytes; i++) {
// System.out.println(readBuffer[i]);
n = readBuffer[i] & 0xFF;
emid += n;
}
// emids.clear();
// emids.add(emid);
map.put("emid", emid);
System.out.println(map.get("emid"));
}
} catch (IOException e) {
}
break;
}
}
@Override
public void run() {
try {
Thread.sleep(30000);
serialPort.close();
System.out.println("串口关闭");
// 设定30秒后端口关闭,程序随之结束
} catch (InterruptedException e) {
}
}
}
}