This is a RS485 class that uses the second UART and was tested on a Nucleo F030R8. A main demo program howto use the class is included. This class control the direction pin on the transceiver buffer automatically, and used transmit and receive interrupts. Ring buffers (256 bytes) are implemented on both transmission and reception. It assumes a ADM3485 'type' buffer where pins 2 ans 3 are connected and seen as direction. This test program could easily be adapted as base for other programs.

Dependencies:   mbed

Revision:
11:ca1e0ca3a673
Parent:
9:d49cdc77f867
--- a/main.cpp	Tue Nov 11 21:02:06 2014 +0000
+++ b/main.cpp	Sun Nov 23 08:13:26 2014 +0000
@@ -1,10 +1,22 @@
 #include "mbed.h"
 #include "dlms_comms.h"
+/** ---------------------------------------------------------------------------
+ * @brief This module was tested on a Nucleo F030R8 with dual UARTS
+ *  The debugger uart is the normal , and the second uart was used for
+ *  RS485 communications
+ * @note The ring buffers are 256 bytes, that implies that if an unsigned
+ *       char is used as index , the ++index wraps automatically, is other
+ *       ring buffer sizes is used, please wrap the head and tail indexes 
+ *       appropiate 
+ * @note The timer was instantiate and used dering debugging to display 
+ *       the timing.. kept just for incase one needs it
+**/
+
 // instantiate classes
 Timer       timer;
 // debug serial comms
 RawSerial   debugger(SERIAL_TX, SERIAL_RX);
-// rs485 comms 2nd uart
+// rs485 comms class on 2nd uart
 dlms_comms  dlms;
 
 DigitalOut myled(LED1);
@@ -18,10 +30,16 @@
 {
     // initialize serial communication at 115200 bits per second:
     debugger.baud (115200);
-    debugger.format();                           
-    debugger.printf ("\r\n\n>> DCU Battery Charger Nov 2014  \r\n");
+    // use the defaults
+    debugger.format();       
+    // annouce yourself to the world                    
+    debugger.printf ("\r\n\n>> Creatron RS485 test program Nov 2014  \r\n");
+    // start the timer
     timer.start();
+    // this allows the rs485 class to use the parent timer and printf classes
+    // in this class
     dlms.init  ( &debugger, &timer);
+    // initialise the rs485 class
     dlms.initialise  (9600, // baudrate    
                       8,    // bits,
                       'n',  // parity
@@ -36,14 +54,19 @@
     // timer to do some timing
     UINT_64        compare_ms  = 0l;
     UINT_64        current_ms; 
+    // my variable.. just a tester to 'see' the state of the RS485 transceiver
     bool           direction_485 = false;
+    // char to command functions
     char           ch;
     
     // initialise 
     setup();
-    debugger.printf (">> Demo .. \r\n");
+    debugger.printf (">> Ready.. \r\n"
+                     "   Press 1 to send RS485 PacketDemo .. \r\n");
+    // endless loop, glad I dont have to do this
     do
     {
+         // use the timer to toggle I am alife led 
          current_ms = timer.read_ms();
          current_ms -= compare_ms;         
          if (current_ms > POLL_TIME_MS)
@@ -54,33 +77,45 @@
              else
                 myled = true;
          }
+         
+         // try to detect a direction change on the RS485 transceiver
+         // and keep the user informed .. just for ammusement
          if (direction_485 != dlms.get_dir485())
          {
              direction_485 = dlms.get_dir485();
-             debugger.printf("\r\nDir Changed ");
+             debugger.printf("\r\n> Dir Changed ");
              if (direction_485)
                 debugger.printf("1 \r\n");
              else
                 debugger.printf("0 \r\n");
          }
+         // check to see if the user has a 'request' to be executed
          if (debugger.readable())
          {
               ch = debugger.getc() & 0x7f;
               switch (ch)
               {
+                 // fine .. 
                  case '1':
-                    dlms.send_packet ("\r\nDLMS Test 1 \r\n", 16);
+                    //  this was designed to send binary packets (structure), thus
+                    // if you want to send null terminated strings one could
+                    // just use the strlen() function for the amount of bytes 
+                    // to be send, the transmission (send_packet) will happily send zeros 
+                    dlms.send_packet ("\r\nSend test packet to RS485\r\n", 29);
                     break;
               }
+              // show some stats .. keep em happy
               debugger.printf (">> %c 0x%02X IRQ rxcount=%5ld txcount=%5ld Dir_485=%d\r\n", ch, ch, 
                                dlms.ret_rx_irq_count(),
                                dlms.ret_tx_irq_count(),
                                dlms.ret_dir_485());
          }
+         // if any char received on the RS485 bus, display it ..
+         // assumes this is printable
          if (dlms.char_available())
          {
-            debugger.printf("Rx Char ");
-            debugger.printf("%c\r\n", dlms.get_char());
+            ch = dlms.get_char();
+            debugger.printf("RX Char %c 0x%02X\r\n", ch, ch);
          }
     }
     while (1);