コード例 #1
0
ファイル: link.py プロジェクト: dnikulin/motorvate
class ControlLink(object):
    def __init__(self, host, port=Defaults.Port):
        self.conn = ModbusTcpClient(host, port)
        if not self.conn.connect():
            raise RuntimeError('Could not connect to host %s port %d' % (host, port))

    def read(self, offset):
        # Adjust offset due to weirdness.
        offset -= 40000

        # Create request.
        assert offset is not None
        assert offset >= 0
        req = ReadInputRegistersRequest(offset, 1)
        assert req is not None

        # Execute and return response.
        res = self.conn.execute(req)
        assert res is not None

        # Extract single value from response.
        values = res.registers
        assert values is not None
        assert len(values) == 1
        return long(values[0])

    def write(self, offset, value):
        # Adjust offset due to weirdness.
        offset -= 40000

        # Create request.
        assert offset is not None
        assert offset >= 0
        assert value is not None
        req = WriteSingleRegisterRequest(offset, value)

        # Execute and return response.
        res = self.conn.execute(req)
        assert res is not None
        assert res.value is not None
        nvalue = res.value
        if nvalue != value:
            report('address %d, wrote %d, returned %d'
                   % (offset, value, nvalue))
        return nvalue
コード例 #2
0
def access_plc(ip, port):

    global total_found

    try:
        client = ModbusTcpClient(ip, port=port)
        req = ReadDeviceInformationRequest()
        resp = client.execute(req)
        print_info(resp.information, ip + ":" + str(port))
        total_found += 1

    except Exception as ex:
        return
コード例 #3
0
class MB():
    def __init__(self, ip, port=502):
        self.ip = ip
        self.port = port
        self.connect()

    def connect(self):
        self.client = ModbusTcpClient(self.ip, port=self.port, timeout=10)

    def read_reg(self):
        val = self.client.read_holding_registers(6, 2)
        return val.registers

    def write_reg(self, value):
        self.client.write_register(6, scada_value)
        return 0

    def deviceinfo(self):
        rq = mei_message.ReadDeviceInformationRequest()
        val = self.client.execute(rq)
        return (val.information)

    def close(self):
        self.client.close()
コード例 #4
0
#
# request  = ClearCountersRequest()
# response = client.execute(request)
# if isinstance(response, ClearCountersResponse):
#     ... do something with the response
#
#
# What follows is a listing of all the supported methods. Feel free to
# comment, uncomment, or modify each result set to match with your reference.
#---------------------------------------------------------------------------#

#---------------------------------------------------------------------------#
# information requests
#---------------------------------------------------------------------------#
rq = ReadDeviceInformationRequest()
rr = client.execute(rq)
#assert(rr == None)                             # not supported by reference
assert (rr.function_code < 0x80)  # test that we are not an error
assert (rr.information[0] == 'proconX Pty Ltd')  # test the vendor name
assert (rr.information[1] == 'FT-MBSV')  # test the product code
assert (rr.information[2] == 'EXPERIMENTAL')  # test the code revision

rq = ReportSlaveIdRequest()
rr = client.execute(rq)
assert (rr == None)  # not supported by reference
#assert(rr.function_code < 0x80)                # test that we are not an error
#assert(rr.identifier  == 0x00)                 # test the slave identifier
#assert(rr.status  == 0x00)                     # test that the status is ok

rq = ReadExceptionStatusRequest()
rr = client.execute(rq)
コード例 #5
0
def run_sync_client():
    # ------------------------------------------------------------------------#
    # choose the client you want
    # ------------------------------------------------------------------------#
    # make sure to start an implementation to hit against. For this
    # you can use an existing device, the reference implementation in the tools
    # directory, or start a pymodbus server.
    #
    # If you use the UDP or TCP clients, you can override the framer being used
    # to use a custom implementation (say RTU over TCP). By default they use
    # the socket framer::
    #
    #    client = ModbusClient('localhost', port=5020, framer=ModbusRtuFramer)
    #
    # 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('localhost', port=5020)
    # from pymodbus.transaction import ModbusRtuFramer
    # client = ModbusClient('localhost', port=5020, framer=ModbusRtuFramer)
    # client = ModbusClient(method='binary', port='/dev/ptyp0', timeout=1)
    # client = ModbusClient(method='ascii', port='/dev/ptyp0', timeout=1)
    # client = ModbusClient(method='rtu', port='/dev/ptyp0', timeout=1,
    #                       baudrate=9600)
    client.connect()

    # ------------------------------------------------------------------------#
    # specify slave to query
    # ------------------------------------------------------------------------#
    # The slave to query is specified in an optional parameter for each
    # individual request. This can be done by specifying the `unit` parameter
    # which defaults to `0x00`
    # ----------------------------------------------------------------------- #
    log.debug("Reading Device Information")
    information = {}
    rr = None

    while not rr or rr.more_follows:
        next_object_id = rr.next_object_id if rr else 0
        rq = ReadDeviceInformationRequest(read_code=0x03, unit=UNIT,
                                          object_id=next_object_id)
        rr = client.execute(rq)
        information.update(rr.information)
        log.debug(rr)

    print("Device Information : ")
    for key in information.keys():
        print(key, information[key])

    # ----------------------------------------------------------------------- #
    # You can also have the information parsed through the
    # ModbusDeviceIdentificiation class, which gets you a more usable way
    # to access the Basic and Regular device information objects which are
    # specifically listed in the Modbus specification
    # ----------------------------------------------------------------------- #
    di = ModbusDeviceIdentification(info=information)
    print('Product Name : ', di.ProductName)

    # ----------------------------------------------------------------------- #
    # close the client
    # ----------------------------------------------------------------------- #
    client.close()
