Browse Source

plc的modbusrtu链接

master
sc 1 week ago
parent
commit
0f55442fc6
  1. 27
      applications/PLC_link.c
  2. 84
      applications/data/SC828_DATA_table.c
  3. 10
      applications/data/SC828_DATA_table.h
  4. 1
      applications/lvgl/lv__user_gui.c

27
applications/PLC_link.c

@ -65,23 +65,22 @@ static void mb_plc_read_regs(mb_inst_t *hinst)
// 写入 Modbus // 写入 Modbus
mb_write_bits(hinst, 2256, DO_TABLE_SIZE, w_buffer); mb_write_bits(hinst, 2256, DO_TABLE_SIZE, w_buffer);
//输入寄存器d0->6000 u16 r_regsD[AI_TABLE_SIZE];
/* u16 regs[64]; // 读 Modbus
int addr = 7000; mb_read_regs(hinst, 6000, AI_TABLE_SIZE, r_regsD);
int nb = 10; for (int i = 0; i < AI_TABLE_SIZE; i++)
int total = mb_write_regs(hinst, addr, nb, regs);
if (total <= 0)
{ {
return; ai_table[i].current_Value = r_regsD[i];
} }
u8 regsM[64];
int addrM = 3000; //输出寄存器d100->6100
int nbM = 10; u16 w_regsD [AO_TABLE_SIZE];
int totalM = mb_read_bits(hinst, addrM, nbM, regsM); for (int i = 0; i < AO_TABLE_SIZE; i++)
if (totalM <= 0)
{ {
return; w_regsD [i] = ao_table[i].current_Value;
}*/ }
// 写入 Modbus
mb_write_regs(hinst, 6100, AO_TABLE_SIZE, w_regsD);
} }
static void mb_plc_thread(void *args)//线程服务函数 static void mb_plc_thread(void *args)//线程服务函数

84
applications/data/SC828_DATA_table.c

