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:
9:d49cdc77f867
Parent:
6:70460dcbc43c
Child:
11:ca1e0ca3a673

File content as of revision 9:d49cdc77f867:

#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
                      1);   // 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() & 0x7f;
              switch (ch)
              {
                 case '1':
                    dlms.send_packet ("\r\nDLMS Test 1 \r\n", 16);
                    break;
              }
              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 (dlms.char_available())
         {
            debugger.printf("Rx Char ");
            debugger.printf("%c\r\n", dlms.get_char());
         }
    }
    while (1);
}
 //----[ then end]-------------------------------------------------------------