Coder Social home page Coder Social logo

Comments (10)

wilkpio avatar wilkpio commented on May 29, 2024

@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.

gijzelaerr avatar gijzelaerr commented on May 29, 2024

Thanks @wilkpio for answering this question.

from python-snap7.

grawee avatar grawee commented on May 29, 2024

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.

spreeker avatar spreeker commented on May 29, 2024

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?

  1. Read out the _bytearray your boolean values are in.
  2. In this byte_array you need to find the byte (byteindex)
  3. Find the bit in this byte (bitindex) so you can
  4. 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),
  5. 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.

KeshariGupta-ML avatar KeshariGupta-ML commented on May 29, 2024

Is there any way to write integer/string in PLC db..... Any link or help would be appreciated....

from python-snap7.

Mutexman avatar Mutexman commented on May 29, 2024

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.

swamper123 avatar swamper123 commented on May 29, 2024

@Mutexman

  1. This isue is closed.
  2. Before you start opening a new issue, pls. use Discussions for questions (so we can seperate it)
  3. 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

def get_bool(bytearray_: bytearray, byte_index: int, bool_index: int) -> bool:
?

from python-snap7.

KeshariGupta-ML avatar KeshariGupta-ML commented on May 29, 2024

I have done through python.. I didn't use c++ directly

from python-snap7.

Mutexman avatar Mutexman commented on May 29, 2024

Is there a similar C ++ layer around snap7?

from python-snap7.

Mutexman avatar Mutexman commented on May 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.