mysticpants / modbus Goto Github PK
View Code? Open in Web Editor NEWModbus Library
Modbus Library
Using the TCP Master code on a Fieldbus Accelerator with a Koyo Click, the following code throws a parsing error when processing the second request.
#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
}
function writeCB(err, res) {
if (err) {
server.error(err);
} else {
if (typeof res == "array") {
foreach(item in res) {
server.log(item)
}
} else {
server.log(res);
}
}
}
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.write(MODBUSRTU_TARGET_TYPE.COIL, 8192, 1, true, writeCB);
modbus.write(MODBUSRTU_TARGET_TYPE.COIL, 8194, 1, true, writeCB);
}
})
logs:
2017-05-08 14:59:26 -07:00 [Status] Downloading new code; 27.01% program storage used
2017-05-08 14:59:33 -07:00 [Device] true
2017-05-08 14:59:33 -07:00 [Device] ERROR: Error parsing the response, transactionID 1 does not exist
2017-05-08 15:00:33 -07:00 [Device] true
2017-05-08 15:00:33 -07:00 [Device] ERROR: Error parsing the response, transactionID 3 does not exist
Whenever I open a connection it seems to disconnect and reconnect every 60s.
Logs from code running with only an onConnect handler:
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
2017-05-08 15:08:31 -07:00 [Device] Index : 0, value : 28
2017-05-08 15:08:31 -07:00 [Device] Index : 1, value : 88
2017-05-08 15:09:31 -07:00 [Device] Index : 0, value : 28
2017-05-08 15:09:31 -07:00 [Device] Index : 1, value : 88
2017-05-08 15:10:31 -07:00 [Device] Index : 0, value : 28
2017-05-08 15:10:31 -07:00 [Device] Index : 1, value : 88
2017-05-08 15:11:31 -07:00 [Device] Index : 0, value : 28
2017-05-08 15:11:31 -07:00 [Device] Index : 1, value : 88
2017-05-08 15:12:31 -07:00 [Device] Index : 0, value : 28
2017-05-08 15:12:31 -07:00 [Device] Index : 1, value : 88
2017-05-08 15:13:31 -07:00 [Device] Index : 0, value : 28
2017-05-08 15:13:31 -07:00 [Device] Index : 1, value : 88
2017-05-08 15:14:31 -07:00 [Device] Index : 0, value : 28
2017-05-08 15:14:31 -07:00 [Device] Index : 1, value : 88
2017-05-08 15:15:31 -07:00 [Device] Index : 0, value : 28
2017-05-08 15:15:31 -07:00 [Device] Index : 1, value : 88
Logs from code running with onConnect and onReconnect handlers:
2017-05-08 15:15:43 -07:00 [Status] Downloading new code; 27.15% program storage used
2017-05-08 15:15:43 -07:00 [Status] Agent restarted: reload.
2017-05-08 15:15:52 -07:00 [Device] Index : 0, value : 28
2017-05-08 15:15:52 -07:00 [Device] Index : 1, value : 88
2017-05-08 15:16:52 -07:00 [Device] (instance : 0x534050)
2017-05-08 15:17:52 -07:00 [Device] (instance : 0x53c540)
2017-05-08 15:18:52 -07:00 [Device] (instance : 0x54a428)
2017-05-08 15:19:52 -07:00 [Device] (instance : 0x54a1c8)
2017-05-08 15:20:52 -07:00 [Device] (instance : 0x54d138)
2017-05-08 15:21:52 -07:00 [Device] (instance : 0x5362d8)
2017-05-08 15:22:52 -07:00 [Device] (instance : 0x5363c0)
2017-05-08 15:23:52 -07:00 [Device] (instance : 0x538828)
2017-05-08 15:24:52 -07:00 [Device] (instance : 0x53db20)
2017-05-08 15:25:52 -07:00 [Device] (instance : 0x535fc8)
2017-05-08 15:26:52 -07:00 [Device] (instance : 0x5472c8)
2017-05-08 15:27:52 -07:00 [Device] (instance : 0x535b30)
2017-05-08 15:28:52 -07:00 [Device] (instance : 0x535bc8)
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.