Comments (10)
@grawee python-snap7 is just a ctypes wrapper around snap7 c library.
The python function db_read()
just calls Cli_DBRead()
function from c library.
According to header file (snap7.h) the "start" augment is declared as integer.
See: https://github.com/SCADACS/snap7/blob/master/examples/cpp/snap7.h#L436
The types must match. Therefore you have to use integer as a start parameter in db_read()
and read whole byte(s) from PLC.
You should be able to read individual bits from returned bytearray with snap7.util.get_bool().
See example:
reading = plc.db_read(31, 120, 1) # read 1 byte from db 31 staring from byte 120
snap7.util.get_bool(reading, 0, 5) # read a value of fifth bit
from python-snap7.
Thanks @wilkpio for answering this question.
from python-snap7.
Thanls for the answer @wilkpio. It's working fine. I have one more question or problem..
So far, I used the db_write(db_number, start, data) command to write to the PLC. The problem is the same with this function as well, when I try to set float offset value for start. So, I tried to use the set_bool(_bytearray, byte_index, bool_index, value), but I don't know what I need to set to "_bytearray". Can you show me an example for this function.
Maybe good to know, when I want to write in PLC, I read from MySQL database, so I get a bool from SQL what I want to read in PLC.
from python-snap7.
Dear @grawee
The numbers are not FLOATS they look like floats but they are definatly NOT floats.
The smallest thing you can read from a PLC is a BYTE with snap7. One byte is 8 bits. 8 boolean values fit in a byte. on position 0 till position 7. BYTEINDEX.BITINDEX. =! FLOAT.
00000000 = 1 BYTE
1 byte can be used to store 8 boolean values. Packing so many values in a byte is done to so save precious PLC space and memory.
So how to change a bool value?
- Read out the _bytearray your boolean values are in.
- In this byte_array you need to find the byte (byteindex)
- Find the bit in this byte (bitindex) so you can
- set the correct value with utility function set_bool which does all the hard work.
set_bool(_bytearray = data which you read out before.., byte_index, bool_index, value), - write back the changed bytearray back to the PLC.
A round trip wil take 5ms.
The example code
https://github.com/gijzelaerr/python-snap7/blob/master/example/
reading = plc.db_read(31, 120, 1) # read 1 byte from db 31 staring from byte 120
snap7.util.set_bool(reading, 0, 5) # set a value of fifth bit
plc.db_write(reading, 31, 120, 1) # write back the bytearray and now the boolean value is changed
# in the PLC.
hope this helps :)
from python-snap7.
Is there any way to write integer/string in PLC db..... Any link or help would be appreciated....
from python-snap7.
Good day!
Please tell me how to use snap7.util.get_bool () in C ++. Why can't I figure it out myself! And is there any documentation and where can I see it?
from python-snap7.
- This isue is closed.
- Before you start opening a new issue, pls. use Discussions for questions (so we can seperate it)
- This repo is a python layer around snap7 ...I don't know what you mean with "how to use this in C++"???
EDIT: Where do you got problems with
Line 114 in b6481ec
from python-snap7.
I have done through python.. I didn't use c++ directly
from python-snap7.
Is there a similar C ++ layer around snap7?
from python-snap7.
I understand correctly, this was used
https://python-snap7.readthedocs.io/en/1.0/_modules/snap7/util.html#get_bool
?
It turns out that you can rewrite this code in C ++ and there will be a wrapper in C ++.
from python-snap7.
Related Issues (20)
- PLC crashed when executing the "get_cpu_info" function HOT 1
- How to read and write values of type bool using snap7 module?
- Can not use this library to read timers of simatic S7-200 smart PLC HOT 2
- Redundant 400-H get_cpu_state() returning incorrect value.
- Add python 3.12 support, build wheels HOT 2
- RuntimeError: b' ISO : An error occurred during recv TCP : Connection timed out'
- Error in util.Set_String and util.Get_String
- as_db_read() doesnt work
- Wrong values set_lreal
- db[0]['x']=22.33 raises ValueError HOT 2
- Improve doc for area_read in client HOT 2
- Communication between PLC and PC HOT 1
- Can't read from S210 Drive
- Get data block layout
- TypeError: '_ctypes.PyCArrayType' object is not subscriptable HOT 1
- Make client and server a contextmanager HOT 1
- Drop Python 3.8 from metadata and doc HOT 1
- Drop Snap7Library singleton and use python functools caching HOT 1
- version 1.4 broken on python 3.8 HOT 2
- Snap7Exception is not used HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-snap7.