pySerialTransfer 2.6.9__tar.gz → 2.6.11__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,14 +1,19 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pySerialTransfer
3
- Version: 2.6.9
3
+ Version: 2.6.11
4
4
  Summary: Python package used to transmit and receive low overhead byte packets - especially useful for PC<-->Arduino USB communication (compatible with https://github.com/PowerBroker2/SerialTransfer)
5
5
  Home-page: https://github.com/PowerBroker2/pySerialTransfer
6
- Download-URL: https://github.com/PowerBroker2/pySerialTransfer/archive/2.6.9.tar.gz
6
+ Download-URL: https://github.com/PowerBroker2/pySerialTransfer/archive/2.6.11.tar.gz
7
7
  Author: Power_Broker
8
8
  Author-email: gitstuff2@gmail.com
9
9
  Keywords: Arduino,serial,usb,protocol,communication
10
10
  Description-Content-Type: text/markdown
11
11
  License-File: LICENSE
12
+ Requires-Dist: pyserial
13
+ Provides-Extra: dev
14
+ Requires-Dist: pytest>=8.1.1; extra == "dev"
15
+ Requires-Dist: pytest-cov>=5.0.0; extra == "dev"
16
+ Requires-Dist: pytest-mock>=3.14.0; extra == "dev"
12
17
 
13
18
  # pySerialTransfer
14
19
  [![GitHub version](https://badge.fury.io/gh/PowerBroker2%2FpySerialTransfer.svg)](https://badge.fury.io/gh/PowerBroker2%2FpySerialTransfer) [![PyPI version](https://badge.fury.io/py/pySerialTransfer.svg)](https://badge.fury.io/py/pySerialTransfer)<br /><br />
@@ -67,15 +72,16 @@ if __name__ == '__main__':
67
72
  # Wait for a response and report any errors while receiving packets
68
73
  ###################################################################
69
74
  while not link.available():
70
- if link.status < 0:
71
- if link.status == txfer.CRC_ERROR:
75
+ # A negative value for status indicates an error
76
+ if link.status.value < 0:
77
+ if link.status == txfer.Status.CRC_ERROR:
72
78
  print('ERROR: CRC_ERROR')
73
- elif link.status == txfer.PAYLOAD_ERROR:
79
+ elif link.status == txfer.Status.PAYLOAD_ERROR:
74
80
  print('ERROR: PAYLOAD_ERROR')
75
- elif link.status == txfer.STOP_BYTE_ERROR:
81
+ elif link.status == txfer.Status.STOP_BYTE_ERROR:
76
82
  print('ERROR: STOP_BYTE_ERROR')
77
83
  else:
78
- print('ERROR: {}'.format(link.status))
84
+ print('ERROR: {}'.format(link.status.name))
79
85
 
80
86
  ###################################################################
81
87
  # Parse response list
@@ -55,15 +55,16 @@ if __name__ == '__main__':
55
55
  # Wait for a response and report any errors while receiving packets
56
56
  ###################################################################
57
57
  while not link.available():
58
- if link.status < 0:
59
- if link.status == txfer.CRC_ERROR:
58
+ # A negative value for status indicates an error
59
+ if link.status.value < 0:
60
+ if link.status == txfer.Status.CRC_ERROR:
60
61
  print('ERROR: CRC_ERROR')
61
- elif link.status == txfer.PAYLOAD_ERROR:
62
+ elif link.status == txfer.Status.PAYLOAD_ERROR:
62
63
  print('ERROR: PAYLOAD_ERROR')
63
- elif link.status == txfer.STOP_BYTE_ERROR:
64
+ elif link.status == txfer.Status.STOP_BYTE_ERROR:
64
65
  print('ERROR: STOP_BYTE_ERROR')
65
66
  else:
66
- print('ERROR: {}'.format(link.status))
67
+ print('ERROR: {}'.format(link.status.name))
67
68
 
68
69
  ###################################################################
69
70
  # Parse response list
@@ -0,0 +1,62 @@
1
+ import sys
2
+ from functools import lru_cache
3
+
4
+
5
+ class CRC:
6
+ def __init__(self, polynomial=0x9B, crc_len=8):
7
+ self.poly = polynomial & 0xFF
8
+ self.crc_len = crc_len
9
+ self.table_len = pow(2, crc_len)
10
+
11
+ @lru_cache(2 ^ 16)
12
+ def calculate_checksum(self, index: int):
13
+ """Calculate the checksum for a given index.
14
+ An LRU cached version of the CRC calculation function, with an upper bound on the cache size of 2^16
15
+ """
16
+ if index > self.table_len:
17
+ raise ValueError('Index out of range')
18
+ curr = index
19
+ for j in range(8):
20
+ if (curr & 0x80) != 0:
21
+ curr = ((curr << 1) & 0xFF) ^ self.poly
22
+ else:
23
+ curr <<= 1
24
+ return curr
25
+
26
+ def print_table(self):
27
+ for i in range(self.table_len):
28
+ sys.stdout.write(hex(self.calculate_checksum(i)).upper().replace('X', 'x'))
29
+
30
+ if (i + 1) % 16:
31
+ sys.stdout.write(' ')
32
+ else:
33
+ sys.stdout.write('\n')
34
+
35
+ def calculate(self, arr, dist=None):
36
+ crc = 0
37
+
38
+ try:
39
+ if dist:
40
+ indicies = dist
41
+ else:
42
+ indicies = len(arr)
43
+
44
+ for i in range(indicies):
45
+ try:
46
+ nex_el = int(arr[i])
47
+ except ValueError:
48
+ nex_el = ord(arr[i])
49
+
50
+ crc = self.calculate_checksum(crc ^ nex_el)
51
+
52
+ except TypeError:
53
+ crc = self.calculate_checksum(arr)
54
+
55
+ return crc
56
+
57
+
58
+ if __name__ == '__main__':
59
+ crc_instance = CRC()
60
+ print(crc_instance.print_table())
61
+ print(' ')
62
+ print(hex(crc_instance.calculate(0x31)).upper().replace('X', 'x'))
@@ -0,0 +1,5 @@
1
+ import logging
2
+
3
+ from . import *
4
+
5
+ logging.getLogger(__name__).addHandler(logging.NullHandler())