Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from serial import Serial
- import struct
- class ModbusRTU(object):
- #class for master querry
- class MasterQuerryClass(object):
- def __init__(self,data):
- self.SlaveAdress = data[0]
- self.Function = data[1]
- self.StartingAdress = data[2] + data[3]
- self.NoRegisters = data[4] + data[5]
- self.ErrorCheck = data[6] + data[7]
- self.CorrectMessage = self.CRC(self.ErrorCheck,data[:-2])
- #class for a slave response
- class SlaveResponse(object):
- def __init__(data):
- self.SlaveAdress = data[0]
- self.Function = data[1]
- self.ByteCount = data[2]
- self.Data = data[2:self.ByteCount]
- self.ErrorCheck = data[sef.ByteCount+2:sef.ByteCount+4]
- self.CorrectMessage = CRC(self.ErrorCheck,data[:-2])
- def __init__(self):
- self.SlaveData = []
- self.Connection = [1]
- self.SlaveID = [10]
- #Starts slave mode with preset settings
- def SlaveMode(self,ID):
- print("Slave mode initated")
- self.SlaveID = ID
- #if there is no connection object, it returns to command selection
- if self.Connection == "":
- print("No valid COM port specified, use 'scp' command to specify port")
- print("Press Enter to return to command selection")
- input()
- CommandHandler()
- print("Waiting for message")
- rawmessage = self.Connection.read(size = 8) #reads the raw message
- print("message got")
- print(message)#debug
- message = self.MasterQuerryClass(message) #constructs the message class
- print(message.SlaveAdress)#debug
- print(self.SlaveID)#debug
- #if the CRC is broken, it discards the message
- if not message.CorrectMessage:
- print("Malformatted message")
- #if the ID is the slave's id the it proceeds
- if message.SlaveAdress == self.SlaveID:
- print("Modbus message recved")
- if SlaveData[0] == message.Function:
- serial.write(self.SlaveData[1])
- else:
- print("Unknown function")
- SlaveMode(self,ID)
- #friendly help screen
- def Help(self):
- print("scp port : Set Communication Port, i.e COM3/COM4...")
- print("ssm slaveadress : Set Slave mode, Turn on the slave mode with previously set function")
- print("smq slaveadress function startadress noregisters : Send MasterQuerry, Sends a querry to the bus")
- print("asf functioncode data : Add Slave Function, adds a function to the slave")
- print("rsf functioncode : Remove Slave Function, removes a slave function")
- print("Press Enter to return to command selection")
- input()
- self.CommandHandler()
- #sets a single slave function
- def SetSlaveFunction(self,command):
- command = command.split(" ")
- adress = int(command[1].strip()).to_bytes(1, byteorder='big')
- data = int(command[2].strip()).to_bytes(int(len(command[2].strip())/2),byteorder='big')
- self.SlaveFunction = (adress, data)
- print(self.SlaveFunction)
- self.CommandHandler()
- def MasterQuerry(self,querry):
- # if there is no existing connection, it returns to selection screen
- if self.Connection == "":
- print("No valid COM port specified, use 'scp' command to specify port")
- print("Press Enter to return to command selection")
- input()
- self.CommandHandler()
- querry = querry.split(" ") #splitting the querry to bytes
- querry = querry[1:]
- for byte in querry:
- try:
- print(struct.pack('<B',int(byte)))
- self.Connection.write(struct.pack('<B',int(byte)))
- #if the sending fails it returns to command selection
- except:
- print("Unable to send mondbus request")
- print("Press Enter to return to command selection")
- input()
- self.CommandHandler()
- self.CommandHandler()
- def CRC(CRC,Data):
- return True
- def SetCommPort(self,command):
- command = command.split(" ")
- print(command[1])
- try:
- self.Connection = Serial(port = command[1] , baudrate = 9600)
- except:
- print("Can't open port, use 'scp' to specify a valid port")
- print("No valid COM port specified, use 'scp' command to specify port")
- print("Press Enter to return to command selection")
- raise
- input()
- self.CommandHandler()
- def CommandHandler(self):
- print("Python RTU Modbus master/slave by Tamás Miseta")
- print("Specify a command, type 'help' for the list of avalible commands")
- command = input()
- print(command[:4])
- if command[:3] == "ssm":
- self.SlaveMode(command)
- if command[:3] == "smq":
- self.MasterQuerry(command)
- if command[:3] == "asf":
- self.SetSlaveFunction(command)
- if command[:3] == "scp":
- self.SetCommPort(command)
- if command[:4] == "help":
- self.Help()
- ModbusConnection = ModbusRTU()
- ModbusConnection.CommandHandler()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement