You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
1.1 KiB
Python
30 lines
1.1 KiB
Python
import sys
|
|
sys.path.append('../')
|
|
sys.path.append('../../')
|
|
sys.path.append('../../../')
|
|
from protocol.ModBus.rtumaster_example import RTUMaster
|
|
|
|
class HartProtocol(object):
|
|
def __init__(self):
|
|
self.rtuMaster = RTUMaster(port = 'COM2', baudrate = 9600, bytesize = 8, parity = 'N', stopbits = 1, xonxoff = 0)
|
|
# self.rtuMaster = RTUMaster(port = 'COM5', baudrate = 9600, bytesize = 8, parity = 'N', stopbits = 1, xonxoff = 0)
|
|
self.addressList = [x for x in range(40000, 40010) if x % 2 == 0]
|
|
|
|
def readValues(self):
|
|
valueList = []
|
|
for index, address in enumerate(self.addressList):
|
|
value = self.rtuMaster.readHoldingRegisters(slaveId = 1, startAddress = address, varNums = 2, order = 'ABCD')
|
|
valueList.append(value)
|
|
# print(self.addressList, valueList)
|
|
return valueList
|
|
|
|
def writeValue(self, devIndex, elemIndex, value):
|
|
address = self.addressList[devIndex * 5 + elemIndex]
|
|
# print(address)
|
|
self.rtuMaster.writeSingleRegister(slaveId = 1, address = address, outputValue = value, order = 'DCBA')
|
|
|
|
if __name__ == '__main__':
|
|
hart = HartProtocol()
|
|
# hart.writeValue(0, 2, 1.52)
|
|
hart.readValues()
|