如何用Python将数据写入plc中的int值



my code:

import snap7.client as s7
ip = '192.168.0.7'
rack = 0
slot = 1
data_blok = 100
start_adress = 0
size = 260
try:
plc = s7.Client()
plc.connect(ip, rack, slot)
con = plc.get_connected()
print(f"Bağlantı Durumu: {con}")
db = plc.db_read(data_blok, start_adress, size) //read
name = db[0:256].decode('UTF-8').strip('x00')
print(f'Data AA: {name}')
value = int.from_bytes(db[256:258], byteorder='big')
print(f'Data BB: {value}')
boll = bool(db[258])
print(f'Data CC: {boll}')
except:
print("hata")

输出:

Bağlantı Durumu: True
数据AA: HELLO WORD//string
数据BB: 55//int
数据CC:True//bool
进程结束,退出码0…

///////////////////////////////////////////

如何在这里改变整数值??
plc.db_write (?????????)

我知道现在很晚了,但是这次讨论对我有帮助,也许它对其他人也有帮助。

您应该首先使用snap7中的set_int()函数。直到将bytearray中的整数值更改为:

your_int_value = 219

snap7.util.set_int(db, 256, your_int_value)

它接受你的bytearray "db",并写入整数值"your_int_value"从索引为256的字节开始

其次,你应该发送修改后的bytearray "db"返回PLC为:

plc.db_write(data_blok, start_addres, db)

它的作用是写入bytearray "db"到编号为"data_block"的数据块从索引为" start_address ">

的字节开始下面是我为plc编写特定数据类型的函数:

plc = snap7.client.Client()
#PLC'ye yazma fonksiyonları
def write_bool(db_num,start_byte,boolean_index,bool_value): #Bool yazma 
data = bytearray(1)
snap7.util.set_bool(data,0,boolean_index,bool_value)
plc.db_write(db_num,start_byte,data)
def write_byte(db_num,start_byte,byte_value): #Byte yazma 
data = bytearray(1)
snap7.util.set_byte(data,0,byte_value)
plc.db_write(db_num,start_byte,data)
def write_int(db_num,start_byte,int_value): #Integer yazma 
data = bytearray(2)
snap7.util.set_int(data,0,int_value)
plc.db_write(db_num,start_byte,data)  
def write_real(db_num,start_byte,real_value): #Real yazma 
data = bytearray(4)
snap7.util.set_real(data,0,real_value)
plc.db_write(db_num,start_byte,data)

只需将数据块号赋给"db_num",写入"start_byte"的起始字节的字节索引,以及对应函数的实值,字节,int或布尔值。

首先,需要操作缓冲区。(db命名变量).

client.db_write(data_blok, start_adress, size, db)

https://github.com/gijzelaerr/python-snap7/blob/master/example/example.py

相关内容

  • 没有找到相关文章

最新更新