Python 通过S7协议读取PLC数据 原创

虹喵小仙女
发布于 2023-5-28 09:20
浏览
0收藏

python下载snap7模块

pip3 install python-snap7

测试程序

demo.py

import snap7
import s7_utils


def connectPLC():
    s7_utils.S7_200_Connect('192.168.1.2', 3, 0, 1, "1#PLC")
    s7_utils.S7_200_Connect('192.168.1.3', 3, 0, 1, "2#PLC")
    s7_utils.S7_200_Connect('192.168.1.4', 3, 0, 1, "3#PLC")
    s7_utils.S7_200_Connect('192.168.1.5', 3, 0, 1, "4#PLC")
    s7_utils.S7_200_Connect('192.168.1.6', 3, 0, 1, "5#PLC")

def getAllPLCData():
    for bzplc in s7_utils.PLCArray:
        plcobj = bzplc["plcObj"]
        ## 读取DB块
        V = s7_utils.dbRead(plcobj, bool, 1, 200, 0, 4, 1)
        PV = s7_utils.dbRead(plcobj, float, 1, 300, 0, 4, 0)、
        ## 读取M区
        bj1 = s7_utils.mReadBool(plcobj,1,4)
        bj2 = s7_utils.mReadBool(plcobj,1,3)
        bj3 = s7_utils.mReadBool(plcobj,1,2)
        bj4 = s7_utils.mReadBool(plcobj,1,1)

        print(bzplc["name"]+"\n","电压:",V,"频率",PV,"报警1",bj1,"报警2",bj2,"报警3",bj3,"报警4",bj4)

connectPLC()
getAllPLCData()

s7_utils.S7_200_DisconnectAll()

S7协议工具类

s7_utils.py

import snap7
from snap7.util import *

PLCArray = []

## 连接PLC 加入数组
def S7_200_Connect(ip, type, rack, slot, name):
    plcObj = snap7.client.Client()
    plcObj.disconnect()
    plcObj.set_connection_type(type)


    try:
        plcObj.connect(ip, rack, slot)
    except:
        print(f"连接状态: {ip} 连接失败")
        return 0
    
    print(f"连接状态:{plcObj.get_connected()} {ip} 连接成功")

    saveData = {
        "plcObj": plcObj,
        "name": name,
        "ip": ip
    }

    PLCArray.append(saveData)
    return 1

def S7_200_getplcObj(ip):
    for plc in PLCArray:
        if plc["ip"] == ip:
            return plc["plcObj"]
    
    return 1

## 关闭所有PLC连接
def S7_200_DisconnectAll():
    for plc in PLCArray:
        # print(plc)
        if plc["plcObj"] != None:
            plc["plcObj"].disconnect()
    
    return 1

## 读取DB块
def dbRead(plcObj, type, dbnum, startAddress, address, dblength, boolX):
    """
    DB块的读操作;如果是200smart系列的将dbnum设置为0
    :param dbnum: DB1
    :param dblength: 
    :return:
    """
    data = plcObj.read_area(snap7.types.Areas.DB, dbnum, startAddress, dblength)

    # print(data)

    if type == int:
        return get_int(data, address)
    elif type == bool:
        return get_bool(data, address, boolX)
    elif type == str:
        return get_dword(data, address)
    elif type == float:
        return get_real(data, address)
    elif type == bytes:
        return get_byte(data, address)
    else:
        return None
    
## 写DB块
def dbWrite(plcObj, type, data, dbnum, startAddress, dblength, address):
    """
    DB块的写操作;如果是200smart系列的将dbnum设置为0
    :param dbnum: DB块的序号
    :param dblength:
    :return: 字节长度,根据需要设定
    """
    data = plcObj.read_area(snap7.types.Areas.DB, dbnum, startAddress, dblength)

    if type == int:
        set_int(data, address, data)
    elif type == bool:
        set_bool(data, address, 0, data)
    elif type == str:
        set_dword(data, address, data)
    elif type == float:
        set_real(data, address, data)
    elif type == bytes:
        set_byte(data, address, data)
    else:
        return None
    
    plcObj.write_area(snap7.types.Areas.DB, dbnum, startAddress, data)

def mReadBool(plcObj, num, bit):
    """
    M区的读操作--------bool
    :param num:
    :param bit:
    :return:
    """
    data = plcObj.read_area(snap7.types.Areas.MK, 0, num, 1)

    # print(data)

    return get_bool(data, 0, bit)
 
 
def mRead(plcObj, type, num):
    """
    M区的读操作--------int/word/dint/dword
    :param num:
    :return:
    """
    data = plcObj.read_area(snap7.types.Areas.MK, 0, num, 2)


    if type == int:
        get_int(data, 0)
    elif type == bool:
        get_byte(data, 0)
    elif type == "dint":
        get_dint(data, 0)
    else:
        return None
 
 
def mWriteBool(plcObj, byte, bit, value):
    """
    M块的写操作---------bool
    :param byte:
    :param bit:
    :param value:
    :return:
    """
    data = plcObj.read_area(snap7.types.Areas.MK, 0, byte, 1)
    set_bool(data, 0, bit, value)
    plcObj.write_area(snap7.types.Areas.MK, 0, byte, data)
 
 
def mWrite2(plcObj, byte, value):
    """
    M块的写操作---------int/word/dint/dword
    :param byte:
    :param value:
    :return:
    """
    data = plcObj.read_area(snap7.types.Areas.MK, 0, byte, 4)
    # set_int(data, 0, value)
    set_dint(data, 0, value)
    plcObj.write_area(snap7.types.Areas.MK, 0, byte, data)

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
3
收藏
回复
举报
回复
    相关推荐