CRemoteModbus
class CRemoteModbus : public CModbus
openConnection - open a connection with a slave device.
CloseConnection - close a connection with a slave device.
TxRxMessage virtual CModbus implementation. Send and receive a message from a slave.
ErrorMessage virtual CModbus implementation.Return string description of an error code.
properties
Host - Host name
Port - IP Port
Timeout - timeout ms
ReconnectOnEveryMessage - when this property is true each call to a modbus function call openConnection before sending a query and close the connection after response was received.
BOOL openConnection(CString sHost="", WORD nPort=0,DWORD dwTimeOut=0); //
Return Value
TRUE if the open was successful; otherwise FALSE.
Parameters
sHost - Host name, IP (127.0.0.1) or name (pep1.modicon.com) format.
nPort - IP port. Open Modbus protocol use port 502, but you can use another port if you wish.
dwTimeOut - Time out in ms used for sending modbus messages to a slave.
Remarks
This function try to connect with a slave using TCP/IP network. When ReconnectOnEveryMessage is TRUE this function is called before sending a query to the slave, otherwise the connection remains opened until is explicit closed by closeconnection or by class destructor.Instead of using function parameters you can use class properties for seting sHost , nPort and dwTimeOut.
example:
CRemoteModbus remoteModbus;
WORD wError;
CWordArray anRegs;
anRegs.SetSize(3);
//open connection on host pep1.modicon.com , port 502 and timeout of 5 seconds.
if (remoteModbus.openConnection("pep1.modicon.com",502,5000) {
//Reads 40108 ...40110 registers from slave 17
wError = remoteModbus.ReadOutputRegisters(17,107,3,anRegs) ;if (CModbus::ERR_OK==wError) {
TRACE("Reg 40108=%d", anRegs[0]);
}
remoteModbus.CloseConnection();
}
See also: CModbus,CloseConnection
CRemoteModbus::CloseConnection
BOOL CRemoteModbus::CloseConnection()
Return Value
TRUE if the close was successful; otherwise FALSE.
Remarks:
Closes an the connection associated with this object. If ReconnectOnEveryMessage is TRUE this function is called after the response from a slave was received.
example:
See openConnection.
See also: CModbus,openConnection
WORD Port();
void Port(WORD wPort);
Remarks:
Get/Set IP port. Open modbus protocol use port 502 as a default.
CString Host();
void Host(LPCSTR szHost);
Remarks:
Get/Set Host name. Can be numeric IP format like 198.345.125.30 or a name like pep1.modicon.com.
void Timeout(DWORD dwTimeout);
DWORD Timeout();
Remarks:
Modbus functions timeout in ms.
CRemoteModbus::ReconnectOnEveryMessage
void ReconnectOnEveryMessage(BOOL bReconnect);
BOOL ReconnectOnEveryMessage();
Remarks:
When this property is true each call to a modbus function call openConnection before sending a query and close the connection after response was received. Making this property as TRUE can save some resources from the server or slave because there is limit of connections that a server can manage simultaneously, but the communication will be slower because opening a connection can take good amount of time.
See also: openConnection,CloseConnection.