gijzelaerr / python-snap7

A Python wrapper for the snap7 PLC communication library
http://python-snap7.readthedocs.org/
MIT License
643 stars 246 forks source link

using as_write_area Not able to write a single bit #413

Closed Diptee24 closed 1 year ago

Diptee24 commented 1 year ago

hi sir, i am getting below error for snap7 as_write_area function. Traceback (most recent call last): File ".\forumsend.py", line 17, in client.as_write_area(snap7.types.S7AreaDB,1,56,1,snap7.types.S7WLBit,byte_array_true) File "....\lib\site-packages\snap7\client.py", line 1053, in as_write_area logger.debug(f"writing area: {area.name} dbnumber: {dbnumber} start: {start}: size {size}: " AttributeError: 'int' object has no attribute 'name'

my code is: import sys import snap7 from snap7.types import areas,Areas from ctypes import byref

ANPIP='192.168.2.1'

byte_array_true = bytearray([1]) byte_array_false = bytearray([0])

client = snap7.client.Client() client.set_connection_type(3) client.connect(ANPIP, 0, 1)

buffer1 = client.db_read(1, 0, 5000)

client.as_write_area(snap7.types.S7AreaDB,1,56,1,snap7.types.S7WLBit,byte_array_true)

gijzelaerr commented 1 year ago

you should supply snap7.types.Areas.DB to that function, the S7Area* constants are deprecated.

Diptee24 commented 1 year ago

i changed from snap7.types.S7AreaDB to snap7.types.Areas.DB as per below code still getting same error

Traceback (most recent call last): File ".\forumsend.py", line 17, in client.as_write_area(snap7.types.Areas.DB,1,56,1,snap7.types.S7WLBit,byte_array_true) File "\lib\site-packages\snap7\client.py", line 1056, in as_write_area
res = self._library.Cli_AsWriteArea(self._pointer, area.value, dbnumber, start, size, wordlen.value, byref(cdata)) AttributeError: 'int' object has no attribute 'value'

import sys import snap7 from snap7.types import areas,Areas from ctypes import byref

ANPIP='192.168.2.1'

byte_array_true = bytearray([1]) byte_array_false = bytearray([0])

client = snap7.client.Client() client.set_connection_type(3) client.connect(ANPIP, 0, 1)

buffer1 = client.db_read(1, 0, 5000)

client.as_write_area(snap7.types.Areas.DB,1,56,1,snap7.types.S7WLBit,byte_array_true)

gijzelaerr commented 1 year ago

now it is the wordlen argument, should be snap7.types.WordLen.Bit

Diptee24 commented 1 year ago

okay i have changed the same...error is solved...thank you so much for solution and quick response.