Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set_string not working correctly #54

Closed
zappfinger opened this issue Oct 16, 2015 · 4 comments
Closed

set_string not working correctly #54

zappfinger opened this issue Oct 16, 2015 · 4 comments

Comments

@zappfinger
Copy link

I try to use set_string in a byte array, but only the first 2 characters are written correctly, the rest is garbled. For example, when I use: set_string(db10, 0, 'abcd')
and I read it back with get_string, I get: 'ab@I'
Also, the docstring mentions a size argument that is not present..

@zappfinger
Copy link
Author

Sorry, my fault, I was doing something wrong. Ignore this.

@spreeker
Copy link
Collaborator

I spend significant time making sure writing strings is done safely.
There was a serious bug where the set_string function did not check if the input string fitted in the intended space and just used some extra memory in the plc if the string was too big.. oops..

@zappfinger
Copy link
Author

Are you aware that there are two types of strings in the S7 series? The String type is preceded by 2 bytes. Byte one is the maximum string length, byte 2 is the actual string length. I believe your string function does nothing with byte one, only byte 2 is written?
But the other type is a character array, that can start anywhere in a DB and has no preceding bytes...
This type has no functions for it to read or write...

@spreeker
Copy link
Collaborator

no I had no need for the character array so i have not encountered it.

The reason I don't write the first byte is is that the first byte value (max_size) is set by the siemens step7 project and snap7 / python should under no circumstance change the first byte!!
By setting the actual string size it was still needed for the functions to clear's out the rest of the bytes with ' ' empty space to make it work correctly and prevent weird string values to show 'newvalue_partoflargeroldstring' on the siemen screens.

The max size is given to the function set_string for two reasons. 1 to make sure you know about what you put in and 2 set_string function among others is user by the DB class to edit large a object. The DB object expects a specification / layout of the data and in it the max string size is also specified.
The DB object makes it possible to use a DB in a PLC like a Dict in Python but it is rather hard to setup correctly and I don't know how to make it any more easy..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants