Serial binding is not sending values above 63 in Decimal or 0x3F in HEX

I am trying to send a packet of HEX codes out the serial port, but every time I try to send the packet every number is correct so long as it is below 0x3F or 63 in decimal. I am using a Null modem cable to view the sent serial data in a terminal program. Very new at OpenHAB so any help would be great.

This is the line I use…

sendCommand(RS485Bus,’\u0091\u0002\u0001\u0008\u0059\u0010\u0020\u0002’)

As mentioned all the values are sent correctly except the \u0091 which shows up as 0x3F. Any values over 63 decimal value simply shows up in my terminal as 0x3F.

If I use this command
sendCommand(RS485Bus,“z”)
I then get the correct value of 0x7A, so is the serial binding limited to ASCI characters only?

I would also like to send the value of a slider and using the following line
sendCommand(RS485Bus,percent)
But this only sends the ASCI, so is there a way to convert the value of a variable to HEX?

Thanks for any help with all this.

I’m starting to think this is a bug as there is no errors reported in the logs to indicate an error, so I’ll add the following information. If you can let me know if my method is wrong or if this is a bug and how to report it that would be great.

OpenHAB 1.7 is running on windows 7 64bit, not tested under any other OS and I am running the latest Java release.

In my items file I have added this line

String RS485Bus “RS485Bus [%x]” (All) {serial=“COM8@115200”}

Then in rules I have tried the following:

sendCommand(RS485Bus,’‘’+’\u0002\u0001\u0008\u0010\u0020\u0002’)
This works and all numbers are sent correctly in HEX as I send any hex bytes above 0x3F as an ASCII char.

sendCommand(RS485Bus,’\u0091’)
Will not send correctly and shows up as 0x3F

sendCommand(RS485Bus,’\u0097’)
Will not send correctly and shows up as 0x3F

sendCommand(RS485Bus,’\u0009’)
sends correctly and shows up as 0x09

The logs just reports “… received command ?” as the character ? equals 0x3F, or 63 in decimal.

FIXED !

The solution was to add an environment variable to change the default charset. I will be getting a Raspberry to run this on so its also good to know the same fix will work on that too…

Thanks to a post by John Malawic after 2 days of googling…

On win7 add to environ variables JAVA_TOOL_OPTIONS = -Dfile.encoding=ISO-8859-1
On raspberry I select en_US ISO-8859-1 and it’s ok also.
Do not know if it’s the best solution.
Better solution can be that serial binding provide a configurable charset in openhab.cfg?
File: SerialDevice.java
Like -> outputStream.write(msg.getBytes()); to outputStream.write(msg.getBytes(MyCharSet));

In case anyone else wants to send raw HEX via the serial binding, here is how I finally got it to work after adding the environment variable mentioned in my last reply. If you know of a better way please let me know :slight_smile:

You will notice some of it is from the DEMO. This will send a hex string containing the value from the slider/fader/dimmer.

My item file contains:

String RS485Bus “RS485Bus [%x]” (All) {serial=“COM8@115200”} //serial binding
Dimmer RS485Bus_Light_91_02 “Ceiling [%d %%]” (Bathroom, Lights) // creates the dimmer

My rules contain:

rule “RS485Bus_Light_91_02”

when

  Item RS485Bus_Light_91_02 received command

then
var Number percent

  	if(RS485Bus_Light_91_02.state instanceof DecimalType) percent = RS485Bus_Light_91_02.state as DecimalType		
	if(receivedCommand==INCREASE) percent = percent + 5
	if(receivedCommand==DECREASE) percent = percent - 5
	if(percent<0)   percent = 0
	if(percent>100) percent = 100
	postUpdate(RS485Bus_Light_91_02, percent);
			
	//Convert 0-100 range into 8bit hexadecimal.
	val Number scale = Math::floor( percent.floatValue * 2.55).intValue
	if(percent==0)
	{sendCommand(RS485Bus,'\u0091\u0002\u0001\u0008\u0000\u0010\u0020\u0002')}
	else if(percent==100)
	{sendCommand(RS485Bus,'\u0091\u0002\u0001\u0008\u00FF\u0010\u0020\u0002')}
	else 
	{sendCommand(RS485Bus,'\u0091\u0002\u0001\u0008'+ String::format("%c", scale) +'\u0010\u0020\u0002')}

end

Hi @matt1, I’m glad I found this. I’m having a similar issue sending hex to a JVC projector. Everything works great except the higher numbers.

\u0021\u0089\u0001\u0050\u0057\u0031\u000A
comes across as
21 3F 01 50 57 31

I tried setting the raspberry characterset with en_US ISO-8859-1 from raspi-config but that didn’t seem to do the trick. Any other ideas? Did you get this to work with a raspberry pi?

No have not tried it yet. I have a brand new raspberry pi 2 sitting on my desk with raspbian loaded from img and have not powered it on yet. Will try in a weeks time as I am flat out building my house and installing cat6 at the moment.

Best regards,

Matthew Skinner
matt@pcmus.com
www.pcmus.com

Hello!

There was the same problem. It is necessary to send 0xFF (255) by one byte. Anything more than 0x7F (127) does not pass. The reason is UTF-8.

in “openhab.sh” record: JAVA_ARGS = "- Dfile.encoding = ISO-8859-1

The problem is not solved. What else can I do? Made is a layer between the end device and OpenHAB as Arduino.
OpenHAB working on raspberry pi 2

1 Like

In case anyone hits this issue like me, i chose Latin1 encoding in TCP.conf file, which gave me correct values.

1 Like

Gewisser:
You don’t add that into the openhab.sh you need to do this if your running openhab under windows…

https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/sysdm_advancd_environmnt_addchange_variable.mspx?mfr=true

Create an environment variable called JAVA_TOOL_OPTIONS
Then make it contain this string -Dfile.encoding=ISO-8859-1

If running on a raspberry pi you would need to google how to set the default character set for JAVA. Still have not done this myself on the PI so if someone does it then please post in this thread to say what needs to be done.

Also be aware this may break something else if another binding needs a different charset.