Error parsing the string

Hi, I have set up serial communication between RPi 3B+ and Arduino, all working fine, but sometimes I see error in the OH Log file and string does not parse.

Error is:
2020-04-04 11:47:25.596 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Arduino’: For input string: “”

items file:

String Arduino                  "Arduino [%s]" <serial_port_32> (gSS) {serial="/dev/ttyUSB0@57600"}

rules:

rule "Arduino"  
 when   
      Item Arduino received update  
 then  
      var String ArduinoUpdate = Arduino.state.toString.trim  
	  
      var int verStartsOn = ArduinoUpdate.indexOf("ver=") + "ver=".length  
      var String ver = ArduinoUpdate.mid(verStartsOn, ArduinoUpdate.indexOf('_D22')-verStartsOn)   
      Arduino_ver.postUpdate(ver)      

      var int D22StartsOn = ArduinoUpdate.indexOf("D22=") + "D22=".length  
      var String D22 = ArduinoUpdate.mid(D22StartsOn, ArduinoUpdate.indexOf('_D23')-D22StartsOn) 
      var D22n = Integer::parseInt(D22) as Number	  
      Arduino_D22.postUpdate(D22)
	  if(D22n == 0){
	  Light_Garden_Garage.postUpdate(OFF)
	  }else{
	  Light_Garden_Garage.postUpdate(ON)
	  }

blablabla...

end

String is quite long:

Arduino changed from ver=0.26_D22=0_D23=0_D24=255_D25=0_D26=0_D27=0_D28=0_D29=0_D30=0_D31=0_D32=0_D33=0_D34=0_D35=0_D36=0_D37=0_D38=0_D39=0_D40=0_D41=0_D42=0_D43=0_D44=0_D45=0_D46=0_D47=0_D48=0_D49=0_A54=288_A55=304_A56=302_A57=311_A58=305_A59=297_A60=318_A61=324_A62=360_A63=359_A64=407_A65=387_A66=379_A67=381_A68=378_A69=380_DS00=775414_DS01=0_DS02=0_DS03=0_DS04=0_DS05=0_DS06=0_DS07=0_DS08=0_DS09=0_DS10=0_DS11=0_DS12=0_DS13=0_DS14=0_DS15=0_DS16=0_DS17=0_DS18=0_DS19=0_DS20=0_DS21=0_DS22=0_DS23=0_DS24=0_DS25=0_DS26=0_DS27=0_DS28=0_DS29=0_MS00=14189484_MS01=0_MS02=0_MS03=0_MS04=0_MS05=0_MS06=0_MS07=0_MS08=0_MS09=0_MS10=0_MS11=0_MS12=0_MS13=0_MS14=0_MS15=0_MS16=0_MS17=0_MS18=0_MS19=0_MS20=0_MS21=0_MS22=0_MS23=0_MS24=0_MS25=0_MS26=0_MS27=0_MS28=0_MS29=0_SS00=374494_SS01=0_SS02=0_SS03=0_SS04=0_SS05=0_SS06=0_SS07=0_SS08=0_SS09=0_SS10=0_SS11=0_SS12=0_SS13=0_SS14=0_SS15=0_SS16=0_SS17=0_SS18=0_SS19=0_SS20=0_SS21=0_SS22=0_SS23=0_SS24=0_SS25=0_SS26=0_SS27=0_SS28=0_SS29=0;

what could be wrong?

The part of the error messages you posted tells that you that the input string is empty. The use of some logInfo lines in the rule could tell you more a out the situation. My guess, you are receiving an empty string, but without seeing the whole error message and where in the rule this error happened ( using logInfo lines you would know) this is only a guess.

Thanks for reply, but I have tested sketch for Arduino and it does not send empty strings, so I would say that this is more related to RPi or OH. I have tested different baudrates, still, errors once every 15-20 seconds…

Still leaves the ball in your court.
In the real world, serial comms goes wrong, you need to deal with it. What is received is not necessarily what you think you sent.

A pretty obvious thing to do is test if the string is what you expect before trying to process specific things in it.
Code defensively.

If (ArduinoUpdate.contains("ver=")) {
   // do your stuff
} else {
   logInfo("Arduino" , "Unexpected serial data")
}

Wow, thanks for that, did not think about it! thanks Rossko!

The problem is, when I send data to serial port it seems that it is being processed by rule too, there for rule does not “understand” it and gives an error…

??
You mean, you’re sending openHAB commands to your serial Item, and you have not set autoupdate=“false” ?

for instance when I send command to serial:

rule "Light_Garden_Garage"  
 when   
     Item Light_Garden_Garage changed  
 then  
      var state = Light_Garden_Garage.state as OnOffType
	  if (Light_Garden_Garage.state == ON) sendCommand (Arduino , "D22=1\r\n") else sendCommand (Arduino , "D22=0\r\n")
	  
end

then this is processed too with

rule "Arduino"  
 when   
      Item Arduino received update  
 then  
      var String ArduinoUpdate = Arduino.state.toString.trim  
	  
.....

there for it is giving me an error

checking this…

If (ArduinoUpdate.contains("ver=")) {

It was a typed, unvalidated, example that you copy-pasted.
Not If with big-I, but if with little-i

I strongly recommend the use of VSCode with openHAB extension for rules editing, something else to install and learn but saves hours on syntax errors.

understood…thanks for hint! Its working now )

Simply added code:

rule "Arduino"  
 when   
      Item Arduino received update  
 then  
      var String ArduinoUpdate = Arduino.state.toString.trim
	  
	  if (ArduinoUpdate.contains("ver=")) {
	  
      var int verStartsOn = ArduinoUpdate.indexOf("ver=") + "ver=".length  
      var String ver = ArduinoUpdate.mid(verStartsOn, ArduinoUpdate.indexOf('_D22')-verStartsOn)   
      Arduino_ver.postUpdate(ver)  

...
      } else {
           logInfo("Arduino" , "Unexpected serial data")
        } 	  
end

Did you see mention of autoupdate earlier?

yes, so what shall I do with it? where to set it for which item? what effect will it have?

I suppose you could search these forums to find out. You only get a bit of spoonfeeding :wink:

:smiley:will do bud, thanks for help!

Hello All,

I wanted to point out that in the latest OH, the “.mid” command no longer works.

I used “.substring” ( String substring(int beginIndex, int endIndex) ) which worked for me.

For this, the subtraction “-verStartsOn” at the end of
“var String ver = ArduinoUpdate.mid(verStartsOn, ArduinoUpdate.indexOf(’_D22’)-verStartsOn)”
be thrown out.

Feel free to correct me if there are any adjustments needed.

Otherwise, many thanks to the very helpful forum and the quick support!