Lua write to port com Windows

Moderator: leecollings

Post Reply
St7ven
Posts: 22
Joined: Friday 10 March 2017 9:27
Target OS: Windows
Domoticz version:
Contact:

Lua write to port com Windows

Post by St7ven »

Hi,

I have try to find a USB library for Lua in windows.

I would like to write data through USB (port COM) thank to a lua script. In fact, I want to send a serial data through a USB300 EnOcean (RORG MSC, so not handle by domoticz). Every topic or forum present a process to use a serial peripheral in a Raspberry :cry: . I could not figure out, how to manage it when domoticz is used in windows...

Do you know any method I could use to send data through a serial port thanks to lua script ?
gerardvs
Posts: 81
Joined: Sunday 04 January 2015 0:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest-1
Location: /dev/null
Contact:

Re: Lua write to port com Windows

Post by gerardvs »

St7ven
Posts: 22
Joined: Friday 10 March 2017 9:27
Target OS: Windows
Domoticz version:
Contact:

Re: Lua write to port com Windows

Post by St7ven »

I tried this one (along many others)

If i remember well this one pop-up a missing lua.5.1.dll. file

I guess it has not been update since.
User avatar
Dnpwwo
Posts: 820
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: Lua write to port com Windows

Post by Dnpwwo »

@St7ven,

If you know any Python you could create add support to Domoticz by creating a Python plugin, have a look at http://www.domoticz.com/wiki/Developing_a_Python_plugin. Looks like the protocol is pretty well documented.

Then you can send commands from the web interface or Lua.

I could send you a simple sample plugin that does Serial communication that could get you started.
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
St7ven
Posts: 22
Joined: Friday 10 March 2017 9:27
Target OS: Windows
Domoticz version:
Contact:

Re: Lua write to port com Windows

Post by St7ven »

Hi Dnpwwo,

I have never code python. I usually code in C or asm. But I can give it a try. It seams that it can be a good thing to have this plugging in domoticz. Please, send me this simple sample plugin that does Serial communication. I will try to do something.
User avatar
Dnpwwo
Posts: 820
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: Lua write to port com Windows

Post by Dnpwwo »

@St7ven,

Create a file from the code below called plugin.py and put it in a folder under \domtoicz\plugins call it something like 'Enocean'.

Code: Select all

#           Serial Plugin
#
#           Author:     Dnpwwo, 2017
#
#
#   Plugin parameter definition below will be parsed during startup and copied into Manifest.xml, this will then drive the user interface in the Hardware web page
#
"""
<plugin key="Serial" name="Serial base plugin" author="dnpwwo" version="1.0.0" >
    <params>
        <param field="SerialPort" label="Serial Port" width="150px" required="true" default=""/>
        <param field="Mode6" label="Debug" width="75px">
            <options>
                <option label="True" value="Debug"/>
                <option label="False" value="Normal"  default="true" />
            </options>
        </param>
    </params>
</plugin>
"""
import Domoticz

isConnected = False

def onStart():
    if Parameters["Mode6"] == "Debug":
        Domoticz.Debugging(1)
    if (len(Devices) == 0):
        # Create devices here, see examples
        Domoticz.Log("Devices created.")
    Domoticz.Log("Plugin has " + str(len(Devices)) + " devices associated with it.")
    DumpConfigToLog()
    Domoticz.Transport("Serial", Parameters["SerialPort"], 115200)
    #Domoticz.Protocol("XML")  # None,XML,JSON,HTTP
    Domoticz.Connect()
    with open(Parameters["HomeFolder"]+"Response.txt", "wt") as text_file:
        print("Started.", file=text_file)
    return

def onConnect(Status, Description):
    global isConnected
    if (Status == 0):
        isConnected = True
        Domoticz.Log("Connected successfully to: "+Parameters["SerialPort"])
        # Send any initialisation commands here e.g:
        #Domoticz.Send("<Command>\n  <Name>get_device_info</Name>\n</Command>")
    else:
        Domoticz.Log("Failed to connect ("+str(Status)+") to: "+Parameters["SerialPort"])
        Domoticz.Debug("Failed to connect ("+str(Status)+") to: "+Parameters["SerialPort"]+" with error: "+Description)
    return True

def onMessage(self, Data, Status, Extra):
    Domoticz.Log("onMessage called")
    try:
        strData = Data.decode()
        if Parameters["Mode6"] == "Debug":
            with open(Parameters["HomeFolder"]+"Response.txt", "at") as text_file:
                print(strData, file=text_file)

        # update Domoticz devices here
        
    except Exception as inst:
        Domoticz.Error("Exception in onMessage, called with Data: '"+str(strData)+"'")
        Domoticz.Error("Exception detail: '"+str(inst)+"'")
        raise
        
