Serial RPC Interface using the MODSERIAL library. Does not hang when serial port misbehaves.

Committer:
zainulcharbiwala
Date:
Fri Feb 04 01:54:41 2011 +0000
Revision:
3:d5d7e52769e7
Parent:
2:e26e61ea802b
Added Hexley Ball\s fix for memory leak in RFCFunction

Who changed what in which revision?

UserRevisionLine numberNew contents of line
zainulcharbiwala 0:66e48adbb27b 1 /**
zainulcharbiwala 0:66e48adbb27b 2 * @section LICENSE
zainulcharbiwala 0:66e48adbb27b 3 *Copyright (c) 2010 ARM Ltd.
zainulcharbiwala 0:66e48adbb27b 4 *
zainulcharbiwala 0:66e48adbb27b 5 *Permission is hereby granted, free of charge, to any person obtaining a copy
zainulcharbiwala 0:66e48adbb27b 6 *of this software and associated documentation files (the "Software"), to deal
zainulcharbiwala 0:66e48adbb27b 7 *in the Software without restriction, including without limitation the rights
zainulcharbiwala 0:66e48adbb27b 8 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
zainulcharbiwala 0:66e48adbb27b 9 *copies of the Software, and to permit persons to whom the Software is
zainulcharbiwala 0:66e48adbb27b 10 *furnished to do so, subject to the following conditions:
zainulcharbiwala 0:66e48adbb27b 11 *
zainulcharbiwala 0:66e48adbb27b 12 *The above copyright notice and this permission notice shall be included in
zainulcharbiwala 0:66e48adbb27b 13 *all copies or substantial portions of the Software.
zainulcharbiwala 0:66e48adbb27b 14 *
zainulcharbiwala 0:66e48adbb27b 15 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
zainulcharbiwala 0:66e48adbb27b 16 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
zainulcharbiwala 0:66e48adbb27b 17 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
zainulcharbiwala 0:66e48adbb27b 18 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
zainulcharbiwala 0:66e48adbb27b 19 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
zainulcharbiwala 0:66e48adbb27b 20 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
zainulcharbiwala 0:66e48adbb27b 21 *THE SOFTWARE.
zainulcharbiwala 0:66e48adbb27b 22 *
zainulcharbiwala 0:66e48adbb27b 23 *
zainulcharbiwala 0:66e48adbb27b 24 * @section DESCRIPTION
zainulcharbiwala 0:66e48adbb27b 25 *
zainulcharbiwala 0:66e48adbb27b 26 *This class sets up RPC communication. This allows objects on mbed to be controlled. Objects can be created or existing objects can be used
zainulcharbiwala 0:66e48adbb27b 27 */
zainulcharbiwala 0:66e48adbb27b 28 #include "SerialRPCInterface.h"
zainulcharbiwala 0:66e48adbb27b 29
zainulcharbiwala 0:66e48adbb27b 30 using namespace mbed;
zainulcharbiwala 0:66e48adbb27b 31
zainulcharbiwala 0:66e48adbb27b 32 //Requires multiple contstructors for each type, serial to set different pin numbers, TCP for port.
zainulcharbiwala 0:66e48adbb27b 33 SerialRPCInterface::SerialRPCInterface(PinName tx, PinName rx, int baud):pc(tx, rx) {
zainulcharbiwala 0:66e48adbb27b 34 _RegClasses();
zainulcharbiwala 0:66e48adbb27b 35 _enabled = true;
zainulcharbiwala 1:cf1aa160edc1 36 if (baud != 9600) pc.baud(baud);
zainulcharbiwala 1:cf1aa160edc1 37 pc.attach(this, &SerialRPCInterface::_RPCSerial, MODSERIAL::RxAutoDetect);
zainulcharbiwala 1:cf1aa160edc1 38 pc.autoDectectChar('\n');
zainulcharbiwala 1:cf1aa160edc1 39 pc.rxBufferFlush();
zainulcharbiwala 1:cf1aa160edc1 40 }
zainulcharbiwala 0:66e48adbb27b 41
zainulcharbiwala 0:66e48adbb27b 42 void SerialRPCInterface::_RegClasses(void){
zainulcharbiwala 0:66e48adbb27b 43 //Register classes with base
zainulcharbiwala 0:66e48adbb27b 44 Base::add_rpc_class<AnalogIn>();
zainulcharbiwala 0:66e48adbb27b 45 Base::add_rpc_class<AnalogOut>();
zainulcharbiwala 0:66e48adbb27b 46 Base::add_rpc_class<DigitalIn>();
zainulcharbiwala 0:66e48adbb27b 47 Base::add_rpc_class<DigitalOut>();
zainulcharbiwala 0:66e48adbb27b 48 Base::add_rpc_class<DigitalInOut>();
zainulcharbiwala 0:66e48adbb27b 49 Base::add_rpc_class<PwmOut>();
zainulcharbiwala 0:66e48adbb27b 50 Base::add_rpc_class<Timer>();
zainulcharbiwala 0:66e48adbb27b 51 Base::add_rpc_class<BusOut>();
zainulcharbiwala 0:66e48adbb27b 52 Base::add_rpc_class<BusIn>();
zainulcharbiwala 0:66e48adbb27b 53 Base::add_rpc_class<BusInOut>();
zainulcharbiwala 0:66e48adbb27b 54 Base::add_rpc_class<Serial>();
zainulcharbiwala 0:66e48adbb27b 55 }
zainulcharbiwala 0:66e48adbb27b 56
zainulcharbiwala 0:66e48adbb27b 57 void SerialRPCInterface::Disable(void){
zainulcharbiwala 0:66e48adbb27b 58 _enabled = false;
zainulcharbiwala 0:66e48adbb27b 59 }
zainulcharbiwala 0:66e48adbb27b 60 void SerialRPCInterface::Enable(void){
zainulcharbiwala 0:66e48adbb27b 61 _enabled = true;
zainulcharbiwala 0:66e48adbb27b 62 }
zainulcharbiwala 0:66e48adbb27b 63 void SerialRPCInterface::_MsgProcess(void) {
zainulcharbiwala 0:66e48adbb27b 64 if(_enabled == true){
zainulcharbiwala 0:66e48adbb27b 65 rpc(_command, _response);
zainulcharbiwala 0:66e48adbb27b 66 }
zainulcharbiwala 0:66e48adbb27b 67 }
zainulcharbiwala 0:66e48adbb27b 68
zainulcharbiwala 0:66e48adbb27b 69 void SerialRPCInterface::_RPCSerial() {
zainulcharbiwala 0:66e48adbb27b 70 _RPCflag = true;
zainulcharbiwala 1:cf1aa160edc1 71 if(_enabled == true) {
zainulcharbiwala 0:66e48adbb27b 72 pc.gets(_command, 256);
zainulcharbiwala 0:66e48adbb27b 73 _MsgProcess();
zainulcharbiwala 2:e26e61ea802b 74 pc.printf("%s\n", _response);
zainulcharbiwala 0:66e48adbb27b 75 }
zainulcharbiwala 0:66e48adbb27b 76 _RPCflag = false;
zainulcharbiwala 0:66e48adbb27b 77 }