async def writeSingleRegister(self, reader): data = await reader.read(4) registerAddress = modbusUtils.bytes2int(data[0:2]) registerValue = modbusUtils.bytes2int(data[2:4]) offset = self.PDUTranslationTable['holdingRegisters'] self.memory[offset + registerAddress] = registerValue return data
async def readDiscreteInputs(self, reader): startingAddress = modbusUtils.bytes2int(await reader.read(2)) quantityOfInputs = modbusUtils.bytes2int(await reader.read(2)) offset = self.PDUTranslationTable['discreteInput'] inputStatus = modbusUtils.compressBools( self.memory[offset + startingAddress:offset + startingAddress + quantityOfInputs]) byteCount = len(inputStatus) return modbusUtils.int2bytes(byteCount) + inputStatus
async def readCoils(self, reader): startingAddress = modbusUtils.bytes2int(await reader.read(2)) quantityOfCoils = modbusUtils.bytes2int(await reader.read(2)) offset = self.PDUTranslationTable['coils'] coilStatus = modbusUtils.compressBools( self.memory[offset + startingAddress:offset + startingAddress + quantityOfCoils]) byteCount = len(coilStatus) return modbusUtils.int2bytes(byteCount) + coilStatus
async def writeSingleCoil(self, reader): data = await reader.read(4) outputAddress = modbusUtils.bytes2int(data[0:2]) outputValue = modbusUtils.bytes2int(data[2:4]) offset = self.PDUTranslationTable['coils'] if outputValue == 0x0000: self.memory[offset + outputAddress] = 0 elif outputValue == 0xFF00: self.memory[offset + outputAddress] = 1 return data
async def readInputRegisters(self, reader): startingAddress = modbusUtils.bytes2int(await reader.read(2)) quantityOfInputRegisters = modbusUtils.bytes2int(await reader.read(2)) offset = self.PDUTranslationTable['inputRegisters'] inputRegisters = b''.join( map( lambda x: x.to_bytes(2, 'big'), self.memory[offset + startingAddress:offset + startingAddress + quantityOfInputRegisters])) byteCount = len(inputRegisters) return modbusUtils.int2bytes(byteCount) + inputRegisters
async def writeMultipleCoils(self, reader): data = await reader.read(5) startingAddress = modbusUtils.bytes2int(data[0:2]) quantityOfOutputs = modbusUtils.bytes2int(data[2:4]) byteCount = modbusUtils.bytes2int(data[4:5]) outputValue = modbusUtils.zeroPadOrTruncate( modbusUtils.decompressBools(await reader.read(byteCount)), quantityOfOutputs) offset = self.PDUTranslationTable['coils'] self.memory[offset + startingAddress:offset + startingAddress + quantityOfOutputs] = outputValue return data[0:4]
async def writeMultipleRegisters(self, reader): data = await reader.read(5) startingAddress = modbusUtils.bytes2int(data[0:2]) quantityOfRegisters = modbusUtils.bytes2int(data[2:4]) byteCount = modbusUtils.bytes2int(data[4:5]) registersValue = await reader.read(byteCount) registersValue = list( map(modbusUtils.bytes2int, modbusUtils.splitIntoChunks(registersValue, 2))) offset = self.PDUTranslationTable['holdingRegisters'] self.memory[offset + startingAddress:offset + startingAddress + quantityOfRegisters] = registersValue return data[0:4]