feat add modbus tcp protocol for control framework
This commit is contained in:
133
APP_Framework/Applications/connection_app/4g_app/4g_app.c
Normal file
133
APP_Framework/Applications/connection_app/4g_app/4g_app.c
Normal file
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* Copyright (c) 2022 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file 4g_app.c
|
||||
* @brief support get data from and send data to 4g server
|
||||
* @version 3.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2022.12.12
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <transform.h>
|
||||
#include <adapter.h>
|
||||
|
||||
static uint8_t adapter_4g_status = 0;
|
||||
static pthread_t recv_4g_heart_task;
|
||||
struct Adapter *adapter_4g;
|
||||
|
||||
static const uint8_t server_addr[] = "xxx.xxx.xxx.xxx";
|
||||
static const uint8_t server_port[] = "xxx";
|
||||
|
||||
#define ADAPTER_4G_HEART "HEART"
|
||||
|
||||
int Adapter4GConnectFunction(struct Adapter *adapter, uint8_t reconnect)
|
||||
{
|
||||
int ret = 0;
|
||||
int baud_rate = BAUD_RATE_115200;
|
||||
|
||||
if (1 != reconnect) {
|
||||
ret = AdapterDeviceOpen(adapter);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = AdapterDeviceControl(adapter, OPE_INT, &baud_rate);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
|
||||
ret = AdapterDeviceConnect(adapter, CLIENT, server_addr, server_port, IPV4);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
out:
|
||||
if (ret < 0) {
|
||||
AdapterDeviceClose(adapter);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void Adapter4gSend(uint8_t *send_data, size_t length)
|
||||
{
|
||||
if (adapter_4g_status) {
|
||||
AdapterDeviceSend(adapter_4g, send_data, length);
|
||||
}
|
||||
}
|
||||
|
||||
static void *Receive4gHeartTask(void* parameter)
|
||||
{
|
||||
char recv_msg[16] = {0};
|
||||
ssize_t recv_length = 0;
|
||||
uint8_t net_status_cnt = 0;
|
||||
|
||||
while (1) {
|
||||
|
||||
SetTaskStatus(0x01);
|
||||
|
||||
if (net_status_cnt > 5) {
|
||||
adapter_4g_status = 0;
|
||||
|
||||
while (Adapter4GConnectFunction(adapter_4g, 1) < 0) {
|
||||
PrivTaskDelay(10000);
|
||||
}
|
||||
|
||||
net_status_cnt = 0;
|
||||
}
|
||||
|
||||
adapter_4g_status = 1;
|
||||
|
||||
recv_length = AdapterDeviceRecv(adapter_4g, recv_msg, 6);
|
||||
if (recv_length > 0) {
|
||||
//if (0 == strcmp(recv_msg, ADAPTER_4G_HEART)) {
|
||||
net_status_cnt = 0;
|
||||
//}
|
||||
} else {
|
||||
printf("4G recv heart error re-recv cnt %d\n", net_status_cnt);
|
||||
net_status_cnt++;
|
||||
}
|
||||
memset(recv_msg, 0, sizeof(recv_msg));
|
||||
}
|
||||
}
|
||||
|
||||
int Adapter4GActive(void)
|
||||
{
|
||||
int ret = 0;
|
||||
adapter_4g = AdapterDeviceFindByName(ADAPTER_4G_NAME);
|
||||
|
||||
#ifdef ADAPTER_EC200T
|
||||
adapter_4g->socket.socket_id = 0;
|
||||
|
||||
ret = Adapter4GConnectFunction(adapter_4g, 0);
|
||||
if (ret < 0) {
|
||||
printf("Adapter4GConnect failed %d\n", ret);
|
||||
}
|
||||
|
||||
adapter_4g_status = 1;
|
||||
|
||||
pthread_attr_t attr;
|
||||
attr.schedparam.sched_priority = 22;
|
||||
attr.stacksize = 2048;
|
||||
|
||||
PrivTaskCreate(&recv_4g_heart_task, &attr, &Receive4gHeartTask, NULL);
|
||||
PrivTaskStartup(&recv_4g_heart_task);
|
||||
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
SRC_FILES := 4g_app.c
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
@@ -10,6 +10,10 @@ endif
|
||||
|
||||
ifeq ($(CONFIG_ADD_XIZI_FETURES),y)
|
||||
|
||||
ifeq ($(CONFIG_CONNECTION_ADAPTER_4G),y)
|
||||
SRC_DIR += 4g_app
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_RESOURCES_LWIP),y)
|
||||
SRC_DIR += socket_demo
|
||||
endif
|
||||
|
||||
@@ -18,6 +18,47 @@
|
||||
* @date 2022.9.27
|
||||
*/
|
||||
|
||||
|
||||
#include <control.h>
|
||||
|
||||
extern int Adapter4GActive(void);
|
||||
|
||||
void ControlFx3uTest(void)
|
||||
{
|
||||
int i, j = 0;
|
||||
int read_data_length = 0;
|
||||
uint8_t read_data[128] = {0};
|
||||
|
||||
#ifdef CONNECTION_ADAPTER_4G
|
||||
Adapter4GActive();
|
||||
#endif
|
||||
|
||||
ControlProtocolType modbus_tcp_protocol = ControlProtocolFind();
|
||||
if (NULL == modbus_tcp_protocol) {
|
||||
printf("%s get modbus tcp protocol %p failed\n", __func__, modbus_tcp_protocol);
|
||||
return;
|
||||
}
|
||||
|
||||
printf("%s get modbus tcp protocol %p successfull\n", __func__, modbus_tcp_protocol);
|
||||
|
||||
if (CONTROL_REGISTERED == modbus_tcp_protocol->protocol_status) {
|
||||
ControlProtocolOpen(modbus_tcp_protocol);
|
||||
|
||||
for (;;) {
|
||||
read_data_length = ControlProtocolRead(modbus_tcp_protocol, read_data, sizeof(read_data));
|
||||
printf("%s read [%d] modbus tcp data %d using receipe file\n", __func__, i, read_data_length);
|
||||
if (read_data_length) {
|
||||
for (j = 0; j < read_data_length; j ++) {
|
||||
printf("j %d data 0x%x\n", j, read_data[j]);
|
||||
}
|
||||
}
|
||||
i++;
|
||||
memset(read_data, 0, sizeof(read_data));
|
||||
PrivTaskDelay(10000);
|
||||
}
|
||||
|
||||
//ControlProtocolClose(modbus_tcp_protocol);
|
||||
}
|
||||
}
|
||||
PRIV_SHELL_CMD_FUNCTION(ControlFx3uTest, Mitsubishi fx3u Demo, PRIV_SHELL_CMD_MAIN_ATTR);
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user