Manchester receiver demo.

Dependencies:   Manchester mbed CRC16 ManchesterMsg

main.cpp

Committer:
hudakz
Date:
2017-05-18
Revision:
3:0c6680f77855
Parent:
2:1cb83aa7b99a
Child:
4:649d9a4219c0

File content as of revision 3:0c6680f77855:

#include "mbed.h"
#include "Manchester.h"
#include "CRC16.h"

DigitalOut      led(LED1);

int main(void) {
    Manchester      man(p11, p12, 9600);    // Tx pin, Rx pin, speed [bps]
    ManchesterMsg   msg(100);               // Message container (max bytes)
    char            str[80];                // Storage for the received array of char
    uint32_t        val;                    // Storage for the value received
    CRC16           crc16;                  // CRC16 object
    unsigned short  msgCRC16;               // CRC16 received in message
    unsigned short  calcCRC16;              // CRC16 calculated

    //pc.baud(9600);

    while(1) {
        if(man.receive(msg)) {      // Receive message
            // Print data length and raw data bytes
            printf("\r\n----------------------\r\n");
            printf("Length = %d\r\n", msg.len);
            printf("Raw data :\r\n");
            for(size_t i = 0; i < msg.len; i++) {
                if((i + 1) % 10 == 0)
                    printf("  %.2x\r\n", msg.data[i]);
                else
                    printf("  %.2x", msg.data[i]);
            }
            printf("\r\n");
            
            // Calculate CRC16 (CRC bytes are excluded from calculation)
            calcCRC16 = crc16.calc(msg.data, msg.len - 2); 
            
            // Extract data and CRC16 from the message
            msg >> str >> val >> msgCRC16;
            
            printf("msgCRC16  = %d\r\n", msgCRC16);
            printf("calcCRC16 = %d\r\n", calcCRC16);
            
 
            if( calcCRC16 != msgCRC16) {
                printf("CRC error\r\n");
            }
            else {
                printf("Extracted data :\r\n");
                printf("  str = %s\r\n", str);
                printf("  val = 0x%x\r\n", val);
            }
        }
        else
            printf("Error\r\n");

        led = !led;
    }
}