gijzelaerr / python-snap7

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

Reading Single Input or Output #53

Closed ufukizgi closed 9 years ago

ufukizgi commented 9 years ago

Hello,

I am really not very good at PLC also Python :smile: I am working on python-snap7 on a few days. I am able to connect, read data and convert to boolean but I can not understand which one is the boolean that I am looking for. In brief, how can I read a specific input or output from PLC? (e.g. : I3.2 or Q0.4 etc.) I will be glad if you explain or share some links. Because I couldn't find any.

Thanks, Ufuk.

ufukizgi commented 9 years ago

That is what I mean. I can't find my fault. Bits matchs on Snap7 Client and on Step7 but they are not same as the booleans my codes returns.

snap7

spreeker commented 9 years ago

ab_read(0,8) is reading 8 bytes and puts it in an bytearray.

bytearray = ab_read(0, 1)    # read the fist bite 

for index in range(8):           # loop over for each bit
     byte_with_bools = 0
     print getbool(bytearray, byte_with_bools, index)    # 

this should work! if you have weird stuff like that, read the source of for example the snap7.util.getbool function you would have figured it out in 10 minutes :)

ufukizgi commented 9 years ago

Thank you but unfortunately nothing changed. Your advice as same as mine indeed. Only the size of bytearray changed. I have read the source. I am in doubt about bit order on bytearray and PLC are not same. If you have a look at ss, values are changing but they should be same on VAT_3 screen.

ss

spreeker commented 9 years ago

Yes the byte order could be different... Maybe the magic variable indicating the correct position in the plc of the ab memory block could be wrong. You could check the variable used in the python source compared to the one used by the snap7 or use the read area function. If you know the exact memory location.

ufukizgi commented 9 years ago

Eureka!!

Finally I found my mistake. I was using ab_read() and I was looking for match in Inputs :sob: In order to find I changed the codes little bit but at least I have found match in Outputs. But if these are outputs, where are inputs? How can I read them? I can't find in documents. http://python-snap7.readthedocs.org/en/latest/client.html

Thank you for your help, spreeker.

1510040220

spreeker commented 9 years ago

Read the source of the client clode. The read_area function can do it all.

  def read_area(self, area, dbnumber, start, size):
    """This is the main function to read data from a PLC.
    With it you can read DB, Inputs, Outputs, Merkers, Timers and Counters.

    :param dbnumber: The DB number, only used when area= S7AreaDB
    :param start: offset to start writing
    :param size: number of units to read
    """
    assert area in snap7.snap7types.areas.values()
    wordlen = snap7.snap7types.S7WLByte
    type_ = snap7.snap7types.wordlen_to_ctypes[wordlen]
    logging.debug("reading area: %s dbnumber: %s start: %s: amount %s: "
                  "wordlen: %s" % (area, dbnumber, start, size, wordlen))
    data = (type_ * size)()
    result = self.library.Cli_ReadArea(self.pointer, area, dbnumber, start,
                                       size, wordlen, byref(data))
    check_error(result, context="client")
    return bytearray(data)
ufukizgi commented 9 years ago

Ah OK thanks.

gijzelaerr commented 9 years ago

@ufukizgi did this solve your problem?

ufukizgi commented 9 years ago

Yes, I am abble to read inputs also. I attached the picture below in order to be an example for newbies as me. :smile:

Thanks for help.

ssinput