Brad Smith
/
rtcController
Gets current time from the PC over a serial connection
Python script available on the wiki page
Parser.cpp@0:c8dd8b2c6942, 2015-11-12 (annotated)
- Committer:
- mbedDevLondon
- Date:
- Thu Nov 12 19:17:47 2015 +0000
- Revision:
- 0:c8dd8b2c6942
Converted to FRDM board
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedDevLondon | 0:c8dd8b2c6942 | 1 | #include "./Parser.h" |
mbedDevLondon | 0:c8dd8b2c6942 | 2 | #include <string.h> |
mbedDevLondon | 0:c8dd8b2c6942 | 3 | #include <stdio.h> |
mbedDevLondon | 0:c8dd8b2c6942 | 4 | |
mbedDevLondon | 0:c8dd8b2c6942 | 5 | char *commandTokens[20]; |
mbedDevLondon | 0:c8dd8b2c6942 | 6 | char invalidMessage[] = "INVALID NEMA SENTENCE"; |
mbedDevLondon | 0:c8dd8b2c6942 | 7 | char endOfTokensMessage[] = "END OF TOKENS"; |
mbedDevLondon | 0:c8dd8b2c6942 | 8 | |
mbedDevLondon | 0:c8dd8b2c6942 | 9 | char validateCommandSentence(char *pSentence) |
mbedDevLondon | 0:c8dd8b2c6942 | 10 | { |
mbedDevLondon | 0:c8dd8b2c6942 | 11 | char chkSumToken[3]; |
mbedDevLondon | 0:c8dd8b2c6942 | 12 | unsigned char calculatedChecksum = 0; |
mbedDevLondon | 0:c8dd8b2c6942 | 13 | char errorCode = NOERROR; |
mbedDevLondon | 0:c8dd8b2c6942 | 14 | printf("\n\r\n\r>>>>>>>>>>>> %s <<<<\n\n\n\r", pSentence); |
mbedDevLondon | 0:c8dd8b2c6942 | 15 | if(*pSentence != '$') |
mbedDevLondon | 0:c8dd8b2c6942 | 16 | { |
mbedDevLondon | 0:c8dd8b2c6942 | 17 | printf("error no $\n\r"); |
mbedDevLondon | 0:c8dd8b2c6942 | 18 | return ERROR; |
mbedDevLondon | 0:c8dd8b2c6942 | 19 | } |
mbedDevLondon | 0:c8dd8b2c6942 | 20 | pSentence++; |
mbedDevLondon | 0:c8dd8b2c6942 | 21 | |
mbedDevLondon | 0:c8dd8b2c6942 | 22 | while (*pSentence != '\0' && *pSentence != '*') { |
mbedDevLondon | 0:c8dd8b2c6942 | 23 | calculatedChecksum ^= *pSentence; |
mbedDevLondon | 0:c8dd8b2c6942 | 24 | pSentence++; |
mbedDevLondon | 0:c8dd8b2c6942 | 25 | } |
mbedDevLondon | 0:c8dd8b2c6942 | 26 | |
mbedDevLondon | 0:c8dd8b2c6942 | 27 | if(*pSentence == '*'){ |
mbedDevLondon | 0:c8dd8b2c6942 | 28 | sprintf(chkSumToken, "%02x", calculatedChecksum); |
mbedDevLondon | 0:c8dd8b2c6942 | 29 | printf("Calc chksum %s<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n\r", chkSumToken); |
mbedDevLondon | 0:c8dd8b2c6942 | 30 | pSentence++; |
mbedDevLondon | 0:c8dd8b2c6942 | 31 | if(strcmp(pSentence, chkSumToken) != 0) |
mbedDevLondon | 0:c8dd8b2c6942 | 32 | { |
mbedDevLondon | 0:c8dd8b2c6942 | 33 | printf("checksum error>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); |
mbedDevLondon | 0:c8dd8b2c6942 | 34 | errorCode = ERROR; |
mbedDevLondon | 0:c8dd8b2c6942 | 35 | } |
mbedDevLondon | 0:c8dd8b2c6942 | 36 | } |
mbedDevLondon | 0:c8dd8b2c6942 | 37 | else { |
mbedDevLondon | 0:c8dd8b2c6942 | 38 | printf("error:***** %s\n\r",pSentence); |
mbedDevLondon | 0:c8dd8b2c6942 | 39 | errorCode = ERROR; |
mbedDevLondon | 0:c8dd8b2c6942 | 40 | } |
mbedDevLondon | 0:c8dd8b2c6942 | 41 | |
mbedDevLondon | 0:c8dd8b2c6942 | 42 | printf("<<<<< errorcode %d\n\r", errorCode); |
mbedDevLondon | 0:c8dd8b2c6942 | 43 | return errorCode; |
mbedDevLondon | 0:c8dd8b2c6942 | 44 | } |
mbedDevLondon | 0:c8dd8b2c6942 | 45 | |
mbedDevLondon | 0:c8dd8b2c6942 | 46 | // parseCommandSentence() requires that the trailing <cr><lf> characters |
mbedDevLondon | 0:c8dd8b2c6942 | 47 | // be removed prior to calling this function. Failing to do so will cause |
mbedDevLondon | 0:c8dd8b2c6942 | 48 | // validateNemaSentence() to report an ERROR |
mbedDevLondon | 0:c8dd8b2c6942 | 49 | |
mbedDevLondon | 0:c8dd8b2c6942 | 50 | void parseCommandSentence( char *pBuffer) |
mbedDevLondon | 0:c8dd8b2c6942 | 51 | { |
mbedDevLondon | 0:c8dd8b2c6942 | 52 | if (validateCommandSentence(pBuffer) == ERROR) { |
mbedDevLondon | 0:c8dd8b2c6942 | 53 | commandTokens[0]= invalidMessage; |
mbedDevLondon | 0:c8dd8b2c6942 | 54 | commandTokens[1]= endOfTokensMessage; |
mbedDevLondon | 0:c8dd8b2c6942 | 55 | } |
mbedDevLondon | 0:c8dd8b2c6942 | 56 | else{ |
mbedDevLondon | 0:c8dd8b2c6942 | 57 | //so valid sentence ................................... |
mbedDevLondon | 0:c8dd8b2c6942 | 58 | int tokenCounter = 0; |
mbedDevLondon | 0:c8dd8b2c6942 | 59 | |
mbedDevLondon | 0:c8dd8b2c6942 | 60 | commandTokens[tokenCounter]=pBuffer; |
mbedDevLondon | 0:c8dd8b2c6942 | 61 | pBuffer++; |
mbedDevLondon | 0:c8dd8b2c6942 | 62 | tokenCounter++; |
mbedDevLondon | 0:c8dd8b2c6942 | 63 | |
mbedDevLondon | 0:c8dd8b2c6942 | 64 | while( *pBuffer != '\0'){ |
mbedDevLondon | 0:c8dd8b2c6942 | 65 | //until the end of sentence is found, |
mbedDevLondon | 0:c8dd8b2c6942 | 66 | //search for comma deliminators |
mbedDevLondon | 0:c8dd8b2c6942 | 67 | if(*pBuffer == ','){ |
mbedDevLondon | 0:c8dd8b2c6942 | 68 | //found end of token |
mbedDevLondon | 0:c8dd8b2c6942 | 69 | *pBuffer = '\0'; //replace deliminator with a null character |
mbedDevLondon | 0:c8dd8b2c6942 | 70 | |
mbedDevLondon | 0:c8dd8b2c6942 | 71 | pBuffer++; // save address of next token |
mbedDevLondon | 0:c8dd8b2c6942 | 72 | commandTokens[tokenCounter]=pBuffer; |
mbedDevLondon | 0:c8dd8b2c6942 | 73 | tokenCounter++; |
mbedDevLondon | 0:c8dd8b2c6942 | 74 | } |
mbedDevLondon | 0:c8dd8b2c6942 | 75 | else pBuffer++; |
mbedDevLondon | 0:c8dd8b2c6942 | 76 | }//eo while not end of string |
mbedDevLondon | 0:c8dd8b2c6942 | 77 | commandTokens[tokenCounter] = endOfTokensMessage; |
mbedDevLondon | 0:c8dd8b2c6942 | 78 | |
mbedDevLondon | 0:c8dd8b2c6942 | 79 | }//eo Valid sentence::::::::::::::::::::::::::::::::::::: |
mbedDevLondon | 0:c8dd8b2c6942 | 80 | |
mbedDevLondon | 0:c8dd8b2c6942 | 81 | }//eo parseNemaSentence( char *pBuffer) ======================================== |
mbedDevLondon | 0:c8dd8b2c6942 | 82 | |
mbedDevLondon | 0:c8dd8b2c6942 | 83 | |
mbedDevLondon | 0:c8dd8b2c6942 | 84 | //calculate a XOR checksum for a string |
mbedDevLondon | 0:c8dd8b2c6942 | 85 | char calcChecksum(char *pSentence) |
mbedDevLondon | 0:c8dd8b2c6942 | 86 | { |
mbedDevLondon | 0:c8dd8b2c6942 | 87 | unsigned char calculatedChecksum = 0; |
mbedDevLondon | 0:c8dd8b2c6942 | 88 | |
mbedDevLondon | 0:c8dd8b2c6942 | 89 | while (*pSentence != '\0' ) { |
mbedDevLondon | 0:c8dd8b2c6942 | 90 | calculatedChecksum ^= *pSentence; |
mbedDevLondon | 0:c8dd8b2c6942 | 91 | pSentence++; |
mbedDevLondon | 0:c8dd8b2c6942 | 92 | } |
mbedDevLondon | 0:c8dd8b2c6942 | 93 | return calculatedChecksum; |
mbedDevLondon | 0:c8dd8b2c6942 | 94 | } |