/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package CalcWgtPro;
/**
*
* @author luolai
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;
import java.util.TooManyListenersException;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;
public class Readst {
static String retValue = "000000";
public void init() {
try {
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier("COM1");
// 直接取得COM3端口
System.out.println(portId.getName() + ":開啓");
LogInfo.appendLog(portId.getName() + ":開啓");
@SuppressWarnings("unused")
Read reader = new Read(portId);
} catch (Exception ex) {
ex.printStackTrace();
LogInfo.appendLog(ex.getMessage());
}
}
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();
LogInfo.appendLog(e.getMessage());
}
try {
inputStream = serialPort.getInputStream();
//從COM3獲取數據
} catch (IOException e) {
LogInfo.appendLog(e.getMessage());
}
try {
serialPort.addEventListener(this);
//添加監聽器
} catch (TooManyListenersException e) {
}
serialPort.notifyOnDataAvailable(true);
/*
* 偵聽到串口有數據,觸發串口事件
*/
try {
serialPort.setSerialPortParams(2400,//波特率
SerialPort.DATABITS_8,//數據位數
SerialPort.STOPBITS_1,//停止位
SerialPort.PARITY_NONE);//校驗
} catch (UnsupportedCommOperationException e) {
}
readThread = new Thread(this);
readThread.start();
//啓動線程
}
public void run() {
try {
Thread.sleep(2000);
serialPort.close();
System.out.println("COM1:關閉");
//設定30秒後端口關閉,程序隨之結束
} catch (InterruptedException e) {
LogInfo.appendLog(e.getMessage());
}
}
/**
* BI -通訊中斷. CD -載波檢測. CTS -清除發送. DATA_AVAILABLE -有數據到達. DSR -數據設備準備好.
* FE -幀錯誤. OE -溢位錯誤. OUTPUT_BUFFER_EMPTY -輸出緩衝區已清空. PE -奇偶校驗錯. RI -
* 振鈴指示. 一般最常用的就是DATA_AVAILABLE--串口有數據到達事件。
*/
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];
try {
while (inputStream.available() > 0) {
//int numBytes = inputStream.read(readBuffer);
//System.out.println("numBytes" + numBytes);
//retValue = new String(readBuffer).trim();
//System.out.println(retValue);
BufferedReader comReader = new BufferedReader(new InputStreamReader(inputStream));
String temp = comReader.readLine().trim();
System.out.println(comReader.read() + ":" + temp);
if (temp.indexOf("#0") >= 0 || temp.indexOf("#8") >= 0) {
int p = temp.indexOf("#");
temp = temp.substring(p + 3, temp.length()).replace(" ", "").trim();
try {
Integer.parseInt(temp);
} catch (Exception e) {
System.out.println("忽略次數據:" + temp);
LogInfo.appendLog("忽略次數據:" + temp);
retValue = "0";
return;
}
System.out.println(temp);
retValue = String.valueOf(Integer.parseInt(temp.substring(1, 5))) + "." + temp.substring(5, 6);
System.out.println(retValue);
}
}
//strWGT = new String(readBuffer).trim();
//System.out.println(strWGT);
//輸出讀入的字符
} catch (IOException e) {
LogInfo.appendLog(e.getMessage());
}
break;
}
}
}
public static void main(String[] args) {
Readst reader = new Readst();
reader.init();
reader.retValue = "0";
//(reader.retValue);
}
}
//此程序只用於監聽COM3端口,當程序執行時開啓COM3端口,等待30秒後,端口COM3關閉,程序隨之關閉
//Read 爲內部類,這樣可以共用讀取出來的字符串信息
//在Readstr 中定義一個靜態變量static String str = "000000";
//然後在讀出數據後str = new String(readBuffer).trim();
//這樣可以在一個事件觸發後,將str中的值賦給相應的值(比如說,讓這個值顯示在輸入框中)
java 讀取串口 com 外部設備
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.