我在java中使用RXTX庫,如果我寫空間ascii(32)outpurstream我可以獲取數據,除非我無法獲取。我不知道爲什麼,它可能是一個問題?當設備斷開連接時RXTX仍然讀取數據
問題是:我從歐姆表中獲取數據,測量產品,但是當我脫下探針時,ohmetter顯示一個值,然後顯示「0.L」,但我總是得到最後的數據,所以我無法檢測如果ohmetter測量產品或不。
例如
02.00 OHM(coreect值)>> 125.20 OHM(同時起飛探頭)>> 0.L(起飛後探頭)
我總是得到125.20 OHM,我怎樣才能得到0.L的價值?
有什麼建議嗎?
編輯:
public class SerialComm {
SerialPort serialPort;
OutputStream outStream = null;
InputStream inStream = null;
public SerialComm() {
super();
}
void connect(String portName) throws Exception {
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(portName);
serialPort = (SerialPort) portId.open("gnu.io.CommPortIdentifier", 5000);
int baudRate = 9600;
serialPort.setSerialPortParams(
baudRate,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(
SerialPort.FLOWCONTROL_NONE);
setSerialListener();
}
public void initIOStream() throws InterruptedException {
try {
outStream = serialPort.getOutputStream();
inStream = serialPort.getInputStream();
write();
} catch (IOException ex) {
Logger.getLogger(SerialComm.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void disconnect() {
if (serialPort != null) {
try {
outStream.close();
inStream.close();
} catch (IOException ex) {
}
serialPort.close();
}
}
public void write() throws IOException, InterruptedException {
outStream.write(32);
outStream.flush();
}
private byte[] readBuffer = new byte[400];
private void readSerial() throws InterruptedException, Exception {
try {
initIOStream();
Thread.sleep(2000);
int availableBytes = inStream.available();
if (availableBytes > 0) {
inStream.read(readBuffer, 0, availableBytes);
System.out.println("value: " + new String(readBuffer, 0, 30));
}
} catch (IOException e) {
}
}
private class SerialEventHandler implements SerialPortEventListener {
@Override
public void serialEvent(SerialPortEvent event) {
System.out.println("event.getEventType()" + event.getEventType());
switch (event.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE: {
try {
readSerial();
} catch (Exception ex) {
Logger.getLogger(SerialComm.class.getName()).log(Level.SEVERE, null, ex);
}
}
break;
}
}
}
private class ReadThread implements Runnable {
@Override
public void run() {
while (true) {
try {
readSerial();
} catch (InterruptedException ex) {
Logger.getLogger(SerialComm.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(SerialComm.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public void setSerialListener() throws InterruptedException, TooManyListenersException {
new Thread(new ReadThread()).start();
}
public void setSerialEventHandler(SerialPort serialPort) throws Exception {
try {
initIOStream();
serialPort.addEventListener(new SerialEventHandler());
serialPort.notifyOnDataAvailable(true);
} catch (TooManyListenersException ex) {
System.err.println(ex.getMessage());
}
}
public static void main(String args[]) throws IOException, Exception {
SerialComm serial = new SerialComm();
serial.connect("COM3");
}
}
您使用的是什麼精確的歐姆表? – 2015-02-11 08:48:04
Prova Model 700 Ohmmetter – Chrial 2015-02-11 08:49:30