RPC serial, fixed delayed reading on RPCFunction

Dependencies:   mbed-rpc mbed

Fork of RPC_Serial by Michael Walker

Committer:
ewoud
Date:
Wed Dec 16 22:33:55 2015 +0000
Revision:
6:60fe21fd6048
Parent:
5:3f83ade6195b
RPC serial comunication program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ewoud 5:3f83ade6195b 1 #include <string>
MichaelW 0:78952cd3935b 2 #include "mbed.h"
MichaelW 2:37712731e13d 3 #include "mbed_rpc.h"
MichaelW 2:37712731e13d 4
MichaelW 3:4ddd10908e46 5 /**
MichaelW 3:4ddd10908e46 6 * This example program has been updated to use the RPC implementation in the new mbed libraries.
MichaelW 3:4ddd10908e46 7 * This example demonstrates using RPC over serial
MichaelW 3:4ddd10908e46 8 */
MichaelW 2:37712731e13d 9
MichaelW 2:37712731e13d 10 //Use the RPC enabled wrapped class - see RpcClasses.h for more info
MichaelW 2:37712731e13d 11 RpcDigitalOut myled(LED4,"myled");
ewoud 4:12e9b5c57bd4 12 RpcDigitalOut secondled(LED2,"secondled");
MichaelW 2:37712731e13d 13
MichaelW 0:78952cd3935b 14 Serial pc(USBTX, USBRX);
ewoud 4:12e9b5c57bd4 15
ewoud 6:60fe21fd6048 16 // defining a custom function:
ewoud 4:12e9b5c57bd4 17 void doEchoInput(Arguments* input, Reply* output);
ewoud 4:12e9b5c57bd4 18 RPCFunction echoInput(&doEchoInput, "echoInput");
ewoud 4:12e9b5c57bd4 19
ewoud 4:12e9b5c57bd4 20 void doEchoInput(Arguments* input, Reply* output) {
ewoud 5:3f83ade6195b 21 const char* arg0 = input->getArg<const char*>();
ewoud 5:3f83ade6195b 22 if (0 == strcmp(arg0, "on")){
ewoud 4:12e9b5c57bd4 23 printf("led has been turned on!");
ewoud 4:12e9b5c57bd4 24 secondled.write(0);
ewoud 4:12e9b5c57bd4 25 }
ewoud 4:12e9b5c57bd4 26 else {
ewoud 4:12e9b5c57bd4 27 printf("led has been turned off!");
ewoud 4:12e9b5c57bd4 28 secondled.write(1);
ewoud 4:12e9b5c57bd4 29 }
ewoud 4:12e9b5c57bd4 30
ewoud 4:12e9b5c57bd4 31 /*
ewoud 4:12e9b5c57bd4 32 // Arguments are already parsed into argv array of char*
ewoud 4:12e9b5c57bd4 33 printf("Object name = %s\n",input->obj_name);
ewoud 4:12e9b5c57bd4 34 printf("Method name = %s\n",input->method_name);
ewoud 4:12e9b5c57bd4 35 for (int i=0; i < input->argc; i++)
ewoud 4:12e9b5c57bd4 36 printf("argv[%1d] = %s \n",i,input->argv[i]);
ewoud 4:12e9b5c57bd4 37
ewoud 4:12e9b5c57bd4 38 // Alternatively the arguments can be recovered as the types expected
ewoud 4:12e9b5c57bd4 39 // by repeated calls to getArg()
ewoud 4:12e9b5c57bd4 40 int arg0 = input->getArg<int>(); // Expecting argv[0] to be int
ewoud 4:12e9b5c57bd4 41 printf("Expecting argv[0] to be int = %d\n",arg0);
ewoud 4:12e9b5c57bd4 42 float arg1 = input->getArg<float>(); // Expecting argv[1] to be float
ewoud 4:12e9b5c57bd4 43 printf("Expecting argv[1] to be float = %f\n",arg1);
ewoud 4:12e9b5c57bd4 44 const char *arg2 = input->getArg<const char*>(); // Expecting argv[2] to be a string
ewoud 4:12e9b5c57bd4 45 printf("Expecting argv[2] to be a string = %s\n",arg2);
ewoud 4:12e9b5c57bd4 46
ewoud 4:12e9b5c57bd4 47 // The output parameter string is generated by calls to putData, which separates them with spaces.
ewoud 4:12e9b5c57bd4 48 output->putData(arg0);
ewoud 4:12e9b5c57bd4 49 output->putData(arg1);
ewoud 4:12e9b5c57bd4 50 output->putData(arg2);
ewoud 4:12e9b5c57bd4 51 */
ewoud 4:12e9b5c57bd4 52 }
MichaelW 0:78952cd3935b 53 int main() {
MichaelW 3:4ddd10908e46 54 //The mbed RPC classes are now wrapped to create an RPC enabled version - see RpcClasses.h so don't add to base class
MichaelW 2:37712731e13d 55
MichaelW 1:de34af25056a 56 // receive commands, and send back the responses
MichaelW 0:78952cd3935b 57 char buf[256], outbuf[256];
MichaelW 0:78952cd3935b 58 while(1) {
MichaelW 0:78952cd3935b 59 pc.gets(buf, 256);
MichaelW 2:37712731e13d 60 //Call the static call method on the RPC class
MichaelW 2:37712731e13d 61 RPC::call(buf, outbuf);
MichaelW 0:78952cd3935b 62 pc.printf("%s\n", outbuf);
MichaelW 0:78952cd3935b 63 }
MichaelW 0:78952cd3935b 64 }