support plc demo

This commit is contained in:
wlyu
2022-02-15 14:24:46 +08:00
parent 3bc3577d35
commit 338f4ad41a
21 changed files with 188 additions and 88 deletions

View File

@@ -15,25 +15,29 @@
#include "ua_api.h"
#define UA_DEV_IP_LEN 128
#define UA_NODE_LEN 50
typedef struct _ua_dev_t
{
char ua_remote_ip[UA_DEV_IP_LEN];
char ua_node[UA_NODE_LEN];
UA_Client *client;
}ua_dev_t;
int ua_open(void *dev)
{
ua_dev_t *pdev = (ua_dev_t *)dev;
pdev->client = UA_Client_new();
ua_pr_info("ua: [%s] start ...\n", __func__);
if (pdev->client == NULL)
{
ua_print("ua: [%s] tcp client null\n", __func__);
return EEMPTY;
}
UA_ClientConfig *config = UA_Client_getConfig(pdev->client);
UA_ClientConfig_setDefault(config);
ua_pr_info("ua: [%s] %d %s\n", __func__, strlen(pdev->ua_remote_ip), pdev->ua_remote_ip);
UA_StatusCode retval = UA_Client_connect(pdev->client, pdev->ua_remote_ip);
if(retval != UA_STATUSCODE_GOOD) {
UA_Client_delete(pdev->client);
// UA_Client_delete(pdev->client);
ua_pr_info("ua: [%s] deleted ret %x!\n", __func__, retval);
return (int)retval;
}
return EOK;

View File

@@ -14,10 +14,20 @@
#include "open62541.h"
#define UA_DEV_IP_LEN 128
#define UA_NODE_LEN 50
typedef struct _ua_dev_t
{
char ua_remote_ip[UA_DEV_IP_LEN];
char ua_node[UA_NODE_LEN];
UA_Client *client;
}ua_dev_t;
#define OPC_SERVER "opc.tcp://192.168.250.5:4840"
#define ua_print //printf
#define ua_trace() //printf("ua: [%s] line %d checked!\n", __func__, __LINE__)
#define ua_print //KPrintf
#define ua_trace() //KPrintf("ua: [%s] line %d checked!\n", __func__, __LINE__)
#define ua_pr_info KPrintf
#define ua_debug

View File

@@ -149,19 +149,19 @@ void ua_read_attr(UA_Client *client)
{
/* Read attribute */
UA_Int32 value = 0;
ua_print("\nReading the value of node (1, \"the.answer\"):\n");
ua_pr_info("\nReading the value of node (1, \"the.answer\"):\n");
UA_Variant *val = UA_Variant_new();
UA_StatusCode retval = UA_Client_readValueAttribute(client, UA_NODEID_STRING(1, "the.answer"), val);
if(retval == UA_STATUSCODE_GOOD && UA_Variant_isScalar(val) &&
val->type == &UA_TYPES[UA_TYPES_INT32]) {
value = *(UA_Int32*)val->data;
ua_print("the value is: %i\n", value);
ua_pr_info("the value is: %i\n", value);
}
UA_Variant_delete(val);
/* Write node attribute */
value++;
ua_print("\nWriting a value of node (1, \"the.answer\"):\n");
ua_pr_info("\nWriting a value of node (1, \"the.answer\"):\n");
UA_WriteRequest wReq;
UA_WriteRequest_init(&wReq);
wReq.nodesToWrite = UA_WriteValue_new();
@@ -174,7 +174,7 @@ void ua_read_attr(UA_Client *client)
wReq.nodesToWrite[0].value.value.data = &value;
UA_WriteResponse wResp = UA_Client_Service_write(client, wReq);
if(wResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD)
ua_print("the new value is: %i\n", value);
ua_pr_info("the new value is: %i\n", value);
UA_WriteRequest_clear(&wReq);
UA_WriteResponse_clear(&wResp);
@@ -297,7 +297,7 @@ int ua_get_server_info(UA_Client *client)
#ifdef UA_ENABLE_SUBSCRIPTIONS
/* Take another look at the.answer */
UA_Client_run_iterate(client, 100);
UA_Client_run_iterate(client, 10000);
/* Delete the subscription */
if(UA_Client_Subscriptions_deleteSingle(client, subId) == UA_STATUSCODE_GOOD)
ua_print("Subscription removed\n");

View File

@@ -25,13 +25,6 @@
#include "plc_bus.h"
#include "plc_dev.h"
#define PLC_BUS_NAME "plc bus"
#define PLC_DRV_NAME "plc driver"
struct PlcDevice plc_device;
struct PlcBus plc_bus;
struct PlcDriver plc_drv;
static DoubleLinklistType plcdev_list;
@@ -52,7 +45,7 @@ static int PlcDeviceOpen(void *dev)
if(plc_dev->net == PLC_IND_ENET_OPCUA)
{
return ua_open(dev);
return ua_open(plc_dev->priv_data);
}
return EOK;
@@ -66,21 +59,21 @@ static void PlcDeviceClose(void *dev)
if(plc_dev->net == PLC_IND_ENET_OPCUA)
{
ua_close(dev);
ua_close(plc_dev->priv_data);
}
}
static int PlcDeviceWrite(void *dev, const void *buf, size_t len)
{
NULL_PARAM_CHECK(dev);
NULL_PARAM_CHECK(write_param);
NULL_PARAM_CHECK(buf);
int ret;
struct PlcDevice *plc_dev = (struct PlcDevice *)dev;
if(plc_dev->net == PLC_IND_ENET_OPCUA)
{
ret = ua_write(dev, buf, len);
ret = ua_write(plc_dev->priv_data, buf, len);
}
return ret;
@@ -89,20 +82,20 @@ static int PlcDeviceWrite(void *dev, const void *buf, size_t len)
static int PlcDeviceRead(void *dev, void *buf, size_t len)
{
NULL_PARAM_CHECK(dev);
NULL_PARAM_CHECK(read_param);
NULL_PARAM_CHECK(buf);
int ret;
struct PlcDevice *plc_dev = (struct PlcDevice *)dev;
if(plc_dev->net == PLC_IND_ENET_OPCUA)
{
ret = ua_read(dev, buf, len);
ret = ua_read(plc_dev->priv_data, buf, len);
}
return ret;
}
static const struct PlcOps plc_done =
static struct PlcOps plc_done =
{
.open = PlcDeviceOpen,
.close = PlcDeviceClose,
@@ -145,6 +138,7 @@ int PlcDevRegister(struct PlcDevice *plc_device, void *plc_param, const char *de
if (DEV_INSTALL != plc_device->state) {
strncpy(plc_device->name, device_name, strlen(device_name));
plc_device->ops = &plc_done;
DoubleLinkListInsertNodeAfter(&plcdev_list, &(plc_device->link));
plc_device->state = DEV_INSTALL;
} else {
@@ -189,17 +183,3 @@ int PlcDeviceAttachToBus(const char *dev_name, const char *bus_name)
return EOK;
}
void PlcTestInit(void)
{
PlcBusInit(&plc_bus, PLC_BUS_NAME);
PlcDriverInit(&plc_drv, PLC_DRV_NAME);
}
void test_plc_bus(int argc, char *argv[])
{
PlcTestInit();
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
plc, test_plc_bus, test PLC);

View File

@@ -58,6 +58,11 @@ union PlcCfg {
struct PlcDevice;
#undef open
#undef close
#undef read
#undef write
// operation API
struct PlcOps {
int (*open)(void *dev); // open and connect PLC device
@@ -123,9 +128,13 @@ struct PlcDevice {
struct PlcInfo info;/* Plc info, such as vendor name and model name */
union PlcCfg cfg;
struct PlcOps ops; /* filesystem-like APIs for data transferring */
struct PlcOps *ops; /* filesystem-like APIs for data transferring */
struct PlcInterface interface; /* protocols used for transferring data from program to plc */
void *priv_data;
DoubleLinklistType link;/* link list node */
};
int PlcDevRegister(struct PlcDevice *plc_device, void *plc_param, const char *device_name);
#endif

View File

@@ -231,7 +231,6 @@ int PlcHardwareDevConfigureCs(struct HardwareDev *dev, uint8 plc_chip_select, ui
msg->plc_chip_select = plc_chip_select;
msg->plc_cs_release = plc_cs_release;
ret = plc_dev->plc_dev_done->dev_write(plc_dev, msg);
x_free(msg);