2

https://code.google.com/p/java-simple-serial-connector/wiki/jSSC_examplesに示されている例を変更して、Java プログラムからの読み取り/書き込みを示しました。プログラムは実行できますが、serialPort.writeString("HelloWorld"); を使用してデータを送信します。SerialPortReader イベント クラスで読み取られないようです。問題が何であるかを教えてください。

 public class SerialReaderWriter {

static SerialPort serialPort;

public static void main(String[] args) {
    serialPort = new SerialPort("COM1"); 
    try {
        serialPort.openPort();
        serialPort.setParams(9600, 8, 1, 0);
        //Preparing a mask. In a mask, we need to specify the types of events that we want to track.
        //Well, for example, we need to know what came some data, thus in the mask must have the
        //following value: MASK_RXCHAR. If we, for example, still need to know about changes in states 
        //of lines CTS and DSR, the mask has to look like this: SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR
        int mask = SerialPort.MASK_RXCHAR;
        //Set the prepared mask
        serialPort.setEventsMask(mask);
        //Add an interface through which we will receive information about events
        serialPort.addEventListener(new SerialPortReader());

        serialPort.writeString("HelloWorld");
    }
    catch (SerialPortException ex) {
        System.out.println(ex);
    }
}

static class SerialPortReader implements SerialPortEventListener {

    public void serialEvent(SerialPortEvent event) {
        //Object type SerialPortEvent carries information about which event occurred and a value.
        //For example, if the data came a method event.getEventValue() returns us the number of bytes in the input buffer.
        System.out.println(event.getEventType());
        if(event.isRXCHAR()){
            if(event.getEventValue() == 10){
                try {
                    String data= serialPort.readString();
                    System.out.println(data);
                }
                catch (SerialPortException ex) {
                    System.out.println(ex);
                }
            }
        }
        //If the CTS line status has changed, then the method event.getEventValue() returns 1 if the line is ON and 0 if it is OFF.
        else if(event.isCTS()){
            if(event.getEventValue() == 1){
                System.out.println("CTS - ON");
            }
            else {
                System.out.println("CTS - OFF");
            }
        }
        else if(event.isDSR()){
            if(event.getEventValue() == 1){
                System.out.println("DSR - ON");
            }
            else {
                System.out.println("DSR - OFF");
            }
        }
    }
}

}

4

3 に答える 3

0

コマンド

serialPort.writeString("HelloWorld");

文字列を送信します

こんにちは世界

COM1から。実装している SerialPortReader クラスにより、COM1はイベント タイプisRXCHAR (COM1が charを受信したときの別名) をリッスンします。

シリアルポートにシリアルケーブルが接続されていますか?

COM1 の RX および TX ピンを交差させるか、別の COM ポート (TX および RX がそれぞれ COM1 の RX および TX ピンに接続されている) を持たない限り、SerialPortReader はアクティブになりません。

于 2013-05-16T19:07:38.133 に答える