Serial Port - Base64

Hello,
I have problem with serial port, when I send data Base64 encoded.

Port definition: serial="/dev/ttyAMA0@38400,CHARSET(ISO-8859-1),BASE64"

Data to send: AA012000030110000A0307027155 (hex)
Base64 : qgEgAAMBEAAKAwcCcVU=

On hadware serial a get ASCII number of Base64 characters: q 0x71, g 0x67 …

But I need this AA012000030110000A0307027155 (hex)

What is wrong?

Thanks.

Sorry for my bad english.

Hi Carl,
have a look at https://www.openhab.org/addons/bindings/serial1/ . You need to decode the received BASE64 string.

Thanks for replay.
Problem is not when I received data. Problem is sending data over serial port. I use command
ItemName.SendCommand

Here’s a post about sending hex

and another

It is working, but only when is command hardcoded in rule. But in my case I need dynamic create a command…It looks like is impossible to do it.

Show us your rule, if that works.

When I have this in Rule:

Serial_Port.sendCommand(’\u00AA\u0001\u0020\u0000\u0003\u0001\u0010\u0000\u000A\u0003\u0007\u0002\u0071\u0055’)
It is working. But this:

val hex = new String(‘AA012000030110000A0307027155’)

val hexCommand = new String(’’)
for ( var j = 0; j < hex.length(); j+=2 ) {
hexCommand = hexCommand+"\"+“u00”+hex.substring(j, j+2)
}
Serial_Port.sendCommand(hexCommand)

Not working.

Hi Carl,
the backslash in
\u00AA
is used as an escape identifier.
The backslash in your code is a literal backslash and does not escape the character after it.
Your may try this code in your rule:

  val hex = new String('AA012000030110000A0307027155')
  val hexCommand = new String('')

  for ( var j = 0; j < hex.length(); j+=2 ) {
     hexCommand = hexCommand + Integer::parseInt( hex.substring( j, j+2 ), 16 ) as char
  Serial_Port.sendCommand(hexCommand)
  }

I tested the conversion itself but not if the sendcommand itself works.
end

1 Like

Many thanks, your code is working.
:+1:

Problem is solved :slight_smile: