def threadFunc(c): while 1: for i in range(100): bits = DataBank.get_bits(0,1) temp = os.popen("vcgencmd measure_temp").readline() now = datetime.now() str_date = now.strftime("%M%S") temp = temp.replace("temp=", "").replace("'C\n" , "") tempint = int(float(temp)) int_date = int(str_date) temperature.append(tempint) time_now.append(int_date) dizi = time_now[i],temperature[i] time.sleep(1) #print(dizi, type(dizi)) DataBank.set_words(0, dizi) if bits[0] == 1: print("1 geldi babba") GPIO.output(40, GPIO.HIGH) else: print("0 geldi babba") GPIO.output(40, GPIO.LOW) time.sleep(0.25)
def escreveModbus(self, contador): self.server = ModbusServer("127.0.0.1", int(self.portaTCP), no_block=True) self.server.start() DataBank.set_words(0, [int(contador)])
def write(index: int, value: any): index = index - 40001 if index > 40000 else index DataBank.set_words(index, value) value = value[0] if isinstance(value, list) else value index += 40001 log('Wrote %s to index %s' % (value, index))
def run_modbus_server(host, port): print('starting modbus server at %s:%d' % (host, port)) server = ModbusServer(host=host, port=port, no_block=True) server.start() DataBank.set_words(0, [0] * 100) while True: DataBank.set_words(0x39, [random.randint(1, 10), 0]) DataBank.set_words(0x41, [random.randint(1, 10), 0]) DataBank.set_words(0x4b, [random.randint(1, 10), 0]) DataBank.set_words(0x50, [random.randint(1, 10), 0]) time.sleep(1)
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 run_server(): server = ModbusServer(host="", port=502, no_block=True) try: server.start() logging.info("Modbus server started") #Initialize 500 registers from address 0 DataBank.set_words(0, [0x0000] * 500) while True: continue #Stop server if interrupted except: server.close()
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()
def set_databank_words(): words = [] words.append(random.randrange(0, 10000)) words.append(random.randrange(220, 240)) words.append(random.randrange(0, 800)) print(words) DataBank.set_words(0, [words[0]]) # power DataBank.set_words(1, [words[1]]) # ac voltage DataBank.set_words(2, [words[2]]) # dc voltage
def lerModbus(self): zera = DataBank.get_words(1) if int(zera[0]) == 1: self.eggCount = 0 zera[0] = 0 DataBank.set_words(1, [0])
""" Example Modbus Server: run 60 seconds Reference: https://youtu.be/FYPQgnQE9fk Author: Zihan Date: 2021-07-10 """ from pyModbusTCP.server import ModbusServer, DataBank import time try: server = ModbusServer(host='localhost', port=1234, no_block=True) server.start() state = 10 t0 = time.time() while True: DataBank.set_words(0, [1, 2, 3]) if state != DataBank.get_words(10, 1)[0]: state = DataBank.get_words(10, 1)[0] print('state changed to {}'.format(state)) time.sleep(0.05) if time.time() - t0 > 60: break server.stop() except: print('Failed to create a server')
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()
def alive_word_job(): DataBank.set_words(0, [int(time.time()) % (24*3600) // 10])
Script cliente: from pyModbusTCP.client import ModbusClient from time import sleep SERVER_HOST="192.168.0.12" SERVER_PORT=502 c=ModbusClient(host=SERVER_HOST,port=SERVER_PORT,auto_open=True) c.open() while True: if c.is_open(): regs=c.read_input_registers(reg_addr=10,reg_nb=1) print(regs) sleep(3) else: print("can't connect") sleep(3) Script servidor: from pyModbusTCP.server import ModbusServer , DataBank DataBank.set_words(address=10,word_list=[15]) server=ModbusServer(host="192.168.0.12",port=502,no_block=True) server.start() while True: pass
#server = ModbusServer("158.38.140.10", 502, no_block=True) server = ModbusServer("10.0.1.92", 502, no_block=True) print("Start server...") server.start() print("Server is online") state = [0] while True: for event in pygame.event.get(): if event.type == pygame.JOYAXISMOTION: axis_data[event.axis] = round(event.value * 100, 1) x = axis_data[0] y = axis_data[1] * -1 elif event.type == pygame.JOYBUTTONDOWN: button_data[event.button] = True elif event.type == pygame.JOYBUTTONUP: button_data[event.button] = False buzzer = button_data[0] # Square button Stop = button_data[9] # Options button #print(button_data) if buzzer: DataBank.set_words(4, "1") else: DataBank.set_words(4, "0") if Stop: DataBank.set_words(3, "1") else: DataBank.set_words(3, "0") DataBank.set_words(0, [x]) DataBank.set_words(1, [y]) sleep(0.2)
def server_wordset(address, wordlist): DataBank.set_words(address, wordlist)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Modbus/TCP server import socket import errno import time from pyModbusTCP.server import ModbusServer, DataBank if __name__ == "__main__": # init server server = ModbusServer(host='localhost', port=502, no_block=True) server.start() # main loop arr = [1] a = 1 while True: a += 1 arr[0] = a DataBank.set_words(0, arr) time.sleep(1)
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 while True: DataBank.set_words( 30002, [DataBank.get_words(30002)[0] + random.randint(-6, 8)])
#!/bin/python from pyModbusTCP.server import ModbusServer, DataBank from time import sleep from random import uniform # Create an instance of ModbusServer server = ModbusServer("127.0.0.1", 502, no_block=True) try: print("Start server...") server.start() print("Server is online") state = [0] while True: DataBank.set_words(0, [int(uniform(0, 100))]) if state != DataBank.get_words(1): state = DataBank.get_words(1) print("Value of Register 1 has changed to " + str(state)) sleep(0.5) except: print("Shutdown server ...") server.stop() print("Server is offline")
def alive_word_job(): DataBank.set_words(0, [int(time.time()) % (24 * 3600) // 10])