2013-12-09 34 views
1

我正在開發一個需要連接rs232與外設的小軟件...如何寫在rs232連接上

我已經下載了RXTX庫的最新版本,並且從同一個站點使用類TwoWaySerialComm通過串行電纜

http://rxtx.qbang.org/wiki/index.php/Event_based_two_way_Communication

import gnu.io.CommPort; 
import gnu.io.CommPortIdentifier; 
import gnu.io.SerialPort; 

import java.io.FileDescriptor; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

public class TwoWaySerialComm 
{ 
    public TwoWaySerialComm() 
    { 
     super(); 
    } 

    void connect (String portName) throws Exception 
    { 
     CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName); 
     if (portIdentifier.isCurrentlyOwned()) 
     { 
      System.out.println("Error: Port is currently in use"); 
     } 
     else 
     { 
      CommPort commPort = portIdentifier.open(this.getClass().getName(),2000); 

      if (commPort instanceof SerialPort) 
      { 
       SerialPort serialPort = (SerialPort) commPort; 
       serialPort.setSerialPortParams(57600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); 

       InputStream in = serialPort.getInputStream(); 
       OutputStream out = serialPort.getOutputStream(); 

       (new Thread(new SerialReader(in))).start(); 
       (new Thread(new SerialWriter(out))).start(); 

      } 
      else 
      { 
       System.out.println("Error: Only serial ports are handled by this example."); 
      } 
     }  
    } 

    /** */ 
    public static class SerialReader implements Runnable 
    { 
     InputStream in; 

     public SerialReader (InputStream in) 
     { 
      this.in = in; 
     } 

     public void run() 
     { 
      byte[] buffer = new byte[1024]; 
      int len = -1; 
      try 
      { 
       while ((len = this.in.read(buffer)) > -1) 
       { 
        System.out.print(new String(buffer,0,len)); 
       } 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      }    
     } 
    } 

    /** */ 
    public static class SerialWriter implements Runnable 
    { 
     OutputStream out; 

     public SerialWriter (OutputStream out) 
     { 
      this.out = out; 
     } 

     public void run() 
     { 
      try 
      {     
       int c = 0; 
       while ((c = System.in.read()) > -1) 
       { 
        this.out.write(c); 
       }     
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      }    
     } 
    } 

    public static void main (String[] args) 
    { 
     try 
     { 
      (new TwoWaySerialComm()).connect("COM3"); 
     } 
     catch (Exception e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

據說現在我要發送文本「STX 1000 T ETX」的設備,我怎麼做溝通?

感謝

回答

0

您關注的例子是寫你在控制檯(System.in)輸入的一切,拿出來的串行端口。您需要修改程序,以便輸出特定的字符串,而不是輸入任何內容。可能的解決方案太多了,無法在此處提供答案,並且需要至少具有Java編程的一些背景知識。

作爲一個開始,雖然,考慮創建一個新的類是這樣的:

public class MyCustomSerialWriter 
{ 
    OutputStream out; 

    public SerialWriter (OutputStream out) 
    { 
     this.out = out; 
    } 

    public void writeString (String str) 
    { 
     try { 
      this.out.write(str.getBytes(Charset.forName("ASCII")); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     }    
    } 
} 

然後,您可以從您的主要方法調用它的任何字符串你想輸出。