Esempio n. 1
0
 def write_holding(self, reg,
                   val):  # write val in the holding register at reg address
     try:
         serial_port = self.get_serial_port()
         message = rtu.write_single_register(SLAVE_ID, reg, val)
         response = rtu.send_message(message, serial_port)
         self.release_serial_port()
         return response
     except:
         traceback.print_exc()
         self.close_serial_port()
     return None
Esempio n. 2
0
    def write_holding_register(self, slave_id, address, port, value):
        self.serial_port = self.get_serial_port(port)
        add = int(address) - 1

        if int(value) < 0:
            value1 = int(value) + 2 ** 16
        else:
            value1 = int(value)
        print(value1)
        message = rtu.write_single_register(slave_id=int(slave_id), address=add, value=int(value1))
        print(message)
        hexadecimal_string = message.hex()
        print(hexadecimal_string)
        response = rtu.send_message(message, self.serial_port)
        print(response)
        print(type(response))
        self.serial_port.close()
Esempio n. 3
0
def do_sendreg(devnum, parmlist):
    # parms = <regnum> <single_word> in dec or hex (if "0x" prepended)
    baseConv = 10
    # print("-- do_sendreg: DEV({}), WORD({})".format(devnum, repr(parmlist)))
    try:
        regnum = int(parmlist[0])
    except:
        print("<register> can only be a number")
        return

    singleWord_s = parmlist[1]
    if singleWord_s.startswith("0x"):
        baseConv = 16
        singleWord_s = singleWord_s[2:]
    try:
        singleWord = int(singleWord_s, baseConv)
    except ValueError:
        print("<singleWord> can only be a dec or hex (0x) number")
        return

    # Send the command
    # def write_single_register(slave_id, address, value):

    message = rtu.write_single_register(slave_id=devnum,
                                        address=regnum,
                                        value=singleWord)

    try:
        response = rtu.send_message(message, serial_port)
    except TimeoutError:
        print("Timeout error, ignoring", file=sys.stderr)
        if ignore_timeouts:
            return
        sys.exit(1)
    except:
        e = sys.exc_info()[0]
        print("Some other exception: %" % repr(e))
        sys.exit(3)

    print("Got a response back!")
    import pdb
    pdb.set_trace()
    def write_register(self, register_address, write_value):
        """ Modbus command : Write data to single register (function code = 06)

            Argument :
            register_address (int16) : Address where to write a data
            write_value (int16)      : Write data
        """

        # Enable byte system to be signed-value type
        if self.signed_type == True:
            conf.SIGNED_VALUES = True
        else:
            conf.SIGNED_VALUES = False

        # Generate modbus RTU message
        try:
            message = modbus_rtu.write_single_register(
                slave_id=self.device_id,
                address=register_address,
                value=write_value)
        except Exception as e:
            print("Error during generate modbus message.")
            print(
                "\tMaybe, write value is less than 0 as signed integer and .signed_type is set to 'False'."
            )

        # Send message via serial port
        try:
            if self.serialport.is_open:
                response = modbus_rtu.send_message(message, self.serialport)
                print("response={}".format(response))
            else:
                print("Error : Cannot send data. Serial port is closed.")
        except Exception as e:
            print("Error during send modbus message.")
            print(e)
Esempio n. 5
0
 def stop(self):
     message = rtu.write_single_register(DEVICE_ADDRESS, REG_STATUS_CONTROL,
                                         0b1)
     response = rtu.send_message(message, self.serial)
     return response