Skip to content

TCP readWriteMultipleRegisters behaves differently than expected #4

Open
@betzrhodes

Description

@betzrhodes

The documentation states:
"This method performs a combination of one read operation and one write operation in a single Modbus transaction. The write operation is performed before the read."

However when I run the following code the read doesn't show the values I just wrote.

#require "ModbusRTU.class.nut:1.0.0"
#require "ModbusMaster.class.nut:1.0.0"
#require "ModbusTCPMaster.class.nut:1.0.0"
#require "W5500.device.nut:1.0.0"

FieldbusGateway_005 <- {
    "LED_RED" : hardware.pinP,
    "LED_GREEN" : hardware.pinT,
    "LED_YELLOW" : hardware.pinQ,

    "MIKROBUS_AN" : hardware.pinM,
    "MIKROBUS_RESET" : hardware.pinH,
    "MIKROBUS_SPI" : hardware.spiBCAD,
    "MIKROBUS_PWM" : hardware.pinU,
    "MIKROBUS_INT" : hardware.pinXD,
    "MIKROBUS_UART" : hardware.uart1,
    "MIKROBUS_I2C" : hardware.i2cJK,

    "XBEE_RESET" : hardware.pinH,
    "XBEE_AND_RS232_UART": hardware.uart0,
    "XBEE_DTR_SLEEP" : hardware.pinXD,

    "RS485_UART" : hardware.uart2,
    "RS485_nRE" : hardware.pinL,

    "WIZNET_SPI" : hardware.spi0,
    "WIZNET_RESET" : hardware.pinXA,
    "WIZNET_INT" : hardware.pinXC,

    "USB_EN" : hardware.pinR,
    "USB_LOAD_FLAG" : hardware.pinW
}

local CONNECTION_SETTINGS_DEST_IP = "192.168.0.10";
local CONNECTION_SETTINGS_DEST_PORT = 502;
local NETWORK_SETTINGS_SOURCE_IP = "192.168.0.2";
local NETWORK_SETTINGS_SUBNET_MASK = "255.255.255.0";
local NETWORK_SETTINGS_GATEWAY_IP = "192.168.0.1";

FieldbusGateway_005.WIZNET_SPI.configure(CLOCK_IDLE_LOW | MSB_FIRST | USE_CS_L, 500);
local wiz = W5500(FieldbusGateway_005.WIZNET_INT, FieldbusGateway_005.WIZNET_SPI, null, FieldbusGateway_005.WIZNET_RESET);
wiz.configureNetworkSettings(NETWORK_SETTINGS_SOURCE_IP, NETWORK_SETTINGS_SUBNET_MASK, NETWORK_SETTINGS_GATEWAY_IP);

// Initialize Modbus
local modbus = ModbusTCPMaster(wiz);

// Open Connection
local connectionSettings =  {"destIP" : CONNECTION_SETTINGS_DEST_IP, "destPort" : CONNECTION_SETTINGS_DEST_PORT};
modbus.connect(connectionSettings, function(err, conn) {
    if (err) {
        server.error (err);
    } else {
        modbus.readWriteMultipleRegisters(0x0A, 2, 0x0A, 2, [28, 88], function(error, result) {
            if (error) {
                server.error(error);
            } else {
                foreach (index, value in result) {
                    server.log(format("Index : %d, value : %d", index, value));
                }
            }
        });
    }
})

Logs:
2017-05-08 15:07:24 -07:00 [Status] Downloading new code; 27.09% program storage used
2017-05-08 15:07:31 -07:00 [Device] Index : 0, value : 0
2017-05-08 15:07:31 -07:00 [Device] Index : 1, value : 0

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions