【问题标题】:a for loop not executed in an operating system C未在操作系统中执行的 for 循环 C
【发布时间】:2017-06-30 07:44:09
【问题描述】:

我在执行 for 循环时遇到问题,我创建了一个包含已定义值的静态表,然后我将我的表作为参数传递给函数进行处理。 基本上我的代码如下所示:

#define ID_01 0x0000
#define ID_02 0x0001
#define ID_03 0x0002
#define ID_04 0x0003
#define ID_05 0x0004
#define ID_06 0x0005
#define ID_07 0x0006
#define ID_08 0x0007
#define ID_09 0x0008
/*...
*/
#define ID_LAST 0xFFFF
static char table[]={
ID_01, ID_02 ,ID_03, ID_04, .... , ID_LAST}

void process( char *table){

    int LastId=0; 
    char *Command; 

    for ( Command=table; LastId==0 ; Command++){
        switch(Command) 
        {
            case ID_01: 
                do_stuff01();
                break;
            case ID_02: 
                do_stuff02();
                break; 
            ...

            case ID_LAST: 
                LastId=1;
                break;
            default: 
                 break;
          } 
    }
}

我尝试打印一些消息进行调试,但程序不执行任何打印,即使是在 for 循环之前和之后的那些。

但是当我将 for 循环更改为:

for(i=0;i<10;i++)

所有消息都已打印。但我必须像一开始那样处理。

PS:这部分代码是在运行到微控制器的操作系统任务中执行的,我只是一个初学者。

【问题讨论】:

    标签: c operating-system microcontroller


    【解决方案1】:

    现在您正在使用switch (Command),其中Command 保存地址od table 变量。

    将switch 更改为

    switch (*Command) { //Use value at pointed Command.
    
    }
    

    请注意,在执行 *Command 时,您取消引用 char 这是 1 个字节。您的 ID 有 2 个字节,因此您丢失了数据。

    变化:

    static char table[] = {ID_01, ID_02 ,ID_03, ID_04, .... , ID_LAST}
    

    短到有 16 位值

    static unsigned short table[]={ID_01, ID_02 ,ID_03, ID_04, .... , ID_LAST}
    

    稍后,修改您的 process 函数以接受 unsigned short

    void process( const unsigned short *table) { //Unsigned short
        int LastId = 0; 
        unsigned short *Command;  //Unsigned short
    
        for ( Command=table; LastId==0 ; Command++){
            switch(*Command) { //Added star
                //...
            }
        }
        //...
    

    我会将您的 process 代码重写为:

    void process(const unsigned short *table, size_t tableLen) {
        while (tableLen--) {
            switch (*table) {
                case ID_1: /* Do stuff */ break;
            }
            table++; //Increase pointer to next ID element
        }
    }
    
    //Usage then like this:
    static unsigned short table[] = {ID_1, ID_2, ID_3, ..., ID_n};
    //Put pointer and length of table
    process(table, sizeof(table)/sizeof(table[0]));
    

    【讨论】:

    • 我的循环正在执行但table++没有工作,循环卡在第一个定义的值
    • 卡住是什么意思。你调试了吗?您是否涵盖了所有可能的情况和default 声明?
    • 是的,我涵盖了所有这些,并使用了默认值。我的程序在循环中只执行“*table”的第一项
    【解决方案2】:

    一般来说,它会像下面这样构造和映射 ID/FUNC。

    #include <stdio.h>
    
    #define ID_01 0x0000
    #define ID_02 0x0001
    /* ... */
    #define ID_LAST 0xFFFF
    
    typedef void (*func)();
    
    typedef struct {
      char n;
      func f;
    } fmap;
    
    void do_something01() { }
    void do_something02() { }
    /* ... */
    
    static fmap fmaps[] = {
      {ID_01, do_something01},
      {ID_02, do_something02},
      /* ... */
      {ID_LAST, NULL},
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-11
      • 2016-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多