Fazecast / jSerialComm

Platform-independent serial port access for Java
GNU Lesser General Public License v3.0
1.34k stars 286 forks source link

readBytes always empty byte-array #565

Open Bizarrus opened 4 months ago

Bizarrus commented 4 months ago

The method <serialPort>.readBytes​(byte[] buffer, int bytesToRead) wont fill the byte array.

Version: 2.11.0

Sample

import com.fazecast.jSerialComm.SerialPort;

public class Main {
    private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = HEX_ARRAY[v >>> 4];
            hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
        }
        return new String(hexChars);
    }

    public static void main(String[] args) {
        String target = "COM3";
        SerialPort connection = null;       
        SerialPort[] ports = SerialPort.getCommPorts();

        System.out.println("+++++ List Serial Devices +++++");

        for(SerialPort port : ports) {
            System.out.println("Name: " + port.toString() + ", BaudRate=" +  port.getBaudRate() + ", Port=" + port.getSystemPortName());

            if(target.equals(port.getSystemPortName())) {
                connection = port;
                break;
            }
        }

        System.out.println("");

        connection.setComPortParameters(56700, 8, SerialPort.TWO_STOP_BITS, SerialPort.NO_PARITY, false);
        //connection.setBaudRate(56700);
        //connection.setFlowControl(SerialPort.FLOW_CONTROL_DISABLED);
        //connection.setNumDataBits(8);
        //connection.setNumStopBits(SerialPort.ONE_STOP_BIT);
        //connection.setParity(SerialPort.NO_PARITY);
        connection.setComPortTimeouts(SerialPort.TIMEOUT_NONBLOCKING, 0, 0);
        if(!connection.openPort()) {
            System.err.println("Port already used/blocked!");
            System.exit(-1);
        }

        try {
               while (true)
               {
                  while (connection.bytesAvailable() == 0)
                     Thread.sleep(20);

                  byte[] readBuffer = new byte[connection.bytesAvailable()];
                  int numRead = connection.readBytes(readBuffer, readBuffer.length, 0);
                  System.out.println("[" + numRead + " bytes] " + bytesToHex(readBuffer));
               }
            } catch (Exception e) { e.printStackTrace(); }

        connection.closePort();

    }

}

Output

+++++ List Serial Devices +++++
Name: Kommunikationsanschluss (COM1), BaudRate=9600, Port=COM1
Name: com0com - serial port emulator, BaudRate=9600, Port=COM6
Name: com0com - serial port emulator, BaudRate=9600, Port=COM5
Name: Prolific USB-to-Serial Comm Port (COM3), BaudRate=9600, Port=COM3

[13 bytes] 00000000000000000000000000
[1 bytes] 00
[1 bytes] FC
[372 bytes] 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
[132 bytes] 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
[200 bytes] 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
NorbertSandor commented 4 months ago

You should check using another tool (with the same serial port settings) that the device really doesn't send zeros, just to be sure (especially because a non-zero byte 0xFC has been received as well). This readBytes() method is a very "core" one, I use it in my app without problems (although with TIMEOUT_READ_SEMI_BLOCKING mode, using an older library version 2.10.2). (Otherwise, I see no problem in your code, the reading loop is almost the same as in the offical example.)