Skip to content

Conversation

@tilman19
Copy link
Contributor

This allows to have a custom send method triggering a hardware Driver Enable (DE) using the RTS pin e.g. on Raspberry Pis for the server (modbus slave) as well. This solution is based on #33 (comment) (Thanks for this one!)

Something similar is already available for the async-io server, but there it is hard to get the correct timing for the RTS pin.

class MyModbusSingleRequestHandler(CustomSingleRequestHandler):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def send(self, message):
        if message.should_respond:
            pdu = self.framer.buildPacket(message)
            tx_time = (len(pdu) + 1) * (10.0 / self.request.baudrate)
            self.request.setRTS(False)
            time.sleep(0.001)
            status = self.request.send(pdu)
            time.sleep(tx_time)
            self.request.setRTS(True)
            logging.debug('send: [%s]- %s' % (message, b2a_hex(pdu)))
            return status

server = StartSerialServer(context, framer=ModbusAsciiFramer, identity=identity,
                           port='/dev/ttyAMA1', timeout=.005, baudrate=9600, handler=MyModbusSingleRequestHandler)

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@dhoomakethu dhoomakethu merged commit bf0bc1c into pymodbus-dev:dev Jan 11, 2022
@tilman19 tilman19 deleted the dev branch January 11, 2022 13:57
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants