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.
main.cpp@2:c99c596be3d1, 2014-11-11 (annotated)
- Committer:
- creatron
- Date:
- Tue Nov 11 16:16:34 2014 +0000
- Revision:
- 2:c99c596be3d1
- Parent:
- 1:7091401482c5
- Child:
- 3:29454cac7930
Fix the 115200 baudrate, top bit most of the time set (baudrate to high)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
creatron | 0:044dfba47660 | 1 | #include "mbed.h" |
creatron | 0:044dfba47660 | 2 | #include "dlms_comms.h" |
creatron | 0:044dfba47660 | 3 | // instantiate classes |
creatron | 0:044dfba47660 | 4 | Timer timer; |
creatron | 0:044dfba47660 | 5 | // debug serial comms |
creatron | 0:044dfba47660 | 6 | RawSerial debugger(SERIAL_TX, SERIAL_RX); |
creatron | 0:044dfba47660 | 7 | // rs485 comms 2nd uart |
creatron | 0:044dfba47660 | 8 | dlms_comms dlms; |
creatron | 0:044dfba47660 | 9 | |
creatron | 0:044dfba47660 | 10 | DigitalOut myled(LED1); |
creatron | 0:044dfba47660 | 11 | |
creatron | 0:044dfba47660 | 12 | #define POLL_TIME_MS 100 |
creatron | 0:044dfba47660 | 13 | |
creatron | 0:044dfba47660 | 14 | /** ------------------------------------------------------------------------- |
creatron | 0:044dfba47660 | 15 | * @brief the setup routine runs once when you press reset |
creatron | 0:044dfba47660 | 16 | */ |
creatron | 0:044dfba47660 | 17 | void setup(void) |
creatron | 0:044dfba47660 | 18 | { |
creatron | 0:044dfba47660 | 19 | // initialize serial communication at 115200 bits per second: |
creatron | 0:044dfba47660 | 20 | debugger.baud (115200); |
creatron | 0:044dfba47660 | 21 | debugger.format(); |
creatron | 0:044dfba47660 | 22 | debugger.printf ("\r\n\n>> DCU Battery Charger Nov 2014 \r\n"); |
creatron | 0:044dfba47660 | 23 | timer.start(); |
creatron | 0:044dfba47660 | 24 | dlms.init ( &debugger, &timer); |
creatron | 0:044dfba47660 | 25 | dlms.initialise (9600, // baudrate |
creatron | 0:044dfba47660 | 26 | 8, // bits, |
creatron | 0:044dfba47660 | 27 | 'n', // parity |
creatron | 0:044dfba47660 | 28 | 2); // stop_bits |
creatron | 0:044dfba47660 | 29 | debugger.printf (">> Setup Done \r\n"); |
creatron | 0:044dfba47660 | 30 | } |
creatron | 0:044dfba47660 | 31 | /** --------------------------------------------------------------------------- |
creatron | 0:044dfba47660 | 32 | * @brief M A I N |
creatron | 0:044dfba47660 | 33 | */ |
creatron | 0:044dfba47660 | 34 | int main(void) |
creatron | 0:044dfba47660 | 35 | { |
creatron | 0:044dfba47660 | 36 | // timer to do some timing |
creatron | 0:044dfba47660 | 37 | UINT_64 compare_ms = 0l; |
creatron | 0:044dfba47660 | 38 | UINT_64 current_ms; |
creatron | 0:044dfba47660 | 39 | bool direction_485 = false; |
creatron | 0:044dfba47660 | 40 | char ch; |
creatron | 0:044dfba47660 | 41 | |
creatron | 0:044dfba47660 | 42 | // initialise |
creatron | 0:044dfba47660 | 43 | setup(); |
creatron | 0:044dfba47660 | 44 | debugger.printf (">> Demo .. \r\n"); |
creatron | 0:044dfba47660 | 45 | do |
creatron | 0:044dfba47660 | 46 | { |
creatron | 0:044dfba47660 | 47 | current_ms = timer.read_ms(); |
creatron | 0:044dfba47660 | 48 | current_ms -= compare_ms; |
creatron | 0:044dfba47660 | 49 | if (current_ms > POLL_TIME_MS) |
creatron | 0:044dfba47660 | 50 | { |
creatron | 0:044dfba47660 | 51 | compare_ms += POLL_TIME_MS; |
creatron | 0:044dfba47660 | 52 | if (myled) |
creatron | 0:044dfba47660 | 53 | myled = false; |
creatron | 0:044dfba47660 | 54 | else |
creatron | 0:044dfba47660 | 55 | myled = true; |
creatron | 0:044dfba47660 | 56 | } |
creatron | 0:044dfba47660 | 57 | if (direction_485 != dlms.get_dir485()) |
creatron | 0:044dfba47660 | 58 | { |
creatron | 0:044dfba47660 | 59 | direction_485 = dlms.get_dir485(); |
creatron | 0:044dfba47660 | 60 | debugger.printf("\r\nDir Changed "); |
creatron | 0:044dfba47660 | 61 | if (direction_485) |
creatron | 0:044dfba47660 | 62 | debugger.printf("1 \r\n"); |
creatron | 0:044dfba47660 | 63 | else |
creatron | 0:044dfba47660 | 64 | debugger.printf("0 \r\n"); |
creatron | 0:044dfba47660 | 65 | } |
creatron | 1:7091401482c5 | 66 | if (debugger.readable()) |
creatron | 1:7091401482c5 | 67 | { |
creatron | 2:c99c596be3d1 | 68 | ch = debugger.getc() & 0x7f; |
creatron | 1:7091401482c5 | 69 | switch (ch) |
creatron | 1:7091401482c5 | 70 | { |
creatron | 1:7091401482c5 | 71 | case '1': |
creatron | 1:7091401482c5 | 72 | dlms.send_packet ("\r\nDLMS Test 1 \r\n", 16); |
creatron | 1:7091401482c5 | 73 | break; |
creatron | 1:7091401482c5 | 74 | } |
creatron | 2:c99c596be3d1 | 75 | debugger.printf (">> %c 0x%02X ret_irq_count = %ld\r\n", ch, ch, dlms.ret_irq_count()); |
creatron | 1:7091401482c5 | 76 | } |
creatron | 0:044dfba47660 | 77 | // if (dlms.char_available()) |
creatron | 0:044dfba47660 | 78 | // { |
creatron | 0:044dfba47660 | 79 | // debugger.printf("Rx Char "); |
creatron | 0:044dfba47660 | 80 | // debugger.printf("%c\r\n", dlms.get_char()); |
creatron | 0:044dfba47660 | 81 | // } |
creatron | 0:044dfba47660 | 82 | // dlms.poll_rs485(); |
creatron | 0:044dfba47660 | 83 | } |
creatron | 0:044dfba47660 | 84 | while (1); |
creatron | 0:044dfba47660 | 85 | } |
creatron | 0:044dfba47660 | 86 | //----[ then end]------------------------------------------------------------- |
creatron | 0:044dfba47660 | 87 | |
creatron | 0:044dfba47660 | 88 |