拙网论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 267|回复: 0

Setup of Modbus TCP on Raspberry Pi

[复制链接]

949

主题

1001

帖子

3736

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3736
发表于 2020-1-14 21:59:03 | 显示全部楼层 |阅读模式

https://www.youtube.com/watch?v=p3Dgd0PDjnU
https://jacekhryniewicz.wixsite.com/website/raspberry-pi-with-modbus-tcp
Introduction

Last time I was working with Modbus TCP slaves and masters. I was thinking how to use it and convert Raspberry Pi into Modbus TCP Slave. Luckily, I've found library written in Python - pyModbus that enbles full functionalities of Modbus. In the article, I'd like to show you how it is easy and how you can use this idea e.g. with your home automation system.

Good example of such integration you can find here:












Setup of pyModbus

At the beginning you have to install python 2.7 and repository of pyModbus. To do it use following commands:

sudo apt-get update
sudo apt-get install python-dev
sudo apt-get install python-pip
pip install pymodbus

Additionally, I had to install some extra packages:


pip install pycrypto
pip install pyasn1

More info how to install python you will find here:







Setup of Kepware's Modbus OPC Server Suite

Modbus TCP Server

Finally, we can look at Modbus TCP Server. To make it easy, I've based my code on the example from pyModbus. In this programme, server updates their registers you will find here:


The function that is responsible for udpating the Modbus memory - context - is called "updating_writer". In this line, the current values are retrieved:
values = context[slave_id].getValues(register, address, count=5)
Below, the context is being changed:
values = [v + 1 for v in values]
And finally, Modbus memory is updated:
context[slave_id].setValues(register, address, values)

DHT11 - Temperature & Humidity sensor
In my example, Raspberry Pi works as a Modbus TCP Gateway that transfers data about temprature and humidity. I found two Python libraries that enable to read data from this sensor:

However, I recommend to use the second one, because the first one is not working. First of all - there is small mistake in for loop (some bits are omitted), secondly - Raspbian is not Real Time OS, so counting time on the level of μ seconds without any prioritization of threads makes that reading values from DHT11 is highly error-prone. Lucikly, the second library works perfectly (inside the library you will find the code responsbile for assign high prority fot this task).

The program with compiled Python library, you will find here.

In the second step you have to install Kepware's Modbus OPC Server Suite. Here you can find link to free demo:

Generally, it the configuration process is quite easy. You have to type proper Slave's IP and port number and the add variables. You will have to indicate correct address of registers, type of the OPC Item and time of scan.
Remeber that in Modbus there are four types of registers (read: http://www.csimn.com/CSI_pages/Modbus101.html#mb101_40001), so you shall determine proper type either on OPC side and on Raspberry Pi side.

Links

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|抱朴守拙BBS

GMT+8, 2025-5-26 02:39 , Processed in 0.185424 second(s), 19 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表