2012-11-14 21 views
0

我正在編寫一個java程序,該程序通過rs232寫入命令並讀取溫度計的輸出。 我正在使用JSSC。寫入部分工作正常,但是當我讀取輸出並將其轉換爲字符串並使用System.out.println()打印時,會出現一些隨機的新行。當我用System.out.write()寫結果時,一切正常。 我檢查了字節碼,我沒有找到任何NL字符。讀取rs232時出現隨機新行rs232

繼承人我的代碼:

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 outpout():

--- START (C) --- 

21.0, 
21.1,21.3, 
21.1 

21.0, 
21.2,21.3, 
21.2 

而且具有寫入所需的輸出()

--- START (C) --- 
21.0,21.1,21.3,21.1 
21.0,21.1,21.3,21.1 

你會說「你爲什麼不使用write()?「,但我需要將這個輸出轉換爲一個字符串。

有人可以幫助我嗎?

+1

您想要的輸出中的溫度計是否在4個值之後發送換行符?你說它的期望輸出,但你想知道在熱舔回答你不期待換行符,我很困惑。 – Pedantic

+0

是的,我可能已經解釋得很糟糕......我想在4個值之後有一個新的行。但是當我使用println()時,到處都是新線 – loics2

回答

0

println在最後添加換行符。要不添加,請使用print

+0

我忘了用print()來測試......但是爲什麼我的字符串長度不一樣? – loics2

+0

您的字符串長度不一樣,因爲在一種情況下,設備會設法在「事件」之間發送兩個讀數,因此您只需爲它們兩個執行一次println。 –

+0

嘗試從您獲得的字節緩衝區中創建一個字符串,並對其進行修剪。您可能會讀取不可預知的數據,串行格式良好的字符串很少見,也是天賜之物。 – Pedantic

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(); 
    } 
}