Skip to content

Commit

Permalink
include types into doc
Browse files Browse the repository at this point in the history
  • Loading branch information
gijzelaerr committed Jul 7, 2024
1 parent 0295428 commit 19d6a32
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
5 changes: 5 additions & 0 deletions doc/API/type.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Types
=====

.. automodule:: snap7.type
:members:
1 change: 1 addition & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Contents:
API/server
API/partner
API/logo
API/type
API/util


Expand Down
29 changes: 28 additions & 1 deletion snap7/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@


class Parameter(IntEnum):
# // PARAMS LIST
"""
The snap7 parameter types
"""

LocalPort = 1
RemotePort = 2
PingTimeout = 3
Expand Down Expand Up @@ -87,6 +90,10 @@ def ctype(self) -> CDataType:
# Area ID
# Word Length
class WordLen(IntEnum):
"""
The snap7 word length types
"""

Bit = 0x01
Byte = 0x02
Char = 0x03
Expand All @@ -113,6 +120,10 @@ def ctype(self) -> CDataType:


class Area(IntEnum):
"""
The snap7 area types
"""

PE = 0x81
PA = 0x82
MK = 0x83
Expand All @@ -134,6 +145,8 @@ def wordlen(self) -> WordLen:

class SrvArea(IntEnum):
"""
The snap7 server area types
NOTE: these values are DIFFERENT from the normal area IDs.
"""

Expand All @@ -146,6 +159,10 @@ class SrvArea(IntEnum):


class Block(IntEnum):
"""
The snap7 block type
"""

OB = 0x38
DB = 0x41
SDB = 0x42
Expand Down Expand Up @@ -173,6 +190,10 @@ def ctype(self) -> c_int:


class SrvEvent(Structure):
"""
The snap7 server event structure
"""

_fields_ = [
("EvtTime", time_t),
("EvtSender", c_int),
Expand All @@ -193,6 +214,10 @@ def __str__(self) -> str:


class BlocksList(Structure):
"""
The snap7 block list structure
"""

_fields_ = [
("OBCount", c_int32),
("FBCount", c_int32),
Expand Down Expand Up @@ -250,6 +275,8 @@ def __str__(self) -> str:


class S7DataItem(Structure):
""" """

_pack_ = 1
_fields_ = [
("Area", c_int32),
Expand Down
21 changes: 9 additions & 12 deletions snap7/util/getters.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,25 +493,24 @@ def get_lint(bytearray_: bytearray, byte_index: int) -> int:
def get_lreal(bytearray_: bytearray, byte_index: int) -> float:
"""Get the long real
Notes:
Datatype `lreal` (long real) consists in 8 bytes in the PLC.
Negative Range: -1.7976931348623158e+308 to -2.2250738585072014e-308
Positive Range: +2.2250738585072014e-308 to +1.7976931348623158e+308
Zero: ±0
Datatype `lreal` (long real) consists in 8 bytes in the PLC.
Negative Range: -1.7976931348623158e+308 to -2.2250738585072014e-308
Positive Range: +2.2250738585072014e-308 to +1.7976931348623158e+308
Zero: ±0
Args:
bytearray_: buffer to read from.
byte_index: byte index from where to start reading.
Returns:
Value read.
The real value.
Examples:
read lreal value (here as example 12345.12345) from DB1.10 of a PLC
>>> from snap7 import Client
>>> data = Client().db_read(db_number=1, start=10, size=8)
>>> get_lreal(data, 0)
12345.12345
12345.12345
"""
return float(struct.unpack_from(">d", bytearray_, offset=byte_index)[0])

Expand Down Expand Up @@ -637,7 +636,7 @@ def get_char(bytearray_: bytearray, byte_index: int) -> str:
>>> from snap7 import Client
>>> data = Client().db_read(db_number=1, start=10, size=1)
>>> get_char(data, 0)
'C'
C
"""
char = chr(bytearray_[byte_index])
return char
Expand All @@ -646,9 +645,7 @@ def get_char(bytearray_: bytearray, byte_index: int) -> str:
def get_wchar(bytearray_: bytearray, byte_index: int) -> str:
"""Get wchar value from bytearray.
Notes:
Datatype `wchar` in the PLC is represented in 2 bytes. It has to be in utf-16-be format.
Datatype `wchar` in the PLC is represented in 2 bytes. It has to be in utf-16-be format.
Args:
bytearray_: buffer to read from.
Expand All @@ -662,7 +659,7 @@ def get_wchar(bytearray_: bytearray, byte_index: int) -> str:
>>> from snap7 import Client
>>> data = Client().db_read(db_number=1, start=10, size=2)
>>> get_wchar(data, 0)
'C'
C
"""
if bytearray_[byte_index] == 0:
return chr(bytearray_[1])
Expand Down

0 comments on commit 19d6a32

Please sign in to comment.