import asyncio import serial import time class CardReader(): def __init__(self, dConfig): self.dConfig = dConfig self.dConfig['CR'] = self self.conn = serial.Serial(port=self.dConfig['ComPort'], baudrate=9600) self.new_key = False self.STX = b'\x02' self.ETX = b'\x03' self.DLE = b'\x10' self.NAK = b'\x15' self.telegram_length = b'\x07' self.read_eks = b'\x54\x4C' self.device_adress = b'\x01' self.data_start_adress = b'\x00\x00' self.data_length = b'\x7C' self.command = self.telegram_length + self.read_eks + self.device_adress + self.data_start_adress + self.data_length self.crc_sum = self.crc(self.command + self.DLE + self.ETX) print("CRC-Sum: " + str(self.crc_sum)) dConfig['eLoop'].run_until_complete(self.callback()) async def callback(self): while True: await self.key_detection() await self.read_key() async def key_detection(self): while True: if (self.conn.cts == True) and (self.new_key == False): self.new_key = True print("new Key detected") break if (self.conn.cts == False) and (self.new_key == True): self.new_key = False print("Key removed") await asyncio.sleep(1) async def start_communikation(self): self.conn.write(self.STX) return self.conn.read() async def send_command_telegram(self): self.conn.write(self.command) async def end_communikation(self): self.conn.write(self.crc_sum) def crc (self,buffer): bcc= 0x00 for c in buffer: #print (c) bcc ^= c return bytes([bcc]) async def read_key(self): if await self.start_communikation() == self.DLE: await self.send_command_telegram() await self.end_communikation() response = self.conn.read() print("Response from EKS: " + str(response))