0

I am working on an embedded system project. It is on ARM M3-Cortex platform. The equipment I am working with, is a measurement controller. It can be connected to a PC and takes some commands. I try to design the command parser but have seen totally different approaches on internet. There are more than 30 commands. Each command can have different arguments: e.g. command Date:? will ask for the date, so the device LCD should show the date, but command Date:2012,05,30 should set the date. Some commands are even more complex and have different types of arguments, i.e., the algorithm and the data structure should be intelligently designed. Do you have any clue or recommendation. Thanks buddies :)

Django
  • 159
  • 1
  • 3
  • 9
  • Why don't you use ASCII protocols? DCON, for example. If you want to make a binary one, there are another options. I mean, why don't you implement existing open protocol? – Sergei Danielian Jun 20 '12 at 12:29
  • 3
    -1, this isn't a good fit for the Stack Overflow format. If you can ask a *specific* question about where you're stuck developing your command parser, we can probably help. Tell us what you've done so far and what isn't working. – Michael Kristofik Jun 20 '12 at 12:31
  • 1
    Ok, may be you can give us more info about those mentioned commands and what are the requirements to your intelligently designed data structure? – Sergei Danielian Jun 20 '12 at 12:33

2 Answers2

4

Ok, here is a thing.

You need to implement a protocol. And I think a text protocol (because of your command requirement: Date:?). If you don't want to use existing ones (which is really confusing) you should take a look at least at their implementation and design.

What can I highly recommend to you so far is to look at DCON protocol (there is a PDF, but it is a bit hard to find it, if you are going to interested in, I'll search it for you), Modbus ASCII protocol (which is a really good option and a world wide used). Next, Siemens SPA (Serial Protocol ASCII) is an interestihng one too.

If you want to implement your own protocol (assume, you want to implement a binary one), here are the main points you should take into account while designing:

  • Byte and/or Words endianess,
  • Float representation (IEEE754 or fixed point))
  • Begin/End message markers and a ByteStuffing or BitStuffing (note, it's only in Beg/End Markers existence)
  • Byte/Bytes for commands (for example, 0x01 is for read a coil, 0x02 is for write a coil, etc.)
  • ACK/NASK bytes / confirmation message
  • CRC code to check message validity

Of course, there are a huge bunch of such protocols and you getting familiar with some of them will definitely help you design better (or using existing protocols).

UPDATE:

In case of using parsers/lexers (other then harald mentioned, because of additional libraries) to implement a parser you should definitely read SO question "Is there an alternative for flex/bison that is usable on 8-bit embedded systems".

Community
  • 1
  • 1
Sergei Danielian
  • 4,938
  • 4
  • 36
  • 58
  • Thanks for your answers and comments. The point is that the protocol is already written and I should follow it. It is like Command_name: [argument1],[argument2],...[cr][lf]. then it should return OK or ERR as a response. Actually, I am new to these kinda stuff, maybe that's why my questions was a little bit general. Do you think I can follow open protocols you said? By the way, I have not desgined it yet. By now, I was thinking of having different layers of struct, connected by pointers, in order to parse any given command. P.S I am using ANSI C in Keil uvision 4.0 environment. – Django Jun 20 '12 at 13:03
  • Ok, I misunderstood you. So the question is how to write such parser, which would parse a given command? Ok, than I think you should use [`strtok`](http://msdn.microsoft.com/en-us/library/2c8d19sb(v=vs.71).aspx) function. I believe, it's a very common way to parse a string or command basically in C. Also, for converting you can use smth like [`strtol`](http://msdn.microsoft.com/en-us/library/ms860466.aspx) – Sergei Danielian Jun 20 '12 at 13:11
  • THanks a lot gahcep! I will read about them to see if I can get any help. For using them, should I include big libraries? Since I am restricted for using the exsiting 32kb of flash memory on the board. – Django Jun 20 '12 at 13:19
  • `strtok` is part of the ANSI C standard. So, I'm almost 100% sure you have this at your platform. – Sergei Danielian Jun 20 '12 at 13:32
  • 1
    be aware that strtok is not thread safe. May not matter on the platform in question, but worth noting. – harald Jun 20 '12 at 13:34
  • We are talking about small embedded system, as I understood, so your notice is outside of the scope. – Sergei Danielian Jun 20 '12 at 13:40
  • 1
    @gahcep: In what way? I'm developing embedded software myself, where thread safe concerns are very much an issue. – harald Jun 20 '12 at 13:44
  • @harald, I'm not talking about embedded software with Linux on the board and about tiny OS implementation. See at ICPDAS for example. But, yes, I didn't include in consideration that the talk could be about embedded linux. P.S. I developed such software a few years ago too, but now, I concern with EmbeddedLinux (mostly PowerPC and MIPS). – Sergei Danielian Jun 20 '12 at 13:53
  • dear pals, I don't use any embedded linux. As I said, I am using Keil uvision compiler and developing environment (on a Windows 7 machine), using a jtag connector to debug the code on the board. Still, I am confused if it is easier to write and implement a parser or follow the solution that Herald suggested i.e. using a parser generator! Sometimes your comments are higher than my knowledge. just to say that I am not a professional in software programming. – Django Jun 20 '12 at 14:41
  • I think, using parser generator is indeed a good choice. – Sergei Danielian Jun 20 '12 at 14:49
  • @gahcep no worries :) Just saying embedded does not always imply minimal systems. – harald Jun 21 '12 at 08:52
4

What you seem to want is a parser generator. This is a tool where you can specify the grammar of your command language fairly easily, and it will generate the surce code to parse this grammar for you. Have a look at Yacc or Bison, they should be able to do this for you provided the licenses are compatible with your work.

You can also add a lexer (Lexx/Flexx) to the mix which can simplify turning the character stream into lexographical tokens for the parser to chew on.

Update:

Apart from the absolutely most basic parsers, a generator will probably make your life easier. Provided resource constraints or licensing issues don't come in the way, of course. A good parser generator should not demand much more resources than doing it by hand anyways. And as @gahcep pointed out in his answer, there are other alternatives to Yacc and Bison as well, including commercial alternatives. Perhaps some of those will suit you better.

Some of the advantages of using a parser generator is:

  • You can concentrate on the grammar instead of how to parse it
  • The generated code will often solve the problem in a more optimal way than a straight forward one-off parser will do.
  • The generated code should be virtually bug free.

The disadvantages being that the generated code may be difficult to debug, and may introduce dependencies you could be without by handcrafting the code yourself. Check around and find the one that suits you and the project best.

Good luck!

harald
  • 5,976
  • 1
  • 24
  • 41