[Solved] Read multiple values from a serial device

Hi,

I try to read the following information from a serial device:
Mi.;05;06;19;13;38;18; 24.8; 33.4; 30.0; 31.1; 29.0; 28.1;-30.0;-30.0;1;0;0;
which is transfered in one line.

String Temperature “My Temp. Sensor” (LivingRoom) { serial="/dev/ttyAMA0@9600"}

The string doesn’t contain the whole line but only parts of it. (See log file below)

019-06-05 16:17:00.099 [vent.ItemStateChangedEvent] - Temperature changed from Mi.;05;06;19;15;49;05; 25.1; 33.9; to 30.3; 30.9; 29.3; 28.5;-30.0;

2019-06-05 16:17:00.334 [vent.ItemStateChangedEvent] - Temperature changed from 30.3; 30.9; 29.3; 28.5;-30.0; to -30.0;1;

2019-06-05 16:17:00.583 [vent.ItemStateChangedEvent] - Temperature changed from -30.0;1; to 0;0;

2019-06-05 16:17:05.182 [vent.ItemStateChangedEvent] - Temperature changed from 0;0;

to Mi.;05;06;19;15;49;10; 25.2; 33.8; 30.3;

  1. Is there a simple way to store the whole line in the string or do I have to do this in an external script / program ?
  2. The line contains 8 temperature values which I want to display. What is the best way to do this ?

Thanks in advance
Rainer

I am far from an expert on the Serial binding. But I do know it is one of the more challenging ones to get working correctly. Is it possible the baud rate isn’t set right which is causing the Strings to be cut into parts? I really don’t know if that even makes sense. It might be easier to make it work with an external script to push the data to OH if messing with the settings for the binding can’t help.

I’d start with How to get started (there is no step-by-step tutorial) because this question belies a lack of understanding of how OH works over all and the scope of the answer is far to great to quickly type out here.

Hi Rich,
Thanks for your response. The problem of the splitted string is not a baudrate problem, as all characters are received well. The readout function of OH and the transmit of the external device are not synchronised.
This caused the problem.

I would probably use an external script or something like nodered where you have more control about where data starts or ends and timeouts. It looks to me like openhab might be splitting the string because of an included timeout where if there is no data for some milliseconds it just thinks the string is finished.
Johannes

The serial binding is a rather ancient thing, it doesn’t offer any means to determine end of transmission i.e. when to place data in an Item.
I’d guess there’s just a timeout, I wonder if it varies by baud rate.

I think you could use rules to reconstruct incoming fragments. Is your packet start always “Mi”, does it always appear at the beginning of a fragment? That would make it easier.

There might be time to work this at 9600 baud

items

String RxData “Raw serial” { autoupdate="false", serial="/dev/ttyAMA0@9600"}

rules

var Timer rxTimer = null
var String rxBuffer = ""   // global variable, faster then updating Item

rule "capture fragment"
when
   Item RxData changed
then
      // rule kept lean for speed
   var String fragment = RxData.state  // grab it quick
   if ( fragment.contains("Mi") ) {
         // leading fragment
      rxBuffer = fragment
   } else {
         // trailing fragment
      rxBuffer = rxBuffer + fragment
   }
   if (rxTimer === null) {
         // set up a fragment timeout
      rxTimer = createTimer(now.plusSeconds(1), [  |
            //trigger processing
         RxData.sendCommand("PROCESS")
         rxTimer = null
         ] )
   } else {
      rxTimer.reschedule(now.plusSeconds(1))
   }
end 

rule "process packet"
when
   Item RxData received command
then
   // process data packet in variable rxBuffer
   logInfo("serial", "packet " + rxBuffer)
end

First thanks for the responses.
“Mi” are the first 2 letters of the “Mittwoch” the german word for Wednesday.
I will use some already available ‘C’ code to the serial port and read out the needed values.
The program will send the values with curl to openhab, which I already tested.

1 Like