[英]Java timeout period to read serial port
我有一個通過 rxtx 讀取串口的代碼。 但是我希望 input-stream 的 bufferedreader 應該檢查數據是否到來的指定時間。 超時后,它應該停止我開始讀取 serialReader 的程序或線程。
你可以這樣做:
ExecutorService exec = new Executors.newSingleThreadExecutor();
// ... where you want to start reading and wait:
Future<String> readResult = exec.submit( new Callable<String>(){
@Override public String call(){
// Your reading code here.
}
} );
try{
String strResult = readResult.get( 1, TimeUnit.MINUTES );
} catch(TimeoutException tex) {
// handle Timeout
} catch ( /*... you should also catch other exceptions like */ IOException ioex ){}
只是初學者的大綱。 讀取操作將在單獨的線程上完成。 然后在主線程上等待它完成,捕獲 TimeoutException。 如果發生,您可以清理您的資源(關閉 COM 端口!!)並退出。 如果沒有發生,則讀取成功。
一些資源:
根據您的代碼:
public class SerialReaderWithTimeout implements Runnable {
private final InputStream inputStream;
private final ExecutorService exec = Executors.newSingleThreadExecutor();
public SerialReaderWithTimeout(InputStream in) {
inputStream = in;
br = new BufferedReader(new InputStreamReader(inputStream));
try {
bw = new BufferedWriter(new FileWriter(FILENAME));
} catch (IOException e) {
e.printStackTrace();
}
}
private String readWithTimeout(){
String strResult = null;
Future<String> readResult = exec.submit( new Callable<String>(){
@Override public String call(){
return br.readLine();
}} );
try{
strResult = readResult.get( 10, TimeUnit.MINUTES );
} catch(TimeoutException tex) {
// handle Timeout: NOP -> Just return null
} catch ( Exception ex ){
// handle Exception
throw;
}
return strResult;
}
public void run() {
String inputLine = null;
try {
//while ((inputLine = br.readLine()) != null) {
while ((inputLine = readWithTimeout()) != null) {
//series.put(num, inputLine);
//num++;
bw.write(inputLine);
bw.newLine();
bw.flush();
System.out.println(inputLine);
if (inputLine.trim().startsWith("DATE") | inputLine.trim().startsWith("REMARK")
| inputLine.trim().startsWith("TESTED BY")) {
serialPort.close();
inputStream.close();
br.close();
bw.close();
break;
}
}
//new CompareClass();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
請注意,我沒有使用 IDE 來編寫或編譯它,可能有錯別字,需要一些調整。
public ReadWithoutTimings() {
super();
num = 1;
try {
try {
serialPort = (SerialPort) portId.open("Read1", 2000);
} catch (PortInUseException e) {
System.out.println(e);
JOptionPane.showMessageDialog(null, e);
}
try {
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e1) {
System.out.println(e1);
}
try {
in = serialPort.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*timer.schedule(new TimerTask() {
@Override
public void run() {
(new Thread(new SerialReader(in))).start();
}
}, time);*/
(new Thread(new SerialReader(in))).start();
}
public class SerialReader implements Runnable {
public SerialReader(InputStream in) {
inputStream = in;
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
br = new BufferedReader(new InputStreamReader(inputStream));
try {
bw = new BufferedWriter(new FileWriter(FILENAME));
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
String inputLine = null;
try {
while ((inputLine = br.readLine()) != null) {
//series.put(num, inputLine);
//num++;
bw.write(inputLine);
bw.newLine();
bw.flush();
System.out.println(inputLine);
if (inputLine.trim().startsWith("DATE") | inputLine.trim().startsWith("REMARK")
| inputLine.trim().startsWith("TESTED BY")) {
serialPort.close();
inputStream.close();
br.close();
bw.close();
break;
}
}
//new CompareClass();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
可能將 Timer (Schedule) 與您的讀取循環結合使用。
使用如下 -> timer.schedule('your_read_task','time_when_you_want_to_end')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.