TelenkovDmitry 1 vuosi sitten
vanhempi
commit
9205442820
3 muutettua tiedostoa jossa 219 lisäystä ja 7 poistoa
  1. 130 0
      books/python/classes1.py
  2. 0 3
      codewars.code-workspace
  3. 89 4
      hw_libs/modbus.py

+ 130 - 0
books/python/classes1.py

@@ -0,0 +1,130 @@
+
+class Animal:
+    def says(self):
+        return 'I speak!'
+    
+class Horse(Animal):
+    def says(self):
+        return 'Neigh!'
+    
+class Donkey(Animal):
+    def says(self):
+        return 'Hee-haw!'
+    
+class Mule(Donkey, Horse):
+    pass
+
+class Hinny(Horse, Donkey):
+    pass
+
+
+def test_animals():
+    mule = Mule()
+    hinny = Hinny()
+    print(mule.says())
+    print(hinny.says())
+
+    print(Mule.mro())
+    print(Mule.__mro__)
+
+# Миксины
+    
+class Super():
+    def dump(self):
+        print('Super!!!')
+
+class PrettyMixin():
+    def dump(self):
+        import pprint
+        pprint.pprint(vars(self))
+
+class Thing(Super, PrettyMixin):
+    pass
+
+def test_mixin():
+    t = Thing()
+    t.name = "Nyarlathotep"
+    t.feature = 'ichor'
+    t.age = 'eldritch'
+    t.dump()
+
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+# Инструкция property
+    
+class Duck():
+    def __init__(self, input_name) -> None:
+        self.hidden_name = input_name
+
+    def get_name(self):
+        print('inside the getter')
+        return self.hidden_name
+    
+    def set_name(self, input_name):
+        print('inside the setter')
+        self.hidden_name = input_name
+
+    name = property(get_name, set_name)
+
+
+class ImprovedDuck():
+    def __init__(self, input_name) -> None:
+        self.__name = input_name
+
+    @property
+    def name(self):
+        print('inside the getter')
+        return self.__name
+    
+    @name.setter
+    def name(self, input_name):
+        print('inside the setter')
+        self.__name = input_name
+
+
+def set_property():
+    # don = Duck('Donald')
+    don = ImprovedDuck('Donald')
+    print(don.name)
+    don.name = 'Donna'
+    print(don.name)
+    print(don._ImprovedDuck__name)
+
+# set_property()
+    
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    
+# Методы классов @classmethod
+    
+class A():
+    count = 0
+
+    def __init__(self):
+        A.count += 1
+
+    def exclaim(self):
+        print('I`m an A!')
+
+    @classmethod
+    def kids(cls):
+        print("A has", cls.count, "little objects.")
+
+
+def test_classmethod():
+    easy_a = A()
+    breezy_a = A()
+    wheezy_a = A()
+    A.kids()
+
+# test_classmethod()
+    
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    
+# Статические методы @staticmethod
+    
+class CoyoteWeapon():
+    @staticmethod
+    def commercial():
+        print('This CoyoWeapon has been brought to you by Acme')
+
+def test_staticmethod():
+    CoyoteWeapon.commercial()
+
+# test_staticmethod()

+ 0 - 3
codewars.code-workspace

@@ -2,9 +2,6 @@
 	"folders": [
 	"folders": [
         {
         {
             "path": "."
             "path": "."
-        },
-        {
-            "path": "../tmp"
         }
         }
     ],
     ],
 	"settings": {}
 	"settings": {}

+ 89 - 4
hw_libs/modbus.py

@@ -2,12 +2,82 @@ import time
 from serial import Serial
 from serial import Serial
 import colorama
 import colorama
 from colorama import Fore, Style 
 from colorama import Fore, Style 
