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

Files at this revision

API Documentation at this revision

Comitter:
creatron
Date:
Sun Nov 23 08:13:26 2014 +0000
Parent:
10:e104a1b24165
Commit message:
Commit to be publised

Changed in this revision

dlms_comms.cpp 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
--- a/dlms_comms.cpp	Tue Nov 11 21:02:06 2014 +0000
+++ b/dlms_comms.cpp	Sun Nov 23 08:13:26 2014 +0000
@@ -5,11 +5,11 @@
 
 //#define DEBUG_DLMS_COMMS 
 
-   // Instantiate the raw serial (2nd uart) Tx then Rx
+// Instantiate the raw serial (2nd uart) Tx then Rx
    RawSerial  rs485(PA_9, PA_10);
-  // pin to switch the rs485 buffer direction
+  
+// pin to switch the rs485 buffer direction
   DigitalOut dir_485 (PB_0);
-  //DigitalOut dir_485 (D7);
   
 /** ---------------------------------------------------------------------------
  * @brief  C O N S T R U C T O R
@@ -30,24 +30,11 @@
     // when characters received
     rs485.attach(this,
                  &dlms_comms::Rx_interrupt,
-                 //RawSerial::RxIrq
                  Serial::RxIrq);
     timer = NULL;
     debug_uart = NULL;
     tx_irq_count = 0l;
     rx_irq_count = 0l;
-    
-    
-//        /** Set the flow control type on the serial port
-//     *
-//     *  @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
-//     *  @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
-//     *  @param flow2 the second flow control pin (CTS for RTSCTS)
-//     */
-//    rs485.set_flow_control(RTSCTS , //Flow type, 
-//                           dir_485, //PB_0, //PinName flow1=NC, 
-//                           dir_485 //PB_0 //PinName flow2=NC
-//                           );
 }
 /** ---------------------------------------------------------------------------
  * @brief bq34z110::init_port_expander Put all ports in INPUT mode
@@ -60,9 +47,9 @@
     // we use the parent timer and debug port
     debug_uart  = parent_uart; 
     timer       = parent_timer;
-//#ifdef DEBUG_DLMS_COMMS    
-    //debug_uart->printf ("   init_dlms_comms..\r\n");
-//#endif      
+#ifdef DEBUG_DLMS_COMMS    
+    debug_uart->printf ("   init_dlms_comms..\r\n");
+#endif      
 }
 /** ---------------------------------------------------------------------------
  * @brief dlms_comms::initialise This method initialise the rs485 comms port
@@ -107,12 +94,12 @@
     rs485.format((int) bits,
                  my_parity,
                  (int) stop_bits);
-//#  ifdef DEBUG_DLMS_COMMS    
-     //debug_uart->printf ("   init_dlms_comms..Baudrate (%d) %d bits Parity=%c\r\n",
-     //                     baudrate, bits, parity);
-//#  endif                          
+#  ifdef DEBUG_DLMS_COMMS    
+     debug_uart->printf ("   init_dlms_comms..Baudrate (%d) %d bits Parity=%c\r\n",
+                         baudrate, bits, parity);
      // dummy send packet to rs485 
-     //send_packet ("\r\nHello cruel world\r\n",12);       
+     send_packet ("\r\nHello cruel world\r\n",12);       
+#  endif                          
      if ( rs485.readable())
           rs485.getc();
 }
@@ -130,9 +117,11 @@
 void dlms_comms::Tx_interrupt(void)
 {
     tx_irq_count++;
+    
+    // disable the transmit interrupt
+    rs485.attach(NULL, Serial::TxIrq);
     // enable the receiver, we are finito with the transmission of characters
     // this changes the direction on the transceiver buffer
-    rs485.attach(NULL, Serial::TxIrq);
     dir_485 = 0;       
 }
 /** ---------------------------------------------------------------------------
--- 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);