Tutorial Code for working with RPC Functions
Dependencies: RPCInterface mbed
Fork of RPC_Tutorial by
main.cpp@1:50a40baa79bf, 2016-03-12 (annotated)
- Committer:
- nambvarun
- Date:
- Sat Mar 12 02:39:07 2016 +0000
- Revision:
- 1:50a40baa79bf
- Parent:
- 0:74f2764b636b
RPC Function Example;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| nambvarun | 0:74f2764b636b | 1 | #include "mbed.h" |
| nambvarun | 0:74f2764b636b | 2 | #include "mbed_rpc.h" |
| nambvarun | 0:74f2764b636b | 3 | |
| nambvarun | 0:74f2764b636b | 4 | /** |
| nambvarun | 0:74f2764b636b | 5 | * This example program has been updated to use the RPC implementation in the new mbed libraries. |
| nambvarun | 0:74f2764b636b | 6 | * This example demonstrates using RPC over serial |
| nambvarun | 0:74f2764b636b | 7 | */ |
| nambvarun | 0:74f2764b636b | 8 | |
| nambvarun | 0:74f2764b636b | 9 | Serial pc(USBTX, USBRX); |
| nambvarun | 0:74f2764b636b | 10 | void moveTo(Arguments *in, Reply *out); |
| nambvarun | 0:74f2764b636b | 11 | RPCFunction rpcMove(&moveTo, "moveTo"); |
| nambvarun | 0:74f2764b636b | 12 | double xLoc, yLoc; |
| nambvarun | 0:74f2764b636b | 13 | |
| nambvarun | 0:74f2764b636b | 14 | int main() { |
| nambvarun | 0:74f2764b636b | 15 | //The mbed RPC classes are now wrapped to create an RPC enabled version - see RpcClasses.h so don't add to base class |
| nambvarun | 0:74f2764b636b | 16 | |
| nambvarun | 0:74f2764b636b | 17 | // receive commands, and send back the responses |
| nambvarun | 0:74f2764b636b | 18 | char buf[256], outbuf[256]; |
| nambvarun | 0:74f2764b636b | 19 | while(1) { |
| nambvarun | 0:74f2764b636b | 20 | pc.gets(buf, 256); |
| nambvarun | 0:74f2764b636b | 21 | //Call the static call method on the RPC class |
| nambvarun | 0:74f2764b636b | 22 | RPC::call(buf, outbuf); |
| nambvarun | 0:74f2764b636b | 23 | pc.printf("%s\n", outbuf); |
| nambvarun | 0:74f2764b636b | 24 | } |
| nambvarun | 0:74f2764b636b | 25 | } |
| nambvarun | 0:74f2764b636b | 26 | |
| nambvarun | 0:74f2764b636b | 27 | // Make sure the method takes in Arguments and Reply objects. |
| nambvarun | 0:74f2764b636b | 28 | void moveTo (Arguments *in, Reply *out) { |
| nambvarun | 0:74f2764b636b | 29 | bool success = true; |
| nambvarun | 0:74f2764b636b | 30 | |
| nambvarun | 0:74f2764b636b | 31 | // In this scenario, when using RPC delimit the two arguments with a space. |
| nambvarun | 0:74f2764b636b | 32 | xLoc = in->getArg<double>(); |
| nambvarun | 0:74f2764b636b | 33 | yLoc = in->getArg<double>(); |
| nambvarun | 0:74f2764b636b | 34 | |
| nambvarun | 0:74f2764b636b | 35 | // Have code here to move robot to location (xLoc, yLoc) and update success. |
| nambvarun | 0:74f2764b636b | 36 | |
| nambvarun | 0:74f2764b636b | 37 | char buffer[200]; |
| nambvarun | 0:74f2764b636b | 38 | sprintf(buffer, "Successfully moved to location (%f, %f)", xLoc, yLoc); |
| nambvarun | 0:74f2764b636b | 39 | if (success) { |
| nambvarun | 0:74f2764b636b | 40 | out->putData(buffer); |
| nambvarun | 0:74f2764b636b | 41 | } else { |
| nambvarun | 0:74f2764b636b | 42 | out->putData("Failed to move to location."); |
| nambvarun | 0:74f2764b636b | 43 | } |
| nambvarun | 0:74f2764b636b | 44 | } |
