Search code examples
javaserial-portjssc

Writing data to serial port, sends wrong character


When monitoring data sent to device i get this: https://i.sstatic.net/u0kIT.png

but i expect one character: F0 ð

 public void WritingDataToPort() {
        SerialPort port = new SerialPort("COM26");
        try {
            System.out.println(port.openPort());
            port.setParams(9600, 8, 1, 0);
            port.writeString((char)240+""));
            port.closePort();
        } catch (SerialPortException ex) {
            System.out.println(ex);
        }
 }

I completly don't know how to send this "ð" characater. Tried all ascii codes.


Solution

  • You can't just send the char (or UTF8) through the serial port. You have to convert it. First get the length of the char in UTF-8, then get the bytes and send the bytes.

    Getting the size of UTF-8: Getting the actual length of a UTF-8 encoded std::string?

    Then send it one by one. On the other side you need something to assemble them back.

    You could also consider using a better alternative to writing bytes directly to the port, such as http://code.google.com/p/java-simple-serial-connector/