Page 1 of 1

Lua write to port com Windows

Posted: Friday 10 March 2017 15:12
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 ?

Re: Lua write to port com Windows

Posted: Friday 10 March 2017 15:30
by gerardvs

Re: Lua write to port com Windows

Posted: Friday 10 March 2017 15:44
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.

Re: Lua write to port com Windows

Posted: Saturday 11 March 2017 6:35
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.

Re: Lua write to port com Windows

Posted: Saturday 11 March 2017 12:59
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.

Re: Lua write to port com Windows

Posted: Monday 13 March 2017 8:49
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.

Re: Lua write to port com Windows

Posted: Monday 13 March 2017 12:25
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.

Re: Lua write to port com Windows

Posted: Monday 13 March 2017 17:12
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

Re: Lua write to port com Windows

Posted: Monday 13 March 2017 18:37
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

Re: Lua write to port com Windows

Posted: Monday 13 March 2017 18:49
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.

Re: Lua write to port com Windows

Posted: Tuesday 14 March 2017 11:19
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.