def __init__(self): self._client = ModbusClient(method='rtu', port = PORT, bytesize = BITS, parity = PARITY, baudrate = BAUDRATE, timeout = 1) if self._client.connect() == False: logger_modbus.error(bcolors.FAIL + "*******************************" + bcolors.ENDC) logger_modbus.error(bcolors.FAIL + "*** ERROR, DEVICE NOT FOUND ***" + bcolors.ENDC) logger_modbus.error(bcolors.FAIL + "*******************************" + bcolors.ENDC) time.sleep(1) return
from time import sleep import os from datetime import datetime #---------------------------------------------------------------------------# # configure the client logging #---------------------------------------------------------------------------# import logging logging.basicConfig() log = logging.getLogger() log.setLevel(logging.INFO) #---------------------------------------------------------------------------# # We are going to use a simple client to send our requests #---------------------------------------------------------------------------# client = ModbusClient('192.168.33.1', port=10502) client.connect() FILENAME = 'rtu_5_log.csv' try: os.remove(FILENAME) except OSError: pass print("*"*30) print("SCADA server, reading the values of switch states, current and voltage readings") print("*"*30+"\n\n") fd = open(FILENAME, 'w+') csvrow = "timestamp;switch19;switch24;switch25;switch36;voltageb5;current19;current24;current25;current36\n"
import time import pymodbus3 import serial import traceback from pymodbus3.pdu import ModbusRequest from pymodbus3.client.sync import ModbusSerialClient as ModbusClient from pymodbus3.transaction import ModbusRtuFramer from serial.tools.list_ports import comports import sys READ_COILS_PDU_Addr_Test_1 = 0x1000 SLAVE_UNIT = 0x1 client = ModbusClient("rtu", port="COM9", stopbits=2, bytesize=8, baudrate=9600, timeout=10) connected = client.connect() if connected: print("Tristar Morningstar 1 connected!") else: print("Something went wrong.") exit(1) start = time.time() rr = client.read_holding_registers(0xE000, 1, unit=0) assert (rr.function_code < 0x80) print(rr) #data = client.read_coils(READ_COILS_PDU_Addr_Test_1, 1) stop = time.time()
STOPC = 29835 #This are complements of control keys, must be write together with the control key AUTO = 35701 AUTOC = 29834 MANUAL = 35702 MANUALC = 29833 AUTOM = 35704 AUTOMC = 29831 START = 35705 STARTC = 29830 RESETAL = 35734 print("Waiting for the unit to start") time.sleep(30) host = sys.argv[1] print("Connecting to DSE with IP ", host) client = ModbusClient(host) client.connect() time.sleep(0.1) def sync_client_read(registerNumber): try: result = client.read_holding_registers(registerNumber, 1) return result.registers except: print("Connection Error Handled") output = False return output def read_register(PageNumber, RegisterOffset, Scale):
while threads_switch: global read_discrete_inputs_data global discrete_inputs_data if discrete_inputs_data != read_discrete_inputs_data: msg = str(read_discrete_inputs_data) rc, mid = mqtt_client.publish("one2one/fpq", payload=msg, qos=1) # qos on_publish(msg, rc) print(read_discrete_inputs_data) discrete_inputs_data = read_discrete_inputs_data logging.basicConfig() log = logging.getLogger() log.setLevel(logging.CRITICAL) modbus_client = ModbusClient('192.168.1.133', port=503, retries=2, framer=ModbusFramer) modbus_client.connect() write_coils_data = [] read_discrete_inputs_data = [] discrete_inputs_data = [] threads_switch = 1 while True: time.sleep(1) ''' threads = [] t1 = threading.Thread(target=modbus_rr) threads.append(t1) t2 = threading.Thread(target=modbus_print) threads.append(t2)
# # It should be noted that you can supply an ipv4 or an ipv6 host address for # both the UDP and TCP clients. # # There are also other options that can be set on the client that controls # how transactions are performed. The current ones are: # # * retries - Specify how many retries to allow per transaction (default = 3) # * retry_on_empty - Is an empty response a retry (default = False) # * source_address - Specifies the TCP source address to bind to # # Here is an example of using these options:: # # client = ModbusClient('localhost', retries=3, retry_on_empty=True) #---------------------------------------------------------------------------# client = ModbusClient('192.168.1.4', port=502) #client = ModbusClient(method='ascii', port='/dev/pts/2', timeout=1) # client = ModbusClient(method='rtu', port='/dev/ttyp0', timeout=1) client.connect() #---------------------------------------------------------------------------# # example requests #---------------------------------------------------------------------------# # simply call the methods that you would like to use. An example session # is displayed below along with some assert checks. Note that some modbus # implementations differentiate holding/input discrete/coils and as such # you will not be able to write to these, therefore the starting values # are not known to these tests. Furthermore, some use the same memory # blocks for the two sets, so a change to one is a change to the other. # Keep both of these cases in mind when testing as the following will # _only_ pass with the supplied async modbus server (script supplied).
def __init__(self, uri='localhost', port=502): self.host = socket.gethostbyname(uri) self.client = ModbusClient(self.host, port=port) self.client.connect()
DCCurrentRegisterOffset = 201 STOP = 35700 #This are control keys STOPC = 29835 #This are complements of control keys, must be write together with the control key AUTO = 35701 AUTOC = 29834 MANUAL = 35702 MANUALC = 29833 AUTOM = 35704 AUTOMC = 29831 START = 35705 STARTC = 29830 RESETAL = 35734 host = sys.argv[1] print("Connecting to DSE with IP ", host) client = ModbusClient(str(host), port=502) client.connect() time.sleep(0.1) def sync_client_read(registerNumber): try: result = client.read_holding_registers(registerNumber, 1) return result.registers except: print("Connection Error Handled") output = False return output def read_register(PageNumber, RegisterOffset, Scale):
DCCurrentPageNumber = 4 DCCurrentRegisterOffset = 201 STOP = 35700 #This are control keys STOPC = 29835 #This are complements of control keys, must be write together with the control key AUTO = 35701 AUTOC = 29834 MANUAL = 35702 MANUALC = 29833 AUTOM = 35704 AUTOMC = 29831 START = 35705 STARTC = 29830 RESETAL = 35734 print("Connecting to DSE") client = ModbusClient(method = "rtu", port="/dev/ttyUSB2", stopbits = 1, bytesize = 8, parity = 'E' baudrate = 19200) client.connect() time.sleep(0.1) def sync_client_read(registerNumber): try: result = client.read_holding_registers(registerNumber,1, unit=UnitNumber) return result.registers except: print("Connection Error Handled") output = False return output def read_register(PageNumber, RegisterOffset, Scale, UnitNumber): register = 256 * PageNumber + RegisterOffset read = sync_client_read(register)
def main(): registersPerPage = 256 RPMPageNumber = 4 RPMRegisterOffset = 6 DCCurrentPageNumber = 4 DCCurrentStartOffset = 204 DCCurrentEndOffset = 205 STOP = 35700 # 10001011(H8B,D139) 01110100(H74,116) # -29836 (35700) STOPC = 29835 # 01110100(H74,D116) 10001011(H8B,D139) # 29835 AUTO = 35701 # 10001011(H8B,D139) 01110101(H75,D117) # -29835 (35701) AUTOC = 29834 # 01110100(H74,D116) 10001010(H8A,D138) # 29834 MANUAL = 35702 # 10001011(H8B,D139) 01110110(H76,D118) # -29834 (35702) MANUALC = 29833 # 01110100(H74,D116) 10001001(H89,D137) # 29833 AUTOM = 35704 # 10001011(H8B,D139) 01111000(H78,D120) # -29832 (35704) AUTOMC = 29831 # 01110100(H74,D116) 10000111(H87,D135) # 29831 START = 35705 # 10001011(H8B,D139) 01111001(H79,D121) # -29831 (35705) STARTC = 29830 # 01110100(H74,D116) 10000110(H87,D134) # 29830 RESETAL = 35734 # 10001011(H8B,D139) 10010110(H96,D150) # -35734 (35734) #RESETALC = 29801; # #RESETA2 = 35734; # 10001011(H8B,D139) 10010110(H96,D150) # (35734) #RESETA2C = 29828; #RESETAT = -29801; # 10001011(H8B,D139) 10010110(H96,D150) # -35734 (35734) #RESETATC = 29800; MODE_STOP = 0 #Stop mode MODE_AUTO = 1 #Auto mode MODE_MANUAL = 2 #Manual mode MODE_TEST = 3 #Test on load mode MODE_AUTO_MRESTORE = 4 #Auto with manual restore mode/Prohibit Return MODE_USER_CONFIG = 5 #User configuration mode MODE_TEST_OFF_LOAD = 6 #Test off load mode host = sys.argv[1] print(host) client = ModbusClient(str(host), port=502) client.connect() time.sleep(0.1) rm = client.write_registers(4104, [MANUAL, MANUALC]) print("Change DSE to MANUAL mode, starting engine in 5 seconds...") x = 4 while x >= 1: time.sleep(1) print(str(x)) x -= 1 rq = client.write_registers(4104, [START, STARTC]) time.sleep(3) print("Starting engine, proceding to read RPM and DC current") time.sleep(1) x = 1 while x == 1: time.sleep(0.01) register = registersPerPage * RPMPageNumber + RPMRegisterOffset global RPMRegister RPMRegister = sync_client_read(register) register = registersPerPage * DCCurrentPageNumber + DCCurrentEndOffset global DCRegister DCRegister = sync_client_read(register) DCRegister = float(DCRegister) x = 0 os.system('sudo python3 readings.py') while 1 == 1: time.sleep(0.01) register = registersPerPage * RPMPageNumber + RPMRegisterOffset RPMRegister = sync_client_read(register) register = registersPerPage * DCCurrentPageNumber + DCCurrentEndOffset DCregister = sync_client_read(register) DCRegister = float(DCRegister)