Hi! I have noticed that the github repository has received updates implementing bi-directional support for serial devices.
I’d like to know when this update can be expected to be able to install over paper-ui. I’d love to have this functionality for the feedback communication with my AV equipment.
Hi @Oleksandr_Danylchenk The functionality is in the latest snapshot, so if you are on snapshot releases, it should be available now. If not on snapshot builds, it will be in the OH 2.1 release (the timing of which I’m not certain).
If you install it, let me know if you run into any issues. It’s been tested by myself and one other person so far.
If any of your AV devices use a binary protocol (i.e. use non-printable characters), I can give you a code snippet that URL decodes the received string, then converts it into a byte array. It’s not much code, but it took me a while to get it right for my own rules.
Here are a couple links that explain how to change versions, or how to install a newer binding than the one that came with the distro. I’ve not done this myself (I’ve been on snapshot builds from the beginning), so I can’t provide much guidance other than these links.
import java.util.concurrent.locks.ReentrantLock
import java.net.URLDecoder
var ReentrantLock lock = new ReentrantLock()
rule "GlobalCache Receive"
when
Item TestRule_Switch received command ON
then
lock.lock
try {
var String encodedString
var String decodedString
var byte[] decodedBytes
logInfo("gc-receive", "")
// Example 1
logInfo("gc-receive", "Example 1: encoded string containing all printable characters (easy)")
encodedString = "%3CThis%20is%20a%20%26simple%26%20example!%3E"
logInfo("gc-receive", "Encoded string is '" + encodedString + "'")
decodedString = URLDecoder::decode(encodedString, "ISO-8859-1")
logInfo("gc-receive", "Decoded string is '" + decodedString + "'")
if (decodedString.contains("simple")) {
logInfo("gc-receive", "Yep. This is a simple example")
}
logInfo("gc-receive", "")
// Example 2
logInfo("gc-receive", "Example 2: encoded string containing unprintable characters (more complex)")
encodedString = "%F0%02ABCDE%03"
logInfo("gc-receive", "Encoded string is '" + encodedString + "'")
decodedBytes = (URLDecoder::decode(encodedString, "ISO-8859-1")).getBytes("ISO-8859-1")
var int decodedLength = decodedBytes.length
var int b
val int SOH = 0xF0
val int STX = 0x02
val int ETX = 0x03
var int sohByte = decodedBytes.get(0).bitwiseAnd(0xff)
var int stxByte = decodedBytes.get(1).bitwiseAnd(0xff)
var int etxByte = decodedBytes.get(decodedLength - 1).bitwiseAnd(0xff)
if(sohByte == SOH) {
logInfo("gc-receive", "Found valid start of header")
if (stxByte == STX && etxByte == ETX) {
logInfo("gc-receive", "Found valid start and end of text")
var int counter = 0
while (decodedLength > 0) {
b = decodedBytes.get(counter).bitwiseAnd(0xff)
logInfo("gc-receive", "byte[" + counter + "]=" + Integer::toHexString(b))
counter = counter + 1
decodedLength = decodedLength - 1
}
} else {
logInfo("gc-receive", "Didn't find valid start and end of text")
}
} else {
logInfo("gc-receive", "Didn't find valid start of header")
}
} finally {
lock.unlock
}
end
Which should produce output like this in the log
2018-01-18 15:31:33.126 [INFO ] [se.smarthome.model.script.gc-receive] -
2018-01-18 15:31:33.127 [INFO ] [se.smarthome.model.script.gc-receive] - Example 1: encoded string containing all printable characters (easy)
2018-01-18 15:31:33.127 [INFO ] [se.smarthome.model.script.gc-receive] - Encoded string is '%3CThis%20is%20a%20%26simple%26%20example!%3E'
2018-01-18 15:31:33.129 [INFO ] [se.smarthome.model.script.gc-receive] - Decoded string is '<This is a &simple& example!>'
2018-01-18 15:31:33.130 [INFO ] [se.smarthome.model.script.gc-receive] - This is a simple example
2018-01-18 15:31:33.130 [INFO ] [se.smarthome.model.script.gc-receive] -
2018-01-18 15:31:33.131 [INFO ] [se.smarthome.model.script.gc-receive] - Example 2: encoded string containg unprintable characters (more complex)
2018-01-18 15:31:33.131 [INFO ] [se.smarthome.model.script.gc-receive] - Encoded string is '%F0%02ABCDE%03'
2018-01-18 15:31:33.133 [INFO ] [se.smarthome.model.script.gc-receive] - Found valid start of header
2018-01-18 15:31:33.134 [INFO ] [se.smarthome.model.script.gc-receive] - Found valid start and end of text
2018-01-18 15:31:33.136 [INFO ] [se.smarthome.model.script.gc-receive] - byte[0]=f0
2018-01-18 15:31:33.138 [INFO ] [se.smarthome.model.script.gc-receive] - byte[1]=2
2018-01-18 15:31:33.139 [INFO ] [se.smarthome.model.script.gc-receive] - byte[2]=41
2018-01-18 15:31:33.141 [INFO ] [se.smarthome.model.script.gc-receive] - byte[3]=42
2018-01-18 15:31:33.143 [INFO ] [se.smarthome.model.script.gc-receive] - byte[4]=43
2018-01-18 15:31:33.145 [INFO ] [se.smarthome.model.script.gc-receive] - byte[5]=44
2018-01-18 15:31:33.146 [INFO ] [se.smarthome.model.script.gc-receive] - byte[6]=45
2018-01-18 15:31:33.148 [INFO ] [se.smarthome.model.script.gc-receive] - byte[7]=3