forked from xuos/xiuos
format OPUCA codes and optimize demo cases
This commit is contained in:
@@ -40,6 +40,9 @@
|
||||
/*******************************************************************************
|
||||
* Variables
|
||||
******************************************************************************/
|
||||
// to count test
|
||||
static int test_cnt = 0;
|
||||
static int fail_cnt = 0; // count failure times
|
||||
|
||||
char test_ua_ip[] = {192, 168, 250, 2};
|
||||
|
||||
@@ -50,37 +53,56 @@ char test_ua_ip[] = {192, 168, 250, 2};
|
||||
static void UaConnectTestTask(void* arg)
|
||||
{
|
||||
struct netif net;
|
||||
UA_StatusCode retval;
|
||||
char ua_uri[UA_URL_SIZE];
|
||||
memset(ua_uri, 0, sizeof(ua_uri));
|
||||
UA_StatusCode ret;
|
||||
char url[UA_URL_SIZE];
|
||||
memset(url, 0, sizeof(url));
|
||||
|
||||
UA_Client* client = UA_Client_new();
|
||||
|
||||
if(client == NULL)
|
||||
{
|
||||
ua_print("ua: [%s] tcp client null\n", __func__);
|
||||
ua_error("ua: [%s] tcp client null\n", __func__);
|
||||
return;
|
||||
}
|
||||
|
||||
UA_ClientConfig* config = UA_Client_getConfig(client);
|
||||
UA_ClientConfig_setDefault(config);
|
||||
snprintf(ua_uri, sizeof(ua_uri), "opc.tcp://%d.%d.%d.%d:4840",
|
||||
test_ua_ip[0], test_ua_ip[1], test_ua_ip[2], test_ua_ip[3]);
|
||||
ua_notice("ua uri: %d %s\n", strlen(ua_uri), ua_uri);
|
||||
retval = UA_Client_connect(client,ua_uri);
|
||||
|
||||
if(retval != UA_STATUSCODE_GOOD)
|
||||
UA_ClientConfig_setDefault(config);
|
||||
|
||||
snprintf(url, sizeof(url), "opc.tcp://%d.%d.%d.%d:4840",
|
||||
test_ua_ip[0], test_ua_ip[1], test_ua_ip[2], test_ua_ip[3]);
|
||||
|
||||
ua_notice("ua connect cnt %d fail %d\n", test_cnt++, fail_cnt ++);
|
||||
ua_notice("ua connect uri: %.*s\n", strlen(url), url);
|
||||
|
||||
ret = UA_Client_connect(client, url);
|
||||
|
||||
if(ret != UA_STATUSCODE_GOOD)
|
||||
{
|
||||
ua_notice("ua: [%s] connected failed %x\n", __func__, retval);
|
||||
ua_error("ua: [%s] connected failed %x\n", __func__, ret);
|
||||
UA_Client_delete(client);
|
||||
fail_cnt++;
|
||||
return;
|
||||
}
|
||||
|
||||
ua_notice("ua: [%s] connected ok!\n", __func__);
|
||||
ua_notice("ua connected ok!\n");
|
||||
UA_Client_delete(client);
|
||||
}
|
||||
|
||||
void UaConnectTest(void* arg)
|
||||
static void UaConnectTest(int argc, char *argv[])
|
||||
{
|
||||
if(argc == 2)
|
||||
{
|
||||
if(isdigit(argv[1][0]))
|
||||
{
|
||||
if(sscanf(argv[1], "%d.%d.%d.%d", &test_ua_ip[0], &test_ua_ip[1], &test_ua_ip[2], &test_ua_ip[3]) == EOF)
|
||||
{
|
||||
lw_notice("input wrong ip\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lwip_config_tcp(lwip_ipaddr, lwip_netmask, test_ua_ip);
|
||||
sys_thread_new("ua test", UaConnectTestTask, NULL, UA_STACK_SIZE, UA_TASK_PRIO);
|
||||
}
|
||||
@@ -90,10 +112,10 @@ SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) |
|
||||
|
||||
void UaBrowserObjectsTestTask(void* param)
|
||||
{
|
||||
static int test_cnt = 0;
|
||||
UA_Client* client = UA_Client_new();
|
||||
ua_notice("ua: [%s] start %d ...\n", __func__, test_cnt++);
|
||||
|
||||
UA_Client* client = UA_Client_new();
|
||||
|
||||
if(client == NULL)
|
||||
{
|
||||
ua_error("ua: [%s] tcp client NULL\n", __func__);
|
||||
@@ -102,25 +124,26 @@ void UaBrowserObjectsTestTask(void* param)
|
||||
|
||||
UA_ClientConfig* config = UA_Client_getConfig(client);
|
||||
UA_ClientConfig_setDefault(config);
|
||||
UA_StatusCode retval = UA_Client_connect(client, opc_server_url);
|
||||
UA_StatusCode ret = UA_Client_connect(client, opc_server_url);
|
||||
|
||||
if(retval != UA_STATUSCODE_GOOD)
|
||||
if(ret != UA_STATUSCODE_GOOD)
|
||||
{
|
||||
ua_error("ua: [%s] connect failed %#x\n", __func__, retval);
|
||||
ua_error("ua: [%s] connect failed %#x\n", __func__, ret);
|
||||
UA_Client_delete(client);
|
||||
return;
|
||||
}
|
||||
|
||||
ua_notice("--- start read time ---\n", __func__);
|
||||
ua_read_time(client);
|
||||
UaGetServerTime(client);
|
||||
|
||||
ua_notice("--- get server info ---\n", __func__);
|
||||
ua_test_browser_objects(client);
|
||||
UaTestBrowserObjects(client);
|
||||
|
||||
/* Clean up */
|
||||
UA_Client_delete(client); /* Disconnects the client internally */
|
||||
}
|
||||
|
||||
void* UaBrowserObjectsTest(int argc, char* argv[])
|
||||
static void* UaBrowserObjectsTest(int argc, char* argv[])
|
||||
{
|
||||
if(argc == 2)
|
||||
{
|
||||
@@ -140,35 +163,33 @@ void* UaBrowserObjectsTest(int argc, char* argv[])
|
||||
}
|
||||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
|
||||
UaObj, UaBrowserObjectsTest, UaObj [IP]);
|
||||
UaObject, UaBrowserObjectsTest, UaObject [IP]);
|
||||
|
||||
void UaGetInfoTestTask(void* param)
|
||||
{
|
||||
UA_Client* client = UA_Client_new();
|
||||
ua_notice("ua: [%s] start ...\n", __func__);
|
||||
ua_notice("--- Get OPUCA objects ---\n", __func__);
|
||||
|
||||
if(client == NULL)
|
||||
{
|
||||
ua_print("ua: [%s] tcp client null\n", __func__);
|
||||
ua_error("ua: [%s] tcp client null\n", __func__);
|
||||
return;
|
||||
}
|
||||
|
||||
UA_ClientConfig* config = UA_Client_getConfig(client);
|
||||
UA_ClientConfig_setDefault(config);
|
||||
UA_StatusCode retval = UA_Client_connect(client, opc_server_url);
|
||||
UA_StatusCode ret = UA_Client_connect(client, opc_server_url);
|
||||
|
||||
if(retval != UA_STATUSCODE_GOOD)
|
||||
if(ret != UA_STATUSCODE_GOOD)
|
||||
{
|
||||
ua_print("ua: [%s] connect failed %#x\n", __func__, retval);
|
||||
ua_error("ua: [%s] connect failed %#x\n", __func__, ret);
|
||||
UA_Client_delete(client);
|
||||
return;
|
||||
}
|
||||
|
||||
ua_print("ua: [%s] connect ok!\n", __func__);
|
||||
ua_notice("--- interactive server ---\n", __func__);
|
||||
ua_test_interact_server(client);
|
||||
/* Clean up */
|
||||
UA_Client_disconnect(client);
|
||||
UaTestInteractServer(client);
|
||||
|
||||
UA_Client_delete(client); /* Disconnects the client internally */
|
||||
}
|
||||
|
||||
@@ -201,26 +222,24 @@ void UaAddNodesTask(void* param)
|
||||
|
||||
if(client == NULL)
|
||||
{
|
||||
ua_print("ua: [%s] tcp client null\n", __func__);
|
||||
ua_error("ua: [%s] client null\n", __func__);
|
||||
return;
|
||||
}
|
||||
|
||||
UA_ClientConfig* config = UA_Client_getConfig(client);
|
||||
UA_ClientConfig_setDefault(config);
|
||||
UA_StatusCode retval = UA_Client_connect(client, opc_server_url);
|
||||
UA_StatusCode ret = UA_Client_connect(client, opc_server_url);
|
||||
|
||||
if(retval != UA_STATUSCODE_GOOD)
|
||||
if(ret != UA_STATUSCODE_GOOD)
|
||||
{
|
||||
ua_print("ua: [%s] connect failed %#x\n", __func__, retval);
|
||||
ua_print("ua: [%s] connect failed %#x\n", __func__, ret);
|
||||
UA_Client_delete(client);
|
||||
return;
|
||||
}
|
||||
|
||||
ua_print("ua: [%s] connect ok!\n", __func__);
|
||||
ua_notice("--- add nodes ---\n", __func__);
|
||||
ua_add_nodes(client);
|
||||
/* Clean up */
|
||||
UA_Client_disconnect(client);
|
||||
UaAddNodes(client);
|
||||
|
||||
UA_Client_delete(client); /* Disconnects the client internally */
|
||||
}
|
||||
|
||||
|
||||
@@ -36,6 +36,32 @@ UA_NodeId test_nodeid = {4, UA_NODEIDTYPE_NUMERIC, 5};
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
// get NodeId from str
|
||||
void PlcGetTestNodeId(char *str, UA_NodeId *id)
|
||||
{
|
||||
static char node_str[UA_NODE_LEN];
|
||||
memset(node_str, 0, sizeof(node_str));
|
||||
|
||||
plc_print("plc: arg %s\n", str);
|
||||
|
||||
if(sscanf(str, PLC_NS_FORMAT, &id->namespaceIndex, node_str) != EOF)
|
||||
{
|
||||
if(isdigit(node_str[0]))
|
||||
{
|
||||
id->identifierType = UA_NODEIDTYPE_NUMERIC;
|
||||
id->identifier.numeric = atoi(node_str);
|
||||
plc_print("ns %d num %d\n", id->namespaceIndex, id->identifier.numeric);
|
||||
}
|
||||
else
|
||||
{
|
||||
id->identifierType = UA_NODEIDTYPE_STRING;
|
||||
id->identifier.string.length = strlen(node_str);
|
||||
id->identifier.string.data = node_str;
|
||||
plc_print("ns %d str %s\n", id->namespaceIndex, id->identifier.string.data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PlcDemoChannelDrvInit(void)
|
||||
{
|
||||
static uint8_t init_flag = 0;
|
||||
@@ -52,33 +78,42 @@ void PlcDemoChannelDrvInit(void)
|
||||
memset(&plc_demo_dev, 0, sizeof(plc_demo_dev));
|
||||
}
|
||||
|
||||
static void PlcGetDemoDev(PlcDeviceType *dev, UA_NodeId *id)
|
||||
{
|
||||
// register plc device
|
||||
dev->state = CHDEV_INIT;
|
||||
strcpy(dev->name, "UA Demo");
|
||||
dev->info.product = "CPU 1215C";
|
||||
dev->info.vendor = "SIEMENS";
|
||||
dev->info.model = "S7-1200";
|
||||
dev->info.id = 123;
|
||||
dev->net = PLC_IND_ENET_OPCUA;
|
||||
|
||||
// register UA parameter
|
||||
if(!dev->priv_data)
|
||||
{
|
||||
dev->priv_data = (UaParamType*)malloc(sizeof(UaParamType));
|
||||
}
|
||||
UaParamType* ua_ptr = dev->priv_data;
|
||||
memset(ua_ptr, 0, sizeof(UaParamType));
|
||||
strcpy(ua_ptr->ua_remote_ip, opc_server_url);
|
||||
ua_ptr->act = UA_ACT_ATTR;
|
||||
memcpy(&ua_ptr->ua_id, id, sizeof(*id));
|
||||
}
|
||||
|
||||
static void PlcCtrlDemoInit(void)
|
||||
{
|
||||
static uint8_t init_flag = 0;
|
||||
|
||||
PlcDemoChannelDrvInit();
|
||||
// register plc device
|
||||
plc_demo_dev.state = CHDEV_INIT;
|
||||
strcpy(plc_demo_dev.name, "UA Demo");
|
||||
plc_demo_dev.info.product = "CPU 1215C";
|
||||
plc_demo_dev.info.vendor = "SIEMENS";
|
||||
plc_demo_dev.info.model = "S7-1200";
|
||||
plc_demo_dev.info.id = 123;
|
||||
plc_demo_dev.net = PLC_IND_ENET_OPCUA;
|
||||
|
||||
// register UA parameter
|
||||
if(!plc_demo_dev.priv_data)
|
||||
{
|
||||
plc_demo_dev.priv_data = (UaParamType*)malloc(sizeof(UaParamType));
|
||||
}
|
||||
UaParamType* ua_ptr = plc_demo_dev.priv_data;
|
||||
memset(ua_ptr, 0, sizeof(UaParamType));
|
||||
strcpy(ua_ptr->ua_remote_ip, opc_server_url);
|
||||
ua_ptr->act = UA_ACT_ATTR;
|
||||
memcpy(&ua_ptr->ua_id, &test_nodeid, sizeof(test_nodeid));
|
||||
// register plc device
|
||||
PlcGetDemoDev(&plc_demo_dev, &test_nodeid);
|
||||
|
||||
if(init_flag)
|
||||
{
|
||||
return;
|
||||
}
|
||||
init_flag = 1;
|
||||
|
||||
if(PlcDevRegister(&plc_demo_dev, NULL, plc_demo_dev.name) != EOK)
|
||||
@@ -121,24 +156,7 @@ void PlcReadTest(int argc, char* argv[])
|
||||
|
||||
if(argc > 1)
|
||||
{
|
||||
plc_print("plc: arg %s\n", argv[1]);
|
||||
|
||||
if(sscanf(argv[1], PLC_NS_FORMAT, &test_nodeid.namespaceIndex, node_str) != EOF)
|
||||
{
|
||||
if(isdigit(node_str[0]))
|
||||
{
|
||||
test_nodeid.identifierType = UA_NODEIDTYPE_NUMERIC;
|
||||
test_nodeid.identifier.numeric = atoi(node_str);
|
||||
plc_print("ns %d num %d\n", test_nodeid.namespaceIndex, test_nodeid.identifier.numeric);
|
||||
}
|
||||
else
|
||||
{
|
||||
test_nodeid.identifierType = UA_NODEIDTYPE_STRING;
|
||||
test_nodeid.identifier.string.length = strlen(node_str);
|
||||
test_nodeid.identifier.string.data = node_str;
|
||||
plc_print("ns %d str %s\n", test_nodeid.namespaceIndex, test_nodeid.identifier.string.data);
|
||||
}
|
||||
}
|
||||
PlcGetTestNodeId(argv[1], &test_nodeid);
|
||||
}
|
||||
|
||||
sys_thread_new("plc read", PlcReadUATask, NULL, PLC_STACK_SIZE, PLC_TASK_PRIO);
|
||||
@@ -168,7 +186,7 @@ void PlcWriteUATask(void* arg)
|
||||
|
||||
if(EOK != ret)
|
||||
{
|
||||
plc_print("plc: [%s] read failed\n", __func__);
|
||||
plc_print("plc: [%s] write failed\n", __func__);
|
||||
}
|
||||
|
||||
ops->close(&plc_demo_dev);
|
||||
@@ -183,30 +201,13 @@ void PlcWriteTest(int argc, char* argv[])
|
||||
|
||||
if(argc > 1)
|
||||
{
|
||||
plc_print("plc: arg %s\n", argv[1]);
|
||||
PlcGetTestNodeId(argv[1], &test_nodeid);
|
||||
}
|
||||
|
||||
if(sscanf(argv[1], PLC_NS_FORMAT, &test_nodeid.namespaceIndex, node_str) != EOF)
|
||||
{
|
||||
if(isdigit(node_str[0]))
|
||||
{
|
||||
test_nodeid.identifierType = UA_NODEIDTYPE_NUMERIC;
|
||||
test_nodeid.identifier.numeric = atoi(node_str);
|
||||
plc_print("ns %d num %d\n", test_nodeid.namespaceIndex, test_nodeid.identifier.numeric);
|
||||
}
|
||||
else
|
||||
{
|
||||
test_nodeid.identifierType = UA_NODEIDTYPE_STRING;
|
||||
test_nodeid.identifier.string.length = strlen(node_str);
|
||||
test_nodeid.identifier.string.data = node_str;
|
||||
plc_print("ns %d str %s\n", test_nodeid.namespaceIndex, test_nodeid.identifier.string.data);
|
||||
}
|
||||
}
|
||||
|
||||
if(argc > 2)
|
||||
{
|
||||
strcpy(val_param, argv[2]);
|
||||
plc_print("write value %s\n", val_param);
|
||||
}
|
||||
if(argc > 2)
|
||||
{
|
||||
strcpy(val_param, argv[2]);
|
||||
plc_print("write value %s\n", val_param);
|
||||
}
|
||||
|
||||
sys_thread_new("plc write", PlcWriteUATask, val_param, PLC_STACK_SIZE, PLC_TASK_PRIO);
|
||||
@@ -215,3 +216,79 @@ void PlcWriteTest(int argc, char* argv[])
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
|
||||
PlcWrite, PlcWriteTest, Read PLC);
|
||||
|
||||
// test motor
|
||||
// <20><><EFBFBD><EFBFBD>
|
||||
//PlcWrite n4,2 0b
|
||||
//PlcWrite n4,3 0b
|
||||
//PlcWrite n4,4 0b
|
||||
//PlcWrite n4,5 0b
|
||||
//
|
||||
// ʹ<><CAB9>
|
||||
//PlcWrite n4,2 1b
|
||||
//
|
||||
// <20><>ת<EFBFBD><D7AA>
|
||||
//PlcWrite n4,7 50
|
||||
//
|
||||
// <20><>ת
|
||||
//PlcWrite n4,4 1b
|
||||
//
|
||||
// <20><>ת
|
||||
//PlcWrite n4,5 1b
|
||||
|
||||
|
||||
void PlcMotorTestTask(void* arg)
|
||||
{
|
||||
char *test_target[] = {"n4,2", "n4,3", "n4,4", "n4,5"};
|
||||
char *test_cmd[] = {"1b", "50", "1b", "1b"};
|
||||
|
||||
int ret = 0;
|
||||
struct PlcOps* ops = NULL;
|
||||
char buf[PLC_BUF_SIZE];
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
||||
PlcCtrlDemoInit();
|
||||
ops = plc_demo_dev.ops;
|
||||
ret = ops->open(&plc_demo_dev);
|
||||
|
||||
if(EOK != ret)
|
||||
{
|
||||
plc_print("plc: [%s] open failed %#x\n", __func__, ret);
|
||||
return;
|
||||
}
|
||||
|
||||
UaParamType* ua_ptr = plc_demo_dev.priv_data;
|
||||
|
||||
for(int i = 0; i < 4; i++)
|
||||
{
|
||||
UA_NodeId id;
|
||||
PlcGetTestNodeId(test_target[i], &id);
|
||||
memcpy(&ua_ptr->ua_id, &id, sizeof(id));
|
||||
ret = ops->write(&plc_demo_dev, "0b", PLC_BUF_SIZE);
|
||||
if(EOK != ret)
|
||||
{
|
||||
plc_print("plc: [%s] %d write failed\n", __func__, __LINE__);
|
||||
}
|
||||
}
|
||||
|
||||
for(int i = 0; i < 4; i++)
|
||||
{
|
||||
UA_NodeId id;
|
||||
PlcGetTestNodeId(test_target[i], &id);
|
||||
memcpy(&ua_ptr->ua_id, &id, sizeof(id));
|
||||
ret = ops->write(&plc_demo_dev, test_cmd[i], PLC_BUF_SIZE);
|
||||
if(EOK != ret)
|
||||
{
|
||||
plc_print("plc: [%s] %d write failed\n", __func__, __LINE__);
|
||||
}
|
||||
}
|
||||
ops->close(&plc_demo_dev);
|
||||
}
|
||||
|
||||
void PlcMotorTest(int argc, char* argv[])
|
||||
{
|
||||
sys_thread_new("plc motor", PlcMotorTestTask, NULL, PLC_STACK_SIZE, PLC_TASK_PRIO);
|
||||
}
|
||||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
|
||||
PlcMotorTest, PlcMotorTest, Run motor);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user