Creating Custom RPC Classes

I had a bit of trouble getting started with RPC classes, so I'm including my first experiment with custom RPC classes as an example for others who want to start using RPC.

The basic concept of RPC is to call be able to access the variables and functions on your microcontroller through an external device, via  Serial, Ethernet, or whatever other communication types you folk favor.    Unfortunately, when compiling a program all of the things like comments and variable names are removed, so it becomes difficult to locate the variable you want to access.     This problem is addressed in RPC by storing a string containing the variable name on the microcontroller, and linking that variable name to the address of the variable/function in memory.

This works well, the only catch is you'll have to set up each function or variable you want to be able to access over RPC individually.

To try a quick demo of RPC using built-in mbed classes, try:  http://mbed.org/cookbook/Interfacing-Using-RPC

For trying out RPC on simple functions and variables, check out the documentation at:    http://mbed.org/cookbook/RPC-Interface-Library

You may need to modify some configurations to get RPC working in your terminal.   Each command seems to expect a carriage return + line feed at the end of the line.   In Tera Term, I had to enable CR+LF in the settings, and in PUTTY the best I could figure out is using CTRL-J instead of ENTER.  I also find turning on local echo is pretty handy, since I type the wrong command more often than the right one.

 

RPC commands are of the format: /<Object name>/<Method name> <Arguments separated by spaces>

 

This demonstration focuses on how to use RPC with your own custom classes and member functions.   As a simple example, we will create an LED class attached to a DigitalOut pin with member functions blink and toggle.   RPCTest

Our custom class will need to have inheritance from the class "Base".   Remember to register the name of your object by using in the initialization list the command Base(name).

We will also need to include two member functions which help to link the class to the strings identifying it.

#ifdef MBED_RPC  //this code will not compile unless we have included the rpc.h file.    So this class can also be used without RPC.
    /**Defines the methods available over RPC*/
    virtual const struct rpc_method *get_rpc_methods();
    /**Defines the RPC class*/
    static struct rpc_class *get_rpc_class();
#endif    // MBED_RPC  

 

The body of these functions is still a little bit of a mystery to me.  I copied the basic format and only changed the arguments to match my functions.  I think most of the clues to a deeper understanding lie in http://mbed.org/projects/libraries/svn/mbed/trunk/Base.h , but for now I'm just glad it works.

Note:  I'm having difficulty getting this code to display properly in my notebook.   Since some of the code displayed here may have errors, I suggest you look at the code in the actual program instead: RPCTest

Now that the class has been defined as RPC-able, we have completed the class definition.  The only step that remains is to specify it within the body of your program as an RPC class that you will be using.  I chose to include both this and the built-in Timer class as options in my code.

 

Base::add_rpc_class<Timer>();  //a class included in the core mbed RPC library
Base::add_rpc_class<LED>();    //my own custom LED class

Typing "/" into the terminal will give a list of available RPC commands.  This should include our new class "LED" the built-in "Timer" class, and "Base" which has RPC options to view or delete all objects.


0 comments

You need to log in to post a comment