@@ -515,15 +515,13 @@ def get_lreal(bytearray_: bytearray, byte_index: int) -> float:
515515 return float (struct .unpack_from (">d" , bytearray_ , offset = byte_index )[0 ])
516516
517517
518- def get_lword (bytearray_ : bytearray , byte_index : int ) -> bytearray :
518+ def get_lword (bytearray_ : bytearray , byte_index : int ) -> int :
519519 """Get the long word
520520
521- THIS VALUE IS NEITHER TESTED NOR VERIFIED BY A REAL PLC AT THE MOMENT
522-
523521 Notes:
524522 Datatype `lword` (long word) consists in 8 bytes in the PLC.
525- Maximum value posible is bytearray(b" \\ xFF \\ xFF \\ xFF \\ xFF \\ xFF \\ xFF \\ xFF \\ xFF")
526- Lowest value posible is bytearray(b" \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00")
523+ Maximum value is 18446744073709551615 (0xFFFFFFFFFFFFFFFF).
524+ Minimum value is 0.
527525
528526 Args:
529527 bytearray_: buffer to read from.
@@ -533,15 +531,13 @@ def get_lword(bytearray_: bytearray, byte_index: int) -> bytearray:
533531 Value read.
534532
535533 Examples:
536- read lword value (here as example 0xAB\0 xCD) from DB1.10 of a PLC
537- >>> from snap7 import Client
538- >>> data = Client().db_read(db_number=1, start=10, size=8)
534+ >>> data = bytearray(b"\\ x00\\ x00\\ x00\\ x00\\ x00\\ x00\\ xAB\\ xCD")
539535 >>> get_lword(data, 0)
540- bytearray(b" \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ x00 \\ xAB \\ xCD")
536+ 43981
541537 """
542- data = bytearray_ [byte_index : byte_index + 4 ]
543- dword = struct .unpack (">Q" , struct .pack ("8B" , * data ))[0 ]
544- return bytearray ( dword )
538+ data = bytearray_ [byte_index : byte_index + 8 ]
539+ lword : int = struct .unpack (">Q" , struct .pack ("8B" , * data ))[0 ]
540+ return lword
545541
546542
547543def get_ulint (bytearray_ : bytearray , byte_index : int ) -> int :
@@ -591,16 +587,72 @@ def get_date(bytearray_: bytearray, byte_index: int = 0) -> date:
591587 return date_val
592588
593589
594- def get_ltime (bytearray_ : bytearray , byte_index : int ) -> str :
595- raise NotImplementedError
590+ def get_ltime (bytearray_ : bytearray , byte_index : int ) -> timedelta :
591+ """Get LTIME value from bytearray.
596592
593+ Notes:
594+ Datatype `LTIME` consists of 8 bytes (64-bit signed integer) representing
595+ nanoseconds. Used in S7-1500 PLCs.
597596
598- def get_ltod (bytearray_ : bytearray , byte_index : int ) -> str :
599- raise NotImplementedError
597+ Args:
598+ bytearray_: buffer to read from.
599+ byte_index: byte index from where to start reading.
600600
601+ Returns:
602+ timedelta value.
601603
602- def get_ldt (bytearray_ : bytearray , byte_index : int ) -> str :
603- raise NotImplementedError
604+ Examples:
605+ >>> data = bytearray(8)
606+ >>> data[:] = b'\\ x00\\ x00\\ x00\\ x00\\ x3b\\ x9a\\ xca\\ x00' # 1 second in nanoseconds
607+ >>> get_ltime(data, 0)
608+ datetime.timedelta(seconds=1)
609+ """
610+ raw = bytearray_ [byte_index : byte_index + 8 ]
611+ nanoseconds : int = struct .unpack (">q" , struct .pack ("8B" , * raw ))[0 ]
612+ return timedelta (microseconds = nanoseconds // 1000 )
613+
614+
615+ def get_ltod (bytearray_ : bytearray , byte_index : int ) -> timedelta :
616+ """Get LTOD (Long Time of Day) value from bytearray.
617+
618+ Notes:
619+ Datatype `LTOD` consists of 8 bytes (64-bit unsigned integer) representing
620+ nanoseconds since midnight. Used in S7-1500 PLCs.
621+ Range: 0 to 86399999999999 ns.
622+
623+ Args:
624+ bytearray_: buffer to read from.
625+ byte_index: byte index from where to start reading.
626+
627+ Returns:
628+ timedelta value representing time of day.
629+ """
630+ raw = bytearray_ [byte_index : byte_index + 8 ]
631+ nanoseconds : int = struct .unpack (">Q" , struct .pack ("8B" , * raw ))[0 ]
632+ result = timedelta (microseconds = nanoseconds // 1000 )
633+ if result .days >= 1 :
634+ raise ValueError ("LTOD value exceeds 24 hours" )
635+ return result
636+
637+
638+ def get_ldt (bytearray_ : bytearray , byte_index : int ) -> datetime :
639+ """Get LDT (Long Date and Time) value from bytearray.
640+
641+ Notes:
642+ Datatype `LDT` consists of 8 bytes (64-bit unsigned integer) representing
643+ nanoseconds since 1970-01-01 00:00:00 UTC. Used in S7-1500 PLCs.
644+
645+ Args:
646+ bytearray_: buffer to read from.
647+ byte_index: byte index from where to start reading.
648+
649+ Returns:
650+ datetime value.
651+ """
652+ raw = bytearray_ [byte_index : byte_index + 8 ]
653+ nanoseconds : int = struct .unpack (">Q" , struct .pack ("8B" , * raw ))[0 ]
654+ epoch = datetime (1970 , 1 , 1 )
655+ return epoch + timedelta (microseconds = nanoseconds // 1000 )
604656
605657
606658def get_dtl (bytearray_ : bytearray , byte_index : int ) -> datetime :
@@ -662,7 +714,7 @@ def get_wchar(bytearray_: bytearray, byte_index: int) -> str:
662714 'C'
663715 """
664716 if bytearray_ [byte_index ] == 0 :
665- return chr (bytearray_ [1 ])
717+ return chr (bytearray_ [byte_index + 1 ])
666718 return bytearray_ [byte_index : byte_index + 2 ].decode ("utf-16-be" )
667719
668720
0 commit comments