Skip to content

Commit

Permalink
Some small changes to python code
Browse files Browse the repository at this point in the history
  • Loading branch information
ottlukas committed May 3, 2024
1 parent 67274f5 commit 3876158
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
11 changes: 3 additions & 8 deletions plc4py/plc4py/drivers/modbus/ModbusConfiguration.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ class ModbusConfiguration(PlcConfiguration):
def __init__(self, url):
super().__init__(url)

if self.transport is None:
self.transport = "tcp"

if self.port is None:
self.port = 502

if "unit_identifier" not in self.parameters:
self.unit_identifier = 1
self.transport = self.transport or "tcp"
self.port = self.port or 502
self.unit_identifier = self.parameters.get("unit_identifier", 1)
39 changes: 18 additions & 21 deletions plc4py/plc4py/drivers/modbus/ModbusConnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,12 @@ def __init__(self, config: ModbusConfiguration, transport: Plc4xBaseTransport):
self._transport: Plc4xBaseTransport = transport

@staticmethod
async def create(url: str):
"""
Static Factory to return an instance of a ModbusConnection.
It creates the TCP connection to the Modbus device before returning.
:param url: PLC4X connection string of the Modbus TCP connection
:return ModbusConnection instance using the configuration from the url provided
"""
config = ModbusConfiguration(url)
loop = asyncio.get_running_loop()
connection_future = loop.create_future()
transport = await asyncio.wait_for(
TCPTransport.create(
protocol_factory=lambda: ModbusProtocol(connection_future),
host=config.host,
port=config.port,
),
10,
async def create(connection_string: str) -> "ModbusConnection":
config = ModbusConfiguration(connection_string)
transport = await TCPTransport.create(
protocol_factory=lambda: ModbusProtocol(connection_future),
host=config.host,
port=config.port,
)
return ModbusConnection(config, transport)

Expand Down Expand Up @@ -108,22 +96,31 @@ def read_request_builder(self) -> ReadRequestBuilder:

async def execute(self, request: PlcRequest) -> PlcResponse:
"""
Executes a PlcRequest as long as it's already connected
Executes a PlcRequest as long as it's already connected.
:param request: Plc Request to execute
:return: The response from the Plc/Device
"""
# Check if the connection is established
if not self.is_connected():
# Return a default failed response if not connected
return self._default_failed_request(PlcResponseCode.NOT_CONNECTED)

# Check the type of the request and execute the corresponding method
if isinstance(request, PlcReadRequest):
# Execute a read request
return await self._read(request)
elif isinstance(request, PlcWriteRequest):
# Execute a write request
return await self._write(request)

# Return a default failed response if the request type is not supported
return self._default_failed_request(PlcResponseCode.NOT_CONNECTED)

def _check_connection(self) -> bool:
return self._device is None

def _connection_established(self) -> bool:
"""Check if the connection to the PLC is established."""
return self._device is not None

async def _read(self, request: PlcReadRequest) -> PlcReadResponse:
"""
Expand Down

0 comments on commit 3876158

Please sign in to comment.