mysticpants / Modbus

Modbus Library
0 stars 7 forks source link

TCP readWriteMultipleRegisters behaves differently than expected #4

Open betzrhodes opened 7 years ago

betzrhodes commented 7 years ago

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

DerrickHoPakCheung commented 7 years ago

the excerpt of the description was copied from the Modbus specification. With the PLC we tested with, it actually performed the read action first followed by the write action. so it is either the spec is wrong or this PLC does not comply with the spec

betzrhodes commented 7 years ago

It sounds like your hardware and our hardware are behaving the same, but this is different than the spec. Can you point me to the spec that you copied this from.

DerrickHoPakCheung commented 7 years ago

6.17 23 (0x17) Read/Write Multiple registers on page 38 at (http://www.modbus.org/docs/Modbus_Application_Protocol_V1_1b3.pdf )