2016-06-16 7 views
0

を使用して、私は私がテストするためにラズベリーと私のPC間ヌルケーブルを使用していますTomcatの7ラズベリーPI Tomcat7とシリアルポート通信ISO8859-7

にラズベリーパイにCOMにサーブレットからのメッセージを中継しようとしています。

シリアル通信にjssc API(Java Simple Serial Connector)を使用しています。

ラズベリーパイはJDK 1.8.0_65を使用しています。

私はUTF8でメッセージを取得しています。ISO8859-7で出力する必要があります。

UTF8はISO8859-7のスーパーセットなので、サーブレットを呼び出すアプリケーションは、送信されたすべての文字がISO8859-7の正当なものであることを保証します。

マイコード:

package com.test.servlet; 

import java.io.IOException; 
import java.io.PrintWriter; 
import java.nio.ByteBuffer; 
import java.nio.CharBuffer; 
import java.nio.charset.Charset; 
import java.nio.charset.StandardCharsets; 
import java.util.ResourceBundle; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

import jssc.*; 

import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

@WebServlet(value = "/Relay", name = "Relay") 
public class Relay extends HttpServlet { 

    static Logger app = null; 
    static { 
     app = Logger.getLogger("com.test.app"); 
    } 

    protected void doGet(HttpServletRequest request,HttpServletResponse response) { 
     doPost(request, response); 
    } 

    protected void doPost(HttpServletRequest request,HttpServletResponse response) { 

     try { 
      request.setCharacterEncoding("ISO-8859-7");; 
      response.setCharacterEncoding("ISO-8859-7"); 
      //request.setCharacterEncoding("UTF-8"); 
      //response.setCharacterEncoding("UTF-8") 
      response.setContentType("text/html"); 
      PrintWriter out = response.getWriter(); 

      String message = request.getParameter("message"); 



      app.logp(Level.INFO, this.getClass().getCanonicalName(),"APP", message); 
      String[] portNames = SerialPortList.getPortNames(); 

      app.logp(Level.INFO, this.getClass().getCanonicalName(),"APP", portNames.length+""); 

      for(int i = 0; i < portNames.length; i++){ 

       applogp(Level.INFO, this.getClass().getCanonicalName(),"APP", portNames[i]); 

       byte[] msg = new byte[1024]; 
       msg = message.getBytes("ISO-8859-7"); 

       Charset utf8charset = Charset.forName("UTF-8"); 
       Charset iso88597charset = Charset.forName("ISO-8859-7"); 
       ByteBuffer inputBuffer = ByteBuffer.wrap(message.getBytes()); 
       CharBuffer data = utf8charset.decode(inputBuffer); 
       ByteBuffer outputBuffer = iso88597charset.encode(data); 
       byte[] outputData = outputBuffer.array(); 


       byte[] b1 = message.getBytes(); 
       byte[] b2 = message.getBytes(Charset.forName("ISO-8859-7")); 
       byte[] b3 = message.getBytes(StandardCharsets.ISO_8859_1); 


       SerialPort serialPort = new SerialPort((portNames[i])); 
       try { 
        serialPort.openPort(); 
        serialPort.setParams(SerialPort.BAUDRATE_9600,SerialPort.DATABITS_8, SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); 
        serialPort.writeBytes(msg); 
        serialPort.writeBytes(message.getBytes()); 
        serialPort.writeBytes(outputData); 
        serialPort.writeBytes(b1); 
        serialPort.writeBytes(b2); 
        serialPort.writeBytes(b3); 
        serialPort.closePort(); 


       } catch (SerialPortException ex) { 
        app.logp(Level.INFO, this.getClass().getCanonicalName(),"APP", ex.getMessage()); 
        out.write("NOK"); 
        out.close(); 
       } 
      } 
      out.write("OK"); 
      out.close(); 

     } catch (IOException e) { 
      app.logp(Level.INFO, this.getClass().getCanonicalName(),"APP", e.getMessage()); 
     } 
    } 

    private static final long serialVersionUID = 1L; 
} 

問題は、私がテストしていたとき、私はパテで有効な出力を得ることはありませんということです。 putty output

私は、ISO8859-7文字を表示するようにパテントを構成しました。

変更しますか? 私は何が欠けていますか?

ありがとうございます。

答えて

0

私は、次のコードを生成することによって、問題を分割しようとした:

import java.io.UnsupportedEncodingException; 

import jssc.SerialPort; 
import jssc.SerialPortException; 

public class SerialTest { 
    public static void main(String[] args) { 
     String message = "message μήνυμα"; 
     if (sendTextOnCom(message)) { 
      System.out.println("SUCCESS MESSAGE SENT"); 
     } 
     else{ 
      System.out.println("FAIL MESSAGE NOT SENT"); 
     } 

    } 
    private static boolean sendTextOnCom(String message) { 
     boolean isOverlaid = false; 
     SerialPort com = null; 
     try { 
      String comNo = "COM1"; // String comNo="/dev/ttyUSB0"; //when used in Raspberry 
      com = new SerialPort(comNo); 
      com.openPort(); 
      com.setParams(9600, 8, 1, 0);   
      com.writeString(message); 
      com.writeBytes(message.getBytes("ISO-8859-7")); 
      com.closePort(); 
      isOverlaid = true; 
     } 
     catch (SerialPortException ex) { 
      System.out.println("[ERROR] COM ERROR SENDING MESSAGE"); 
      isOverlaid = false; 
      try { 
       com.closePort(); 
      } catch (SerialPortException e) { 
       e.printStackTrace(); 
      } 
     } catch (UnsupportedEncodingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return isOverlaid; 
    } 
} 

コードは、Windows 7の64ビット版で正常に作動していると、それは右の文字でパテで出力を生成しています。

ラズベリーPIで同じコードをコンパイルして実行すると、パテントの出力に有効な文字が表示されません。

私はそれがラズベリーPI構成の問題だと思う傾向があります。

関連する問題