-1

http://rxtx.qbang.org/wiki/index.php/Event_Based_Two_Way_Communicationから改変されたコード

SerialReader クラスを使用して共通バッファーを読み取り、そのバッファーを SerialWriter クラスを介してシリアル経由で送信しようとしていますが、Writer が呼び出されるたびにバッファーが null として表示されます。コードは、TwoWaySerialCommTest の connect メソッドを使用して初期化されます (参考のために以下に貼り付けます)。

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

            public SerialWriter ( OutputStream out, byte[] buffer)
            {
                    this.out = out;
                    this.buffer = buffer;
            }

            public void run ()
            {
                    while(true)
                    {
                            lock.lock();
                            try
                        {
                            dataAvailable.await();
                            System.out.println("Waking up");
                            int i = 0;
                            if (this.buffer != null)
                            {
                                    System.out.println("Buffer isn't empty");
                                    while(buffer[i] != ((byte)'\n') && i < buffer.length - 1)
                                    {
                                            this.out.write(buffer[i]);
                                    }
                            }
                            else
                            {
                                    System.out.println("Buffer is null");
                                    System.out.println(this.buffer.toString());
                            }
                        }
                        catch ( IOException e )
                        {
                            e.printStackTrace();
                            System.exit(-1);
                        }
                        catch(Exception e)
                        {
                            e.printStackTrace();
                        }

                            finally
                            {
                                    lock.unlock();
                            }
                    }
            }
        }

シリアルリーダークラス

public static class SerialReader implements SerialPortEventListener
        {
            private InputStream in;
            byte[] buffer;

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

            public SerialReader (InputStream in, byte[] buffer)
            {
                    this.in = in;
                    this.buffer = buffer;
            }

            public void serialEvent(SerialPortEvent arg0) {
                lock.lock();
                    int data;
                    if (buffer != null)
                    {
                     for(int i = 0; i < buffer.length; i++)
                    {
                            if (buffer[i] != 0)
                            {
                                    System.out.print((char)buffer[i]);
                            }
                    }
                    }
                    buffer = new byte[1024];

                try
                {
                    int len = 0;
                    while ( ( data = in.read()) > -1 )
                    {
                        if ( data == '\n' ) {
                            break;
                        }
                        buffer[len++] = (byte) data;
                    }
                    System.out.println(new String(buffer,0,len));
                    for(int i = 0; i < buffer.length; i++)
                    {
                            if (buffer[i] != 0)
                            {
                                    System.out.print((char)buffer[i]);
                            }
                    }
                    System.out.println();
                    dataAvailable.signal();
                }
                catch ( IOException e )
                {
                    e.printStackTrace();
                    System.exit(-1);
                }
                finally
                {
                    lock.unlock();
                }
            }

        }

TwoWaySerialCommTest (切り捨て)

    import gnu.io.SerialPortEvent;
    import gnu.io.SerialPortEventListener;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.concurrent.locks.*;

    /**
     * This version of the TwoWaySerialComm example makes use of the
     * SerialPortEventListener to avoid polling.
     *
     */
    public class TwoWaySerialCommTest
    {
            static Lock lock = new ReentrantLock();
            static Condition dataAvailable = lock.newCondition();
            public volatile byte[] buffer;

            public TwoWaySerialCommTest()
        {
            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 SerialWriter(out , buffer))).start();

                    serialPort.addEventListener(new SerialReader(in , buffer));
                    serialPort.notifyOnDataAvailable(true);

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

1 に答える 1

1

1 つの問題は次の行のようです。

buffer = new byte[1024];

bufferそこで新しいバイト配列にローカルを割り当てたいとは思わない。これにより、ライターは、渡された配列ではなく、独自の配列に書き込むようになり、リーダーによって共有されます。のみを割り当てるbufferと、ライター内のローカル変数に影響します。

また、入力ストリームが上書きするのを止めるものは何bufferですか? 行の長さが 1024 を超えると、配列の範囲外の例外が発生します。

\n最後に、文字を に書き込んでいませんbuffer。そのため、リーダーは多数の\0ヌル文字を出力に出力します。

于 2013-04-01T19:25:21.773 に答える