Repository: pymodbus-dev/pymodbus

Issue:
Asynchronous RTU serial server not detecting port string
<!--
Before opening a new issue, make sure you do the following:

- Check that your issue isn't already filed: https://github.com/pymodbus-dev/pymodbus/issues
- Check the discussions forum https://github.com/pymodbus-dev/pymodbus/discussions
- Prepare a short, runnable example that reproduce the issue with the latest development version of Pymodbus
-->

### Versions

- Python: 3.11.4
- OS: Windows
- Pymodbus: Works with 3.3.2 Stop working with 3.4.0
- Modbus Hardware (if used): FTDI Based RS485<->USB Adapter

### Pymodbus Specific

- Server: rtu- async

### Description

With the new Version it is not possible to start a server, because the Port String is not detected and the default Value of 127.0.0.1 is used.

### Code and Log

## Code
```python
import asyncio
import logging

from pymodbus import __version__ as pymodbus_version
from pymodbus.datastore import (
    ModbusSequentialDataBlock,
    ModbusServerContext,
    ModbusSlaveContext,
    ModbusSparseDataBlock,
)
from pymodbus.device import ModbusDeviceIdentification

import argparse
import os

from pymodbus import pymodbus_apply_logging_config
from pymodbus.transaction import ModbusRtuFramer
from pymodbus.server import StartAsyncSerialServer
pymodbus_apply_logging_config()
logging.basicConfig()
_logger = logging.getLogger(__file__)
_logger.setLevel(logging.DEBUG)

async def run_async_server():
    """Run server."""
    datablock = ModbusSequentialDataBlock.create()
    server = await StartAsyncSerialServer(
        context=ModbusServerContext(
            slaves={
                0x01: ModbusSlaveContext(
                    di=datablock,
                    co=datablock,
                    hr=datablock,
                    ir=datablock,
                ),
                0x02: ModbusSlaveContext(
                    di=datablock,
                    co=datablock,
                    hr=datablock,
                    ir=datablock,
                ),
            },
            single=False
        ),
                # Data storage
        identity=ModbusDeviceIdentification(
            info_name={
                "VendorName": "Pymodbus",
                "ProductCode": "PM",
                "VendorUrl": "https://github.com/pymodbus-dev/pymodbus/",
                "ProductName": "Pymodbus Server",
                "ModelName": "Pymodbus Server",
                "MajorMinorRevision": pymodbus_version,
            }
        ),  # server identify
        port="COM11",  # serial port
        framer=ModbusRtuFramer,  # The framer strategy to use
        baudrate=19200,  # The baud rate to use for the serial device
    )    
    return server


async def async_helper():
    _logger.info("Starting...")
    await run_async_server()


if __name__ == "__main__":
    asyncio.run(async_helper(), debug=True)  # pragma: no cover
```

## Log
```txt
INFO:C:\Users\jannik.goebel\Documents\Projects\python\modbusServer\example.py:Starting...
2023-07-25 10:12:35,621 INFO  logging:96 Server(Serial) listening.
INFO:pymodbus.logging:Server(Serial) listening.
2023-07-25 10:12:35,622 DEBUG logging:102 Awaiting connections server_listener
DEBUG:pymodbus.logging:Awaiting connections server_listener
2023-07-25 10:12:35,622 WARNING logging:108 Failed to start server could not open port '127.0.0.1': FileNotFoundError(2, 'Das System kann die angegebene Datei nicht finden.', None, 2)
WARNING:pymodbus.logging:Failed to start server could not open port '127.0.0.1': FileNotFoundError(2, 'Das System kann die angegebene Datei nicht finden.', None, 2)
2023-07-25 10:12:35,622 ERROR logging:114 Server unexpected exception 'NoneType' object has no attribute 'serve_forever'
ERROR:pymodbus.logging:Server unexpected exception 'NoneType' object has no attribute 'serve_forever'
```



Discussion:
Hmmm I am pretty sure we check that, but let me investigate.

Apart from that your port string seems wrong, you need to point at the device driver.

Resolving patch:
diff --git a/pymodbus/transport/transport.py b/pymodbus/transport/transport.py
--- a/pymodbus/transport/transport.py
+++ b/pymodbus/transport/transport.py
@@ -173,10 +173,14 @@ def init_check_nullmodem(self) -> bool:
     def init_setup_connect_listen(self) -> None:
         """Handle connect/listen handler."""
         if self.comm_params.comm_type == CommType.SERIAL:
+            if self.is_server:
+                port = self.comm_params.source_address[0]
+            else:
+                port = self.comm_params.host
             self.call_create = lambda: create_serial_connection(
                 self.loop,
                 self.handle_new_connection,
-                self.comm_params.host,
+                port,
                 baudrate=self.comm_params.baudrate,
                 bytesize=self.comm_params.bytesize,
                 parity=self.comm_params.parity,


Test patch: