In the mbed documentation LPC1768 there are many examples of serial communication implemented in a structured and disorganized way. So, I decided to make it very practical and reusable for other users who need to use this feature. For this, I created my own library based on the OO paradigm.

Dependencies:   mbed

In the mbed documentation LPC1768 there are many examples of serial communication implemented in a structured and disorganized way. So, I decided to make it very practical and reusable for other users who need to use this feature. For this, I created my own library based on the OO paradigm.

SerialCommunication.cpp

Committer:
waspSalander
Date:
2017-09-05
Revision:
0:78c623c147d2

File content as of revision 0:78c623c147d2:

#include "SerialCommunication.h"

SerialCommunication::SerialCommunication(PinName tx, PinName rx, int baudRate): tx(tx), rx(rx){
        serial = new Serial(tx, rx); // tx, rx
        serial->baud(baudRate);
        messageLength = 4; // tam palavra + 1 ('\n')
        
        debug = new DigitalOut(LED1); // debug send info
}


int SerialCommunication::ReceiveCommand(){ // RECEIVE  INFO
    
    char commandReceived[messageLength];    
        
    
    if(serial->readable()){   
        serial->gets(commandReceived,messageLength);           
                
        if (strcmp(commandReceived, "s0b") == 0){ //s0b : ex de palavra q desejo receber"
            return 1;               
        }
    }   
    return 0;
}


void SerialCommunication::SendCommand(string commandSended){ // SEND INFO
    
    // SEND INFO
    //while(1){
            serial->printf("%s",commandSended);         
            debug->write(1);
            wait(DELAY_COMMAND);
            debug->write(0);
            wait(DELAY_COMMAND);  
    //}
}