PLC 读写 西门子PLC

Python使用snap7库进行采集S7协议通信.py

import snap7
from snap7 import util

plc = snap7.client.Client()
# IP地址,机架,槽位
plc.connect('136.129.40.1', 0, 0)
# 测试是否通讯成功
if plc.get_connected():
    print('连接成功')
else:
    print('连接失败')

boolData = bytearray(1)
# 第0个字节第0位
util.set_bool(boolData, 0, 0, True)

plc.db_write(7000, 264, boolData)

# 写入int 值是5
# Status
intData = bytearray(2)
util.set_int(intData, 0, 1000)
plc.db_write(7000, 266, intData)

# 写入int
# Clear
intData = bytearray(2)
util.set_int(intData, 0, 0)
plc.db_write(7000, 166, intData)


# 写入int
# Quantity_of_Processing
intData = bytearray(2)
util.set_int(intData, 0, 1102)
plc.db_write(7000, 1102, intData)

# 写入int
# processing_time
intData = bytearray(2)
util.set_int(intData, 0, 1104)
plc.db_write(7000, 1104, intData)

# 写入int
# Equipment1[1]
# Welding_Number
intData = bytearray(2)
util.set_int(intData, 0, 3000)
plc.db_write(7000, 300, intData)

# 写入real
# Equipment1[1]
# Welding_Current
realData = bytearray(4)
util.set_real(realData, 0, 302.302)
plc.db_write(7000, 302, realData)

# 写入real
# Equipment1[1]
# Welding_Voltage
realData = bytearray(4)
util.set_real(realData, 0, 306.306)
plc.db_write(7000, 306, realData)

# 写入real
# Equipment1[1]
# Welding_Speed
realData = bytearray(4)
util.set_real(realData, 0, 310.1)
plc.db_write(7000, 310, realData)

# 写入int
# Equipment1[1]
# Welding_Number
intData = bytearray(2)
util.set_int(intData, 0, 314)
plc.db_write(7000, 314, intData)

# # string = 'db211'
string = "123"
# data = int.to_bytes(254, 1, 'big') + int.to_bytes(len(string), 1, 'big') + string.encode(encoding='ascii')
data = int.to_bytes(254, 1, 'big') + int.to_bytes(len(string), 1, 'big') + string.encode(encoding='ascii')
plc.write_area(snap7.client.Areas.DB, 7000, 100, data)

#DB块号为7000    若不是读取DB块,此值为0
DB=7000
#要读取的字节开始地址   **记住 是字节**
Statebyte=100
#要读取的字节数量
Numbyte=8
read_date =plc.read_area(snap7.client.Areas.DB, DB, Statebyte, Numbyte)

print("字符为", read_date)

t=snap7.util.get_dword(read_date,0)
#snap7.util.get_dword(数据源,数据源内读取的开始字节位置)
#snap7.util.get_dword  这里叫啥我也不知道,反正输入snap7.util.  就提示了
#我这里是从36字节开始读取8个字节,因为我设置的是Dword数据,占用8字节
print(t)


plc.disconnect()

# 测试是否关闭链接
if plc.get_connected():
    print('关闭链接失败')
else:
    print('关闭链接成功')

OP05A触发.py

import snap7
from snap7 import util

plc = snap7.client.Client()
# IP地址,机架,槽位
plc.connect('136.129.40.1', 0, 0)
# 测试是否通讯成功
if plc.get_connected():
    print('连接成功')
else:
    print('连接失败')

boolData = bytearray(1)
util.set_bool(boolData, 0, 0, True)
# Trigger	Bool	164
plc.db_write(7000, 164, boolData)


plc.disconnect()

# 测试是否关闭链接
if plc.get_connected():
    print('关闭链接失败')
else:
    print('关闭链接成功')

OP05A保存成功反馈.py

import snap7
from snap7 import util

plc = snap7.client.Client()
# IP地址,机架,槽位
plc.connect('136.129.40.1', 0, 0)
# 测试是否通讯成功
if plc.get_connected():
    print('连接成功')
else:
    print('连接失败')


boolData = bytearray(1)
# 第0个字节第0位
util.set_bool(boolData, 0, 0, True)
# Answer	Bool	264
plc.db_write(7000, 264, boolData)

# 写入int 值是1000
# Status
intData = bytearray(2)
util.set_int(intData, 0, 1000)
plc.db_write(7000, 266, intData)


plc.disconnect()

# 测试是否关闭链接
if plc.get_connected():
    print('关闭链接失败')
else:
    print('关闭链接成功')

OP05A复位.py

import snap7
from snap7 import util

plc = snap7.client.Client()
# IP地址,机架,槽位
plc.connect('136.129.40.1', 0, 0)
# 测试是否通讯成功
if plc.get_connected():
    print('连接成功')
else:
    print('连接失败')

# 写入int 值是1000
#
intData = bytearray(2)
util.set_int(intData, 0, 1)
# Clear Int 166
plc.db_write(7000, 166, intData)

boolData = bytearray(1)
# 第0个字节第0位
util.set_bool(boolData, 0, 0, False)
# Answer	Bool	264
plc.db_write(7000, 264, boolData)

# 写入int 值是0
# Status
intData = bytearray(2)
util.set_int(intData, 0, 0)
plc.db_write(7000, 266, intData)


plc.disconnect()

# 测试是否关闭链接
if plc.get_connected():
    print('关闭链接失败')
else:
    print('关闭链接成功')

你可能感兴趣的:(python,python)