Java 读取rs232时随机换行
我正在开发一个java程序,它通过rs232编写命令并读取温度计的输出。 我正在使用。编写部分工作正常,但当我读取输出并将其转换为字符串并使用System.out.println()打印时,会出现一些随机的新行。当我使用System.out.write()编写结果时,一切正常。 我检查了字节码,没有发现任何NL字符 这是我的密码:Java 读取rs232时随机换行,java,serial-port,newline,Java,Serial Port,Newline,我正在开发一个java程序,它通过rs232编写命令并读取温度计的输出。 我正在使用。编写部分工作正常,但当我读取输出并将其转换为字符串并使用System.out.println()打印时,会出现一些随机的新行。当我使用System.out.write()编写结果时,一切正常。 我检查了字节码,没有发现任何NL字符 这是我的密码: public boolean openPort(int rate, int databits, int stopbit, int parity){ try {
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()的输出:
以及所需的write()输出
您会说“为什么不直接使用write()?”,但我需要将此输出转换为字符串
有人能帮我吗?
println
在末尾添加一个换行符。要不添加这些内容,请使用print
我最近也遇到了同样的问题。
最近,我开始研究用Pi读取智能电能表。
我遇到了JSSC,决定试一试。
但我花了好几个小时试图摆脱这些不需要的新词后,我改用了RXTX。
现在这个很有魅力
看
下面是这个项目的一些早期代码。
希望这有帮助
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();
}
}
您所需输出中的温度计是否在4个值后发送换行符?你说它是期望的输出,但你在对Hot Clicks回答的评论中想知道,你不期望换行,我很困惑。是的,我可能解释得很糟糕。。。我想在4个值之后有一个新行。但是当我使用println()时,我忘了用print()测试的每个地方都有新行。。。但是为什么我的字符串长度不一样呢?你的字符串长度不一样,因为在一种情况下,设备在“事件”之间发送两个读数,所以你只能对这两个事件执行一个println。试着从你得到的字节缓冲区创建一个字符串,然后修剪它。您可能正在读取不可预知的数据,串行上格式良好的字符串是罕见的,这是天赐良机。
--- START (C) ---
21.0,21.1,21.3,21.1
21.0,21.1,21.3,21.1
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();
}
}