?? java chuangkoou code.java
字號(hào):
package com.gooseli.comm;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TooManyListenersException;
import javax.comm.*;
import com.gooseli.data.DataProcess;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class SerialComm implements SerialPortEventListener, Runnable {
public final static String PORT_OWER = "MonitorApp";
private boolean isOpen;
private boolean isStart;
private boolean isSave;
private boolean isPrint;
private Thread readThread;
// something use in this class
private String portName;
private String portAddress;
private CommPortIdentifier portId;
private SerialPort serialPort;
private InputStream inputStream;
private OutputStream outputStream;
private SimpleDateFormat formatter;
// prase data with process
private String dataProtocol;
private DataProcess dataProcess;
public SerialComm() {
isOpen = false;
isStart = false;
isSave = true;
isPrint = false;
formatter = new SimpleDateFormat("[yyyy-MM-dd hh:mm:ss,SSS]");
portName = "COM1";
portAddress = "LOCAL";
dataProtocol = "Gooseli";
dataProcess = new DataProcess();
try {
if (!CommLog.isInit) {
CommLog.init();
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
public void init(String port, String protocol) throws Exception {
portName = port;
portAddress = portName;
dataProtocol = protocol;
init();
}
public void init(String port, String address, String protocol) throws Exception {
portName = port;
portAddress = address;
dataProtocol = protocol;
init();
}
public void init() throws IOException, Exception, Exception {
if (isOpen) {
close();
}
try {
portId = CommPortIdentifier.getPortIdentifier(portName);
serialPort = (SerialPort) portId.open(PORT_OWER, 2000);
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
dataProcess.setProtocol(portAddress, dataProtocol);
isOpen = true;
CommLog.CommLogger.debug(portName + " is opened.");
} catch (NoSuchPortException ex) {
throw new Exception(ex.getMessage());
} catch (PortInUseException ex) {
throw new Exception(ex.getMessage());
}
}
public void start() throws Exception {
if (!isOpen) {
throw new Exception(portName + " has not been opened.");
}
try {
readThread = new Thread(this);
readThread.start();
serialPort.notifyOnDataAvailable(true);
serialPort.addEventListener(this);
isStart = true;
CommLog.CommLogger.debug(portName + " is start.");
} catch (TooManyListenersException ex) {
throw new Exception(ex.getMessage());
}
}
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
System.out.println(ex.getMessage());
}
}
public void stop() {
if (isStart) {
serialPort.notifyOnDataAvailable(false);
serialPort.removeEventListener();
isStart = false;
CommLog.CommLogger.debug(portName + " is stop.");
}
}
public void close() {
stop();
if (isOpen) {
try {
inputStream.close();
outputStream.close();
serialPort.close();
isOpen = false;
CommLog.CommLogger.debug(portName + " is close.");
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
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:
readComm();
break;
default:
break;
}
}
public void readComm() {
StringBuffer readBuffer = new StringBuffer();
String scannedInput = "";
Date currentTime = null;
String TimeStamp = "";
int c;
try {
while ((c = inputStream.read()) != '\n') {
readBuffer.append((char) c);
}
scannedInput = readBuffer.toString().trim();
currentTime = new Date();
if (isSave) {
CommLog.CommLogger.info(scannedInput);
}
if (isPrint) {
TimeStamp = formatter.format(currentTime);
System.out.println(TimeStamp + " - " + scannedInput);
}
dataProcess.add(scannedInput, currentTime);
} catch (IOException ex) {
System.out.println(ex.getMessage());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
public void writeComm(String outString) {
try {
outputStream.write(outString.getBytes());
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
public static void main(String[] args) {
SerialComm serialcomm = new SerialComm();
try {
serialcomm.init("COM2", "Air");
serialcomm.start();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -