Add a new configuration parameter
- Last UpdatedNov 18, 2024
- 3 minute read
By default, the Communication Drivers SDK provides IP Address and Port Number configuration. In order to add a new configuration parameter (for example, Reply Timeout), follow the steps in the procedure below.
The name of the Communication Driver in the example below is TestServer. Replace TestServer with the name of of your Communication Driver.
Add a new configuration parameter
-
Add the <ReplyTimeout> entry in the TestServer.AArul file under the "PLC" type Hierarchy Node.
<ReplyTimeout>
<PROPERTYTYPE>VT_UI2</PROPERTYTYPE>
<DEFAULTVALUE>15</DEFAULTVALUE>
<PROPERTYMIN>1</PROPERTYMIN>
<PROPERTYMAX>300</PROPERTYMAX>
<PROPERTYEDITHEADER> ReplyTimeout </PROPERTYEDITHEADER>
<PROPERTYEDITUNIT/>
<PROPERTYEDITHELP> ReplyTimeout </PROPERTYEDITHELP>
</ReplyTimeout >
-
Update the configuration editor:
-
Open the IDD_CFG_PLC dialog from CFG_TestServer_PLC.rc project.

-
Drag and drop the Static Text and Edit Control tools to the UI from the Toolbox.
-
Rename the Static Text to Reply Timeout and Edit Control ID to IDC_EDIT_REPLYTIMEOUT.

-
Add the following attribute definitions in the private CCfgPLC class section in the CFG_PLC.h file.
DWORD m_hReplyTimeout;
CComBSTR m_bstrReplyTimeout;
CComBSTR m_bstrReplyTimeout _Applied;
CComBSTR m_bstrReplyTimeout_Min;
CComBSTR m_bstrReplyTimeout_Max;
-
Add the COMMAND_HANDLER entry in the MSG_MAP (MessageMap) section of the CCfgPLC class in the CFG_PLC.h file.
COMMAND_HANDLER(IDC_EDIT_REPLYTIMEOUT, EN_CHANGE, OnChangeReplyTimeout)
-
Declare the OnChange command handler in the public CCfgPLC class section in CFG_PLC.h file.
LRESULT OnChangeReplyTimeout (WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
-
Define the OnChange command handler in the public CCfgPLC class section in CFG_PLC.cpp file.
LRESULT
CCfgPLC::OnChangeReplyTimeout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
SetHostDirty();
return 0;
}
-
Bind the values entered in the Text Box to a variable in the BindAttributes method of the CCfgPLC class in CFG_PLC.cpp file.
BindAttr(CComBSTR(L"ReplyTimeout"), &m_hReplyTimeout);
-
Initialize property values in the OnInitialize method ofthe CCfgPLC class in the CFG_PLC.cpp file.
hr = InitializeValues(m_hReplyTimeout, m_bstrReplyTimeout, m_bstrReplyTimeout_Min, m_bstrReplyTimeout_Max);
if (hr != S_OK)
return hr;
-
Initialize applied variables in the OnInitialize method ofthe CCfgPLC class in the CFG_PLC.cpp file.
m_bstrReplyTimeout_Applied = m_bstrReplyTimeout;
-
Disable the edit control for read-only mode in the OnInitialize method of the CCfgPLC class in CFG_PLC.cpp file.
if( m_bReadOnly )
{
//Disable the Reply Timeout edit box.
//TODO: Similar block of code for each property.
HWND hwndReplyTimeout = GetDlgItem(IDC_EDIT_REPLYTIMEOUT);
CWindow wReplyTimeout;
wReplyTimeout.Attach(hwndReplyTimeout);
wReplyTimeout.EnableWindow(FALSE);
}
-
Capture the 'Applied' version Reply Timeout property in the OnRestore method of the CCfgPLC class in the CFG_PLC.cpp file.
m_bstrReplyTimeout = m_bstrReplyTimeout_Applied;
-
Close handle for the Reply Timeout property in the OnClose method of the CCfgPLC class in the CFG_PLC.cpp file.
CheckResult(m_PackageSite->CloseAttrHandle(m_hReplyTimeout));
-
Read from the package and write value to dialog control in the DDX method (bUpdate True case) of the CCfgPLC class in theCFG_PLC.cpp file.
SetDlgItemText(IDC_EDIT_REPLYTIMEOUT, W2T(m_bstrReplyTimeout.m_str));
-
Read from the dialog control for the package in the DDX method (bUpdate False case) of the CCfgPLC class in the CFG_PLC.cpp file.
GetDlgItemText(IDC_EDIT_REPLYTIMEOUT, m_bstrReplyTimeout.m_str);
-
Save configured values into the configuration file in the OnApply method of the CCfgPLC class in CFG_PLC.cpp file.
// ReplyTimeout
AttrVar.Clear();
AttrVar.vt = VT_BSTR;
AttrVar.bstrVal = m_bstrReplyTimeout.Copy();
if (!SetAttribute(m_hReplyTimeout, AttrVar))
return E_FAIL;
m_bstrReplyTimeout_Applied = m_bstrReplyTimeout;
-
Check to enable or disable the Reply Timeout text box in the EnableControls method of the CCfgPLC class in CFG_PLC.cpp file.
ControlEnableCheck(IDC_EDIT_REPLYTIMEOUT, m_hReplyTimeout);
-
-
Update the Communication Driver runtime: Use the GetProperty method to read the configuration parameter from the runtime project of the TestServerPLC constructor in the TestServerHierarchy.cpp file as shown below for the Modicon Communication Driver as an example.
ModiconPLC::ModiconPLC(IOTHANDLE hBaseObject) :
SvIoHierarchy(hBaseObject)
{
m_HostName = GetProperty(L"PLC", L"NetworkAddress", wstring(TEXT("")));
m_usPortNumber = (u_short)GetProperty(L"PLC", L"Port", 1234);
m_ReplyTimeout = (u_short)GetProperty(L"PLC", L"ReplyTimeout", 30);
}