Maintenance - We're currently working on things and you might experience some issues. Should be wrapped up soon!

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

0
Raspberry pi PLC modbus comms
Post Body

Hey guys I have been stuck on this for so long so any help is appreciated.

I have a raspberry pi that I'm using as a thermal fire detection camera. I also have a rs485 hat to connects to My pis serial port. The comms between the pi and click plc over rs485 100% works I have confirmed that. The part I'm stuck on is actually getting the raspberry pi to say hey Mr PLC I'm detecting an alarm. From what I understand my plc should be requesting to read registers/coils etc and then the rapsberry pi responds telling it the updated information. This is the part where I'm stuck. I'm using a click plc and I've tried minimal modbus and pymodbus to have the raspberry pi WRITE to a certain coil. Anyway I can never get it to work, register never update in the plc and im just so lost.

Any helps appreciated

Comments

Fyi this is my code

import os import logging import threading import serial import minimalmodbus

os.system("sudo chmod 777 /dev/ttyS0") os.system("sudo chown pi:pi /dev/ttyS0")

logging.basicConfig() log = logging.getLogger() log.setLevel(logging.DEBUG)

Define your custom logic to read coil values

def your_custom_logic_to_read_coils(coil_address, coil_count): # Implement your logic to read coil values from the slave # Replace this with your own implementation # Example: Read coil values from hardware or memory # Return a list of coil values corresponding to the requested address and count return [False] * coil_count # Example: Return a list of False values

Subclass the RtuSlave to handle incoming Modbus requests

class CustomModbusSlave(minimalmodbus.RtuSlave): def _process(self, request): if request.functioncode == 1: # Modbus function code 01: Read Coils coil_address = request.address coil_count = request.count coil_values = your_custom_logic_to_read_coils(coil_address, coil_count) response_pdu = self._response_pdu(request.functioncode, coil_values) self._send(response_pdu)

Run the Modbus RTU Slave in a separate thread

def run_modbus_server(): # Define the serial port parameters serial_port_name = '/dev/ttyS0' baud_rate = 9600 parity = serial.PARITY_NONE data_bits = 8 stop_bits = 1

# Create the serial port object
serial_port = serial.Serial(
    port=serial_port_name,
    baudrate=baud_rate,
    parity=parity,
    bytesize=data_bits,
    stopbits=stop_bits,
)

# Create the Modbus RTU Slave context
instrument = CustomModbusSlave(port=serial_port, slaveaddress=2)

# Start the Modbus RTU Slave server
while True:
    instrument.handle_request()  # Process incoming Modbus requests

server_thread = threading.Thread(target=run_modbus_server) server_thread.start()

Author
Account Strength
60%
Account Age
3 years
Verified Email
Yes
Verified Flair
No
Total Karma
4
Link Karma
1
Comment Karma
3
Profile updated: 5 days ago
Posts updated: 10 hours ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
1 year ago