modbus协议控制变送器和六路继电器
- 案例说明
- 1.器件
- 光照温湿度变送器
- 六路继电器
- 2.测试前操作
- 3.连线
- 代码
- 测试
案例说明
基于modbus协议,本案例实现了下述功能:
(1)采集和上报温度、湿度、光照数据
(2)控制6路继电器
1.器件
haas506或HD1(RTU) | 1 |
---|---|
建大仁科-光照温湿度变送器 | 1 |
郎汉德继电器 | 1 |
光照温湿度变送器
链接: 建大仁科-光照温湿度变送器手册
一款高精度感光变送器,输出数值计量单位为 Lux,设备采用壁挂防水外壳,壁挂式安装,防护等级 IP65。485 通信,标准 ModBus-RTU 通信协议,通信地址及波特率可设置,最远通信距离 2000 米,产品供电为 10-30V 宽电压供电,主要应用于农业大棚、花卉培养温室、农业大田、电子设备生产线等需要光照度监测的场合。
- 建大仁科-光照温湿度变送器参数
- 硬件接线
六路继电器
链接: 继电器手册
采用标准 Modbus- RTU 协议的继电器设备,支持 RS485/232 或网络通信。支持宽电压供电(9-28V DC),多路输入输出控制,可广泛应用于工业生产、农业、智慧城市、写字楼等各类应用场景。
- 继电器参数
2.测试前操作
(1)设置波特率
因为“建大仁科光照温湿度变送器”和“郎汉德6路继电器”两个传感器公用一条rs485总线,所以,两个传感器需要设定为相同的工作波特率。“建大仁科光照温湿度变送器”默认工作波特率为4800, “郎汉德6路继电器”默认工作波特率为9600. 所以,本案例将“郎汉德6路继电器”默认工作波特率设定为4800,具体设置方法参考main.py。
(2)设置设备地址
因为“建大仁科光照温湿度变送器”和“郎汉德6路继电器”两个传感器公用一条rs485总线,所以这两个设备的设备地址不应该相同(默认都是1)。所以本案例将继电器的设备地址改为3。即将继电器上的拨码开关中的第二个推上去。
3.连线
haas506
HD1(RTU)
代码
main.py
import utime
import modbus as mb
import yuanda_htb485
import zzio606
#初始化
def mb_init():
ret = mb.init('modbus_485_4800')
if ret < 0:
raise ValueError("init modbus failed.")
'''
获取htb变送器温度,湿度,光照数据
htb变送器地址默认为1
'''
def test_htb485():
htb485Obj = yuanda_htb485.HTB485(mb, 1)
humidity = htb485Obj.getHumidity()
temperature = htb485Obj.getTemperature()
brightness = htb485Obj.getBrightness()
htb = htb485Obj.getHTB()
print("temperature:", temperature/10,"℃")
print("humidity:", humidity,"%RH")
print("brightness:",brightness,"lx")
print("htb:",htb)
'''
6路继电器,采用标准 Modbus- RTU 协议的继电器设备
将地址调为3
'''
def test_zzio606():
zzio606Obj = zzio606.ZZIO606(mb, 3)
zzio606Obj.openChannel(0)
utime.sleep(1)
zzio606Obj.openChannel(1)
utime.sleep(1)
zzio606Obj.openChannel(2)
utime.sleep(1)
zzio606Obj.openChannel(3)
utime.sleep(1)
zzio606Obj.openChannel(4)
utime.sleep(1)
zzio606Obj.openChannel(5)
status = zzio606Obj.getChannelStatus()
print("status", status[0])
utime.sleep(5)
zzio606Obj.closeChannel(0)
utime.sleep(1)
zzio606Obj.closeChannel(1)
utime.sleep(1)
zzio606Obj.closeChannel(2)
utime.sleep(1)
zzio606Obj.closeChannel(3)
utime.sleep(1)
zzio606Obj.closeChannel(4)
utime.sleep(1)
zzio606Obj.closeChannel(5)
utime.sleep(1)
status = zzio606Obj.getChannelStatus()
print("status", status[0])
if __name__ == '__main__':
mb_init()
test_htb485()
test_zzio606()
mb.deinit()
yuanda_htb485.py
import ustruct
class HTB485(object):
def __init__(self, mbObj, devAddr):
self.mbObj = mbObj
self.devAddr = devAddr
def getHumidity(self):
if self.mbObj is None:
raise ValueError("invalid modbus object.")
value = bytearray(4)
ret = self.mbObj.readHoldingRegisters(self.devAddr, 0, 2, value, 200)
if ret[0] < 0:
raise ValueError("readHoldingRegisters failed. errno:", ret[0])
humidity = ustruct.unpack('hh',value)
return humidity[0]
def getTemperature(self):
if self.mbObj is None:
raise ValueError("invalid modbus object.")
value = bytearray(4)
ret = self.mbObj.readHoldingRegisters(self.devAddr, 0, 2, value, 200)
if ret[0] < 0:
raise ValueError("readHoldingRegisters failed. errno:", ret[0])
temperature = ustruct.unpack('hh',value)
return temperature[1]
def getBrightness(self):
if self.mbObj is None:
raise ValueError("invalid modbus object.")
value = bytearray(4)
ret = self.mbObj.readHoldingRegisters(self.devAddr, 2, 2, value, 200)
if ret[0] < 0:
raise ValueError("readHoldingRegisters failed. errno:", ret[0])
brightness = ustruct.unpack('hh',value)
return brightness[1]
def getHTB(self):
if self.mbObj is None:
raise ValueError("invalid modbus object.")
value = bytearray(10)
ret = self.mbObj.readHoldingRegisters(self.devAddr, 0, 5, value, 200)
if ret[0] < 0:
raise ValueError("readHoldingRegisters failed. errno:", ret[0])
htb = ustruct.unpack('hhhhh',value)
return htb
zzio606.py
class ZZIO606(object):
def __init__(self, mbObj, devAddr):
self.mbObj = mbObj
self.devAddr = devAddr
def openChannel(self, chid):
if self.mbObj is None:
raise ValueError("invalid modbus object.")
ret = self.mbObj.writeCoil(self.devAddr, chid, 0xff00, 200)
return ret[0]
def closeChannel(self, chid):
if self.mbObj is None:
raise ValueError("invalid modbus object.")
ret = self.mbObj.writeCoil(self.devAddr, chid, 0, 200)
return ret[0]
def getChannelStatus(self):
if self.mbObj is None:
raise ValueError("invalid modbus object.")
status = bytearray(1)
ret = self.mbObj.readCoils(self.devAddr, 0, 6, status, 200)
if ret[0] < 0:
raise ValueError("modbus readCoils failed, errno:", ret[0])
return status
board.json
{
"name": "haas506",
"version": "1.0.0",
"io": {
"modbus_485_4800": {
"type": "MODBUS",
"mode": 0,
"port": 2,
"baudrate": 4800,
"priority": 0,
"timeout": 200
},
"modbus_485_9600": {
"type": "MODBUS",
"mode": 0,
"port": 2,
"baudrate": 9600,
"priority": 0,
"timeout": 200
},
"cloud_led": {
"type": "GPIO",
"port": 9,
"dir": "output",
"pull": "pulldown"
},
"net_led": {
"type": "GPIO",
"port": 7,
"dir": "output",
"pull": "pulldown"
}
},
"debugLevel": "ERROR",
"repl": "disable"
}