通过嵌入式计算机中的RS232处理ASCII命令 [英] Processing ASCII commands via RS232 in embedded c

查看:67
本文介绍了通过嵌入式计算机中的RS232处理ASCII命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的微控制器有一个RS232接口,带有一组预定义的命令.有几百个命令,例如 PRESSURE 50 可以将压力设置为50; LIMIT 60 可以设置一些限制为60.

I have an RS232 interface to my microcontroller with a predefined set of commands. There are several hundred commands, e.g. PRESSURE 50 could be set pressure to 50; LIMIT 60 could be set some limit to 60.

当前,这是使用一系列嵌套的switch语句来处理的,但这似乎是一种不太好的解决方案.

Current, this is processed using a series of nested switch statements but this seems like an inelegant solution.

处理这些命令的推荐/最有效/最易读的方式是什么?LUT是最整齐的,但对于不存在的字母组合肯定会有很多空白条目(不理想.).

What would be the recommended / most efficient / most readable way to process these commands? A LUT would be the tidiest, but would surely have a lot of empty entries for letter combinations which don't exist (not ideal..).

感谢任何想法.

推荐答案

  1. 运行时建议:

  1. Run-time proposal:

具有struct {字符串,函数指针)的排序数组

With a sorted array of struct { string, function pointer )

我希望在您的平台( stdlib.h )

设计时建议:

正如评论所建议的,最好在设计时对数组初始化的条目进行排序.存在很多方法,从代码生成(豪华)到宏处理(差).

As comments suggest, it's better to sort the entries of the array initialization at design-time. A lot of ways exists, from code generation (luxurious) to macro processing (poor).

声明的建议,一分为多:

Suggestion of declaration, one into many:

typedef unsigned char byte;

typedef char commandId_t[40];

typedef void (*commandHandler_t)( const byte * payload );

typedef struct command_s {
   const commandId_t      commandId;
   const commandHandler_t handler;
} command_t;

static const command_t commands[] = {
#include "sortedCommandInitializers"
};

文件 sortedCommandInitializers {"command-name",function-name} 的列表,可以通过命令 sort CommandInitializers.c对其进行排序.>Makefile中的sortedCommandInitializers .

The file sortedCommandInitializers is a list of {"command-name", function-name}, which may be sorted by the command sort CommandInitializers.c > sortedCommandInitializers in the Makefile.

对于 make 来说,它与其他任何依赖项一样:

For make it's a dependencies like any other:

sortedCommandInitializers: CommandInitializers.c
    sort CommandInitializers.c > sortedCommandInitializers

myProgram.o: myProgram.c myProgram.h sortedCommandInitializers

这篇关于通过嵌入式计算机中的RS232处理ASCII命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