Wednesday, February 3, 2016

Python Snap7 S7-1200 Simple Reading/Writing Memory Example



import snap7.client as c
from snap7.util import *
from snap7.snap7types import *

def ReadMemory(plc,byte,bit,datatype):
    result = plc.read_area(areas['MK'],0,byte,datatype)
    if datatype==S7WLBit:
        return get_bool(result,0,bit)
    elif datatype==S7WLByte or datatype==S7WLWord:
        return get_int(result,0)
    elif datatype==S7WLReal:
        return get_real(result,0)
    elif datatype==S7WLDWord:
        return get_dword(result,0)
    else:
        return None

def WriteMemory(plc,byte,bit,datatype,value):
    result = plc.read_area(areas['MK'],0,byte,datatype)
    if datatype==S7WLBit:
        set_bool(result,0,bit,value)
    elif datatype==S7WLByte or datatype==S7WLWord:
        set_int(result,0,value)
    elif datatype==S7WLReal:
        set_real(result,0,value)
    elif datatype==S7WLDWord:
        set_dword(result,0,value)
    plc.write_area(areas["MK"],0,byte,result)

if __name__=="__main__":
    plc = c.Client()
    plc.connect('10.10.54.2',0,1)
    print ReadMemory(plc,420,0,S7WLReal)
    WriteMemory(plc,420,0,S7WLReal,3.141592)
    print ReadMemory(plc,420,0,S7WLReal)

    #DONE!!

9 comments:

  1. hello
    can you help me in this problem " snap7.snap7exceptions.Snap7Exception: b' ISO : An error occurred during recv TCP : Connection timed out' " , please ?

    ReplyDelete
  2. Hi. I've followed your video and copy-pasted the program. I even went and created the same variables at the same adresses you use, but I get a "Function refused by CPU" error. I'm able to connect to the PLC, but I can't neither read nor write. Can you help?

    ReplyDelete
    Replies
    1. permission issue i think make read all access and check allow put/get option and later remove the check from optimize block in properties of db block

      Delete
  3. can we tag name instead of memory address

    ReplyDelete
  4. In this manner my acquaintance Wesley Virgin's tale begins in this SHOCKING and controversial VIDEO.

    Wesley was in the military-and shortly after leaving-he discovered hidden, "self mind control" secrets that the CIA and others used to get anything they want.

    These are the exact same methods many famous people (notably those who "became famous out of nothing") and the greatest business people used to become wealthy and famous.

    You probably know that you utilize only 10% of your brain.

    Really, that's because the majority of your brain's power is UNTAPPED.

    Perhaps that conversation has even occurred INSIDE OF YOUR very own head... as it did in my good friend Wesley Virgin's head around 7 years back, while driving an unlicensed, trash bucket of a car without a license and $3 in his bank account.

    "I'm absolutely fed up with living paycheck to paycheck! When will I get my big break?"

    You've taken part in those types of thoughts, ain't it right?

    Your very own success story is waiting to be written. All you need is to believe in YOURSELF.

    Learn How To Become A MILLIONAIRE Fast

    ReplyDelete
  5. Do you realize there is a 12 word sentence you can communicate to your crush... that will induce intense feelings of love and instinctual appeal for you deep within his heart?

    That's because deep inside these 12 words is a "secret signal" that triggers a man's impulse to love, cherish and look after you with his entire heart...

    12 Words Will Fuel A Man's Desire Impulse

    This impulse is so hardwired into a man's genetics that it will drive him to work better than before to make your relationship as strong as it can be.

    As a matter of fact, fueling this powerful impulse is absolutely mandatory to having the best ever relationship with your man that the instance you send your man one of these "Secret Signals"...

    ...You will instantly find him open his heart and soul for you in a way he never experienced before and he will identify you as the one and only woman in the galaxy who has ever truly appealed to him.

    ReplyDelete
  6. Hi, thanks to your help I'm able to connect to a S7 1200PLC. I have some DInt value. How can I read it in the coorect value?
    Thanks

    ReplyDelete
  7. You have to write set_word and get_word functions in usr/local/lib/python3.7/dist-packages/snap7/util.py

    Value 0xffff (all 16 bits set to one) cannot be processed with get_int or set_int

    ReplyDelete
  8. Great post man, keep the nice work, just shared this with my friendz customwriting

    ReplyDelete