Menu

#25 write data on PLC using snap7

1.0
open
nobody
2020-10-23
2020-10-07
No

How can i get i responce from db_write() or write_area() in snap7 ....that daata is written on PLC...please help ...thanks

Discussion

  • Sushil Dhote

    Sushil Dhote - 2020-10-07

    @davenardella can you please help

     
  • Davide Nardella

    Davide Nardella - 2020-10-07

    what wrapper (Hi level language) are you using ?

    Every snap7 function returns an integer, if it is zero, it means that the PLC, in accord to the protocol, sent a message saying that the operation was accomplished. This is valid also for functions that don't return values (like dbwrite and writearea).

    So simply test the result.

     
  • Sushil Dhote

    Sushil Dhote - 2020-10-07

    @davenardella i am using python language and using python-snap7 wrapper ...

     
  • Sushil Dhote

    Sushil Dhote - 2020-10-07

    https://github.com/gijzelaerr/python-snap7 this is github link that i am using

    in my code i tried
    temp = plc.db_write(db_number=db, start=start, data=result)
    print("write...success:",temp)

            but it is getting "None" as response
    
     

    Last edit: Sushil Dhote 2020-10-07
  • Davide Nardella

    Davide Nardella - 2020-10-07

    From the question you asked me, I imagined this answer.
    You should speak with the python-snap7 programmer.

    I don't know python at all, because I don't like it at all.
    Sounds very strange to me that a function does not returns a value and that an exception must be raised also if a return value is not zero.

    Snap7 as dll (or .so) does not raise exceptions, it returns an integer that must be checked.

     
  • Sushil Dhote

    Sushil Dhote - 2020-10-07

    yes , i am trying to connect with him ,i thought u may have some idea..OK thanks for rply

     
  • Davide Nardella

    Davide Nardella - 2020-10-07

    As far I can see in write_area (but I'm not an expert):

    return self.library.Cli_WriteArea(self.pointer, area, dbnumber, start,
    size, wordlen, byref(cdata))

    you should see the value returned by the library function

     
  • Sushil Dhote

    Sushil Dhote - 2020-10-07

    yes right , but i am getting "None" in temp varible as i showned above my above code.
    in both the scenirio ....when it success or fail

     
  • Davide Nardella

    Davide Nardella - 2020-10-07

    Since you don't have a result, what do you mean for "fail" ?
    Do you don't find the values into the DB ?

    1 - Try to write the same values using a non-existent DB as db_number param, what happens ?
    2 - Are you completely sure that in your user program you don't overwrite that values ?

     
  • Sushil Dhote

    Sushil Dhote - 2020-10-07

    1."fail" means ....when i try to write on PLC 2 ,it is not getting write on that particular Dbaddress
    2. when i try to write on PLC 1 with same code it it success ,i can see values on that Dbaddress

    3.yes ,after you ask to write same values on non-existent Db number ,it get's Exception that "Address out of range"
    4.yes tried lots of different values and diffrent datatype values also,,,,,writing string,int,Dint,....getting "None" for all.....pretty sure not overwrite

     

    Last edit: Sushil Dhote 2020-10-07
  • Davide Nardella

    Davide Nardella - 2020-10-07

    Cannot happen that a PLC answers "Ok I performed" (and so Result==0) but it didn't write the values.

    Into PLC 2 create a new DB similar to previous one , and write into that, or write consecutively into the old DB and into the new one.

    I bet that you will find the data into the new DB.
    Let me know ;)

     
  • Sushil Dhote

    Sushil Dhote - 2020-10-07

    1.yes, i checked ,Cli_DBWrite () return 0 if it get successufullly write vvalues on PLC.
    2.Cli_DBWrite() return the error if it not able to write.
    3.the problem i found is when i code below like this

    temp = plc.db_write(db_number=db, start=start, data=result)
    print(temp)

    "temp" varible not able to get that 0 value,it act as None

    when i tried
    if (plc.db_write(db_number=db, start=start, data=result));
    pass
    else:
    print("success")

    it is working properly.

    now the problem is when it not able to write it return error but i also need 1

     
  • Davide Nardella

    Davide Nardella - 2020-10-07

    Sorry I cannot help you on Phyton side...

     
  • Benjamin Wischer

    Its because the function is actually wrapped.
    the function error_wrap returns the executed function without any return Value.
    The function check_error raises an exception in case of any errors.

    #this is what the call actually looks like:
    plc.error_wrap(db_write(db_number=db, start=start, data=result))
    
    
      @error_wrap
       def db_write(self, db_number, start, data):
        dosomething()
    
        def error_wrap(func):
        """Parses a s7 error code returned the decorated function."""
    
            def f(*args, **kw):
                code = func(*args, **kw)
                check_error(code, context="client") #this function raises an exception
    
            return f
    

    Possible Solution:

    try:
      plc.db_write(db_number=db, start=start, data=result)
      # continue with programm everything is fine
    
     except Snap7Exception as s7ex:
     # Fehlerfall
         print(s7ex.message)
    
     

    Last edit: Benjamin Wischer 2020-10-23

Log in to post a comment.