Raspberry PI Tomcat7和使用ISO8859-7的串行端口通信

时间:2016-06-16 09:34:12

标签: raspberry-pi

我试图在Tomcat 7上将来自Servlet的消息中继到覆盆子pi中的COM。

我在覆盆子和我的电脑之间使用空电缆进行测试。

我正在使用jssc API(Java Simple Serial Connector)进行串行通信。

Raspberry pi正在使用JDK 1.8.0_65。

我收到UTF8中的消息,我应该在ISO8859-7中输出。

由于UTF8是ISO8859-7的超集,因此调用servlet的应用程序可确保发送的所有字符对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中获得有效输出。 putty output

我已将putty配置为显示ISO8859-7字符。

任何变化? 我错过了什么?

提前致谢。

1 个答案:

答案 0 :(得分: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 64bit中正常工作,它使用正确的字符在putty中生成输出。

当我在raspberry PI中编译并运行相同的代码时,putty中的输出没有显示有效字符。

我倾向于认为这是一个覆盆子PI配置问题。