Closed yarosdev closed 7 years ago
Option requestHandler
must be specified and be a function(number unit, Request request, function(any) respond)
. For example:
function handleRequest(unit, request, respond)
{
if (isValidRequest(unit, request))
{
respond(new Buffer([1, 2, 3, 4]));
}
else
{
respond(ExceptionCode.IllegalDataAddress);
}
}
respond()
takes a single argument that can be one of:
ExceptionCode
enum - will send an exception responseResponse
corresponding to the received Request
Error
(more like an object with a message
property) - will send the Slave Device Failure exceptionBuffer
- will try to create an instance of Response
corresponding to the received Request
by using the Response.fromBuffer()
(for example, if the request is ReadHoldingRegistersRequest
, then the ReadHoldingRegistersResponse.fromBuffer()
will be called)Response
corresponding to the received Request
by using the Response.fromOptions()
.See examples\slave.js for a full example.
What about transactions if it is tcp/ip?
Use TcpListener
and IpTransport
.
That's clear I mean executing concurrent transactions.
The respond()
callback is unique to each request. If you receive 10 requests, the requestHandler
will be called 10 times, each with different request
and respond
.
Thank you a lot! And last one question. Am I correct? To send command to the client I should send it in the request handler by calling a respond function?
Yes, you must call respond()
to send a response to a request.
So, is modbus.Slave for emulating modbus device?
Yes, modbus.Slave=server and modbus.Master=client.
How to send request to the client?
I have following code:
I'll be very thankful!