OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 5250|回复: 4

主FreeModbus_Slave-Master-RTT-STM32

[复制链接]

32

主题

138

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1757
金钱
1757
注册时间
2018-4-28
在线时间
242 小时
发表于 2019-6-15 13:37:16 | 显示全部楼层 |阅读模式
1金钱
#include "app_task.h"


#ifdef __CC_ARM
extern int Image$$RW_IRAM1$$ZI$$Limit;
#elif __ICCARM__
#pragma section="HEAP"
#else
extern int __bss_end;
#endif


uint8_t CpuUsageMajor, CpuUsageMinor; //CPU使用率
USHORT  usModbusUserData[MB_PDU_SIZE_MAX];
UCHAR   ucModbusUserData[MB_PDU_SIZE_MAX];
//====================操作系统各线程优先级==================================
#define thread_SysMonitor_Prio                            11
#define thread_ModbusSlavePoll_Prio              10
#define thread_ModbusMasterPoll_Prio               9
ALIGN(RT_ALIGN_SIZE)
//====================操作系统各线程堆栈====================================
static rt_uint8_t thread_SysMonitor_stack[256];
static rt_uint8_t thread_ModbusSlavePoll_stack[512];
static rt_uint8_t thread_ModbusMasterPoll_stack[512];


struct rt_thread thread_SysMonitor;
struct rt_thread thread_ModbusSlavePoll;
struct rt_thread thread_ModbusMasterPoll;


//***************************系统监控线程***************************
//函数定义: void thread_entry_SysRunLed(void* parameter)
//入口参数:无
//出口参数:无
//备    注:Editor:Armink   2013-08-02   Company: BXXJS
//******************************************************************
void thread_entry_SysMonitor(void* parameter)
{
        eMBMasterReqErrCode    errorCode = MB_MRE_NO_ERR;
        uint16_t errorCount = 0;
        while (1)
        {
                cpu_usage_get(&CpuUsageMajor, &CpuUsageMinor);
                usSRegHoldBuf[S_HD_CPU_USAGE_MAJOR] = CpuUsageMajor;
                usSRegHoldBuf[S_HD_CPU_USAGE_MINOR] = CpuUsageMinor;
                LED_LED1_ON;
                LED_LED2_ON;
                rt_thread_delay(DELAY_SYS_RUN_LED);
                LED_LED1_OFF;
                LED_LED2_OFF;
                rt_thread_delay(DELAY_SYS_RUN_LED);
                IWDG_Feed(); //feed the dog
                //Test Modbus Master
                usModbusUserData[0] = (USHORT)(rt_tick_get()/10);
                usModbusUserData[1] = (USHORT)(rt_tick_get()%10);
                ucModbusUserData[0] = 0x1F;
//                errorCode = eMBMasterReqReadDiscreteInputs(1,3,8,RT_WAITING_FOREVER);
//                errorCode = eMBMasterReqWriteMultipleCoils(1,3,5,ucModbusUserData,RT_WAITING_FOREVER);
                errorCode = eMBMasterReqWriteCoil(1,8,0xFF00,RT_WAITING_FOREVER);
//                errorCode = eMBMasterReqReadCoils(1,3,8,RT_WAITING_FOREVER);
//                errorCode = eMBMasterReqReadInputRegister(1,3,2,RT_WAITING_FOREVER);
//                errorCode = eMBMasterReqWriteHoldingRegister(1,3,usModbusUserData[0],RT_WAITING_FOREVER);
//                errorCode = eMBMasterReqWriteMultipleHoldingRegister(1,3,2,usModbusUserData,RT_WAITING_FOREVER);
//                errorCode = eMBMasterReqReadHoldingRegister(1,3,2,RT_WAITING_FOREVER);
//                errorCode = eMBMasterReqReadWriteMultipleHoldingRegister(1,3,2,usModbusUserData,5,2,RT_WAITING_FOREVER);
                //记录出错次数
                if (errorCode != MB_MRE_NO_ERR) {
                        errorCount++;
                }
        }
}


//************************ Modbus从机轮训线程***************************
//函数定义: void thread_entry_ModbusSlavePoll(void* parameter)
//入口参数:无
//出口参数:无
//备    注:Editor:Armink   2013-08-02    Company: BXXJS
//******************************************************************
void thread_entry_ModbusSlavePoll(void* parameter)
{
        eMBInit(MB_RTU, 0x01, 1, 115200,  MB_PAR_EVEN);
        eMBEnable();
        while (1)
        {
                eMBPoll();
        }
}


