# ramka

unknown
plain_text
a year ago
4.3 kB
1
Indexable
Never
```# ----------------------------------------------------------------------------------------------------
def OutState():
return bytearray([254, 254, 23, 254, 13])

# ----------------------------------------------------------------------------------------------------

# ----------------------------------------------------------------------------------------------------
def NewData():
return bytearray([254, 254, 127, 254, 13])

# ----------------------------------------------------------------------------------------------------

# ----------------------------------------------------------------------------------------------------
def RotateL(Bin):
BinL = (Bin << 1) & 65535
BinR = (Bin >> 15)
return (BinL | BinR)

# ----------------------------------------------------------------------------------------------------

# ----------------------------------------------------------------------------------------------------
def CRCheck(Frame):
Crc = 0x147A
for i in Frame:
Crc = RotateL(Crc)
Crc = Crc ^ 0xFFFF
Crc = Crc + int(hex(Crc)[:4], 16) + i
Crc = hex(Crc)[2:]
Crc = [int(Crc[:2], 16), int(Crc[2:], 16)]
return Crc

# ----------------------------------------------------------------------------------------------------

# ----------------------------------------------------------------------------------------------------
def OutToDec(Out):
OutBits = [1, 2, 4, 8, 16, 32, 64, 128]
OutBin = 0
OutBit = 0
for i in Out:
OutBit = OutBits[i - 1]
OutBin = OutBin + OutBit
return OutBin

# ----------------------------------------------------------------------------------------------------

# ----------------------------------------------------------------------------------------------------
def OutDec(OutNr):
Outs = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
OutsDec = []

# for i in OutNr:
if OutNr % 8 == 0:
Out8Nr = round(OutNr / 8) - 1
else:
Out8Nr = OutNr // 8
Outs[Out8Nr].append(OutNr - Out8Nr * 8)
#print('Outs in octets to check:', Outs)

for i in Outs:
if len(i) == 0:
OutDec = 0x00
else:
OutDec = OutToDec(i)
OutsDec.append(OutDec)
return OutsDec

# ----------------------------------------------------------------------------------------------------

# ----------------------------------------------------------------------------------------------------
def OutFrame(OnOff, Pass, Out):
"""Generuje ramkę sterującą wyjściami typu BI lub MONO zgodną z protokołem RS232

OnOff : int
wyłączenie wyjścia - 0, załączenie wyjścia - 1, przełączenie wyjścia - 2
Pass : str
hasło użytkownika centrali systemu alarmowego
Out : str
numery wyjść do sterowania oddzielone przecinkami
"""
Out = OutDec(Out)
if OnOff == 0:
OnOff = 137
elif OnOff == 1:
OnOff = 136
else:
OnOff = 145

PassDec = []
if len(Pass) < 16:
Pass = Pass + 'F' * (16 - len(Pass))  # poprawione dodawanie 'F'
for i in range(8):
PassDec.append(int(Pass[0:2], 16))
Pass = Pass[2:]

Frame = [OnOff] + PassDec + Out

Lista = ([254, 254] + Frame + CRCheck(Frame) + [254, 13])  # test ramki
# test = ", ".join(str(x) for x in Lista).replace(',', '').replace(' ','')
hexString = ''.join([format(x, '02x') for x in Lista])
print("wypisz ciąg szesznastkowy: ", hexString)
for i in Lista:  # test ramki
print(hex(i), ' ', end='')  # test ramki
print("\nwypisz ciag binarny ", bytes.fromhex(hexString))
return bytes.fromhex(hexString)  # poprawione zwracanie wartości

# ----------------------------------------------------------------------------------------------------