More than required data sent on serial port through serial transport
--------------------------------------------------------------------

                 Key: DIRMINA-646
                 URL: https://issues.apache.org/jira/browse/DIRMINA-646
             Project: MINA
          Issue Type: Bug
          Components: Transport
    Affects Versions: 2.0.0-M3
         Environment: MINA : 2.0.0-M3
Netbeans 6.5
Serial port (both hardware COM1 and using com0com's virtual serial ports)
JDK 1.6.10
Windows XP (Service Pack 3)
            Reporter: Akbar Munir Chaudhary


The serial transport sends more then required data when IoSession.write() is 
called with the IoBuffer. The following code is the basic usage of serial 
transport:

<code>
SerialAddress a = new SerialAddress("COM1", 115200, 
SerialAddress.DataBits.DATABITS_8, SerialAddress.StopBits.BITS_1, 
SerialAddress.Parity.NONE, SerialAddress.FlowControl.NONE);
                IoConnector c = new SerialConnector();
                c.setHandler(this);
                ConnectFuture cf = c.connect(a);
                cf.awaitUninterruptibly();

                System.out.println("Connection = " + cf.isConnected());

                if (cf.isConnected())
                {
                        IoSession s = cf.getSession();
                        IoBuffer b = IoBuffer.allocate(32);
                        b.put(new String("this is a test message").getBytes());
                        b.flip();

                        WriteFuture wf = s.write(b);
                        wf.awaitUninterruptibly(5, TimeUnit.SECONDS);
                        System.out.println("Message Written = " + 
wf.isWritten());
                }
</code>

The message <code>this is a test message</code> should have been sent on the 
serial port COM1. But the actual output received is (output captured through 
HDD Free Serial Port Monitor) :

<code>
74 68 69 73 20 69 73 20 61 20 74 65 73 74 20 6D   this is a test m
 65 73 73 61 67 65 00 00 00 00 00 00 00 00 00 00   essage..........
</code>

I have looked into the code, and the reason appears to be the following 
statement on line # 18 in the file SerialSessionImpl.java.

<code>
outputStream.write(buf.array());
</code>

Since buf.array() returns the complete array in the IoBuffer, regardless of the 
actual count of valid data, so all bytes are sent. I changed this statement to:

<code>
outputStream.write(buf.array(), buf.position(), writtenBytes);
</code>

to ensure that only the required bytes starting from the first unread position 
is sent on the serial port. This works so far for all my cases.

Thanks,
Akbar.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to