lxbzmy / java-simple-serial-connector

Automatically exported from code.google.com/p/java-simple-serial-connector
0 stars 0 forks source link

Problem with the readBytes method #2

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Reading one byte from the port with serialPort.readBytes(1);

What is the expected output? What do you see instead?

I had expected that the method would block if no bytes were available.
Instead the method returns the last byte which was received even when no new 
bytes have been received.

What version of the product are you using? On what operating system?
jSSC-0.8 Test Build #3, tested on Linux Fedora 15 and Windows XP.

Please provide any additional information below.
I had a problem with the readBytes method when trying to read only one byte 
with this...

serialPort.readBytes(1);

I had expected that the method would block if no bytes were available but in 
fact it appears that the method returns the last byte which was received each 
time it is called even when no new data was received on the RS232 cable.
I tried my code on Linux (Fedora 15) with an RS232 port on the motherboard and 
to confirm the result I tried on Windows with an FTDI USB-serial adapter. Both 
gave the same result.

The following method I wrote appears to solve the problem.
The method will not return until either
a. A byte is received on the RS232 cable or 
b. The thread is interrupted.

protected byte getByte() throws jssc.SerialPortException {
    while (serialPort.getInputBufferBytesCount() < 1) {
        try {
            Thread.sleep(1);
            if (Thread.currentThread().isInterrupted()) {
                return 0;
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return 0;
        }
    }
    return (byte)(serialPort.readBytes(1))[0];
}

Should the read bytes method blocked or have I misunderstood how it should work?

Original issue reported on code.google.com by arthur.m...@googlemail.com on 6 Oct 2011 at 8:11

GoogleCodeExporter commented 9 years ago
jSSC tested on FTDI and Prolific chips, and works correctly on it.

I wrote an example with your method, and it works fine:

public class Main {

    private static SerialPort serialPort;

    public static void main(String[] args) {
        serialPort = new SerialPort("COM15");
        try {
            serialPort.openPort();
            serialPort.setParams(9600, 8, 1, 0);
            while(true){
                System.out.println(getByte());
            }
        }
        catch (SerialPortException ex) {
            ex.printStackTrace();
        }
    }

    protected static byte getByte() throws jssc.SerialPortException {
        while (serialPort.getInputBufferBytesCount() < 1) {
            try {
                Thread.sleep(1);
                if (Thread.currentThread().isInterrupted()) {
                    return 0;
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                return 0;
            }
        }
        return (byte)(serialPort.readBytes(1))[0];
    }
}

Method getByte() blocks while input buffer is empty.

If you have not solve the problem, please mail me.

Original comment by scream3r.org@gmail.com on 7 Oct 2011 at 5:26

GoogleCodeExporter commented 9 years ago
I think that perhaps I did not express my question as clearly as I should have 
done.

The method serialPort.readBytes(1); does not block when there are no new bytes.
Should it block?
If it should block then there is a defect.
If it should not block then I have misunderstood how it works.

In my application I need a method which does block until a new byte is 
received. 
The method I suggested above does work for me successfully and the jSSC library 
seems to be very reliable.
I would consider fine tuning the line 
Thread.sleep(1);
if the baud rate of the interface is very slow.

Original comment by arthur.m...@googlemail.com on 11 Oct 2011 at 7:40

GoogleCodeExporter commented 9 years ago
The method readBytes(1) will blocks if your input buffer is empty. If you have 
any data in input buffer this method will return the one byte from it.

If you need to know that you have any data in input buffer try to use 
EventListener (last example on this page: 
http://code.google.com/p/java-simple-serial-connector/wiki/jSSC_examples)

Original comment by scream3r.org@gmail.com on 12 Oct 2011 at 5:06

GoogleCodeExporter commented 9 years ago

Original comment by scream3r.org@gmail.com on 23 Nov 2011 at 4:21

GoogleCodeExporter commented 9 years ago
Muy bueno ese ultimo metodo me sirbio de mucho
saludos.

Original comment by JhonMast...@gmail.com on 22 Oct 2014 at 12:14