Serial port listen

Python and python framework

Moderator: leecollings

Post Reply
oskarion
Posts: 5
Joined: Friday 13 November 2020 15:05
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Serial port listen

Post 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
User avatar
Dnpwwo
Posts: 819
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: Serial port listen

Post 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
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
oskarion
Posts: 5
Joined: Friday 13 November 2020 15:05
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Serial port listen

Post by oskarion »

Thank you very much :)
oskarion
Posts: 5
Joined: Friday 13 November 2020 15:05
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Serial port listen

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest