我正在开发一个java程序,它通过rs232编写命令并读取温度计的输出。 我正在使用JSSC。写入部分工作正常,但是当我读取输出并将其转换为String并使用System.out.println()打印时,会出现一些随机的新行。当我用System.out.write()写结果时,一切正常。 我检查了字节码,但没有找到任何NL字符。
继承我的代码:
public boolean openPort(int rate, int databits, int stopbit, int parity){
try {
serialPort.openPort();
serialPort.setParams(rate, databits, stopbit, parity);
int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR;//Prepare mask
serialPort.setEventsMask(mask);//Set mask
serialPort.addEventListener(new SerialPortReader());//Add SerialPortEventListener
return true;
} catch (SerialPortException e) {
System.out.println(e);
return false;
}
}
static class SerialPortReader implements SerialPortEventListener {
public void serialEvent(SerialPortEvent event) {
if(event.isRXCHAR()){//If data is available
try {
byte buffer[] = serialPort.readBytes(event.getEventValue());
//with system.out.write
try {
System.out.write(buffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//with system.out.println
String readed = new String(buffer);
System.out.println(readed);
}
catch (SerialPortException ex) {
System.out.println(ex);
}
}
else if(event.isCTS()){//If CTS line has changed state
if(event.getEventValue() == 1){//If line is ON
System.out.println("CTS - ON");
}
else {
System.out.println("CTS - OFF");
}
}
else if(event.isDSR()){///If DSR line has changed state
if(event.getEventValue() == 1){//If line is ON
System.out.println("DSR - ON");
}
else {
System.out.println("DSR - OFF");
}
}
}
}
这是println()的输出:
--- START (C) ---
21.0,
21.1,21.3,
21.1
21.0,
21.2,21.3,
21.2
使用write()
获得所需的输出--- START (C) ---
21.0,21.1,21.3,21.1
21.0,21.1,21.3,21.1
你会说“你为什么不使用write()?”,但我需要将此输出转换为字符串。
有人可以帮助我吗?
答案 0 :(得分:1)
我最近遇到了同样的问题。 最近我开始研究用我的Pi读取我的智能电表。 我遇到了JSSC,并决定尝试一下。 但是经过几个小时试图摆脱这些不必要的新行,我转而使用RXTX。 这就像现在的魅力一样。
请参阅http://rxtx.qbang.org/wiki/index.php/Main_Page
以下是该项目的一些早期代码。 希望这会有所帮助。
package nl.barendregtict.homeautomation;
import gnu.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;
/**
* Created by sbarendr on 09/11/14.
*/
public class KaifaSmartMeterReader implements SerialPortEventListener {
CommPortIdentifier portId1;
SerialPort serialPort1;
InputStream inputStream;
Thread readThread;
public KaifaSmartMeterReader() {
try {
portId1 = CommPortIdentifier.getPortIdentifier("/dev/ttyUSB0");
serialPort1 = (SerialPort) portId1.open("KaifaSmartMeterReader", 2000);
System.out.println(portId1.getName() + " opened");
inputStream = serialPort1.getInputStream();
serialPort1.addEventListener(this);
serialPort1.notifyOnDataAvailable(true);
serialPort1.setSerialPortParams(115200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort1.setDTR(false);
serialPort1.setRTS(false);
Thread.sleep(30000);
serialPort1.close();
} catch (
PortInUseException
| IOException
| TooManyListenersException
| UnsupportedCommOperationException
| InterruptedException
| NoSuchPortException e) {
e.printStackTrace();
}
}
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:
StringBuffer readBuffer = new StringBuffer();
int c;
try {
while ((c = inputStream.read()) != 10) {
if (c != 13) readBuffer.append((char) c);
}
String scannedInput = readBuffer.toString();
System.out.println(scannedInput);
}
catch(IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
KaifaSmartMeterReader reader = new KaifaSmartMeterReader();
}
}
答案 1 :(得分:0)
println
在最后添加换行符。要不添加,请使用print
。