Browse Source

modbus数据打包解包代码

master
sc 1 week ago
parent
commit
64ca79d1a3
  1. 4
      .config
  2. 2239
      .cproject
  3. 33
      applications/PLC_link.c
  4. 12
      applications/data/DATA_comm.c
  5. 19
      applications/data/SC828_DATA_table.c
  6. 9
      applications/data/SC828_DATA_table.h
  7. 40
      applications/data/Variable.c
  8. 2
      applications/data/Variable.h
  9. 1
      applications/lvgl/guider/custom/custom.c
  10. 1
      applications/lvgl/guider/generated/events_init.c
  11. 2
      applications/lvgl/guider/generated/gui_guider.c
  12. 12
      applications/lvgl/guider/generated/setup_scr_screen.c
  13. 53
      applications/lvgl/lv__user_gui.c
  14. 2
      packages/LVGL-v8.3.10/env_support/rt-thread/lv_rt_thread_port.c
  15. 4
      rtconfig.h

4
.config

@ -546,9 +546,9 @@ CONFIG_PKG_CJSON_VER="v1.7.17"
#
CONFIG_PKG_USING_LVGL=y
CONFIG_PKG_LVGL_PATH="/packages/multimedia/LVGL/LVGL"
CONFIG_PKG_LVGL_THREAD_PRIO=15
CONFIG_PKG_LVGL_THREAD_PRIO=25
CONFIG_PKG_LVGL_THREAD_STACK_SIZE=4096
CONFIG_PKG_LVGL_DISP_REFR_PERIOD=20
CONFIG_PKG_LVGL_DISP_REFR_PERIOD=50
# CONFIG_PKG_USING_LVGL_SQUARELINE is not set
# CONFIG_PKG_LVGL_USING_EXAMPLES is not set
# CONFIG_PKG_LVGL_USING_DEMOS is not set

2239
.cproject

File diff suppressed because it is too large

33
applications/PLC_link.c

@ -14,6 +14,7 @@
#include "rtthread.h"
#include "modbus.h"
#include "SC828_DATA_table.h"
#define DBG_TAG "plc.rtu"
#define DBG_LVL DBG_LOG
@ -37,7 +38,35 @@ static void mb_plc_read_regs(mb_inst_t *hinst)
return;
}
u16 regs[64];
//输入m0000->rtu2000
uint8_t r_buffer[(DI_TABLE_SIZE + 7) / 8];
//读取 Modbus bits
int mrx = mb_read_bits(hinst, 2000, DI_TABLE_SIZE, r_buffer);
if (mrx > 0)
{ //解包到数据表
for (int i = 0; i < DI_TABLE_SIZE; i++) {
di_table[i].current_state = (r_buffer[i / 8] >> (i % 8)) & 1;
}
}
// 输出 m0256 -> RTU 2256
uint8_t w_buffer[(DO_TABLE_SIZE + 7) / 8]; // 关键:初始化为0
// 打包 do_table 到 bit_buffer
for (int i = 0; i < DO_TABLE_SIZE; i++) {
const int byte_idx = i / 8;
const int bit_idx = i % 8;
const uint8_t bit_mask = (1U << bit_idx);
if (do_table[i].current_state) {
w_buffer[byte_idx] |= bit_mask;
} else {
w_buffer[byte_idx] &= ~bit_mask;
}
}
// 写入 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);
@ -52,7 +81,7 @@ static void mb_plc_read_regs(mb_inst_t *hinst)
if (totalM <= 0)
{
return;
}
}*/
}
static void mb_plc_thread(void *args)//线程服务函数

12
applications/data/DATA_comm.c

