Datagram via serial in jython HEX bytes problem


I want to send following datagram via the serial binding in jython:

oh.sendCommand("PhotovoltaicSerial",  "QPIWS" + "\x96\xA7\x0D")

The problem is that tha wrong charset is used. In the log file it looks like:


So instead of the hex values I converts to “?” because its higher than 128.

I changed the charset at init to utf8:

def __init__(self):

Thats not a good way, but I have no idea what I can do instead.
The main problem is also, that it doesnt work after a reboot.
I must restart openhab after every reboot to work correctly because the charset is not changed.

Any ideas ?

kind regards

Is this just a logging issue or are the wrong bytes being sent to the serial port?

The wrong string is also send out the serial port.
There are also thrown some exceptions when a string is receveived with some higher hex values in it.

I think this is a Java issue and not specific to Jython. It’s generally not possible to use a java.util.String to store arbitrary binary data. There are too many Unicode encoding/decoding issues. Commands are sent through the openHAB event bus as java.util.Strings and converted to bytes in the binding using the platform encoding. For your purposes, it would be good if openHAB supported a byte array or stream command for communicating non-textual data. However, as far as I know, it does not.

Is there a possibility to use something like PySerial to communicate directly?
I do that allready with UDP, but I have absolutely no idea how I can integrate serial communication directly into Jython…

You could directly use a Java library that provides the Java Communication API extensions. OpenHAB uses nrjavaserial-3.9.3.jar. Pyserial supports Jython but you need the Java extension anyway.

You can experiment with…

import sys
# This is the JAR location in my installation.
from import SerialPort, CommPortIdentifier

Look at the openHAB code for to see how to use the Java Communiations API classes. The javadocs are at:

I’d be interested in hearing if you get this working. Sending binary data to a serial port seems like a useful capability.

Thx for the tipp.
It works!
I can send strings with with hex bytes included and I can receive data.
BUT! I have absolutely no idea how the events ar working.
Here is my testcode.

import sys
import time

from import SerialPort, CommPortIdentifier

class PvSerial(Rule):
    def __init__(self):
        port = "/dev/ttyUSB11"
        Baudrate = 2400
        tryCounter = 0

            while True:
                PortId = None
                portList = CommPortIdentifier.getPortIdentifiers()
                while portList.hasMoreElements():
                    ActPortId = portList.nextElement()

                    if str(ActPortId.getPortType()) == str(CommPortIdentifier.PORT_SERIAL):
                        if ActPortId.getName() == port:
                            oh.logError("PvSerial", "Port found: " + str(ActPortId.getName()))
                            PortId = ActPortId

                if PortId is not None:

                tryCounter += 1
                if tryCounter > 7:

            if PortId is not None:
                self.PvSerialPort ="openhab", 1000)
                inputStream = self.PvSerialPort.getInputStream()
                self.PvSerialPort.setSerialPortParams(Baudrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE)


                outputStream = self.PvSerialPort.getOutputStream()

                oh.logError("PvSerial", "up and running")
                sendstring = "QPIGS\xB7\xA9\x0D"

                oh.logError("PvSerial", "data send " + sendstring)

                tryCounter = 0

                while inputStream.available() == 0:
                    tryCounter += 1
                    if tryCounter > 5:

                oh.logError("PvSerial", "inputStream while fin")

                readBuffer = ""
                byteCnt = 0
                while inputStream.available() > 0:
                    readBuffer += chr(
                    byteCnt += 1

                if byteCnt > 0:
                    oh.logError("PvSerial", " Avail " + str(byteCnt) + " " + readBuffer)
        except Exception, e:
            oh.logError("PvSerial", "exception: " + str(e))

    def serialEvent(SerialPortEvent):
        oh.logError("PvSerial", "serialEvent")

    def __del__(self):
        oh.logError("PvSerial", "del")
        except Exception, e:

Some ideas?