How to set serial parameters in Python plugin framework

Python and python framework

Moderator: leecollings

Post Reply
dvdb
Posts: 5
Joined: Sunday 10 September 2017 13:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

How to set serial parameters in Python plugin framework

Post by dvdb »

in the python plugin framework it's possible to set the serialport and the baudrate with:

Domoticz.Transport("Serial", Parameters["SerialPort"], 19200)

Is it also possible to change other serial parameters like:

ser = serial.Serial(
port='/dev/pts/81',
baudrate=19200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=5
)
zak45
Posts: 953
Joined: Sunday 22 January 2017 11:37
Target OS: Windows
Domoticz version: V2024.4
Contact:

Re: How to set serial parameters in Python plugin framework

Post by zak45 »

dvdb
Posts: 5
Joined: Sunday 10 September 2017 13:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Re: How to set serial parameters in Python plugin framework

Post by dvdb »

Thanks zak45.
The example in my first post was pyserial code and this is working in a standalone python program.

In the plugin framework you're bound to the given structure.

def onStart():
Domoticz.Transport("Serial", Parameters["SerialPort"], 9600)
Domoticz.Connect()
return

The question is how to pass the parity, stopbits etc. parameters in this framework in the onStart().
zak45
Posts: 953
Joined: Sunday 22 January 2017 11:37
Target OS: Windows
Domoticz version: V2024.4
Contact:

Re: How to set serial parameters in Python plugin framework

Post by zak45 »

maybe something like that :

def onStart():
Domoticz.Transport("Serial", Parameters["SerialPort"], Parameters["Mode1"])
Domoticz.Connect()
return

from Mode1 to Mode6: can be used and set on your plugin definition:

Code: Select all

"""
<plugin key="BTLe" name="Bluetooth Low Energy" author="zak45" version="1.0.0" wikilink="http://www.domoticz.com/wiki/plugins/BTLe.html" externallink="https://github.com/peplin/pygatt">
    <params>
        <param field="SerialPort" label="Serial Port" width="200px" required="true" default="COM7"/>
        <param field="Mode2" label="Linux hci Port" width="200px" required="true" default="hci0"/>
        <param field="Address" label="IP Address" width="200px" required="true" default="127.0.0.1"/>
        <param field="Port" label="Domoticz Port" width="200px" required="true" default="8080"/>
        <param field="Mode1" label="Listener Port" width="200px" required="true" default="9001"/>        
        <param field="Mode3" label="Interactive Mode" width="75px">
            <options>                
                <option label= "False" value="no"/>
                <option label= "True" value="yes" default="False"/>
            </options>
        </param>
        <param field="Mode4" label="Manage device with no uuid" width="75px">
            <options>                
                <option label= "False" value="no"/>
                <option label= "True" value="yes" default="False"/>
            </options>
        </param>
        <param field="Mode6" label="Debug" width="75px">
            <options>
                <option label="True" value="Debug"/>
                <option label="False" value="Normal"  default="True" />
            </options>
        </param>
    </params>
</plugin>
"""
dvdb
Posts: 5
Joined: Sunday 10 September 2017 13:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Re: How to set serial parameters in Python plugin framework

Post by dvdb »

Thanks again zak45.

Your example makes it possible to define the baudrate in the hardware GUI of domoticz.
But I am searching for a way to define the parity and the number of stopbits.

In the python plugin framework the port and the baudrate can be set with:

Domoticz.Transport("Serial", PORT, BAUDRATE)

PORT = device e.g. /dev/pts/81
BAUDRATE = baudrate of communications

I would like to set also the parity and the number of stopbits, something like:

Domoticz.Transport("Serial", PORT, BAUDRATE, PARITY, STOPBITS)

PARITY = (ODD, EVEN, NONE)
STOPBITS = 1 OR 2

Can someone tell me where the Domoticz.Transport is defined, so I can extend it with the requested parameters.
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: How to set serial parameters in Python plugin framework

Post by Dnpwwo »

@dvdb,

The Domoticz.Transport method was removed some time ago because it does not make sense to change the transport of a connection, it is now specific during Connection creation. Places to look at are:
  • CConnection is defined in PythonObject.h
  • parameters are now consumed by the CConnection_new in PythonObjects.cpp
  • Serial transports are created by CPlugin::ConnectionConnect in Plugins.cpp
  • The Serial transport itself is handled by CPluginTransportSerial in PluginTransports.cpp, the CPluginTransportSerial::handleConnect is what you will want to look at
You will want to change this:

Code: Select all

	open(m_Port, m_Baud,
		boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none),
		boost::asio::serial_port_base::character_size(8),
		boost::asio::serial_port_base::flow_control(boost::asio::serial_port_base::flow_control::none),
		boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::one));
i'm not sure that adding parameters is the way to go though. It isn't very flexible and not particularly Pythonic (yes, that appears to be 'a thing') and with hindsight optional parameters should have been passed as a 'Options' dictionary with connection specific keys so that the API would remain constant. Maybe this is the time to start that.
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
dvdb
Posts: 5
Joined: Sunday 10 September 2017 13:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Re: How to set serial parameters in Python plugin framework

Post by dvdb »

Dnpwwo, many thanks for your reply on this topic (and PM).

The defaults of the serialport are:
- Parity = NONE
- Stopbits = 1
- Charactersize = 8
- Flowcontrol = NONE

Lucky for me this is what I need, so I don't need to change it.

Code: Select all

	bool CPluginTransportSerial::handleConnect()
	{
		try
		{
			if (!isOpen())
			{
				m_bConnected = false;
				open(m_Port, m_Baud,
					boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none),
					boost::asio::serial_port_base::character_size(8),
					boost::asio::serial_port_base::flow_control(boost::asio::serial_port_base::flow_control::none),
					boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::one));

				m_tLastSeen = time(0);
				m_bConnected = isOpen();
				
Post Reply

Who is online

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