Skip to content

TCP readWriteMultipleRegisters behaves differently than expected #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
betzrhodes opened this issue May 8, 2017 · 3 comments
Open
Assignees

Comments

@betzrhodes
Copy link
Contributor

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
Copy link
Collaborator

DerrickHoPakCheung commented May 10, 2017

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
Copy link
Contributor Author

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
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants