Skip to content
This repository has been archived by the owner on May 17, 2021. It is now read-only.

[Serial] Problems with encoding on serial binding #5548

Closed
TheNetStriker opened this issue Apr 9, 2018 · 1 comment
Closed

[Serial] Problems with encoding on serial binding #5548

TheNetStriker opened this issue Apr 9, 2018 · 1 comment

Comments

@TheNetStriker
Copy link
Contributor

TheNetStriker commented Apr 9, 2018

I noticed a strange behaviour when I tried to send and receive raw byte date on a serial device. Sometimes I would get completely different bytes in the rule that handled the serial port string item. Later I found out that this seams to be a problem because OpenHab uses UTF-8 to build strings by default. This means that if certain bytes are sent over the serial port those are encoded to a different byte by the UTF-8 standard.

I also created a forum thread for this issue here, but didn't receive any feedback so far.

Expected Behavior

I expect that the bytes that are sent by the serial device appear 1 to 1 in the string item received in the OpenHab rule.

Current Behavior

By the current behaviour all bytes from serial ports are being UTF-8 encoded and decoded. Devices that are not using UTF-8 to send data may have problems with this.

Possible Solution

I already tested some changes and it seams to work very good with my device now. I made the following changes in SerialDevice.java:
Line 279 changed to: sb.append(new String(readBuffer, 0, bytes, Charset.forName(“ISO-8859-1”)));
Line 328 changed to: result = Base64.encodeBase64String(result.getBytes(Charset.forName(“ISO-8859-1”)));
Line 386 changed to: outputStream.write(msg.getBytes(Charset.forName(“ISO-8859-1”)));

The question is if this should be the default and only charset or if some devices or if the charset should be customizable for each device.

Steps to Reproduce (for bugs)

Just send some bytes that would be UTF-8 decoded to a different byte (e.g. 0xAE ) and see if the correct byte is inserted into the string. Here is also a small example that can be run in ecplise in a simple test application:

byte[] testArray = new byte[] {(byte) 0xAE };
String utf8Test = new String(testArray, 0, 1, Charset.forName("UTF-8"));
byte[] utf8Array = utf8Test.getBytes(Charset.forName("ISO-8859-1"));
String isoTest = new String(testArray, 0, 1, Charset.forName("ISO-8859-1"));
byte[] isoArray = isoTest.getBytes(Charset.forName("ISO-8859-1"));

Your Environment

OpenHabian on Raspberry Pi 3

@TheNetStriker TheNetStriker changed the title Problems with encoding on serial binding [Serial] Problems with encoding on serial binding Apr 12, 2018
@9037568
Copy link
Contributor

9037568 commented Aug 18, 2018

Closed via merged PR.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants