iec61850_client_example1的例子
- client_example1.c的文件最上边注释,/*
- client_example1.c
- This example is intended to be used with server_example_basic_io or server_example_goose.
*/
在libiec61850\examples\server_example_basic_io里找到simpleIO_direct_control.cid这个文件,修改ip地址127.0.0.1。使用iedscout打开simpleIO_direct_control.cid文件,运行仿真服务器功能。
这时可以启动调试iec61850_client_example1程序。
int main(int argc, char** argv) {
char* hostname;
**int tcpPort = 102;** //服务器监听端口,客户端连接服务端ip+端口
if (argc > 1)
hostname = argv[1];
else
**hostname = "localhost";**
if (argc > 2)
tcpPort = atoi(argv[2]);
IedClientError error;
//创建客户端连接对象
**IedConnection con = IedConnection_create();**
//连接服务器
**IedConnection_connect(con, &error, hostname, tcpPort);**
//error 是否有错误
if (error == IED_ERROR_OK) {
/* read an analog measurement value from server */
**//根据索引读取模拟量值simpleIOGenericIO/GGIO1.AnIn1.mag.f**
MmsValue* value = IedConnection_readObject(con, &error, "simpleIOGenericIO/GGIO1.AnIn1.mag.f", IEC61850_FC_MX);
if (value != NULL) {
if (MmsValue_getType(value) == MMS_FLOAT) {
float fval = MmsValue_toFloat(value);
printf("read float value: %f\n", fval);
}
else if (MmsValue_getType(value) == MMS_DATA_ACCESS_ERROR) {
printf("Failed to read value (error code: %i)\n", MmsValue_getDataAccessError(value));
}
MmsValue_delete(value);
}
/* write a variable to the server */
**//写变量simpleIOGenericIO/GGIO1.NamPlt.vendor**
value = MmsValue_newVisibleString("libiec61850.com");
IedConnection_writeObject(con, &error, "simpleIOGenericIO/GGIO1.NamPlt.vendor", IEC61850_FC_DC, value);
if (error != IED_ERROR_OK)
printf("failed to write simpleIOGenericIO/GGIO1.NamPlt.vendor! (error code: %i)\n", error);
MmsValue_delete(value);
/* read data set */
ClientDataSet clientDataSet = IedConnection_readDataSetValues(con, &error, "simpleIOGenericIO/LLN0.Events", NULL);
**//读数据集里的数据**
**value = ClientDataSet_getValues(clientDataSet);**
**//数据集里元素个数**
**int size = ClientDataSet_getDataSetSize(clientDataSet);**
//根据索引得到元素的值
//4个元素 我添加的代码,根据索引获得值
**int size = ClientDataSet_getDataSetSize(clientDataSet);
MmsValue *tmp = MmsValue_getElement(value, 0);
MmsValue* tmp1 = MmsValue_getElement(value, 1);
MmsValue* tmp2 = MmsValue_getElement(value, 2);
MmsValue* tmp3 = MmsValue_getElement(value, 3);
//bool类型
if (MmsValue_getType(tmp) == MMS_BOOLEAN)
{
bool ret = MmsValue_getBoolean(tmp);
}**
if (clientDataSet == NULL) {
printf("failed to read dataset\n");
goto close_connection;
}
/* Read RCB values */
ClientReportControlBlock rcb =
IedConnection_getRCBValues(con, &error, "simpleIOGenericIO/LLN0.RP.EventsRCB01", NULL);
if (rcb) {
bool rptEna = ClientReportControlBlock_getRptEna(rcb);
printf("RptEna = %i\n", rptEna);
/* Install handler for reports */
IedConnection_installReportHandler(con, "simpleIOGenericIO/LLN0.RP.EventsRCB01",
ClientReportControlBlock_getRptId(rcb), reportCallbackFunction, NULL);
/* Set trigger options and enable report */
ClientReportControlBlock_setTrgOps(rcb, TRG_OPT_DATA_UPDATE | TRG_OPT_INTEGRITY | TRG_OPT_GI);
ClientReportControlBlock_setRptEna(rcb, true);
ClientReportControlBlock_setIntgPd(rcb, 5000);
IedConnection_setRCBValues(con, &error, rcb, RCB_ELEMENT_RPT_ENA | RCB_ELEMENT_TRG_OPS | RCB_ELEMENT_INTG_PD, true);
if (error != IED_ERROR_OK)
printf("report activation failed (code: %i)\n", error);
Thread_sleep(1000);
/* trigger GI report */
ClientReportControlBlock_setGI(rcb, true);
IedConnection_setRCBValues(con, &error, rcb, RCB_ELEMENT_GI, true);
if (error != IED_ERROR_OK)
printf("Error triggering a GI report (code: %i)\n", error);
Thread_sleep(60000);
/* disable reporting */
ClientReportControlBlock_setRptEna(rcb, false);
IedConnection_setRCBValues(con, &error, rcb, RCB_ELEMENT_RPT_ENA, true);
if (error != IED_ERROR_OK)
printf("disable reporting failed (code: %i)\n", error);
ClientDataSet_destroy(clientDataSet);
ClientReportControlBlock_destroy(rcb);
}
close_connection:
IedConnection_close(con);
}
else {
printf("Failed to connect to %s:%i\n", hostname, tcpPort);
}
IedConnection_destroy(con);
}