Fazecast / jSerialComm

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

SerialPort.readBytes(byte[] buffer, long bytesToRead) - Stopping reading bytes #217

Closed DanielMartensson closed 5 years ago

DanielMartensson commented 5 years ago

Hello!

I found a bug in the JSerialComm package. First i use the program CuteCom in Linux to open the com port and read. It works fine. All I see is the letter "BBBBBBBB" where I sending from a STM32 micro controller.

Then I try to read with Java.

package se.danielmartensson.controller;

import java.util.ArrayList;

import com.fazecast.jSerialComm.SerialPort;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;

public class Connection {

    /*
     * Fields
     */
    private ArrayList<SerialPort> portArray = new ArrayList<>();
    private SerialPort serialPort;
    private float[] measurements = new float[12];

    @FXML
    private ComboBox<String> comPorts;

    @FXML
    private TextField status;

    @FXML
    private Button connectButton;

    @FXML
    private Button disconnectButton;

    @FXML
    void connect(ActionEvent event) {
        connectButton.setStyle("-fx-background-color: #53ff1a"); // Green
        disconnectButton.setStyle("-fx-background-color: #ff0000"); // Red

        /*
         * Open port
         */
        if(portArray.get(0) != null) {
            int index = comPorts.getSelectionModel().getSelectedIndex();
            serialPort = portArray.get(index);
            serialPort.setBaudRate(11520);
            serialPort.openPort();

            /*
             * Disable
             */
            connectButton.setDisable(true);
            disconnectButton.setDisable(false);
            comPorts.setDisable(true);

            /*
             * Set status
             */
            status.setText("Connected!");

        }else {
            /*
             * Set status
             */
            status.setText("No COM ports!");
        }
    }

    @FXML
    void disconnect(ActionEvent event) {
        disconnectButton.setStyle("-fx-background-color: #53ff1a"); // Green
        connectButton.setStyle("-fx-background-color: #ff0000"); // Red

        /*
         * Close port
         */
        if(portArray.get(0) != null) {
            int index = comPorts.getSelectionModel().getSelectedIndex();
            serialPort = portArray.get(index);
            serialPort.closePort();

            /*
             * Enable
             */
            connectButton.setDisable(false);
            disconnectButton.setDisable(true);
            comPorts.setDisable(false);

            /*
             * Set status
             */
            status.setText("Disconnected!");
        }else {
            /*
             * Set status
             */
            status.setText("No COM ports!");
        }
    }

    @FXML
    void initialize() {
        /*
         * Add all connected ports. 
         */
        SerialPort[] ports = SerialPort.getCommPorts();
        for(int i = 0; i < ports.length; i++) {
            comPorts.getItems().add(ports[i].getDescriptivePortName());
            portArray.add(ports[i]);
        }

        /*
         * if ports.length == 0
         */
        if(ports.length == 0) {
            portArray.add(null);
        }

        /*
         * Set the first selected port
         */
        comPorts.getSelectionModel().selectFirst();

        /*
         * Initial disconnect
         */
        disconnect(null);

        /*
         * Disable the status box
         */
        status.setDisable(true);
    }

    /*
     * Get the measurements
     */
    public float[] getMeasurements() {
        int receivedBytes = serialPort.bytesAvailable();
        System.out.println(receivedBytes);
        if(receivedBytes >= 12) {
            /*
             * Create our buffer
             */
            byte[] buffer = new byte[12];
            byte[] flush = new byte[receivedBytes-buffer.length];

            /*
             * Read bytesGet bytes so we will empty the buffer
             */
            serialPort.readBytes(buffer, buffer.length);

            /*
             * Then convert two uint8 to uint16 and then sum them to float. We need 12 bytes in total to get 6 values
             */
            for(int i = 0; i < buffer.length; i++) {
                System.out.println("Buffer[" + i + "] = " + buffer[i]);
            }

            /*
             * Read all so the COM port buffer will be empty
             */
            serialPort.readBytes(flush, receivedBytes-buffer.length); 

        }
        return measurements;
    }

}

And the output will be:


835
Buffer[0] = 66
Buffer[1] = 66
Buffer[2] = 66
Buffer[3] = 66
Buffer[4] = 66
Buffer[5] = 66
Buffer[6] = 66
Buffer[7] = 0
Buffer[8] = 0
Buffer[9] = 0
Buffer[10] = 0
Buffer[11] = 0
24
Buffer[0] = 0
Buffer[1] = 0
Buffer[2] = 0
Buffer[3] = 0
Buffer[4] = 0
Buffer[5] = 0
Buffer[6] = 0
Buffer[7] = 0
Buffer[8] = 0
Buffer[9] = 0
Buffer[10] = 0
Buffer[11] = 0
22
Buffer[0] = 0
Buffer[1] = 0
Buffer[2] = 0
Buffer[3] = 0
Buffer[4] = 0
Buffer[5] = 0
Buffer[6] = 0
Buffer[7] = 0
Buffer[8] = 0
Buffer[9] = 0
Buffer[10] = 0
Buffer[11] = 0
22
Buffer[0] = 0
Buffer[1] = 0
Buffer[2] = 0
Buffer[3] = 0
Buffer[4] = 0
Buffer[5] = 0
Buffer[6] = 0
Buffer[7] = 0
Buffer[8] = 0
Buffer[9] = 0
Buffer[10] = 0
Buffer[11] = 0

