voor willem test

Dependencies:   4DGL MODSERIAL mbed mbos

Committer:
LvdK
Date:
Tue Jan 29 14:54:14 2013 +0000
Revision:
6:e0bf3b244d7c
Goed werkende mbos versie met 0.5 sec ALIVE message

Who changed what in which revision?

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