@ -1,6 +1,10 @@
#include "SC828_DATA_table.h" #include "SC828_DATA_table.h"
#include "Variable.h" #include "Variable.h"
#define DBG_TAG "data.table"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
// 系统数据状态表(表格) // 系统数据状态表(表格)
IO_State_DATA data_table[] = { IO_State_DATA data_table[] = {
{1001, 0, STR_COMM_COUNTER}, {1001, 0, STR_COMM_COUNTER},
@ -173,7 +177,7 @@ IO_State_DIO do_table[] = {
}; };
//AIO状态表 //AIO状态表
IO_State_AIO aio_table[] = { IO_State_AIO ai_table[] = {
// --- AIO 输入(4001~4022)--- // --- AIO 输入(4001~4022)---
{4001, 0, STR_AIO_TEMP1}, {4001, 0, STR_AIO_TEMP1},
{4002, 0, STR_AIO_TEMP2}, {4002, 0, STR_AIO_TEMP2},
@ -197,7 +201,9 @@ IO_State_AIO aio_table[] = {
{4020, 0, STR_AIO_FABRIC_CYCLE5}, {4020, 0, STR_AIO_FABRIC_CYCLE5},
{4021, 0, STR_AIO_FABRIC_CYCLE6}, {4021, 0, STR_AIO_FABRIC_CYCLE6},
{4022, 0, STR_AIO_PH}, {4022, 0, STR_AIO_PH},
};
// --- AO 输出(5001~5014)--- // --- AO 输出(5001~5014)---
IO_State_AIO ao_table[] = {
{5001, 0, STR_AIO_HEAT_RATIO}, {5001, 0, STR_AIO_HEAT_RATIO},
{5002, 0, STR_AIO_COOL_RATIO}, {5002, 0, STR_AIO_COOL_RATIO},
{5003, 0, STR_AIO_MAIN_PUMP_SPEED}, {5003, 0, STR_AIO_MAIN_PUMP_SPEED},
@ -218,7 +224,8 @@ IO_STEP_DATA step_table[99];
const unsigned char DI_TABLE_SIZE = sizeof(di_table) / sizeof(di_table[0]); const unsigned char DI_TABLE_SIZE = sizeof(di_table) / sizeof(di_table[0]);
const unsigned char DO_TABLE_SIZE = sizeof(do_table) / sizeof(do_table[0]); const unsigned char DO_TABLE_SIZE = sizeof(do_table) / sizeof(do_table[0]);
const unsigned char AIO_TABLE_SIZE = sizeof(aio_table) / sizeof(aio_table[0]); const unsigned char AI_TABLE_SIZE = sizeof(ai_table) / sizeof(ai_table[0]);
const unsigned char AO_TABLE_SIZE = sizeof(ao_table) / sizeof(ao_table[0]);
const unsigned char DATA_TABLE_SIZE = sizeof(data_table) / sizeof(data_table[0]); const unsigned char DATA_TABLE_SIZE = sizeof(data_table) / sizeof(data_table[0]);
void DATA_Table_Init(){ void DATA_Table_Init(){
@ -231,10 +238,15 @@ void DATA_Table_Init(){
do_table[i].current_state = 0; do_table[i].current_state = 0;
} }
for(int i=0;i<AIO_TABLE_SIZE;i++) for(int i=0;i<AI_TABLE_SIZE;i++)
{
ai_table[i].current_Value = 0;
}
for(int i=0;i<AO_TABLE_SIZE;i++)
{ {
aio_table[i].current_Value = 0; ao_table[i].current_Value = 0;
} }
for(int i=0;i<DATA_TABLE_SIZE;i++) for(int i=0;i<DATA_TABLE_SIZE;i++)
{ {
data_table[i].current_data = 0; data_table[i].current_data = 0;
@ -354,3 +366,67 @@ void DATA_INSTRUCTION_STOP(){
rt_snprintf(SIDS2, 3,"000"); rt_snprintf(SIDS2, 3,"000");
rt_snprintf(SIDS3, 3,"000"); rt_snprintf(SIDS3, 3,"000");
} }
/**
* pin current_Value
* @param pin: PLC 4001
* @return: current_Value INT16_MIN
*/
rt_int16_t get_ai_value(int pin)
{
for (int i = 0; i < AI_TABLE_SIZE; i++) {
if (ai_table[i].pin == pin) {
return ai_table[i].current_Value;
}
}
return 0; // 表示未找到
}
void get_ao_value(int pin,int16_t dat)
{
for (int i = 0; i < AO_TABLE_SIZE; i++) {
if (ao_table[i].pin == pin) {
ao_table[i].current_Value = dat;
}
}
}
void table_thread_entry(void *parameter)
{
while (1)
{
rt_thread_delay(1000);
MTT = get_ai_value(4001)/10.0f;
MUT = get_ai_value(4002)/10.0f;
STTA = get_ai_value(4003)/10.0f;
STTB = get_ai_value(4004)/10.0f;
STTC = get_ai_value(4005)/10.0f;
MTL = get_ai_value(4007);
STLA = get_ai_value(4008);
STLB = get_ai_value(4009);
STLC = get_ai_value(4010);
MTH = get_ai_value(4022);
}
}
extern struct rt_memheap sram_DTCMRAM;
static struct rt_thread dat_table_thread;
static void *dat_comm_stack __attribute__((aligned(4)))= RT_NULL ;
int data_table_th(void)
{
rt_err_t dat_err;
dat_comm_stack = rt_memheap_alloc(&sram_DTCMRAM, 1024);
dat_err = rt_thread_init(&dat_table_thread, "dat_tab", table_thread_entry, RT_NULL,dat_comm_stack, 1024, 10, 10);
if(dat_err != RT_EOK)
{
LOG_E("Failed to create data table thread!");
return -1;
}
rt_thread_startup(&dat_table_thread);
return 0;
}
INIT_APP_EXPORT(data_table_th);

10
applications/data/SC828_DATA_table.h

@ -14,13 +14,13 @@ typedef struct {
typedef struct { typedef struct {
int pin; // PLCID int pin; // PLCID
unsigned char current_Value; // 当前状态 rt_int16_t current_Value; // 当前状态
str_id_t name; // 名称(可选,用于调试) str_id_t name; // 名称(可选,用于调试)
} IO_State_AIO; } IO_State_AIO;
typedef struct { typedef struct {
int pin; // PLCID int pin; // PLCID
int current_data; // 当前状态 rt_int32_t current_data; // 当前状态
str_id_t name; // 名称(可选,用于调试) str_id_t name; // 名称(可选,用于调试)
} IO_State_DATA; } IO_State_DATA;
@ -54,13 +54,15 @@ typedef struct {
extern IO_State_DIO di_table[]; extern IO_State_DIO di_table[];
extern IO_State_DIO do_table[]; extern IO_State_DIO do_table[];
extern IO_State_AIO aio_table[]; extern IO_State_AIO ai_table[];
extern IO_State_AIO ao_table[];
extern IO_State_DATA data_table[]; extern IO_State_DATA data_table[];
extern IO_STEP_DATA step_table[99]; extern IO_STEP_DATA step_table[99];
extern const unsigned char DI_TABLE_SIZE; extern const unsigned char DI_TABLE_SIZE;
extern const unsigned char DO_TABLE_SIZE; extern const unsigned char DO_TABLE_SIZE;
extern const unsigned char AIO_TABLE_SIZE; extern const unsigned char AI_TABLE_SIZE;
extern const unsigned char AO_TABLE_SIZE;
extern const unsigned char DATA_TABLE_SIZE; extern const unsigned char DATA_TABLE_SIZE;
void DATA_Table_Init(void);// void DATA_Table_Init(void);//

1
applications/lvgl/lv__user_gui.c

@ -20,7 +20,6 @@
#include <guider/generated/widgets_init.h> #include <guider/generated/widgets_init.h>
#include "lvgl.h" #include "lvgl.h"
#define DBG_TAG "gui" #define DBG_TAG "gui"
#define DBG_LVL DBG_LOG #define DBG_LVL DBG_LOG
#include <rtdbg.h> #include <rtdbg.h>

Loading…
Cancel
Save