Strange problem with serial binding

I’am trying to connect an Teensy 3.2 Arduino to my Openhab 2.2 using the serial binding. Most of the time the comunication works perfectly, but sometimes some strange bytes are transfered.

For instance if the Teensy sends the Hex Bytes 02:02:A5:A9 the Openhab rule sometimes receives this: 02:02:EF:BF:BD:EF:BF:BD:

I already tried switching the serial binding to BASE64, but that doesn’t change anything. I also enabled debug logging but I only found the same false bytes in the log after that. I also debugged the serial port using the ssterm python tool (which can display hex values) and there I never saw such strange bytes.

So I Googled those strange bytes and found out that EF:BF:BD has something to do with UTF-8. Does anyone have an idea why those bytes are set by the serial binding?

By the way, this is how I’am getting the bytes in the rule:

var byte[] statusBytes = DatatypeConverter::parseBase64Binary(status)

I think I already found the cause of this. The probem seams to be that Openhab binding constructs a string from the bytes to pass this to the rules and it does that with the default charset:

sb.append(new String(readBuffer, 0, bytes));

My guess is that the default charset is UTF-8, so i changed this to ISO-8859-1:

sb.append(new String(readBuffer, 0, bytes, Charset.forName(“ISO-8859-1”)));

And now it seams to work perfectly! The question is if this should be the default charset or if this should be customizable.

I have now modified tree lines in the code to use a different encoding an now the comunication with my serial device works perfectly. I’ve already created an issue on GitHub to discuss this further: https://github.com/openhab/openhab1-addons/issues/5548

1 Like

This has been happening for a while so BIG thanks if you have made a change to fix it.

See my post about it here. I put that project aside for a while whilst I built my house and was about to come back to take a look so your timing is perfect.

Hi, thanks for the link. I did not change anything in the official repository yet. I first would like to discuss if this should be customizable or not. Also I’am waiting for this issue to be merged first. But I could provide you the modified addon if you like.

Hi Matt, the code just got merged and now I prepared another code update to solve this problem here: https://github.com/TheNetStriker/openhab/commit/9f0d6c79c81faaedc5d6fc3ba9767656026c74b2

I’ve added a CHARSET setting to the serial binding config string. So just add ,CHARSET(YourCharset) to the binding string should set the respective charset.

You can download a build of the new serial binding from my Dropbox to test this: https://www.dropbox.com/s/ga3awqird3qn6fi/org.openhab.binding.serial-1.12.0-SNAPSHOT.jar?dl=0

Once you have confirmed that this is working for you too I will send a request to merge this into the official repository.

Everything got packed away whilst I was building and the house is still a mess so it may take me some time to get things set back up after locating them. I will see if I can find the other person that posted with an issue a few weeks ago to see if they can test it for you.
Also please don’t forget to update the documentation to let people know the feature exists should it get merged.

@mortommy
Are you able to test this change? It may fix the issue you posted about here…

Hi Matt, thanks for the quick reply. I just updated the file because I found a config parsing problem. (Parsing of REGEX strings with a comma inside would not be parsed correctly any more)

I’ve created a new code change on GitHub that should work for every possible combination of settings: https://github.com/TheNetStriker/openhab/commit/3df6afa2ef466b8314f02b0e7db0683c390f0703

The Dropbox link should still be the same. And yes, I’ve also updated the readme file. :slight_smile:

Hi @matt and @TheNetStriker,
I tried the new binding copying the file in the addons folder (after I had uninstalled the previous version).
When I edit the item file I can see the following entries in the log file:

2018-04-13 23:53:05.244 [DEBUG] [inding.serial.internal.SerialBinding] - Port: /dev/ttyUSB0
2018-04-13 23:53:05.245 [DEBUG] [inding.serial.internal.SerialBinding] - Baud rate: 9600
2018-04-13 23:53:05.247 [DEBUG] [binding.serial.internal.SerialDevice] - Serial port ‘/dev/ttyUSB0’ charset ‘null’ set.
2018-04-13 23:53:05.260 [DEBUG] [binding.serial.internal.SerialDevice] - Serial port ‘/dev/ttyUSB0’ has been found.
2018-04-13 23:53:05.269 [DEBUG] [inding.serial.internal.SerialBinding] - Port: /dev/ttyUSB0
2018-04-13 23:53:05.270 [DEBUG] [inding.serial.internal.SerialBinding] - Baud rate: 9600

then via Karaf console I tried to send a command to the item, but as before I don’t get any data back, and the log is:

2018-04-13 23:53:44.708 [DEBUG] [binding.serial.internal.SerialDevice] - Writing ‘POWR0 \r’ to serial port /dev/ttyUSB0

I didn’t reboot openhab (2.1)

@mortommy

I will reply to try and help narrow down the issue in your thread that I linked to 3 posts above. Please keep discussion about your problem in your thread in case it is not related to the original subject of this thread.

Hi @matt1 , were you able to test my modified addon? Does everything work as expected?

Sorry I have a baby due soon and I am not in a position to help.