コード例 #6
0
ファイル: Client.py プロジェクト: MarcoStucchi/modbus-client
debug_level = logging.DEBUG

# Start logger
logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(debug_level)
logging.disable(debug_level)

try:
    # Create client connection
    log.info("Connecting to Huawei Converter..")
    client = ModbusTcpClient('192.169.11.30')
    # Extract information from the device
    log.info("Getting Huawei Converter registers..")
    for reg in Converter.holding_registers:
        result = client.read_holding_registers(address=reg["address"], count=reg["nRegs"], unit=CONVERTER)
        print(reg["name"] + ": " + reg["function"](result.registers))
        log.debug(result.registers)
    log.info("Getting Huawei Converter blocks..")
    ClientDecoder.register(client.framer.decoder, function=huawei_block_functions.ReadBlockRegistersResponse)
    for bl in Converter.blocks:
        request = huawei_block_functions.ReadBlockRegistersResponse(block=bl["block"], slice=0, unit=CONVERTER)
        result = client.execute(request)
        print(bl["name"] + ": " + result.data)
    # Close client connection
    log.info("Closing connection..")
    client.close()
except:
    # Close client connection
    log.error("Huawei Converter not found")
コード例 #7
0
#
# request  = ClearCountersRequest()
# response = client.execute(request)
# if isinstance(response, ClearCountersResponse):
#     ... do something with the response
#
#
# What follows is a listing of all the supported methods. Feel free to
# comment, uncomment, or modify each result set to match with your reference.
#---------------------------------------------------------------------------# 

#---------------------------------------------------------------------------# 
# information requests
#---------------------------------------------------------------------------# 
rq = ReadDeviceInformationRequest()
rr = client.execute(rq)
#assert(rr == None)                             # not supported by reference
assert(rr.function_code < 0x80)                 # test that we are not an error
assert(rr.information[0] == 'proconX Pty Ltd')  # test the vendor name
assert(rr.information[1] == 'FT-MBSV')          # test the product code
assert(rr.information[2] == 'EXPERIMENTAL')     # test the code revision

rq = ReportSlaveIdRequest()
rr = client.execute(rq)
assert(rr == None)                              # not supported by reference
#assert(rr.function_code < 0x80)                # test that we are not an error
#assert(rr.identifier  == 0x00)                 # test the slave identifier
#assert(rr.status  == 0x00)                     # test that the status is ok

