Page 1 of 1

Serial port listen

Posted: Tuesday 01 December 2020 2:13
by oskarion
Hello everyone :)
Maybe this time someone will help me.
How can I listen my serial port (rs232) from domoticz plugin? Is there some function like Receive? I need receive commands from my hardware connected to raspberry.
Thank you

Re: Serial port listen

Posted: Tuesday 01 December 2020 6:09
by Dnpwwo
@oskarion,

If you use the plugin framework's connection object you just connect to the port and onMessage will be called everytime a message arrives.

Have a look at: https://github.com/dnpwwo/Domoticz-RAVE ... /plugin.py

The connection open is requested in onStart, reporting in onConnect and messages arrive in onMessage

Re: Serial port listen

Posted: Tuesday 01 December 2020 13:58
by oskarion
Thank you very much :)

Re: Serial port listen

Posted: Wednesday 02 December 2020 20:33
by oskarion
@Dnpwwo

I have a question. My hardware sends to me commands like those:

Code: Select all

"message" : "2020-12-01 20:43:28.294  (Sterownik) AT+Time=-1,-1,-1,-1\r\nAT+Time=-1,-1,-1,-1\r\nAT+Time=-1,-1,-1,-1\r\nAT+Time=-1,-1,-1,-1\r\nAT+Time=-1,-1,-1,-1\r\nAT+Time=-1,-1,-1,-1\r\nO=1,0,1,1,0,0,0\r\nI=1,0,0,1,0,0,0\r\nAT+Time=-1,-1,-1,-1\r\nAT+Time=-1,-1,-1,-1\r\nAT+Time=-1,-1,-1,-1\r\nAT+Time=-1,-1,-1,-1\r\nAT+Time=-1,-1,-1,-1\r\nAT+Time=-1,-1,-1,-1\r\n"
and I wanted to pull out command "I=1,0,0,1,0,0,0\r\n" from this string to know which input was turned on.
I wrote code in onMessage to parse that:

Code: Select all

def onMessage(Connection, Data):
    global SerialConn   
    SerialConn = Connection
    
    strData = Data.decode("utf-8", "ignore")
    LogMessage(strData)
    
    pozycja=strData.find('I')
    strData=(strData[pozycja:len(strData)])
    pozycja1=strData.find('\r')
    strData=(strData[0:pozycja1])
    strData=strData.replace(",", "")
    
    for char in strData:
        if char in " I=OPSL":
            strData = strData.replace(char,'')

    for i in range(1,len(strData)):
        if strData[i]=='1':
            Devices[i].Update(nValue=1, sValue=str(0), SignalLevel=0, Image=8)    
        if strData[i]=='0':
            Devices[i].Update(nValue=0, sValue=str(0), SignalLevel=0, Image=8)     
    return
Can you tell me is that correct way to achieve that? For example if my hardware sends me a command and second input will be "1": "I=1,0,1,0,0,0,0" I want to change unit==2 device.