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

main.cpp

Committer:
creatron
Date:
2014-11-11
Revision:
1:7091401482c5
Parent:
0:044dfba47660
Child:
2:c99c596be3d1

File content as of revision 1:7091401482c5:

#include "mbed.h"
#include "dlms_comms.h"
// instantiate classes
Timer       timer;
// debug serial comms
RawSerial   debugger(SERIAL_TX, SERIAL_RX);
// rs485 comms 2nd uart
dlms_comms  dlms;

DigitalOut myled(LED1);

#define POLL_TIME_MS  100
 
/** -------------------------------------------------------------------------
 * @brief the setup routine runs once when you press reset
 */
void setup(void)
{
    // 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");
    timer.start();
    dlms.init  ( &debugger, &timer);
    dlms.initialise  (9600, // baudrate    
                      8,    // bits,
                      'n',  // parity
                      2);   // stop_bits    
    debugger.printf (">> Setup Done \r\n");
}
/** ---------------------------------------------------------------------------
 * @brief  M A I N 
 */
int main(void)
{  
    // timer to do some timing
    UINT_64        compare_ms  = 0l;
    UINT_64        current_ms; 
    bool           direction_485 = false;
    char           ch;
    
    // initialise 
    setup();
    debugger.printf (">> Demo .. \r\n");
    do
    {
         current_ms = timer.read_ms();
         current_ms -= compare_ms;         
         if (current_ms > POLL_TIME_MS)
         {
             compare_ms += POLL_TIME_MS;
             if (myled) 
                myled = false;
             else
                myled = true;
         }
         if (direction_485 != dlms.get_dir485())
         {
             direction_485 = dlms.get_dir485();
             debugger.printf("\r\nDir Changed ");
             if (direction_485)
                debugger.printf("1 \r\n");
             else
                debugger.printf("0 \r\n");
         }
         if (debugger.readable())
         {
              ch = debugger.getc();
              switch (ch)
              {
                 case '1':
                    dlms.send_packet ("\r\nDLMS Test 1 \r\n", 16);
                    break;
              }
              //debugger.printf ("ret_irq_count = %ld\r\n", dlms.ret_irq_count());
         }
//         if (dlms.char_available())
//         {
//            debugger.printf("Rx Char ");
//            debugger.printf("%c\r\n", dlms.get_char());
//         }
//         dlms.poll_rs485();
    }
    while (1);
}
 //----[ then end]-------------------------------------------------------------