Skip to content

Commit

Permalink
Merge pull request #15 from RainerStaude/develop
Browse files Browse the repository at this point in the history
extend debug output
  • Loading branch information
RainerStaude authored Dec 29, 2022
2 parents b42d6e1 + 1c17ec9 commit e203d43
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pybecker/becker_helper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Helper for Becker centronic USB Stick."""
import logging
import re
import time
Expand Down Expand Up @@ -28,26 +29,29 @@
+ rb'[0-9A-F]{6,6}'
+ rb'(?P<channel>[0-9A-F]{1,1})'
+ rb'00'
+ rb'(?P<command>[124]{1,1})' # HALT, UP, DOWN
+ rb'(?P<command>[0-9A-F]{1,1})'
+ rb'(?P<argument>[0-9A-F]{1,1})'
+ rb'[0-9A-F]{2,2}'
+ ETX, re.I
)

COMMANDS = {b'1': 'HALT', b'2': 'UP', b'4': 'DOWN'}
COMMANDS = {b'0': 'RELEASE', b'1': 'HALT', b'2': 'UP', b'4': 'DOWN', b'8': 'TRAIN'}

_LOGGER = logging.getLogger(__name__)


def hex2(n):
"""Return word"""
return '%02X' % (n & 0xFF)


def hex4(n):
"""Return dword"""
return '%04X' % (n & 0xFFFF)


def checksum(code):
"""Calculate checksum"""
code_length = len(code)
if code_length != 40:
_LOGGER.error("The code must be 40 characters long (without <STX>, <ETX> and checksum)")
Expand All @@ -62,6 +66,7 @@ def checksum(code):


def generate_code(channel, unit, cmd_code, with_checksum=True):
"""Generate code"""
unit_id = unit[0] # contains the unit code in hex (5 chars)
unit_inc = unit[1] # contains the next increment (required to convert into hex4)

Expand All @@ -75,10 +80,12 @@ def generate_code(channel, unit, cmd_code, with_checksum=True):
return checksum(code) if with_checksum else code

def finalize_code(code):
"""Add frame"""
return b"".join([STX, code.encode(), ETX])


class BeckerConnectionError(Exception):
"""Error class for Becker centronic USB Stick."""
pass


Expand Down

0 comments on commit e203d43

Please sign in to comment.