到http://java.sun.com/products/javacomm/ 下载jar包
解压,把其中so文件复制到/usr/lib
把javax.comm.properties复制到jre目录下
把comm.jar放到classpath目录下
串口连接:
CommPortIdentifier portID;
String owner = new String("modem");
int keeptime = 5000;
Enumeration portList;
portList = CommPortIdentifier.getPortIdentifiers();
// String driverName = "com.sun.comm.Win32Driver";
String driverName = "com.sun.comm.LinuxDriver";
CommDriver driver = null;
try {
// System.loadLibrary("win32com");
driver = (CommDriver) Class.forName(driverName).newInstance();
driver.initialize();
System.out.println("Win32Driver Initialized");
} catch (Exception e) {
e.printStackTrace();
}
// 如果有多个端口
while (portList.hasMoreElements()) {
portID = (CommPortIdentifier) portList.nextElement();
System.out.println("COM:" + portID.getName());
// if (portID.getName().equals("COM1"))
if (portID.getName().equals("/dev/ttyS0"))
try {
sPort = (SerialPort) portID.open(owner, keeptime);
break;
}catch (PortInUseException e) {
e.printStackTrace();
System.exit(1);
}
}
try{
if (sPort != null)
{
in = sPort.getInputStream();
out = sPort.getOutputStream();
try {
sPort.addEventListener(this);
sPort.notifyOnDataAvailable(true);
} catch (TooManyListenersException e) {
e.printStackTrace();
}
}
}catch(Exception e)
{
e.printStackT