What happen? Why did it stop? I'm expecting number 66 (letter B) all the times. If i try with Linux terminal it also works.

asus@asus-pc:~$ cat < /dev/ttyACM0                                              
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBasus@asus-pc:~$ 
hedgecrw commented 5 years ago

You are setting your baud rate to a very low, non-standard value (11520) which could be causing some issues. Try either not setting it at all, or setting it to something standard (probably 115200 is what you meant), and see if that fixes it. If not, please also print out what the return value from each of the readBytes() calls is and post that here as well. Thanks!

DanielMartensson commented 5 years ago

Wow! How can I be so blind! It working now when I set the correct baudrate. :) Great library!

4076
Buffer[0] = 0
Buffer[1] = 0
Buffer[2] = 0
Buffer[3] = 0
Buffer[4] = 0
Buffer[5] = 0
Buffer[6] = 0
Buffer[7] = 0
Buffer[8] = 0
Buffer[9] = 0
Buffer[10] = 0
Buffer[11] = 0
1118
Buffer[0] = 66
Buffer[1] = 66
Buffer[2] = 66
Buffer[3] = 66
Buffer[4] = 66
Buffer[5] = 66
Buffer[6] = 66
Buffer[7] = 66
Buffer[8] = 66
Buffer[9] = 66
Buffer[10] = 66
Buffer[11] = 66
672
Buffer[0] = 66
Buffer[1] = 66
Buffer[2] = 66
Buffer[3] = 66
Buffer[4] = 66
Buffer[5] = 66
Buffer[6] = 66
Buffer[7] = 66
Buffer[8] = 66
Buffer[9] = 66
Buffer[10] = 66
Buffer[11] = 66
672
Buffer[0] = 66
Buffer[1] = 66
Buffer[2] = 66
Buffer[3] = 66
Buffer[4] = 66
Buffer[5] = 66
Buffer[6] = 66
Buffer[7] = 66
Buffer[8] = 66
Buffer[9] = 66
Buffer[10] = 66
Buffer[11] = 66
684
Buffer[0] = 66
Buffer[1] = 66
Buffer[2] = 66
Buffer[3] = 66
Buffer[4] = 66
Buffer[5] = 66
Buffer[6] = 66
Buffer[7] = 66
Buffer[8] = 66
Buffer[9] = 66
Buffer[10] = 66
Buffer[11] = 66
684
Buffer[0] = 66
Buffer[1] = 66
Buffer[2] = 66
Buffer[3] = 66
Buffer[4] = 66
Buffer[5] = 66
Buffer[6] = 66
Buffer[7] = 66
Buffer[8] = 66
Buffer[9] = 66
Buffer[10] = 66
Buffer[11] = 66
672
Buffer[0] = 66
Buffer[1] = 66
Buffer[2] = 66
Buffer[3] = 66
Buffer[4] = 66
Buffer[5] = 66
Buffer[6] = 66
Buffer[7] = 66
Buffer[8] = 66
Buffer[9] = 66
Buffer[10] = 66
Buffer[11] = 66
676
Buffer[0] = 66
Buffer[1] = 66
Buffer[2] = 66
Buffer[3] = 66
Buffer[4] = 66
Buffer[5] = 66
Buffer[6] = 66
Buffer[7] = 66
Buffer[8] = 66
Buffer[9] = 66
Buffer[10] = 66
Buffer[11] = 66
680
Buffer[0] = 66
Buffer[1] = 66
Buffer[2] = 66
Buffer[3] = 66
Buffer[4] = 66
Buffer[5] = 66
Buffer[6] = 66
Buffer[7] = 66
Buffer[8] = 66
Buffer[9] = 66
Buffer[10] = 66
Buffer[11] = 66
672
Buffer[0] = 66
Buffer[1] = 66
Buffer[2] = 66
Buffer[3] = 66
Buffer[4] = 66
Buffer[5] = 66
Buffer[6] = 66
Buffer[7] = 66
Buffer[8] = 66
Buffer[9] = 66
Buffer[10] = 66
Buffer[11] = 66
672
Buffer[0] = 66
Buffer[1] = 66
Buffer[2] = 66
Buffer[3] = 66
Buffer[4] = 66
Buffer[5] = 66
Buffer[6] = 66
Buffer[7] = 66
Buffer[8] = 66
Buffer[9] = 66
Buffer[10] = 66
Buffer[11] = 66
681
Buffer[0] = 66
Buffer[1] = 66
Buffer[2] = 66
Buffer[3] = 66
Buffer[4] = 66
Buffer[5] = 66
Buffer[6] = 66
Buffer[7] = 66
Buffer[8] = 66
Buffer[9] = 66
Buffer[10] = 66
Buffer[11] = 66
675
Buffer[0] = 66
Buffer[1] = 66
Buffer[2] = 66
Buffer[3] = 66
Buffer[4] = 66
Buffer[5] = 66
Buffer[6] = 66
Buffer[7] = 66
Buffer[8] = 66
Buffer[9] = 66
Buffer[10] = 66
Buffer[11] = 66
672
Buffer[0] = 66
Buffer[1] = 66
Buffer[2] = 66
Buffer[3] = 66
Buffer[4] = 66
Buffer[5] = 66
Buffer[6] = 66
Buffer[7] = 66
Buffer[8] = 66
Buffer[9] = 66
Buffer[10] = 66
Buffer[11] = 66