rq = ReadExceptionStatusRequest()
rr = client.execute(rq)
コード例 #8
0
ファイル: ClienteSync.py プロジェクト: JonAlberdi/HFB-Project
def run_sync_client():

    # ------------------------------------------------------------------------#
    # choose the client you want
    # ------------------------------------------------------------------------#
    # make sure to start an implementation to hit against. For this
    # you can use an existing device, the reference implementation in the tools
    # directory, or start a pymodbus server.
    #
    # If you use the UDP or TCP clients, you can override the framer being used
    # to use a custom implementation (say RTU over TCP). By default they use
    # the socket framer::
    #
    #    client = ModbusClient('localhost', port=5020, framer=ModbusRtuFramer)
    #
    # 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
    # * strict - Applicable only for Modbus RTU clients.
    #            Adheres to modbus protocol for timing restrictions
    #            (default = True).
    #            Setting this to False would disable the inter char timeout
    #            restriction (t1.5) for Modbus RTU
    #
    #
    # Here is an example of using these options::
    #
    #    client = ModbusClient('localhost', retries=3, retry_on_empty=True)
    # ------------------------------------------------------------------------#
    client = ModbusClient(os.getenv('IP_SERVER'), port=5020)
    # from pymodbus.transaction import ModbusRtuFramer
    # client = ModbusClient('localhost', port=5020, framer=ModbusRtuFramer)
    # client = ModbusClient(method='binary', port='/dev/ptyp0', timeout=1)
    # client = ModbusClient(method='ascii', port='/dev/ptyp0', timeout=1)
    # client = ModbusClient(method='rtu', port='/dev/ptyp0', timeout=1,
    #                       baudrate=9600)

    client.connect()

    print(client.socket)

    print(client.socket.getsockname())

    rq = ReadDeviceInformationRequest(unit=UNIT)
    rr = client.execute(rq)

    print(rr)

    response = QueryClient()

    message = os.getenv('IP_SERVER')
    message = message.encode("utf-8")

    message = base64.b64encode(hashlib.sha256(message).digest())
    message = message.decode("utf-8")

    if message != response['hash']:
        print("Error en autenticacion")
        print(response)
        print(message)
        return

    # ------------------------------------------------------------------------#
    # specify slave to query
    # ------------------------------------------------------------------------#
    # The slave to query is specified in an optional parameter for each
    # individual request. This can be done by specifying the `unit` parameter
    # which defaults to `0x00`
    # ----------------------------------------------------------------------- #

    #while (not client.is_socket_open()):
    #log.debug("Socket not open, trying again")

    #rq = client.write_coil(0, True, unit=UNIT)

    while True:

        log.debug("Reading Coils")
        rr = client.read_coils(0, 1, unit=UNIT)
        log.debug(rr)

        # ----------------------------------------------------------------------- #
        # 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 asynchronous modbus server (script supplied).
        # ----------------------------------------------------------------------- #
        log.debug("Write to a Coil and read back")
        rq = client.write_coil(0, True, unit=UNIT)
        rr = client.read_coils(0, 1, unit=UNIT)
        assert (not rq.isError())  # test that we are not an error
        assert (rr.bits[0] == True)  # test the expected value

        log.debug("Write to multiple coils and read back- test 1")
        rq = client.write_coils(1, [True] * 8, unit=UNIT)
        assert (not rq.isError())  # test that we are not an error
        rr = client.read_coils(1, 21, unit=UNIT)
        assert (not rr.isError())  # test that we are not an error
        resp = [True] * 21

        # If the returned output quantity is not a multiple of eight,
        # the remaining bits in the final data byte will be padded with zeros
        # (toward the high order end of the byte).

        resp.extend([False] * 3)
        assert (rr.bits == resp)  # test the expected value

        log.debug("Write to multiple coils and read back - test 2")
        rq = client.write_coils(1, [False] * 8, unit=UNIT)
        rr = client.read_coils(1, 8, unit=UNIT)
        assert (not rq.isError())  # test that we are not an error
        assert (rr.bits == [False] * 8)  # test the expected value

        log.debug("Read discrete inputs")
        rr = client.read_discrete_inputs(0, 8, unit=UNIT)
        assert (not rq.isError())  # test that we are not an error

        log.debug("Write to a holding register and read back")
        rq = client.write_register(1, 10, unit=UNIT)
        rr = client.read_holding_registers(1, 1, unit=UNIT)
        assert (not rq.isError())  # test that we are not an error
        assert (rr.registers[0] == 10)  # test the expected value

        log.debug("Write to multiple holding registers and read back")
        rq = client.write_registers(1, [10] * 8, unit=UNIT)
        rr = client.read_holding_registers(1, 8, unit=UNIT)
        assert (not rq.isError())  # test that we are not an error
        assert (rr.registers == [10] * 8)  # test the expected value

        log.debug("Read input registers")
        rr = client.read_input_registers(1, 8, unit=UNIT)
        assert (not rq.isError())  # test that we are not an error

        arguments = {
            'read_address': 1,
            'read_count': 8,
            'write_address': 1,
            'write_registers': [20] * 8,
        }
        log.debug("Read write registeres simulataneously")
        rq = client.readwrite_registers(unit=UNIT, **arguments)
        rr = client.read_holding_registers(1, 8, unit=UNIT)
        assert (not rq.isError())  # test that we are not an error
        assert (rq.registers == [20] * 8)  # test the expected value
        assert (rr.registers == [20] * 8)  # test the expected value

        break

    # ----------------------------------------------------------------------- #
    # close the client
    # ----------------------------------------------------------------------- #
    client.close()
コード例 #9
0
ファイル: test.py プロジェクト: neubatengog/oil-pumpjack
protocol_log = logging.getLogger("pysnmp.protocol")
store_log = logging.getLogger("pysnmp.store")

# Enable logging levels
server_log.setLevel(logging.DEBUG)
protocol_log.setLevel(logging.DEBUG)
client_log.setLevel(logging.DEBUG)
store_log.setLevel(logging.DEBUG)

# Initialize the logging
try:
    logging.basicConfig()
except Exception, e:
    print "Logging is not supported on this system"

from pymodbus.client.sync import ModbusTcpClient
import sys

client = ModbusTcpClient("10.10.10.1")
result = client.read_holding_registers(6, 1)
print(result.registers)
result2 = client.read_holding_registers(7, 1)
print(result2.registers)

from pymodbus import mei_message
rq = mei_message.ReadDeviceInformationRequest()
result3 = client.execute(rq)
print(result3.information)

client.close()