def onCommand(Unit, Command, Level, Hue):
    global isConnected
    Domoticz.Log("onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level))
    
    #if (isConnected == True):
    #     send commands to external hardware here
    
    return

def onDisconnect():
    global isConnected
    isConnected = False
    Domoticz.Log("Device Disconnected")
    return

def onHeartbeat():
    global isConnected
    if (isConnected != True):
        Domoticz.Connect()
    return True

# Generic helper functions
def DumpConfigToLog():
    for x in Parameters:
        if Parameters[x] != "":
            Domoticz.Debug( "'" + x + "':'" + str(Parameters[x]) + "'")
    Domoticz.Debug("Device count: " + str(len(Devices)))
    for x in Devices:
        Domoticz.Debug("Device:           " + str(x) + " - " + str(Devices[x]))
        Domoticz.Debug("Device ID:       '" + str(Devices[x].ID) + "'")
        Domoticz.Debug("Device Name:     '" + Devices[x].Name + "'")
        Domoticz.Debug("Device nValue:    " + str(Devices[x].nValue))
        Domoticz.Debug("Device sValue:   '" + Devices[x].sValue + "'")
        Domoticz.Debug("Device LastLevel: " + str(Devices[x].LastLevel))
    return
then change line 9 (the <plugin' line) to reflect what you are doing (the 'Key' needs to be unique). When you stop and start Domoticz it will show up in the Hardware Tab in the drop down with the name specified and will need you to choose the serial port to use.

The \domoticz\plugins\examples\RAVEn.py file does Serial communication using XML and might be worth looking at.
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
St7ven
Posts: 22
Joined: Friday 10 March 2017 9:27
Target OS: Windows
Domoticz version:
Contact:

Re: Lua write to port com Windows

Post by St7ven »

@Dnpwwo,

Thank you for this plugin.

Today, I tried to run a python script. Finaly, I noticed that It is not possible if I don't install domoticz directly from the source (which seems hard for a novice like me).

I read your template plugin. Unfortunatly, I have not the skill to modify or include this plugin to my domoticz program....

I will give up python script and plugin for now, and return too lua script. In fact, lua script and documentation seems more abundant.
St7ven
Posts: 22
Joined: Friday 10 March 2017 9:27
Target OS: Windows
Domoticz version:
Contact:

Re: Lua write to port com Windows

Post by St7ven »

Hi,

Back to Lua, I have copy the file "luars232.dll" to the folder "C:\Program Files (x86)\Domoticz"

When I run (script_device_MyScript.lua in domoticz/scripts/lua/:

Code: Select all

rs232 = require("luars232")
Domoticz pop-up a message "missing lua.5.1.dll"

Then, I install lua5.1 (LuaForWindows_v5.1.4-46,exe)

I reboot domoticz on windows. Run the script and now the program domoticz freeze.

Code: Select all

rs232 = require("luars232")
Any advices ?



edit :
This is the following error report from windows

Code: Select all

Nom d’événement de problème:	APPCRASH
  Nom de l’application:	domoticz.exe
  Version de l’application:	2.0.0.5877
  Horodatage de l’application:	58249aab
  Nom du module par défaut:	lua5.1.dll
  Version du module par défaut:	5.1.4.0
zak45
Posts: 952
Joined: Sunday 22 January 2017 11:37
Target OS: Windows
Domoticz version: V2024.4
Contact:

Re: Lua write to port com Windows

Post by zak45 »

@St7ven
you do not have to install from source. Just download the last beta release and voila ...
Python plugin work quite well in windows but you need to switch in english local
see : https://www.domoticz.com/forum/viewtopi ... =6&t=16168
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Lua write to port com Windows

Post by simonrg »

Domoticz uses version 5.2 of Lua, so you will need to find the library for Lua version 5.2 for Windows.

A Python plug-in would be better idea if you want serial communciation to run for a long time, Lua scripts only run for 10 seconds maximum. Otherwise if you can find Python libraries and can't find the Lua libraries then might be a show stopper.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
St7ven
Posts: 22
Joined: Friday 10 March 2017 9:27
Target OS: Windows
Domoticz version:
Contact:

Re: Lua write to port com Windows

Post by St7ven »

I gave a second try for a python plugging. I'm full of hope. Until I met :

Code: Select all

PluginSystem: Failed dynamic library load, install the latest libpython3.x library that is available for your platform

I installed domoticz using the last beta version https://www.domoticz.com/downloads/

Then copy the "pluging" folder for git to the installation folder.

I installed Python 3.6. I changed the language of th OS to English (W7 family pack)

While this all process, I look to the source code of domoticz : https://github.com/domoticz/domoticz/bl ... anESP3.cpp

I was wondering if it would be more efficient to directly modify this code. In fact, every message send through serial to EnOcean USB300 requiere a CRC calculation,which is already coded in EnOceanESP3.cpp? ROGR type is missing.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest