Jump to content

Having trouble to read holding registers from V1210 using pymodbus


Recommended Posts

Hi,

I am pretty noob in Unitronics and Modbus, but I am trying my best to learn on the go.

We have a V1210 with modbus configured and I am trying to read holding registers from there as I would like to get this data to my linux server every 1-2 seconds.

I have tried to follow https://pymodbus.readthedocs.io/en/latest/source/client.html official documentation and create a .py for collecting the data.

It seems that I am able to get the connection successfully and also my read request (modbus code 3) is sent successfully, but V1210 is responding with non-standard response, which is not recognised as Modbus protocol by my python client or Wireshark.

I try to give all relevant information, but please ask more if my problem is not clear or detailed enough for giving answers.

Modbus is working normally between this V1210 and other V1210:s we have with same connection details.

I have tried to also use 40321 directly in the code, but it has the same results, and according to my understanding of pymodbus we should use 320.

If anyone has example code of pymodbus client read holding registers towards V1210 I would appreciate a lot to receive it! :)

 

My modbus config in V1210:

Network ID: 255

Port 20257

 

Error I get:

DEBUG:pymodbus.logging:Connection to Modbus server established. Socket ('IP of PLC', 59772)

DEBUG:pymodbus.logging:Current transaction state - IDLE

DEBUG:pymodbus.logging:Running transaction 1

DEBUG:pymodbus.logging:SEND: 0x0 0x1 0x0 0x0 0x0 0x6 0x0 0x3 0x9d 0x80 0x0 0x1

DEBUG:pymodbus.logging:New Transaction state "SENDING"

DEBUG:pymodbus.logging:Changing transaction state from "SENDING" to "WAITING FOR REPLY"

DEBUG:pymodbus.logging:Transaction failed. (Modbus Error: [Invalid Message] Incomplete message received, expected at least 8 bytes (6 received))

DEBUG:pymodbus.logging:Processing:

DEBUG:pymodbus.logging:Getting transaction 1

DEBUG:pymodbus.logging:Changing transaction state from "PROCESSING REPLY" to "TRANSACTION_COMPLETE"

Register Values: Modbus Error: [Input/Output] Modbus Error: [Invalid Message] Incomplete message received, expected at least 8 bytes (6 received)

 

My python code:

import logging

from pymodbus.client import ModbusTcpClient

from pymodbus.exceptions import ModbusIOException



# Configure logging for pymodbus

logging.basicConfig()

log = logging.getLogger('pymodbus')

log.setLevel(logging.DEBUG)



# Unitronics Modbus TCP configuration details

PLC_IP_ADDRESS = "IP of the PLC"

PLC_PORT = 20257

UNIT_ID = 255  # Network ID

TIMEOUT = 10  # Timeout in seconds (100 units of 10 ms each)

RETRIES = 3  # Number of retries



# Create a Modbus TCP client

client = ModbusTcpClient(PLC_IP_ADDRESS, port=PLC_PORT, timeout=TIMEOUT, retries=RETRIES)



# Connect to the PLC

client.connect()



# Reading holding registers corresponding to MI320 to MI322

START_ADDRESS = 320  # Start at MI320 (Modbus address 40321)

REGISTER_COUNT = 3   # Read MI320 to MI322 (3 registers)



try:

    # Read holding registers

    response = client.read_holding_registers(START_ADDRESS, REGISTER_COUNT, unit=UNIT_ID)



    # Check if response is an error

    if response.isError():

        print("Error reading registers:", response)

    else:

        register_values = response.registers

        print("Register Values:", register_values)

except ModbusIOException as e:

    print("Modbus IO error:", e)

except Exception as e:

    print("General error:", e)

finally:

    # Close the client connection

    client.close()

 

 

Link to comment
Share on other sites

43 minutes ago, Joe Tauser said:

The default port for Modbus TCP is 502.  You do have to initialize it in your PLC code.

Can you upload your PLC program so we can see what you've done?

 

Joe T.

Hi Joe, 

Here is the requested image from the start of the program. We do have Modbus also in the port 502 and response from that port is identical to the one in 20257. I have been requested to use port 20257, although I don't think it matters too much which one I use. I have help from our side for building the program, but I do not have help for Modbus or pymodbus.

-Tasanen

image.jpg

Link to comment
Share on other sites

  • MVP 2023

OK, that network needs to be broken into about four separate simpler networks.  The compiler is probably having a nervous breakdown.

What is the purpose of MB 100?

Normally just Init the sockets and run the configs just once at power-up.  I think you're way over-complicating this.

Are you configuring two Modbus IP ports because you want to have a Master and a Slave in the same unit?

 

Joe T.

Link to comment
Share on other sites

11 hours ago, tasanen said:

Hi Joe, 

Here is the requested image from the start of the program. We do have Modbus also in the port 502 and response from that port is identical to the one in 20257. I have been requested to use port 20257, although I don't think it matters too much which one I use. I have help from our side for building the program, but I do not have help for Modbus or pymodbus.

-Tasanen

image.jpg

First of all this rung is awful. No offense to you but visilogic is not a FBD programming, is ladder logic with some function blocks, Visilogic is kind of old and I wouldn't try to push everything on a single rung

 

Which memory addres are you trying to read from the PLC? To make sure you are doing the correct math for the client modbus addresing.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...