Thursday, July 1, 2021

Need some help with Modbus TCP, detailed breakdown inside, wireshark stuff

Here's the situation. I have a device that supposedly uses Modbus TCP to communicate and I'm trying to write a python script to talk with it. I've been alternating between pymodbus and pymodbusTCP libraries. I'm able to make a connection to the device through python, but I can't get any data out of it as it doesn't seem to like my requests. The manufacturer gave me some of their own software to talk to said device, probably written by their engineers, and it works. I'm able to get live data from the device. So it must be that I'm doing something wrong.

Important things to note:

- Device's IP address is 192.168.2.250

- Device's port is 6601

- My laptop's IP address: 192.168.2.5

Ok so lets look at some wireshark data (today was the first time I've ever used it, so go easy on me). First thing I did was listen to the manufacturer's software talking to the device. My thoughts were that maybe I could see how the packets/frames were arranged and then compare them to my python script's frames that it sends out.

Manufacturer's Wireshark Data:

https://i.imgur.com/EN0Oz1q.png

What I'm noticing here, that may be irrelevant, is that it's always sending 8 bytes of data. And the arrangement doesn't really make much sense to me. I can't tell just by looking at the Data section where the unit ID is, or the function code. But, everything's happy and talking.

My Python Script's Wireshark Data and Script:

https://i.imgur.com/oW48ifO.png

Here I'm noticing that my data that I'm sending the device is in Modbus TCP format. It's clear where the Transaction and Protocol ID are, Unit ID, function code, etc. But for some reason the device won't give me a response. (Also note that I was messing around with random Unit ID's, from 0 to whatever). It sends 12 bytes of data, and really doesn't look similar at all to how to manufacturer's software arranges the data.

Something odd as well is that wireshark is telling me these frames are the TCP protocol. But I've seen screenshots where the protocol is displayed as Modbus TCP. So I'm wondering why it's not showing that for me.

And here is my python script

from pyModbusTCP.client import ModbusClient client = ModbusClient(host='192.168.2.250', port=6601, debug=True) print(client.open()) print(client.read_input_registers(9, 1)) client.close() 

I have my script printing what's returned after client.open() to make sure that it's connecting, and it is.

Really hoping some of you experts might be able to see something I'm doing wrong here, or overlooking. ANY help is greatly appreciated. Probably really simple.



No comments:

Post a Comment