0

我正在开发一个通过 rs232 编写命令和读取温度计输出的 java 程序。我正在使用JSSC。写入部分工作正常,但是当我读取输出并将其转换为字符串并使用 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()?”,但我需要将此输出转换为字符串。

有人能帮我吗?

4

2 回答 2

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();
    }
}
于 2014-12-12T09:41:33.897 回答
0

println在末尾添加一个换行符。要不添加,请使用print.

于 2012-11-14T20:56:27.347 回答