Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. from serial import Serial
  2. import struct
  3.  
  4. class ModbusRTU(object):
  5.  
  6. #class for master querry
  7. class MasterQuerryClass(object):
  8. def __init__(self,data):
  9. self.SlaveAdress = data[0]
  10. self.Function = data[1]
  11. self.StartingAdress = data[2] + data[3]
  12. self.NoRegisters = data[4] + data[5]
  13. self.ErrorCheck = data[6] + data[7]
  14. self.CorrectMessage = self.CRC(self.ErrorCheck,data[:-2])
  15.  
  16. #class for a slave response
  17. class SlaveResponse(object):
  18. def __init__(data):
  19. self.SlaveAdress = data[0]
  20. self.Function = data[1]
  21. self.ByteCount = data[2]
  22. self.Data = data[2:self.ByteCount]
  23. self.ErrorCheck = data[sef.ByteCount+2:sef.ByteCount+4]
  24. self.CorrectMessage = CRC(self.ErrorCheck,data[:-2])
  25.  
  26. def __init__(self):
  27. self.SlaveData = []
  28. self.Connection = [1]
  29. self.SlaveID = [10]
  30.  
  31. #Starts slave mode with preset settings
  32. def SlaveMode(self,ID):
  33. print("Slave mode initated")
  34. self.SlaveID = ID
  35.  
  36. #if there is no connection object, it returns to command selection
  37. if self.Connection == "":
  38. print("No valid COM port specified, use 'scp' command to specify port")
  39. print("Press Enter to return to command selection")
  40. input()
  41. CommandHandler()
  42.  
  43. print("Waiting for message")
  44. rawmessage = self.Connection.read(size = 8) #reads the raw message
  45. print("message got")
  46. print(message)#debug
  47.  
  48. message = self.MasterQuerryClass(message) #constructs the message class
  49.  
  50. print(message.SlaveAdress)#debug
  51. print(self.SlaveID)#debug
  52.  
  53. #if the CRC is broken, it discards the message
  54. if not message.CorrectMessage:
  55. print("Malformatted message")
  56.  
  57. #if the ID is the slave's id the it proceeds
  58. if message.SlaveAdress == self.SlaveID:
  59. print("Modbus message recved")
  60. if SlaveData[0] == message.Function:
  61. serial.write(self.SlaveData[1])
  62. else:
  63. print("Unknown function")
  64. SlaveMode(self,ID)
  65.  
  66. #friendly help screen
  67. def Help(self):
  68. print("scp port : Set Communication Port, i.e COM3/COM4...")
  69. print("ssm slaveadress : Set Slave mode, Turn on the slave mode with previously set function")
  70. print("smq slaveadress function startadress noregisters : Send MasterQuerry, Sends a querry to the bus")
  71. print("asf functioncode data : Add Slave Function, adds a function to the slave")
  72. print("rsf functioncode : Remove Slave Function, removes a slave function")
  73. print("Press Enter to return to command selection")
  74. input()
  75. self.CommandHandler()
  76.  
  77. #sets a single slave function
  78. def SetSlaveFunction(self,command):
  79. command = command.split(" ")
  80. adress = int(command[1].strip()).to_bytes(1, byteorder='big')
  81. data = int(command[2].strip()).to_bytes(int(len(command[2].strip())/2),byteorder='big')
  82. self.SlaveFunction = (adress, data)
  83. print(self.SlaveFunction)
  84. self.CommandHandler()
  85.  
  86. def MasterQuerry(self,querry):
  87.  
  88. # if there is no existing connection, it returns to selection screen
  89. if self.Connection == "":
  90. print("No valid COM port specified, use 'scp' command to specify port")
  91. print("Press Enter to return to command selection")
  92. input()
  93. self.CommandHandler()
  94.  
  95. querry = querry.split(" ") #splitting the querry to bytes
  96. querry = querry[1:]
  97. for byte in querry:
  98. try:
  99. print(struct.pack('<B',int(byte)))
  100. self.Connection.write(struct.pack('<B',int(byte)))
  101.  
  102. #if the sending fails it returns to command selection
  103. except:
  104. print("Unable to send mondbus request")
  105. print("Press Enter to return to command selection")
  106. input()
  107. self.CommandHandler()
  108. self.CommandHandler()
  109. def CRC(CRC,Data):
  110.  
  111. return True
  112.  
  113. def SetCommPort(self,command):
  114. command = command.split(" ")
  115. print(command[1])
  116. try:
  117. self.Connection = Serial(port = command[1] , baudrate = 9600)
  118. except:
  119. print("Can't open port, use 'scp' to specify a valid port")
  120. print("No valid COM port specified, use 'scp' command to specify port")
  121. print("Press Enter to return to command selection")
  122. raise
  123. input()
  124. self.CommandHandler()
  125.  
  126.  
  127. def CommandHandler(self):
  128. print("Python RTU Modbus master/slave by Tamás Miseta")
  129. print("Specify a command, type 'help' for the list of avalible commands")
  130. command = input()
  131. print(command[:4])
  132. if command[:3] == "ssm":
  133. self.SlaveMode(command)
  134. if command[:3] == "smq":
  135. self.MasterQuerry(command)
  136. if command[:3] == "asf":
  137. self.SetSlaveFunction(command)
  138. if command[:3] == "scp":
  139. self.SetCommPort(command)
  140. if command[:4] == "help":
  141. self.Help()
  142.  
  143.  
  144. ModbusConnection = ModbusRTU()
  145. ModbusConnection.CommandHandler()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement