voor willem test

Dependencies:   4DGL MODSERIAL mbed mbos

Committer:
LvdK
Date:
Thu Jul 17 10:09:14 2014 +0000
Revision:
7:6576a287e563
CDU V21

Who changed what in which revision?

UserRevisionLine numberNew contents of line
LvdK 7:6576a287e563 1 // L. van der Kolk, ELVEDEKA, Holland //
LvdK 7:6576a287e563 2 // File: USB_receive_3.cpp
LvdK 7:6576a287e563 3
LvdK 7:6576a287e563 4 #include "mbed.h"
LvdK 7:6576a287e563 5 #include "MODSERIAL.h"
LvdK 7:6576a287e563 6 #include "debug_lvdk.h"
LvdK 7:6576a287e563 7
LvdK 7:6576a287e563 8 extern MODSERIAL USB;
LvdK 7:6576a287e563 9 extern int CDU_FS_interface;
LvdK 7:6576a287e563 10
LvdK 7:6576a287e563 11 #ifdef DEBUG_LVDK
LvdK 7:6576a287e563 12 //------- debug only -----------
LvdK 7:6576a287e563 13 extern MODSERIAL SERIAL_DEBUG;
LvdK 7:6576a287e563 14 //------------------------------
LvdK 7:6576a287e563 15 #endif
LvdK 7:6576a287e563 16
LvdK 7:6576a287e563 17 void decode_string(int nummer_of_chars);
LvdK 7:6576a287e563 18 void read_datafields(int command_number);
LvdK 7:6576a287e563 19
LvdK 7:6576a287e563 20 #define max_string_length 80 // : max length of received string starting with $ and ending with CR/LF
LvdK 7:6576a287e563 21 #define min_string_length 10 // : min length of received string starting with $ and ending with CR/LF
LvdK 7:6576a287e563 22 char string_received[max_string_length + 2]; // : holds received string starting with $ and ending with CR/LF
LvdK 7:6576a287e563 23
LvdK 7:6576a287e563 24 #define message_header "$PCDU" // : common message header in all messages
LvdK 7:6576a287e563 25 #define max_commas 10 // : max. nr of possible field separating commas in a valid message string to CDU
LvdK 7:6576a287e563 26 int comma[max_commas]; // : array with positions of all found commas in string_receved[]
LvdK 7:6576a287e563 27
LvdK 7:6576a287e563 28 #define max_nr_of_commands 10 // : max nr of possible FS-to-CDU commands
LvdK 7:6576a287e563 29 // Define array of pointers to possible FS-to-CDU commands with 3 characters:
LvdK 7:6576a287e563 30 const char *command[max_nr_of_commands] = {
LvdK 7:6576a287e563 31 "123", // : no valid CDU command nr. 0 , used for debugging only
LvdK 7:6576a287e563 32 "MSG", // : command nr. 1
LvdK 7:6576a287e563 33 "EXC", // : command nr. 2
LvdK 7:6576a287e563 34 "BLT", // : command nr. 3
LvdK 7:6576a287e563 35 "SBY", // : command nr. 4
LvdK 7:6576a287e563 36 "CLS", // : command nr. 5
LvdK 7:6576a287e563 37 "SBC", // : command nr. 6
LvdK 7:6576a287e563 38 "WTX", // : command nr. 7
LvdK 7:6576a287e563 39 "ETX", // : command nr. 8
LvdK 7:6576a287e563 40 "KTX", // : command nr. 9
LvdK 7:6576a287e563 41 };
LvdK 7:6576a287e563 42
LvdK 7:6576a287e563 43 void collect_FSdata() {
LvdK 7:6576a287e563 44 // Function reads characters from FS written in receive buffer.
LvdK 7:6576a287e563 45 // Wiil be called after each RX receive interrupt, so characters will allways be stored.
LvdK 7:6576a287e563 46 // When analyze_busy is false, function will start reading characters from defined buffer and
LvdK 7:6576a287e563 47 // collects strings starting with $ and ending with CR/LF.
LvdK 7:6576a287e563 48 // Strings shorter than min_string_length or longer than max_string_length will be ignored,
LvdK 7:6576a287e563 49 // others will be analyzed further.
LvdK 7:6576a287e563 50 // After analyzing, flag analyze_busy will be set to false again.
LvdK 7:6576a287e563 51 static int $_detected = false; // : no valid begin of string detected (init only on first call)
LvdK 7:6576a287e563 52 static int string_pntr = 0; // : pointer at begin of string (init only on first call)
LvdK 7:6576a287e563 53 static int nr_of_received_char = 0; // : counter of received characters (init only on first call)
LvdK 7:6576a287e563 54 char rx_char; // : character which is analyzed
LvdK 7:6576a287e563 55
LvdK 7:6576a287e563 56 if ( CDU_FS_interface == 0 ) { // : USB Port will be used
LvdK 7:6576a287e563 57 while ( !USB.rxBufferEmpty() )
LvdK 7:6576a287e563 58 { rx_char = USB.getc(); // : get a char from Rx buffer
LvdK 7:6576a287e563 59 #ifdef DEBUG_LVDK
LvdK 7:6576a287e563 60 // ----------- Debug only ! -------------------------------------------
LvdK 7:6576a287e563 61 //SERIAL_DEBUG.putc(rx_char); // : unprotected immediate echo of char
LvdK 7:6576a287e563 62 // --------------------------------------------------------------------
LvdK 7:6576a287e563 63 #endif
LvdK 7:6576a287e563 64 // Check for string starting with $ char:
LvdK 7:6576a287e563 65 if ( rx_char == '$' && $_detected == false ){
LvdK 7:6576a287e563 66 $_detected = true; // : begin of string is detected
LvdK 7:6576a287e563 67 string_pntr = 0; // : set pointer to begin of string_received[] buffer
LvdK 7:6576a287e563 68 }
LvdK 7:6576a287e563 69 string_received[string_pntr] = rx_char;
LvdK 7:6576a287e563 70 string_pntr++;
LvdK 7:6576a287e563 71 if (string_pntr >= max_string_length) {
LvdK 7:6576a287e563 72 // command string looks too long, so start all over again:
LvdK 7:6576a287e563 73 string_pntr = 0; // : set pointer back to begin of string_received[] again
LvdK 7:6576a287e563 74 nr_of_received_char = 0; // : reset number of received chars
LvdK 7:6576a287e563 75 $_detected = false;
LvdK 7:6576a287e563 76 }
LvdK 7:6576a287e563 77 if ( rx_char == '\r' && $_detected == true ) {
LvdK 7:6576a287e563 78 if ( string_pntr > min_string_length ) { // : check minimum string length
LvdK 7:6576a287e563 79 // Received string can be interesting now because
LvdK 7:6576a287e563 80 // it starts with '$' AND it ends on New-Line AND
LvdK 7:6576a287e563 81 // it has a minimum length AND it is not too long:
LvdK 7:6576a287e563 82 string_received[string_pntr] = '\0'; // : mark end of string
LvdK 7:6576a287e563 83 #ifdef DEBUG_LVDK
LvdK 7:6576a287e563 84 // ----------- Debug only ! -------------------------------------------------------------------
LvdK 7:6576a287e563 85 // SERIAL_DEBUG.printf("string_received : %s",string_received ); // show string for debugging
LvdK 7:6576a287e563 86 // --------------------------------------------------------------------------------------------
LvdK 7:6576a287e563 87 #endif
LvdK 7:6576a287e563 88 nr_of_received_char = string_pntr;
LvdK 7:6576a287e563 89 //Call decoder to analyse this string:
LvdK 7:6576a287e563 90 decode_string(nr_of_received_char);
LvdK 7:6576a287e563 91 // Get ready for receiving new commands:
LvdK 7:6576a287e563 92 $_detected = false;
LvdK 7:6576a287e563 93 string_pntr = 0; // : set pointer back to begin of string_received[] again
LvdK 7:6576a287e563 94 nr_of_received_char = 0; // : reset number of received chars
LvdK 7:6576a287e563 95 }
LvdK 7:6576a287e563 96 else {
LvdK 7:6576a287e563 97 $_detected = false;
LvdK 7:6576a287e563 98 string_pntr = 0; // : set pointer back to begin of string_received[] again
LvdK 7:6576a287e563 99 nr_of_received_char = 0; // : reset number of received chars
LvdK 7:6576a287e563 100 }
LvdK 7:6576a287e563 101 }
LvdK 7:6576a287e563 102 }
LvdK 7:6576a287e563 103
LvdK 7:6576a287e563 104 }
LvdK 7:6576a287e563 105 }
LvdK 7:6576a287e563 106
LvdK 7:6576a287e563 107 void decode_string(int nummer_of_chars)
LvdK 7:6576a287e563 108 { // -- This function decodes a received $.....CR/LF string written in string_received[] --
LvdK 7:6576a287e563 109 // First it checks for a valid checksum and reads the positions of commas in the string.
LvdK 7:6576a287e563 110 // If checksum is OK, it will continue to look for valid 3 char FS-to-CDU commands.
LvdK 7:6576a287e563 111 // When a valid command is found, data fields will be analyzed further using the found positions
LvdK 7:6576a287e563 112 // of commas in the string.
LvdK 7:6576a287e563 113 int i,c, equal;
LvdK 7:6576a287e563 114 char byte_read, exor_byte;
LvdK 7:6576a287e563 115 char command_string[6], received_checksum[4], calc_checksum[4];
LvdK 7:6576a287e563 116 // Get checksum and position of commas in string_received[] :
LvdK 7:6576a287e563 117 exor_byte = 0;
LvdK 7:6576a287e563 118 i = 1; // : i points to first char after $
LvdK 7:6576a287e563 119 c = 1; // : position of first comma
LvdK 7:6576a287e563 120 do {
LvdK 7:6576a287e563 121 byte_read = string_received[i];
LvdK 7:6576a287e563 122 if (byte_read == ',' && c < max_commas) {
LvdK 7:6576a287e563 123 comma[c] = i;
LvdK 7:6576a287e563 124 c++;
LvdK 7:6576a287e563 125 }
LvdK 7:6576a287e563 126 if (byte_read == '*') break;
LvdK 7:6576a287e563 127 exor_byte = exor_byte ^ byte_read;
LvdK 7:6576a287e563 128 i++;
LvdK 7:6576a287e563 129 } while ( i < nummer_of_chars );
LvdK 7:6576a287e563 130
LvdK 7:6576a287e563 131 #ifdef DEBUG_LVDK
LvdK 7:6576a287e563 132 // ----------- Debug only ---------------------------------------------------------
LvdK 7:6576a287e563 133 //SERIAL_DEBUG.printf("commas found : %d\n",c-1 ); // : show commas for debugging
LvdK 7:6576a287e563 134 // --------------------------------------------------------------------------------
LvdK 7:6576a287e563 135 #endif
LvdK 7:6576a287e563 136 i++; // : i points to first checksum char after char *
LvdK 7:6576a287e563 137 strncpy(received_checksum,&string_received[i],2); // : copy 2 char checksum after *
LvdK 7:6576a287e563 138 // Get calculated checksum by transforming exor_byte in 2 hex chars (with upper case A-F) :
LvdK 7:6576a287e563 139 sprintf(calc_checksum,"%02X",exor_byte); // : + extra NULL char added by sprintf
LvdK 7:6576a287e563 140 equal = strncmp(received_checksum,calc_checksum,2);
LvdK 7:6576a287e563 141
LvdK 7:6576a287e563 142 // bypass checksum check: ------- !!!!!!!!!
LvdK 7:6576a287e563 143 equal = 0;
LvdK 7:6576a287e563 144 //-------------------------------
LvdK 7:6576a287e563 145
LvdK 7:6576a287e563 146 if (equal != 0) {
LvdK 7:6576a287e563 147 #ifdef DEBUG_LVDK
LvdK 7:6576a287e563 148 // ----------- Debug only -------------------------------------------------------
LvdK 7:6576a287e563 149 SERIAL_DEBUG.printf("checksum is NOT OK ! \n" ); // : show message for debugging
LvdK 7:6576a287e563 150 // ------------------------------------------------------------------------------
LvdK 7:6576a287e563 151 #endif
LvdK 7:6576a287e563 152 }
LvdK 7:6576a287e563 153 else { // checksum is OK, go on:
LvdK 7:6576a287e563 154 // Check for 5 char "$PCDU" header:
LvdK 7:6576a287e563 155 equal = strncmp(string_received,message_header,strlen(message_header));
LvdK 7:6576a287e563 156 if (equal != 0) {
LvdK 7:6576a287e563 157 #ifdef DEBUG_LVDK
LvdK 7:6576a287e563 158 // ----------- Debug only --------------------------------------------------------------
LvdK 7:6576a287e563 159 SERIAL_DEBUG.printf("no $PCDU header in message !\n" ); // : show message for debugging
LvdK 7:6576a287e563 160 // -------------------------------------------------------------------------------------
LvdK 7:6576a287e563 161 #endif
LvdK 7:6576a287e563 162 }
LvdK 7:6576a287e563 163 else {
LvdK 7:6576a287e563 164 // Read 3 char command after message_header:
LvdK 7:6576a287e563 165 strncpy(command_string,&string_received[strlen(message_header)],3);
LvdK 7:6576a287e563 166 #ifdef DEBUG_LVDK
LvdK 7:6576a287e563 167 // ----------- Debug only ---------------------------------------------------------------------
LvdK 7:6576a287e563 168 //SERIAL_DEBUG.printf("\ncommand found : %3s\n",command_string ); // : show command for debugging
LvdK 7:6576a287e563 169 //SERIAL_DEBUG.printf("commas found : %d\n",c-1 ); // : show commas for debugging
LvdK 7:6576a287e563 170 // --------------------------------------------------------------------------------------------
LvdK 7:6576a287e563 171 #endif
LvdK 7:6576a287e563 172 // Compare found string with known 3 char command list:
LvdK 7:6576a287e563 173 i = 0;
LvdK 7:6576a287e563 174 do {
LvdK 7:6576a287e563 175 equal = strncmp(&command_string[0],command[i],3);
LvdK 7:6576a287e563 176 if( equal == 0) break;
LvdK 7:6576a287e563 177 i++;
LvdK 7:6576a287e563 178 } while ( i < max_nr_of_commands);
LvdK 7:6576a287e563 179
LvdK 7:6576a287e563 180 #ifdef DEBUG_LVDK
LvdK 7:6576a287e563 181 // ----------- Debug only ---------------------------------------------------------------
LvdK 7:6576a287e563 182 //SERIAL_DEBUG.printf("command number is : %d\n",i ); // : show command nr for debugging
LvdK 7:6576a287e563 183 // --------------------------------------------------------------------------------------
LvdK 7:6576a287e563 184 #endif
LvdK 7:6576a287e563 185
LvdK 7:6576a287e563 186 if (equal == 0) {
LvdK 7:6576a287e563 187 // Command is known now, so now read all data fields:
LvdK 7:6576a287e563 188 read_datafields(i);
LvdK 7:6576a287e563 189 }
LvdK 7:6576a287e563 190 }
LvdK 7:6576a287e563 191 }
LvdK 7:6576a287e563 192
LvdK 7:6576a287e563 193 }
LvdK 7:6576a287e563 194
LvdK 7:6576a287e563 195