Search code examples
javaserial-portrxtx

RXTX string encoding, only '?' printed


I try to use java RXTX to read data from a rs232 serial port, but when i print the output, it only prints '?'

Here is my code;

public void read()
{
    int max = 32;
    int total = 0, read;
    byte[] buffer = new byte[max];
    try
    {
        while (total < max && (read = in.read(buffer, total, max - total)) >= 0) 
        {
            total += read;
        }

        System.out.println(new String(buffer, StandardCharsets.UTF_8));
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

I try with a python programm and i receive the good data, so im sure that the problem came from the algorithm. Here the output when i send hello is : ???? ? ?? ?????

Do you have an idea ?


Solution

  • Every time i ask a question on internet, i found the answer by myself a second later... I did copy the connection protocole and i forgot to change the debit. I just replaced serialPort.setSerialPortParams(57600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); with serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); and it works well !