//************************ Modbus主机轮训线程***************************
//函数定义: void thread_entry_ModbusMasterPoll(void* parameter)
//入口参数:无
//出口参数:无
//备    注:Editor:Armink   2013-08-28    Company: BXXJS
//******************************************************************
void thread_entry_ModbusMasterPoll(void* parameter)
{
        eMBMasterInit(MB_RTU, 2, 115200,  MB_PAR_EVEN);
        eMBMasterEnable();
        while (1)
        {
                eMBMasterPoll();
        }
}


//**********************系统初始化函数********************************
//函数定义: int rt_application_init(void)
//入口参数:无
//出口参数:无
//备    注:Editor:Liuqiuhu   2013-1-31   Company: BXXJS
//********************************************************************
int rt_application_init(void)
{
        rt_thread_init(&thread_SysMonitor, "SysMonitor", thread_entry_SysMonitor,
                        RT_NULL, thread_SysMonitor_stack, sizeof(thread_SysMonitor_stack),
                        thread_SysMonitor_Prio, 5);
        rt_thread_startup(&thread_SysMonitor);


        rt_thread_init(&thread_ModbusSlavePoll, "MBSlavePoll",
                        thread_entry_ModbusSlavePoll, RT_NULL, thread_ModbusSlavePoll_stack,
                        sizeof(thread_ModbusSlavePoll_stack), thread_ModbusSlavePoll_Prio,
                        5);
        rt_thread_startup(&thread_ModbusSlavePoll);


        rt_thread_init(&thread_ModbusMasterPoll, "MBMasterPoll",
                        thread_entry_ModbusMasterPoll, RT_NULL, thread_ModbusMasterPoll_stack,
                        sizeof(thread_ModbusMasterPoll_stack), thread_ModbusMasterPoll_Prio,
                        5);
        rt_thread_startup(&thread_ModbusMasterPoll);


        return 0;
}


//**************************初始化RT-Thread函数*************************************
//函数定义: void rtthread_startup(void)
//入口参数:无
//出口参数:无
//备    注:Editor:Armink 2011-04-04    Company: BXXJS
//**********************************************************************************
void rtthread_startup(void)
{
        /* init board */
        rt_hw_board_init();


        /* show version */
        rt_show_version();


        /* init tick */
        rt_system_tick_init();


        /* init kernel object */
        rt_system_object_init();


        /* init timer system */
        rt_system_timer_init();


#ifdef RT_USING_HEAP
#ifdef __CC_ARM
        rt_system_heap_init((void*)&Image$$RW_IRAM1$$ZI$$Limit, (void*)STM32_SRAM_END);
#elif __ICCARM__
        rt_system_heap_init(__segment_end("HEAP"), (void*)STM32_SRAM_END);
#else
        /* init memory system */
        rt_system_heap_init((void*)&__bss_end, (void*)STM32_SRAM_END);
#endif
#endif


        /* init scheduler system */
        rt_system_scheduler_init();


        /* init all device */
        rt_device_init_all();


        /* init application */
        rt_application_init();


#ifdef RT_USING_FINSH
        /* init finsh */
        finsh_system_init();
        finsh_set_device("uart1");
#endif


        /* init timer thread */
        rt_system_timer_thread_init();


        /* init idle thread */
        rt_thread_idle_init();


        /* Add CPU usage to system */
        cpu_usage_init();


        /* start scheduler */
        rt_system_scheduler_start();


        /* never reach here */
        return;
}
移植到H7

QQ图片20190615132954.png
QQ图片20190615133034.png
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165371
金钱
165371
注册时间
2010-12-1
在线时间
2110 小时
发表于 2019-6-16 02:05:59 | 显示全部楼层
回复

使用道具 举报

1

主题

7

帖子

0

精华

新手上路

积分
26
金钱
26
注册时间
2019-7-10
在线时间
3 小时
发表于 2019-7-16 09:13:20 | 显示全部楼层
大神,你好!最近在学这个,可以分享一下源码吗~
回复

使用道具 举报

0

主题

24

帖子

0

精华

初级会员

Rank: 2

积分
113
金钱
113
注册时间
2019-12-25
在线时间
25 小时
发表于 2019-12-26 10:08:12 | 显示全部楼层
学习了
回复

使用道具 举报

1

主题

18

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
272
金钱
272
注册时间
2019-7-29
在线时间
102 小时
发表于 2021-12-7 10:20:10 | 显示全部楼层
这不是github上的那个吗
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-2-26 09:46

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表