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.

Files at this revision

API Documentation at this revision

Comitter:
waspSalander
Date:
Tue Sep 05 22:36:20 2017 +0000
Commit message:
My own Serial Communication library.

Changed in this revision

SerialCommunication.cpp Show annotated file Show diff for this revision Revisions of this file
SerialCommunication.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SerialCommunication.cpp	Tue Sep 05 22:36:20 2017 +0000
@@ -0,0 +1,38 @@
+#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);  
+    //}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SerialCommunication.h	Tue Sep 05 22:36:20 2017 +0000
@@ -0,0 +1,41 @@
+
+#ifndef SERIAL_COMMUNICATION_H
+#define SERIAL_COMMUNICATION_H
+
+#include "mbed.h"
+#include <string.h>
+#include <string>
+#include <iostream>
+
+
+#define DELAY_COMMAND 0.5
+#define DEBUG_SERIAL_COMUNNICATION 1
+
+using namespace std;
+
+
+class SerialCommunication{
+    
+    public:
+            SerialCommunication(PinName tx, PinName rx, int baudRate);
+            
+            int  ReceiveCommand();
+            void SendCommand(string commandSended);
+        
+        
+    private:
+            Serial *serial;
+        
+            DigitalOut tx;
+            DigitalOut rx;
+            DigitalOut *debug;
+            
+            int baudRate;           
+            int messageLength;
+            
+};
+
+#endif
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Sep 05 22:36:20 2017 +0000
@@ -0,0 +1,19 @@
+#include "mbed.h"
+#include "SerialCommunication.h"
+
+DigitalOut myled(LED1);
+SerialCommunication  *serialCommunication = new SerialCommunication(p9,p10,115200);
+
+int main() {
+    int commandReceived = 0;
+    
+    while(1){
+        commandReceived = serialCommunication->ReceiveCommand();
+        if (commandReceived == 1){ // informação correta recebida
+            myled = 1;
+            break;    
+        }
+    
+    }
+    
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Sep 05 22:36:20 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/mbed/builds/e2bfab296f20
\ No newline at end of file