@ -457,7 +457,7 @@ void pasre_DAT(const char *api, const char *json_str)
}
else if (strcmp(api, "SC851") == 0)
{//创建数组
cJSON *Dev = cJSON_CreateArray();
/* cJSON *Dev = cJSON_CreateArray();
for (int i = 0; i < DIO_TABLE_SIZE; i++) {
cJSON* item = cJSON_CreateObject();
// 添加 IO 编号
@ -469,11 +469,11 @@ void pasre_DAT(const char *api, const char *json_str)
// 将当前项添加到数组中
cJSON_AddItemToArray(Dev, item);
}
cJSON_AddItemToObject(dat,"DIO",Dev);
cJSON_AddItemToObject(dat,"DIO",Dev);*/
}
else if (strcmp(api, "SC852") == 0)
{//创建数组
cJSON *Dev = cJSON_CreateArray();
/* cJSON *Dev = cJSON_CreateArray();
for (int i = 0; i < AIO_TABLE_SIZE; i++) {
cJSON* item = cJSON_CreateObject();
// 添加 IO 编号
@ -483,11 +483,11 @@ void pasre_DAT(const char *api, const char *json_str)
// 将当前项添加到数组中
cJSON_AddItemToArray(Dev, item);
}
cJSON_AddItemToObject(dat,"A",Dev);
cJSON_AddItemToObject(dat,"A",Dev);*/
}
else if (strcmp(api, "SC853") == 0)
{//创建数组
cJSON *Dev = cJSON_CreateArray();
/* cJSON *Dev = cJSON_CreateArray();
for (int i = 0; i < DATA_TABLE_SIZE; i++) {
cJSON* item = cJSON_CreateObject();
// 添加 IO 编号
@ -497,7 +497,7 @@ void pasre_DAT(const char *api, const char *json_str)
// 将当前项添加到数组中
cJSON_AddItemToArray(Dev, item);
}
cJSON_AddItemToObject(dat,"M",Dev);
cJSON_AddItemToObject(dat,"M",Dev);*/
}
else if (strcmp(api, "SC854") == 0) {

19
applications/data/SC828_DATA_table.c

@ -1,5 +1,4 @@
#include "SC828_DATA_table.h"
#include <rtthread.h>
#include "Variable.h"
// 系统数据状态表(表格)
@ -19,7 +18,7 @@ IO_State_DATA data_table[] = {
};
// DIO状态表(表格)
IO_State_DIO dio_table[] = {
IO_State_DIO di_table[] = {
{2001, 0, STR_EMERGENCY_STOP},
{2002, 0, STR_TEMP_LOCK},
{2003, 0, STR_PRESSURE_LOCK},
@ -102,8 +101,10 @@ IO_State_DIO dio_table[] = {
{2080, 0, STR_MATERIAL3_FILL},
{2081, 0, STR_MATERIAL3_HEAT},
{2082, 0, STR_MATERIAL3_STIR},
};
// --- DIO 输出(3001~3066)---
{3001, 0, STR_RED}, // 红
IO_State_DIO do_table[] = {
{3001, 1, STR_RED}, // 红
{3002, 0, STR_GREEN}, // 绿
{3003, 0, STR_YELLOW}, // 黄
{3004, 0, STR_ALARM}, // 警报
@ -215,15 +216,21 @@ IO_State_AIO aio_table[] = {
//步骤数据表最大99
IO_STEP_DATA step_table[99];
const unsigned char DIO_TABLE_SIZE = sizeof(dio_table) / sizeof(dio_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 AIO_TABLE_SIZE = sizeof(aio_table) / sizeof(aio_table[0]);
const unsigned char DATA_TABLE_SIZE = sizeof(data_table) / sizeof(data_table[0]);
void DATA_Table_Init(){
for(int i=0;i<DIO_TABLE_SIZE;i++)
for(int i=0;i<DI_TABLE_SIZE;i++)
{
dio_table[i].current_state = 0;
di_table[i].current_state = 0;
}
for(int i=0;i<DO_TABLE_SIZE;i++)
{
do_table[i].current_state = 0;
}
for(int i=0;i<AIO_TABLE_SIZE;i++)
{
aio_table[i].current_Value = 0;

9
applications/data/SC828_DATA_table.h

@ -1,13 +1,14 @@
#ifndef APPLICATIONS_SC828_DATA_Table_H
#define APPLICATIONS_SC828_DATA_Table_H
#include <rtthread.h>
#include "stdbool.h"
#include "language.h"
// IO状态结构体(可扩展)
typedef struct {
int pin; // PLCID
unsigned char current_state; // 当前电平:0=低,1=高
rt_bool_t current_state; // 当前电平:0=低,1=高
str_id_t name; // 名称(可选,用于调试)
} IO_State_DIO;
@ -51,12 +52,14 @@ typedef struct {
double Parameter5_S3;
} IO_STEP_DATA;
extern IO_State_DIO dio_table[];
extern IO_State_DIO di_table[];
extern IO_State_DIO do_table[];
extern IO_State_AIO aio_table[];
extern IO_State_DATA data_table[];
extern IO_STEP_DATA step_table[99];
extern const unsigned char DIO_TABLE_SIZE;
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 DATA_TABLE_SIZE;

40
applications/data/Variable.c

@ -6,8 +6,8 @@
rt_int32_t sys_temperature;//主机环境温度
rt_int32_t sys_humidity;//主机环境湿度
char sys_var[8];//主机版本0.0.00A
unsigned char sys_time[6];//系统时间
unsigned int sys_run_time;//运行时间
char sys_time[9]="00:00:00";//系统时间
unsigned int sys_run_time=0;//运行时间
int pow_bat;//电池电压
int pow_code;//主电压
int pow_dev1;//设备电压.通讯
@ -28,7 +28,7 @@ char MACHINE_CALL = 0;
char MACHINE_USER = 0;
// 字符串变量(RAM 中可修改)
char Work[25];
char Work[25]="SC----------";
char Dye[25];
char Process[50];
char Message[40];
@ -42,25 +42,25 @@ unsigned int RUN = 0;
unsigned int STEPID = 0;
// 浮点变量
float MTT = 0.0f;
float MTL = 0.0f;
float MTH = 6.0f; // 默认目标温度
float MST = 0.0f;
float MUT = 0.0f;
float STTA = 0.0f;
float STLA = 0.0f;
float STTB = 0.0f;
float STLB = 0.0f;
float STTC = 0.0f;
float STLC = 0.0f;
float MTT = 0.0f;//主缸温度
float MTL = 0.0f;//主缸水位
float MTH = 6.0f;//主缸ph
float MST = 0.0f;//目标温度
float MUT = 0.0f;//主缸温度2
float STTA = 0.0f;//料a温度
float STLA = 0.0f;//料a水位
float STTB = 0.0f;//料b温度
float STLB = 0.0f;//料b水位
float STTC = 0.0f;//料c温度
float STLC = 0.0f;//料c水位
// 控制输出
unsigned int Pump = 0;
unsigned int TC = 0;
unsigned int Fan = 0;
unsigned int Pull = 0;
unsigned int Swing = 0;
unsigned int STA = 0;
unsigned int Pump = 0;//主泵
unsigned int TC = 0;//温度比例
unsigned int Fan = 0;//风机
unsigned int Pull = 0;//提布
unsigned int Swing = 0;//摆布
unsigned int STA = 0;//料a比例
unsigned int STB = 0;
unsigned int STC = 0;
unsigned int Time = 0;

2
applications/data/Variable.h

@ -7,7 +7,7 @@
extern rt_int32_t sys_temperature;//主机环境温度
extern rt_int32_t sys_humidity;//主机环境湿度
extern char sys_var[8];//主机版本0.0.00A
extern unsigned char sys_time[6];//系统时间
extern char sys_time[9];//系统时间
extern unsigned int sys_run_time;//运行时间
extern int pow_bat;//电池电压
extern int pow_code;//主电压

1
applications/lvgl/guider/custom/custom.c

@ -15,6 +15,7 @@
*********************/
#include <stdio.h>
#include "lvgl.h"
#include "custom.h"
/*********************
* DEFINES

1
applications/lvgl/guider/generated/events_init.c

@ -11,6 +11,7 @@
#include <stdio.h>
#include "lvgl.h"
#include "gui_guider.h"
#if LV_USE_GUIDER_SIMULATOR && LV_USE_FREEMASTER
#include "freemaster_client.h"

2
applications/lvgl/guider/generated/gui_guider.c

@ -13,6 +13,8 @@
#include "lvgl.h"
#include <stdio.h>
#include "gui_guider.h"
#include "widgets_init.h"
#if LV_USE_GUIDER_SIMULATOR && LV_USE_FREEMASTER
#include "gg_external_data.h"

12
applications/lvgl/guider/generated/setup_scr_screen.c

@ -14,6 +14,8 @@
#include "lvgl.h"
#include <stdio.h>
#include "widgets_init.h"
#include "gui_guider.h"
void setup_scr_screen(lv_ui *ui)
{
@ -138,7 +140,7 @@ void setup_scr_screen(lv_ui *ui)
lv_label_set_text(ui->screen_temp, "120.5");
lv_label_set_long_mode(ui->screen_temp, LV_LABEL_LONG_CLIP);
lv_obj_set_pos(ui->screen_temp, 50, 10);
lv_obj_set_size(ui->screen_temp, 70, 25);
lv_obj_set_size(ui->screen_temp, 75, 25);
//Write style for screen_temp, Part: LV_PART_MAIN, State: LV_STATE_DEFAULT.
lv_obj_set_style_border_width(ui->screen_temp, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
@ -190,7 +192,7 @@ void setup_scr_screen(lv_ui *ui)
lv_obj_set_style_border_width(ui->screen_order, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
lv_obj_set_style_radius(ui->screen_order, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
lv_obj_set_style_text_color(ui->screen_order, lv_color_hex(0x000000), LV_PART_MAIN|LV_STATE_DEFAULT);
lv_obj_set_style_text_font(ui->screen_order, &lv_font_simsun_32, LV_PART_MAIN|LV_STATE_DEFAULT);
lv_obj_set_style_text_font(ui->screen_order, &lv_font_simsun_30, LV_PART_MAIN|LV_STATE_DEFAULT);
lv_obj_set_style_text_opa(ui->screen_order, 255, LV_PART_MAIN|LV_STATE_DEFAULT);
lv_obj_set_style_text_letter_space(ui->screen_order, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
lv_obj_set_style_text_line_space(ui->screen_order, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
@ -5805,7 +5807,7 @@ void setup_scr_screen(lv_ui *ui)
lv_textarea_set_accepted_chars(ui->screen_pname_set, "");
lv_textarea_set_max_length(ui->screen_pname_set, 32);
#if LV_USE_KEYBOARD != 0 || LV_USE_ZH_KEYBOARD != 0
// lv_obj_add_event_cb(ui->screen_pname_set, ta_event_cb, LV_EVENT_ALL, ui->g_kb_top_layer);
lv_obj_add_event_cb(ui->screen_pname_set, ta_event_cb, LV_EVENT_ALL, ui->g_kb_top_layer);
#endif
lv_obj_set_pos(ui->screen_pname_set, 305, -1);
lv_obj_set_size(ui->screen_pname_set, 679, 50);
@ -5965,7 +5967,7 @@ void setup_scr_screen(lv_ui *ui)
lv_textarea_set_accepted_chars(ui->screen_machine_name, "");
lv_textarea_set_max_length(ui->screen_machine_name, 32);
#if LV_USE_KEYBOARD != 0 || LV_USE_ZH_KEYBOARD != 0
//lv_obj_add_event_cb(ui->screen_machine_name, ta_event_cb, LV_EVENT_ALL, ui->g_kb_top_layer);
lv_obj_add_event_cb(ui->screen_machine_name, ta_event_cb, LV_EVENT_ALL, ui->g_kb_top_layer);
#endif
lv_obj_set_pos(ui->screen_machine_name, 150, 70);
lv_obj_set_size(ui->screen_machine_name, 200, 50);
@ -6051,7 +6053,7 @@ void setup_scr_screen(lv_ui *ui)
lv_textarea_set_accepted_chars(ui->screen_machine_id, "");
lv_textarea_set_max_length(ui->screen_machine_id, 32);
#if LV_USE_KEYBOARD != 0 || LV_USE_ZH_KEYBOARD != 0
//lv_obj_add_event_cb(ui->screen_machine_id, ta_event_cb, LV_EVENT_ALL, ui->g_kb_top_layer);
lv_obj_add_event_cb(ui->screen_machine_id, ta_event_cb, LV_EVENT_ALL, ui->g_kb_top_layer);
#endif
lv_obj_set_pos(ui->screen_machine_id, 150, 10);
lv_obj_set_size(ui->screen_machine_id, 200, 50);

53
applications/lvgl/lv__user_gui.c

@ -9,12 +9,16 @@
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <applications/data/Variable.h>
#include <board.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <guider/custom/custom.h>
#include <guider/generated/events_init.h>
#include <guider/generated/gui_guider.h>
#include <guider/generated/widgets_init.h>
#include "lvgl.h"
//#include "lv_demo_widgets.h"
#define DBG_TAG "gui"
@ -24,11 +28,54 @@
#define LCD_BL_PIN GET_PIN(D,12)
#define LCD_RST_PIN GET_PIN(D,11)
void setup_user_screen(lv_ui *ui)
{
lv_obj_clear_flag(lv_tabview_get_content(ui->screen_tabview), LV_OBJ_FLAG_SCROLLABLE);//禁止左右滑动
lv_textarea_set_text(ui->screen_machine_id, machine_ID);//设备id
lv_textarea_set_text(ui->screen_machine_name, machine_name);//设备名
}
time_t nows;
char time_str[9]; //"0000:00\0"
char temp_str[6]; //"120.5"
char wter_str[6]; //"120.5"
void setup_s_screen(lv_ui *ui)
{
//系统时间
time(&nows);
struct tm *t = localtime(&nows);
snprintf(sys_time, sizeof(sys_time), "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec);
lv_label_set_text(ui->screen_SYS_TIME, (const char *)sys_time);//系统时间
//运行时间
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-truncation"
snprintf(time_str, sizeof(time_str), "%04d:%02d", sys_run_time / 60, sys_run_time % 60);
#pragma GCC diagnostic pop
lv_label_set_text(ui->screen_RUN_TIME, (const char *)time_str);//显示运行时间
//工单
lv_label_set_text(ui->screen_order, (const char *)Work);
//水位
snprintf(wter_str, sizeof(wter_str), "%04.0f", MTL);
lv_label_set_text(ui->screen_water, (const char *)wter_str);
//温度
snprintf(temp_str, sizeof(temp_str), "%05.1f", MTT);
lv_label_set_text(ui->screen_temp, (const char *)temp_str);
}
static void time_update_timer_cb(lv_timer_t * timer)
{
setup_s_screen(&guider_ui);
}
void lv_user_gui_init(void)
{
//gui_guider_setup();
//lv_demo_widgets();
setup_ui(&guider_ui);
events_init(&guider_ui);
custom_init(&guider_ui);
setup_user_screen(&guider_ui);
// 创建一个每 500ms 触发一次的定时器
lv_timer_create(time_update_timer_cb, 500, NULL);
}

2
packages/LVGL-v8.3.10/env_support/rt-thread/lv_rt_thread_port.c

@ -69,7 +69,7 @@ static int lvgl_thread_init(void)
rt_err_t err;
err = rt_thread_init(&lvgl_thread, "LVGL", lvgl_thread_entry, RT_NULL,
&lvgl_thread_stack[0], sizeof(lvgl_thread_stack), PKG_LVGL_THREAD_PRIO, 10);
&lvgl_thread_stack[0], sizeof(lvgl_thread_stack), PKG_LVGL_THREAD_PRIO, 20);
if(err != RT_EOK)
{
LOG_E("Failed to create LVGL thread");

4
rtconfig.h

@ -263,9 +263,9 @@
/* LVGL: powerful and easy-to-use embedded GUI library */
#define PKG_USING_LVGL
#define PKG_LVGL_THREAD_PRIO 15
#define PKG_LVGL_THREAD_PRIO 25
#define PKG_LVGL_THREAD_STACK_SIZE 4096
#define PKG_LVGL_DISP_REFR_PERIOD 20
#define PKG_LVGL_DISP_REFR_PERIOD 50
#define PKG_LVGL_USING_V080310
#define PKG_LVGL_VER_NUM 0x080310
/* end of LVGL: powerful and easy-to-use embedded GUI library */

Loading…
Cancel
Save