format OPUCA codes and optimize demo cases
This commit is contained in:
@@ -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
|
||||
// 清零
|
||||
//PlcWrite n4,2 0b
|
||||
//PlcWrite n4,3 0b
|
||||
//PlcWrite n4,4 0b
|
||||
//PlcWrite n4,5 0b
|
||||
//
|
||||
// 使能
|
||||
//PlcWrite n4,2 1b
|
||||
//
|
||||
// 设转速
|
||||
//PlcWrite n4,7 50
|
||||
//
|
||||
// 正转
|
||||
//PlcWrite n4,4 1b
|
||||
//
|
||||
// 反转
|
||||
//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