Zoltan Hudak / Mbed 2 deprecated Manchester_Receiver

Dependencies:   Manchester mbed CRC16 ManchesterMsg

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Manchester.h"
00003 #include "CRC16.h"
00004 
00005 DigitalOut      led(LED1);
00006 Manchester      man(D3, D4, 9600);    // Tx pin, Rx pin, speed [bps]
00007 ManchesterMsg   msg(100);               // Message container (max bytes)
00008 char            str[80];                // Storage for the received array of char
00009 uint32_t        val;                    // Storage for the value received
00010 CRC16           crc16;                  // CRC16 object
00011 unsigned short  recvCRC16;              // CRC16 received in the message
00012 unsigned short  calcCRC16;              // CRC16 calculated
00013 
00014 int main(void) {
00015     man.setPreamble(5);                 // Number of sync pulses in preamble
00016     while(1) {
00017         if(man.receive(msg)) {          // Receive message
00018             
00019             // Print data length and raw data bytes
00020             printf("\r\n----------------------------------------\r\n");
00021             printf("Message length = %d, Raw data :\r\n", msg.len);
00022             for(size_t i = 0; i < msg.len; i++) {
00023                 if((i + 1) % 10 == 0)
00024                     printf("  %.2x\r\n", msg.data[i]);
00025                 else
00026                     printf("  %.2x", msg.data[i]);
00027             }
00028             printf("\r\n\r\n");
00029             
00030             // Calculate CRC16. Exclude CRC bytes (last two bytes) from calculation.
00031             calcCRC16 = crc16.calc(msg.data, msg.len - 2);            
00032             printf("Calculated CRC16 = %d\r\n", calcCRC16);
00033             
00034             // Extract data and CRC16 from the message
00035             msg >> str >> val >> recvCRC16;
00036             
00037             printf("Received   CRC16 = %d\r\n", recvCRC16);
00038             printf("\r\n");           
00039  
00040             if( calcCRC16 == recvCRC16) {
00041                 printf("Received data :\r\n");
00042                 printf("  str = %s\r\n", str);
00043                 printf("  val = 0x%x\r\n", val);
00044             }
00045             else
00046                 printf("CRC error\r\n");
00047         }
00048         else
00049             printf("Error\r\n");
00050 
00051         led = !led;
00052     }
00053 }