TCP server example on Raspberry Pi

2,759 views
Skip to first unread message

haiop...@gmail.com

unread,
Feb 24, 2015, 3:15:34 PM2/24/15
to modb...@googlegroups.com

Hello,


At the moment I want to apply the TCP server example on my needs. I want to read registers from a SMA photovoltaic inverter via MODBUS. The occuring protocol can be found here. The most important parameters are port = 502 and Unit ID = 3.

One registers of interest is 30535 which is an U32 value which means to read two registers.


I tried the following implementation in Python. I tried both to use holding registers and analog inputs.

#!/usr/bin/env python
# -*- coding: utf_8 -*-

import sys

import modbus_tk
import modbus_tk.defines as cst
import modbus_tk.modbus as modbus
import modbus_tk.modbus_tcp as modbus_tcp

if __name__ == '__main__':
   
try:
       
#Create the server
        server
= modbus_tcp.TcpServer(address='192.168.178.26', port=502)

        server
.start()

        slave
= server.add_slave(3)
        slave
.add_block('0', cst.HOLDING_REGISTERS, 30535, 2)
        slave
.add_block('1', cst.ANALOG_INPUTS, 30535, 2)
 
        values
= slave.get_values('0', 30535, 2)
        sys
.stdout.write(str(values))

        values
= slave.get_values('1', 30535, 2)
        sys
.stdout.write(str(values))

   
finally:
        server
.stop()

Unfortunately I only can read the values (0, 0) for both cases.


On the other hand by using QModMaster I can read the correct values for the register. The reading is succesfully for both Read Input Registers and Read Holding Registers.See the following screenshots for more details:


The python script was running on a raspberry pi with the operating system raspbian. The QModMaster was executed on windows 7.

Is there any mistake in my Python script?


Thank you for your help!

Luc JEAN

unread,
Feb 25, 2015, 1:34:09 AM2/25/15
to modb...@googlegroups.com
Hello,

I am not sure to understand correctly but it seems to me that your python script should not be a Modbus slave but a Modbus master.

If you want that your script collect data from a device: you must write a master. If you want to make data available : you can write a slave and use the TcpServer.

You can look at this example for writing a modbus master: https://code.google.com/p/modbus-tk/source/browse/examples/tcpmaster_example.py

I hope it helps
Best
luc

--
You received this message because you are subscribed to the Google Groups "modbus-tk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modbus-tk+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

haiop...@gmail.com

unread,
Feb 27, 2015, 7:24:48 AM2/27/15
to modb...@googlegroups.com
Hello Luc,

yes I need a Master. Thank you very much for your hint. Now my application works!

Best regards,
Thomas

Luc JEAN

unread,
Feb 27, 2015, 7:26:46 AM2/27/15
to modb...@googlegroups.com
Hello Thomas,

Thanks for your message. I am happy to know that everything goes well now.

Best
luc

Roel Nagels

unread,
May 31, 2016, 11:27:42 AM5/31/16
to modbus-tk
Thomas, I'm trying to connect to my SMA TriPower, but can't figure out how to do that.

Any chance in getting your code so I can modify it for my needs?

Kind regards, Roel

Op vrijdag 27 februari 2015 13:26:46 UTC+1 schreef luc:
Reply all
Reply to author
Forward
0 new messages