plc的modbusrtu链接
This commit is contained in:
1 parent
64ca79d1a3
commit
0f55442fc6
4 files changed
+101
-25
No files matched your search
+13
-14
@@ -65,23 +65,22 @@ static void mb_plc_read_regs(mb_inst_t *hinst)
|
||||
// 写入 Modbus
|
||||
mb_write_bits(hinst, 2256, DO_TABLE_SIZE, w_buffer);
|
||||
|
||||
|
||||
/* u16 regs[64];
|
||||
int addr = 7000;
|
||||
int nb = 10;
|
||||
int total = mb_write_regs(hinst, addr, nb, regs);
|
||||
if (total <= 0)
|
||||
//输入寄存器d0->6000 u16 r_regsD[AI_TABLE_SIZE];
|
||||
// 读 Modbus
|
||||
mb_read_regs(hinst, 6000, AI_TABLE_SIZE, r_regsD);
|
||||
for (int i = 0; i < AI_TABLE_SIZE; i++)
|
||||
{
|
||||
return;
|
||||
ai_table[i].current_Value = r_regsD[i];
|
||||
}
|
||||
u8 regsM[64];
|
||||
int addrM = 3000;
|
||||
int nbM = 10;
|
||||
int totalM = mb_read_bits(hinst, addrM, nbM, regsM);
|
||||
if (totalM <= 0)
|
||||
|
||||
//输出寄存器d100->6100
|
||||
u16 w_regsD [AO_TABLE_SIZE];
|
||||
for (int i = 0; i < AO_TABLE_SIZE; i++)
|
||||
{
|
||||
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)//线程服务函数
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
#include "SC828_DATA_table.h"
|
||||
#include "Variable.h"
|
||||
|
||||
#define DBG_TAG "data.table"
|
||||
#define DBG_LVL DBG_LOG
|
||||
#include <rtdbg.h>
|
||||
|
||||
// 系统数据状态表(表格)
|
||||
IO_State_DATA data_table[] = {
|
||||
{1001, 0, STR_COMM_COUNTER},
|
||||
@@ -19,7 +23,7 @@ IO_State_DATA data_table[] = {
|
||||
|
||||
// DIO状态表(表格)
|
||||
IO_State_DIO di_table[] = {
|
||||
{2001, 0, STR_EMERGENCY_STOP},
|
||||
{2001, 0, STR_EMERGENCY_STOP},
|
||||
{2002, 0, STR_TEMP_LOCK},
|
||||
{2003, 0, STR_PRESSURE_LOCK},
|
||||
{2004, 0, STR_CALL_CONFIRM},
|
||||
@@ -104,7 +108,7 @@ IO_State_DIO di_table[] = {
|
||||
};
|
||||
// --- DIO 输出(3001~3066)---
|
||||
IO_State_DIO do_table[] = {
|
||||
{3001, 1, STR_RED}, // 红
|
||||
{3001, 1, STR_RED}, // 红
|
||||
{3002, 0, STR_GREEN}, // 绿
|
||||
{3003, 0, STR_YELLOW}, // 黄
|
||||
{3004, 0, STR_ALARM}, // 警报
|
||||
@@ -173,7 +177,7 @@ IO_State_DIO do_table[] = {
|
||||
};
|
||||
|
||||
//AIO状态表
|
||||
IO_State_AIO aio_table[] = {
|
||||
IO_State_AIO ai_table[] = {
|
||||
// --- AIO 输入(4001~4022)---
|
||||
{4001, 0, STR_AIO_TEMP1},
|
||||
{4002, 0, STR_AIO_TEMP2},
|
||||
@@ -197,7 +201,9 @@ IO_State_AIO aio_table[] = {
|
||||
{4020, 0, STR_AIO_FABRIC_CYCLE5},
|
||||
{4021, 0, STR_AIO_FABRIC_CYCLE6},
|
||||
{4022, 0, STR_AIO_PH},
|
||||
};
|
||||
// --- AO 输出(5001~5014)---
|
||||
IO_State_AIO ao_table[] = {
|
||||
{5001, 0, STR_AIO_HEAT_RATIO},
|
||||
{5002, 0, STR_AIO_COOL_RATIO},
|
||||
{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 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]);
|
||||
|
||||
void DATA_Table_Init(){
|
||||
@@ -231,10 +238,15 @@ void DATA_Table_Init(){
|
||||
do_table[i].current_state = 0;
|
||||
}
|
||||
|
||||
for(int i=0;i<AIO_TABLE_SIZE;i++)
|
||||
for(int i=0;i<AI_TABLE_SIZE;i++)
|
||||
{
|
||||
aio_table[i].current_Value = 0;
|
||||
ai_table[i].current_Value = 0;
|
||||
}
|
||||
for(int i=0;i<AO_TABLE_SIZE;i++)
|
||||
{
|
||||
ao_table[i].current_Value = 0;
|
||||
}
|
||||
|
||||
for(int i=0;i<DATA_TABLE_SIZE;i++)
|
||||
{
|
||||
data_table[i].current_data = 0;
|
||||
@@ -354,3 +366,67 @@ void DATA_INSTRUCTION_STOP(){
|
||||
rt_snprintf(SIDS2, 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);
|
||||
@@ -14,13 +14,13 @@ typedef struct {
|
||||
|
||||
typedef struct {
|
||||
int pin; // PLCID
|
||||
unsigned char current_Value; // 当前状态
|
||||
rt_int16_t current_Value; // 当前状态
|
||||
str_id_t name; // 名称(可选,用于调试)
|
||||
} IO_State_AIO;
|
||||
|
||||
typedef struct {
|
||||
int pin; // PLCID
|
||||
int current_data; // 当前状态
|
||||
rt_int32_t current_data; // 当前状态
|
||||
str_id_t name; // 名称(可选,用于调试)
|
||||
} IO_State_DATA;
|
||||
|
||||
@@ -54,13 +54,15 @@ typedef struct {
|
||||
|
||||
extern IO_State_DIO di_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_STEP_DATA step_table[99];
|
||||
|
||||
extern const unsigned char DI_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;
|
||||
|
||||
void DATA_Table_Init(void);//
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
#include <guider/generated/widgets_init.h>
|
||||
#include "lvgl.h"
|
||||
|
||||
|
||||
#define DBG_TAG "gui"
|
||||
#define DBG_LVL DBG_LOG
|
||||
#include <rtdbg.h>
|
||||
|
||||
Reference in new issue
Block a user