def _setup(self): cryptogen = SystemRandom() while True: bits_value = [ bool(cryptogen.randrange(2)) for indx in range(self.bit_num) ] words_value = [ cryptogen.randrange(10) for _ in range(self.word_num) ] try: DataBank.set_bits(self.bit_adr, bits_value) DataBank.set_words(self.word_adr, words_value) except Exception as err: raise ServerError('Error with writing', err)
def main(): """ Main method to run the pyModbusTCP server for the test modbus endpoint. """ try: print('modbus main start') # Set holding register data to their address. # Set coils to address % 3 == 0. for i in range(0x4000): print('Setting word {} to {}'.format(i, i)) print('Setting bits {} to {}'.format(i, i % 3 == 0)) DataBank.set_words(i, [i]) DataBank.set_bits(i, [i % 3 == 0]) server = ModbusServer(host='', port=1502, no_block=True) server.start() print('started modbus server') global STOP_SERVER while not STOP_SERVER: time.sleep(.01) server.stop() print('stopped modbus server') except Exception: traceback.print_exc()
print("Setting PLC port to DOWN: ", end="", flush=True) cmds = [ "config switch physical-port", "edit port3", "set status down", "end" ] exec_ssh_cmds(cmds) print(f"[{Fore.GREEN}DONE{Style.RESET_ALL}]") # Set IP to PLC's IP print("Setting local IP to PLC IP: ", end="", flush=True) set_ip(interface, targetaddr, targetsubnet, targetgateway) print(f"[{Fore.GREEN}DONE{Style.RESET_ALL}]") print("Waiting for IP to kick in: ", end="", flush=True) time.sleep(2) print(f"[{Fore.GREEN}DONE{Style.RESET_ALL}]") # Run modbus server print("Starting modbus server: ", end="", flush=True) server = ModbusServer(host="0.0.0.0", port=502) DataBank.set_bits(0, [1, 1, 1, 1, 1, 1, 1, 1, 1]) DataBank.set_words(0, [2**15-1, 2**15-1, 2**15-1, 2**15-1, 2**15-1, 2**15-1, 2**15-1, 2**15-1, 2**15-1, 2**15-1]) print(f"[{Fore.GREEN}DONE{Style.RESET_ALL}]") print("Press Ctrl+C to safely stop server") server.start()
import random SERVER_HOST_IP = "localhost" SERVER_PORT = 502 # Create an instance of ModbusServer server = ModbusServer(SERVER_HOST_IP, SERVER_PORT, no_block=True) if __name__ == '__main__': try: print("Starting server...") server.start() print("Server online") # Server enable DataBank.set_bits(9901, [1]) # Sample Rate (Hz) DataBank.set_words(39901, [2]) # Fake sensor configuration (LANE 00) # Lane_Enable DataBank.set_bits(0, [1]) # Sensor value DataBank.set_words(30001, [0]) # Raw voltage value (0.1 mV) DataBank.set_words(30002, [0]) # Sensor type DataBank.set_words(40001, [0]) # Generate random data
def write_bit(index: int, value: int): DataBank.set_bits(index, [value]) log('Wrote %s to index %s' % (value, index + 10001))