+from typing import Sequence
+
+DEFAULT_MB_CRC_TABLE = (
+    00000, 49345, 49537,   320, 49921,   960,   640, 49729, 50689,  1728,  1920,
+    51009,  1280, 50625, 50305,  1088, 52225,  3264,  3456, 52545,  3840, 53185,
+    52865,  3648,  2560, 51905, 52097,  2880, 51457,  2496,  2176, 51265, 55297,
+    6_336,  6528, 55617,  6912, 56257, 55937,  6720,  7680, 57025, 57217,  8000,
+    56577,  7616,  7296, 56385,  5120, 54465, 54657,  5440, 55041,  6080,  5760,
+    54849, 53761,  4800,  4992, 54081,  4352, 53697, 53377,  4160, 61441, 12480,
+    12672, 61761, 13056, 62401, 62081, 12864, 13824, 63169, 63361, 14144, 62721,
+    13760, 13440, 62529, 15360, 64705, 64897, 15680, 65281, 16320, 16000, 65089,
+    64001, 15040, 15232, 64321, 14592, 63937, 63617, 14400, 10240, 59585, 59777,
+    10560, 60161, 11200, 10880, 59969, 60929, 11968, 12160, 61249, 11520, 60865,
+    60545, 11328, 58369,  9408,  9600, 58689,  9984, 59329, 59009,  9792,  8704,
+    58049, 58241,  9024, 57601,  8640,  8320, 57409, 40961, 24768, 24960, 41281,
+    25344, 41921, 41601, 25152, 26112, 42689, 42881, 26432, 42241, 26048, 25728,
+    42049, 27648, 44225, 44417, 27968, 44801, 28608, 28288, 44609, 43521, 27328,
+    27520, 43841, 26880, 43457, 43137, 26688, 30720, 47297, 47489, 31040, 47873,
+    31680, 31360, 47681, 48641, 32448, 32640, 48961, 32000, 48577, 48257, 31808,
+    46081, 29888, 30080, 46401, 30464, 47041, 46721, 30272, 29184, 45761, 45953,
+    29504, 45313, 29120, 28800, 45121, 20480, 37057, 37249, 20800, 37633, 21440,
+    21120, 37441, 38401, 22208, 22400, 38721, 21760, 38337, 38017, 21568, 39937,
+    23744, 23936, 40257, 24320, 40897, 40577, 24128, 23040, 39617, 39809, 23360,
+    39169, 22976, 22656, 38977, 34817, 18624, 18816, 35137, 19200, 35777, 35457,
+    19008, 19968, 36545, 36737, 20288, 36097, 19904, 19584, 35905, 17408, 33985,
+    34177, 17728, 34561, 18368, 18048, 34369, 33281, 17088, 17280, 33601, 16640,
+    33217, 32897, 16448)
+
+
+class ModbusMixin():
+    def print_hex(self, text: str, data: bytes):
+        print(text, *tuple(map(lambda x: '0x{:02X}'.format(x), (i for i in data))))
+
+
+class Modbus(ModbusMixin):
+
+    # MB_CRC_TABLE: Sequence[int] = DEFAULT_MB_CRC_TABLE
+    MB_TIMEOUT: float = 0.05
+    MB_CRC_TABLE: [int] = DEFAULT_MB_CRC_TABLE
+    MB_DEBUG: bool = True
 
 
-class Modbus:
     def __init__(self, tty: str, brate: int, address: str):
     def __init__(self, tty: str, brate: int, address: str):
         self.serial = Serial(port=tty, baudrate=brate, timeout=0.05, parity='N', xonxoff=False)
         self.serial = Serial(port=tty, baudrate=brate, timeout=0.05, parity='N', xonxoff=False)
         self.address = address
         self.address = address
 
 
+    @classmethod
+    def test(cls):
+        print(type(cls.MB_CRC_TABLE))
+
+    @classmethod
+    def crc(cls, data: bytes) -> bytes:
+        crc = 0xFFFF
+        crc_table = cls.MB_CRC_TABLE
+        for char in data:
+            crc = (crc >> 8) ^ crc_table[(crc ^ char) & 0xFF]
+        return crc.to_bytes(2, 'little')
+
+    def raw_communicate(self, data: bytes, predicted_length:int = -1) -> bytes:
+        if self.MB_DEBUG:
+            self.print_hex('Request:', data)
+        self.serial.write(data)
+        response_bytes = list()
+        start_time = time.time()
+        while True:
+            b = self.serial.read(1)
+            if len(b):
+                response_bytes.append(b)
+            elif time.time() - start_time > self.MB_TIMEOUT:
+                break
+            if len(response_bytes) == predicted_length:
+                break
+        if self.MB_DEBUG:
+            self.print_hex('Responce:', response_bytes)
+
+    
+
     def test_send(self, data: bytes):
     def test_send(self, data: bytes):
         while True:
         while True:
             self.serial.write(data)
             self.serial.write(data)
@@ -25,11 +95,26 @@ class Modbus:
 
 
 def main():
 def main():
     colorama.init()
     colorama.init()
-    dev = Modbus('COM22', 115200, 1)
+    st = 'Hello world'
+    # st.encode('utf-8')
+    
+    print(st.encode('utf-8'))
+    # dev = Modbus('COM22', 115200, 1)
     # dev.test_send(bytes('hello world\r\n', encoding="utf_8"))
     # dev.test_send(bytes('hello world\r\n', encoding="utf_8"))
     # tx_data = 'hello world\r\n'
     # tx_data = 'hello world\r\n'
-    tx_data = 'abc'
-    dev.send_recv(bytes(tx_data, encoding="utf_8"), len(tx_data))
+    # tx_data = 'abc'
+    # dev.send_recv(bytes(tx_data, encoding="utf_8"), len(tx_data))
+
+    # Modbus.test()
+
+    # print(i.to_bytes(2, 'big'))
+    # print(Modbus.crc(i.to_bytes(1, 'big')))
+    # print(Modbus.crc(b'\x00\x01'))
+    # crc = Modbus.crc(b'\x00\x01')
+    # dev.raw_communicate(b'\x00\x01')
+    # print(ModbusMixin.print_hex(crc))
+    # print(Modbus.crc(b'\xd0\x00'))
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
     main()
     main()
+