Skip to content

Commit

Permalink
feat(plc4py): Modbus Driver can now connect and disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
hutcheb committed Sep 29, 2022
1 parent 19baa51 commit ca564ce
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 7 deletions.
2 changes: 1 addition & 1 deletion sandbox/plc4py/plc4py/PlcDriverManager.py
Expand Up @@ -82,7 +82,7 @@ def get_connection(self, url: str) -> PlcConnection:

def list_drivers(self) -> list[str]:
"""
Returns the codes of all of the drivers which are currently registered at the PlcDriverManager
Returns the codes of the drivers which are currently registered at the PlcDriverManager
:return: Set of driver codes for all drivers registered
"""
return list(self._driverMap.keys())
Expand Down
16 changes: 11 additions & 5 deletions sandbox/plc4py/plc4py/drivers/modbus/ModbusConnection.py
Expand Up @@ -37,26 +37,32 @@ class ModbusConnection(PlcConnection):
def __init__(self, url: str):
super().__init__(url)


def connect(self):
async def connect(self):
"""
Establishes the connection to the remote PLC.
"""
self._transport = TCPTransport(protocol_factory=lambda: ModbusProtocol(), host=HOST, port=PORT)
self._transport = TCPTransport(protocol_factory=lambda: ModbusProtocol(),
host=self._configuration.host,
port=self._configuration.port)
await self._transport.connect()

def is_connected(self) -> bool:
"""
Indicates if the connection is established to a remote PLC.
:return: True if connection, False otherwise
"""
pass
if self._transport is not None:
return not self._transport.is_closing()
else:
return False

def close(self) -> None:
"""
Closes the connection to the remote PLC.
:return:
"""
pass
if self._transport is not None:
self._transport.close()

def read_request_builder(self) -> ReadRequestBuilder:
"""
Expand Down
7 changes: 6 additions & 1 deletion sandbox/plc4py/plc4py/drivers/modbus/ModbusProtocol.py
Expand Up @@ -24,5 +24,10 @@

@dataclass
class ModbusProtocol(Plc4xBaseProtocol[ChannelMessage]):
pass

def data_received(self, data):
# TODO:- Implement Protocol specific stuff
pass

def close(self):
pass
18 changes: 18 additions & 0 deletions sandbox/plc4py/plc4py/spi/configuration/__init__.py
@@ -0,0 +1,18 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
8 changes: 8 additions & 0 deletions sandbox/plc4py/plc4py/spi/transport/Plc4xBaseTransport.py
Expand Up @@ -46,6 +46,10 @@ def protocol(self, protocol: str):
def is_reading(self):
return self._transport.is_reading()

def is_closing(self):
if self._transport is not None:
return self._transport.is_closing()

def pause_reading(self):
self._transport.pause_reading()

Expand Down Expand Up @@ -77,3 +81,7 @@ def writelines(self, list_of_data: list[Any]) -> None:

def write_eof(self):
self._transport.write_eof()

def close(self):
if self._transport is not None:
self._transport.close()
18 changes: 18 additions & 0 deletions sandbox/plc4py/tests/unit/plc4py/drivers/__init__.py
@@ -0,0 +1,18 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
18 changes: 18 additions & 0 deletions sandbox/plc4py/tests/unit/plc4py/drivers/modbus/__init__.py
@@ -0,0 +1,18 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
@@ -0,0 +1,27 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
from plc4py.PlcDriverManager import PlcDriverManager
from plc4py.drivers.modbus.ModbusConnection import ModbusConnection


async def test_plc_driver_modbus_connect():
driver_manager = PlcDriverManager()
with driver_manager.connection("modbus:tcp://127.0.0.1:502") as connection:
await connection.connect()
assert connection.is_connected()
18 changes: 18 additions & 0 deletions sandbox/plc4py/tests/unit/plc4py/spi/configuration/__init__.py
@@ -0,0 +1,18 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

0 comments on commit ca564ce

Please sign in to comment.