stingr lib for v8_1

Committer:
jmoreno10
Date:
Mon Oct 29 12:45:27 2018 +0000
Revision:
0:0159aa4d2062
v8_1 library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jmoreno10 0:0159aa4d2062 1 /*
jmoreno10 0:0159aa4d2062 2 * mbed STINGR Library
jmoreno10 0:0159aa4d2062 3 * Copyright (c) 2018
jmoreno10 0:0159aa4d2062 4 *
jmoreno10 0:0159aa4d2062 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
jmoreno10 0:0159aa4d2062 6 * of this software and associated documentation files (the "Software"), to deal
jmoreno10 0:0159aa4d2062 7 * in the Software without restriction, including without limitation the rights
jmoreno10 0:0159aa4d2062 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jmoreno10 0:0159aa4d2062 9 * copies of the Software, and to permit persons to whom the Software is
jmoreno10 0:0159aa4d2062 10 * furnished to do so, subject to the following conditions:
jmoreno10 0:0159aa4d2062 11 *
jmoreno10 0:0159aa4d2062 12 * The above copyright notice and this permission notice shall be included in
jmoreno10 0:0159aa4d2062 13 * all copies or substantial portions of the Software.
jmoreno10 0:0159aa4d2062 14 *
jmoreno10 0:0159aa4d2062 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jmoreno10 0:0159aa4d2062 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jmoreno10 0:0159aa4d2062 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jmoreno10 0:0159aa4d2062 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jmoreno10 0:0159aa4d2062 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jmoreno10 0:0159aa4d2062 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jmoreno10 0:0159aa4d2062 21 * THE SOFTWARE.
jmoreno10 0:0159aa4d2062 22 */
jmoreno10 0:0159aa4d2062 23
jmoreno10 0:0159aa4d2062 24 #include "Stingr.h"
jmoreno10 0:0159aa4d2062 25 #include "mbed.h"
jmoreno10 0:0159aa4d2062 26 #include "rtos.h"
jmoreno10 0:0159aa4d2062 27
jmoreno10 0:0159aa4d2062 28 #define QUERY_ESN 'a'
jmoreno10 0:0159aa4d2062 29 #define QUERY_BURSTS 'b'
jmoreno10 0:0159aa4d2062 30 #define QUERY_FIRMWARE 'c'
jmoreno10 0:0159aa4d2062 31 #define QUERY_SETUP 'd'
jmoreno10 0:0159aa4d2062 32 #define QUERY_HARDWARE 'e'
jmoreno10 0:0159aa4d2062 33 #define NAK_COMMAND 'f'
jmoreno10 0:0159aa4d2062 34 #define SETUP 'g'
jmoreno10 0:0159aa4d2062 35 #define SEND_DATA 'h'
jmoreno10 0:0159aa4d2062 36 #define ABORT_TRANSMISSION 'i'
jmoreno10 0:0159aa4d2062 37
jmoreno10 0:0159aa4d2062 38 // These two lines redundant?
jmoreno10 0:0159aa4d2062 39 Serial _stingrUART(p13, p14, 9600); // tx, rx Comunicación Serial con el STINGR
jmoreno10 0:0159aa4d2062 40 Serial _pc(USBTX, USBRX, 9600); // tx, rx Comunicación Serial con la PC
jmoreno10 0:0159aa4d2062 41
jmoreno10 0:0159aa4d2062 42 // Pin Digital de entrada "CTS" en modo Pull-Up
jmoreno10 0:0159aa4d2062 43 // para encontrarse normalmente a VCC cuando no haya un pulso.
jmoreno10 0:0159aa4d2062 44 DigitalIn _CTS(p11, PullUp);
jmoreno10 0:0159aa4d2062 45
jmoreno10 0:0159aa4d2062 46 // Pin Digital de Salida "RTS"
jmoreno10 0:0159aa4d2062 47 // Predefinido para valer 1 en su estado inactivo dentro del código.
jmoreno10 0:0159aa4d2062 48 DigitalOut _RTS(p12, 1);
jmoreno10 0:0159aa4d2062 49
jmoreno10 0:0159aa4d2062 50 DigitalOut led1(LED1);
jmoreno10 0:0159aa4d2062 51 DigitalOut led2(LED2);
jmoreno10 0:0159aa4d2062 52 DigitalOut led3(LED3);
jmoreno10 0:0159aa4d2062 53 DigitalOut led4(LED4);
jmoreno10 0:0159aa4d2062 54
jmoreno10 0:0159aa4d2062 55 // Constructor
jmoreno10 0:0159aa4d2062 56 Stingr::Stingr()
jmoreno10 0:0159aa4d2062 57 {
jmoreno10 0:0159aa4d2062 58 incomingByte = 0;
jmoreno10 0:0159aa4d2062 59 flag = 1;
jmoreno10 0:0159aa4d2062 60 num = 0;
jmoreno10 0:0159aa4d2062 61 incomingByteR = 0;
jmoreno10 0:0159aa4d2062 62 incomingByteX = 0;
jmoreno10 0:0159aa4d2062 63
jmoreno10 0:0159aa4d2062 64 // For Luis Fernando:
jmoreno10 0:0159aa4d2062 65 // This thread is for the Mbed to read and serial print responses from STINGR
jmoreno10 0:0159aa4d2062 66 t1.start(callback(this,&Stingr::respuesta));
jmoreno10 0:0159aa4d2062 67
jmoreno10 0:0159aa4d2062 68 // For Luis Fernando:
jmoreno10 0:0159aa4d2062 69 // This thread is for reading commands from serial PC. (May be commented)
jmoreno10 0:0159aa4d2062 70 t2.start(callback(this,&Stingr::PC_communication));
jmoreno10 0:0159aa4d2062 71 }
jmoreno10 0:0159aa4d2062 72
jmoreno10 0:0159aa4d2062 73 // Accessor
jmoreno10 0:0159aa4d2062 74 const char* Stingr::get_resp() const
jmoreno10 0:0159aa4d2062 75 {
jmoreno10 0:0159aa4d2062 76 return _resp;
jmoreno10 0:0159aa4d2062 77 }
jmoreno10 0:0159aa4d2062 78
jmoreno10 0:0159aa4d2062 79 // Mutator
jmoreno10 0:0159aa4d2062 80 void Stingr::setPacketByte(int incomingByte)
jmoreno10 0:0159aa4d2062 81 {
jmoreno10 0:0159aa4d2062 82 _packet[num] = incomingByte;
jmoreno10 0:0159aa4d2062 83 num++;
jmoreno10 0:0159aa4d2062 84 }
jmoreno10 0:0159aa4d2062 85
jmoreno10 0:0159aa4d2062 86 /******************************************************************************/
jmoreno10 0:0159aa4d2062 87 /********************[SERIAL PACKET PROCESSING FUNCTIONS]**********************/
jmoreno10 0:0159aa4d2062 88 /******************************************************************************/
jmoreno10 0:0159aa4d2062 89
jmoreno10 0:0159aa4d2062 90 void Stingr::clearPacket()
jmoreno10 0:0159aa4d2062 91 {
jmoreno10 0:0159aa4d2062 92 num = 0;
jmoreno10 0:0159aa4d2062 93 for(int i = 0; i < 15 ; i++)
jmoreno10 0:0159aa4d2062 94 _packet[i] = 0;
jmoreno10 0:0159aa4d2062 95 }
jmoreno10 0:0159aa4d2062 96
jmoreno10 0:0159aa4d2062 97 void Stingr::printPacket()
jmoreno10 0:0159aa4d2062 98 {
jmoreno10 0:0159aa4d2062 99 _pc.printf("\nResponse(Stingr)dec: \t");
jmoreno10 0:0159aa4d2062 100 for(int i = 0; i < 15 ; i++)
jmoreno10 0:0159aa4d2062 101 {
jmoreno10 0:0159aa4d2062 102 _pc.printf("%u",_packet[i]); // Format specifier
jmoreno10 0:0159aa4d2062 103 _pc.printf(" ");
jmoreno10 0:0159aa4d2062 104 }
jmoreno10 0:0159aa4d2062 105 }
jmoreno10 0:0159aa4d2062 106
jmoreno10 0:0159aa4d2062 107 void Stingr::waitCTS()
jmoreno10 0:0159aa4d2062 108 {
jmoreno10 0:0159aa4d2062 109 Thread::wait(200); // Se da un tiempo para que el analizador se estabilice
jmoreno10 0:0159aa4d2062 110 incomingByte=0;
jmoreno10 0:0159aa4d2062 111 //pc.printf("El valor de CTS es %d\n\r",CTS.read()); // Se lee el valor de la variable CTS, la cual debe ser 1
jmoreno10 0:0159aa4d2062 112 //pc.printf("El valor de RTS es %d\n\r",RTS.read()); // Se lee el valor de la variable RTS, la cual debe ser 1
jmoreno10 0:0159aa4d2062 113 _RTS=0; // Se manda un pulso en bajo en RTS, para inicial el proceso de transmisión
jmoreno10 0:0159aa4d2062 114
jmoreno10 0:0159aa4d2062 115 while(flag==1)
jmoreno10 0:0159aa4d2062 116 {// Flag inicialmente vale 1, así que el ciclo while cambiará hasta que esa condición no se cumpla
jmoreno10 0:0159aa4d2062 117 flag=_CTS.read(); // Cuando entra el ciclo, se iguala flag a CTS, el cual cuando cambie a 0 provocará que termine el while (máx 125 ms)
jmoreno10 0:0159aa4d2062 118 //pc.printf("El valor de flag es %d\n\r", flag); // Se imprime el valor de flag, para identificar cuando termina el ciclo while
jmoreno10 0:0159aa4d2062 119 }
jmoreno10 0:0159aa4d2062 120 }
jmoreno10 0:0159aa4d2062 121
jmoreno10 0:0159aa4d2062 122 void Stingr::postCommand()
jmoreno10 0:0159aa4d2062 123 {
jmoreno10 0:0159aa4d2062 124 Thread::wait(10); // Se esperan .1 segundos una vez que se terminaron de hacer las transmisiones
jmoreno10 0:0159aa4d2062 125 //El tiempo total de transmisión es; el wait previo a las transmisiones, el tiempo que tarda el Mu en enviar los datos y el wait posterior a la transmisión
jmoreno10 0:0159aa4d2062 126 _RTS=1;
jmoreno10 0:0159aa4d2062 127 Thread::wait(150);
jmoreno10 0:0159aa4d2062 128 //pc.printf("\n\rCTS: %d\n\r",CTS.read());
jmoreno10 0:0159aa4d2062 129 flag=1;
jmoreno10 0:0159aa4d2062 130 }
jmoreno10 0:0159aa4d2062 131
jmoreno10 0:0159aa4d2062 132 void Stingr::command(char* com)
jmoreno10 0:0159aa4d2062 133 {
jmoreno10 0:0159aa4d2062 134 char c = com[0];
jmoreno10 0:0159aa4d2062 135
jmoreno10 0:0159aa4d2062 136 switch(c)
jmoreno10 0:0159aa4d2062 137 {
jmoreno10 0:0159aa4d2062 138 case QUERY_ESN:
jmoreno10 0:0159aa4d2062 139 query_ESN();
jmoreno10 0:0159aa4d2062 140 break;
jmoreno10 0:0159aa4d2062 141
jmoreno10 0:0159aa4d2062 142 case QUERY_BURSTS:
jmoreno10 0:0159aa4d2062 143 query_Bursts();
jmoreno10 0:0159aa4d2062 144 break;
jmoreno10 0:0159aa4d2062 145
jmoreno10 0:0159aa4d2062 146 case QUERY_FIRMWARE:
jmoreno10 0:0159aa4d2062 147 query_Firmware();
jmoreno10 0:0159aa4d2062 148 break;
jmoreno10 0:0159aa4d2062 149
jmoreno10 0:0159aa4d2062 150 case QUERY_SETUP:
jmoreno10 0:0159aa4d2062 151 query_Setup();
jmoreno10 0:0159aa4d2062 152 break;
jmoreno10 0:0159aa4d2062 153
jmoreno10 0:0159aa4d2062 154 case QUERY_HARDWARE:
jmoreno10 0:0159aa4d2062 155 query_Hardware();
jmoreno10 0:0159aa4d2062 156 break;
jmoreno10 0:0159aa4d2062 157
jmoreno10 0:0159aa4d2062 158 case NAK_COMMAND:
jmoreno10 0:0159aa4d2062 159 NAK_command();
jmoreno10 0:0159aa4d2062 160 break;
jmoreno10 0:0159aa4d2062 161
jmoreno10 0:0159aa4d2062 162 case SETUP:
jmoreno10 0:0159aa4d2062 163 _setup(com); //* later make this function to pass buf parameter
jmoreno10 0:0159aa4d2062 164 break;
jmoreno10 0:0159aa4d2062 165
jmoreno10 0:0159aa4d2062 166 case SEND_DATA:
jmoreno10 0:0159aa4d2062 167 send_Data(com);
jmoreno10 0:0159aa4d2062 168 break;
jmoreno10 0:0159aa4d2062 169
jmoreno10 0:0159aa4d2062 170 case ABORT_TRANSMISSION:
jmoreno10 0:0159aa4d2062 171 abort_Transmission();
jmoreno10 0:0159aa4d2062 172 break;
jmoreno10 0:0159aa4d2062 173
jmoreno10 0:0159aa4d2062 174 default:
jmoreno10 0:0159aa4d2062 175 {
jmoreno10 0:0159aa4d2062 176 //do nothing
jmoreno10 0:0159aa4d2062 177 }
jmoreno10 0:0159aa4d2062 178 }
jmoreno10 0:0159aa4d2062 179 }
jmoreno10 0:0159aa4d2062 180
jmoreno10 0:0159aa4d2062 181 void Stingr::execute(char* com)
jmoreno10 0:0159aa4d2062 182 {
jmoreno10 0:0159aa4d2062 183 char c = com[0];
jmoreno10 0:0159aa4d2062 184
jmoreno10 0:0159aa4d2062 185 switch(c)
jmoreno10 0:0159aa4d2062 186 {
jmoreno10 0:0159aa4d2062 187 case 'a':
jmoreno10 0:0159aa4d2062 188 case 'b':
jmoreno10 0:0159aa4d2062 189 case 'c':
jmoreno10 0:0159aa4d2062 190 case 'd':
jmoreno10 0:0159aa4d2062 191 case 'e':
jmoreno10 0:0159aa4d2062 192 case 'f':
jmoreno10 0:0159aa4d2062 193 case 'g':
jmoreno10 0:0159aa4d2062 194 case 'h':
jmoreno10 0:0159aa4d2062 195 case 'i':
jmoreno10 0:0159aa4d2062 196 // if 'a'-'i', set _resp to garbage for 1st call
jmoreno10 0:0159aa4d2062 197 strncpy(_resp,"garbage",sizeof(_resp));
jmoreno10 0:0159aa4d2062 198 break;
jmoreno10 0:0159aa4d2062 199 default:
jmoreno10 0:0159aa4d2062 200 {
jmoreno10 0:0159aa4d2062 201 // do nothing for 2nd call.
jmoreno10 0:0159aa4d2062 202 }
jmoreno10 0:0159aa4d2062 203 }
jmoreno10 0:0159aa4d2062 204 }
jmoreno10 0:0159aa4d2062 205
jmoreno10 0:0159aa4d2062 206 /******************************************************************************/
jmoreno10 0:0159aa4d2062 207 /************************[STINGR COMMAND FUNCTIONS]****************************/
jmoreno10 0:0159aa4d2062 208 /******************************************************************************/
jmoreno10 0:0159aa4d2062 209
jmoreno10 0:0159aa4d2062 210 //0x01 Query ESN
jmoreno10 0:0159aa4d2062 211 void Stingr::query_ESN()
jmoreno10 0:0159aa4d2062 212 {
jmoreno10 0:0159aa4d2062 213 led1=!led1;
jmoreno10 0:0159aa4d2062 214
jmoreno10 0:0159aa4d2062 215 _pc.printf("\r0x01\t\t\tQuery ESN\n");
jmoreno10 0:0159aa4d2062 216 _pc.printf("Command(HEX):\t\tAA 5 1 50 D5\n\r");
jmoreno10 0:0159aa4d2062 217 _pc.printf("Response(Stingr)HEX:\t");
jmoreno10 0:0159aa4d2062 218
jmoreno10 0:0159aa4d2062 219 waitCTS();
jmoreno10 0:0159aa4d2062 220 Thread::wait(10);
jmoreno10 0:0159aa4d2062 221
jmoreno10 0:0159aa4d2062 222 _stingrUART.putc(0XAA);
jmoreno10 0:0159aa4d2062 223 _stingrUART.putc(0X05);
jmoreno10 0:0159aa4d2062 224 _stingrUART.putc(0X01);
jmoreno10 0:0159aa4d2062 225 _stingrUART.putc(0X50);
jmoreno10 0:0159aa4d2062 226 _stingrUART.putc(0XD5);
jmoreno10 0:0159aa4d2062 227
jmoreno10 0:0159aa4d2062 228 postCommand();
jmoreno10 0:0159aa4d2062 229 printPacket();
jmoreno10 0:0159aa4d2062 230
jmoreno10 0:0159aa4d2062 231 // Correct ESN
jmoreno10 0:0159aa4d2062 232 if (_packet[3] == 0 && // 0x00
jmoreno10 0:0159aa4d2062 233 _packet[4] == 41 && // 0x29
jmoreno10 0:0159aa4d2062 234 _packet[5] == 72 && // 0x48 //0x43 STINGR TLE
jmoreno10 0:0159aa4d2062 235 _packet[6] == 254) // 0xFE //0xB3 STINGR TLE
jmoreno10 0:0159aa4d2062 236 {
jmoreno10 0:0159aa4d2062 237 clearPacket();
jmoreno10 0:0159aa4d2062 238 _pc.printf("\nQuery ESN is correct. VALID\n");
jmoreno10 0:0159aa4d2062 239 strncpy(_resp,"Query ESN is correct",sizeof(_resp));
jmoreno10 0:0159aa4d2062 240 }
jmoreno10 0:0159aa4d2062 241 // Wrong ESN
jmoreno10 0:0159aa4d2062 242 else
jmoreno10 0:0159aa4d2062 243 {
jmoreno10 0:0159aa4d2062 244 clearPacket();
jmoreno10 0:0159aa4d2062 245 _pc.printf("\nNAK response. INVALID\n");
jmoreno10 0:0159aa4d2062 246 strncpy(_resp,"NAK response (Query ESN)",sizeof(_resp));
jmoreno10 0:0159aa4d2062 247 }
jmoreno10 0:0159aa4d2062 248 }
jmoreno10 0:0159aa4d2062 249
jmoreno10 0:0159aa4d2062 250 //0x04 Query Bursts
jmoreno10 0:0159aa4d2062 251 void Stingr::query_Bursts()
jmoreno10 0:0159aa4d2062 252 {
jmoreno10 0:0159aa4d2062 253 int bursts = 0; //default value
jmoreno10 0:0159aa4d2062 254
jmoreno10 0:0159aa4d2062 255 led2=!led2;
jmoreno10 0:0159aa4d2062 256 _pc.printf("\r0x04\t\t\tQuery Burst Remaining\n");
jmoreno10 0:0159aa4d2062 257 _pc.printf("Command(HEX):\t\tAA 5 4 FD 82\n\r");
jmoreno10 0:0159aa4d2062 258 _pc.printf("Response(Stingr)HEX:\t");
jmoreno10 0:0159aa4d2062 259 waitCTS();
jmoreno10 0:0159aa4d2062 260 Thread::wait(10);
jmoreno10 0:0159aa4d2062 261
jmoreno10 0:0159aa4d2062 262 _stingrUART.putc(0XAA);
jmoreno10 0:0159aa4d2062 263 _stingrUART.putc(0X05);
jmoreno10 0:0159aa4d2062 264 _stingrUART.putc(0X04);
jmoreno10 0:0159aa4d2062 265 _stingrUART.putc(0XFD);
jmoreno10 0:0159aa4d2062 266 _stingrUART.putc(0X82);
jmoreno10 0:0159aa4d2062 267
jmoreno10 0:0159aa4d2062 268 postCommand();
jmoreno10 0:0159aa4d2062 269 printPacket();
jmoreno10 0:0159aa4d2062 270
jmoreno10 0:0159aa4d2062 271 // NAK response
jmoreno10 0:0159aa4d2062 272 if (_packet[0] == 170 && // 0xAA
jmoreno10 0:0159aa4d2062 273 _packet[1] == 5 && // 0x05
jmoreno10 0:0159aa4d2062 274 _packet[2] == 255 && // 0xFF
jmoreno10 0:0159aa4d2062 275 _packet[3] == 161 && // 0xA1
jmoreno10 0:0159aa4d2062 276 _packet[4] == 203) // 0xCB
jmoreno10 0:0159aa4d2062 277 {
jmoreno10 0:0159aa4d2062 278 clearPacket();
jmoreno10 0:0159aa4d2062 279
jmoreno10 0:0159aa4d2062 280 _pc.printf("\nNAK response. INVALID");
jmoreno10 0:0159aa4d2062 281 strncpy(_resp,"NAK response (Query Bursts)",sizeof(_resp));
jmoreno10 0:0159aa4d2062 282 }
jmoreno10 0:0159aa4d2062 283 // ACK response
jmoreno10 0:0159aa4d2062 284 else
jmoreno10 0:0159aa4d2062 285 {
jmoreno10 0:0159aa4d2062 286 bursts = _packet[3];
jmoreno10 0:0159aa4d2062 287 clearPacket();
jmoreno10 0:0159aa4d2062 288
jmoreno10 0:0159aa4d2062 289 char qry[2];
jmoreno10 0:0159aa4d2062 290 sprintf(qry,"%d",bursts);
jmoreno10 0:0159aa4d2062 291 char strt[] = "Bursts Remaining is ";
jmoreno10 0:0159aa4d2062 292 strcat(strt,qry);
jmoreno10 0:0159aa4d2062 293
jmoreno10 0:0159aa4d2062 294 _pc.printf("\nBursts Remaining: \t");
jmoreno10 0:0159aa4d2062 295 _pc.printf("%u",bursts);
jmoreno10 0:0159aa4d2062 296 _pc.printf("\n");
jmoreno10 0:0159aa4d2062 297
jmoreno10 0:0159aa4d2062 298 strncpy(_resp,strt,sizeof(_resp));
jmoreno10 0:0159aa4d2062 299 }
jmoreno10 0:0159aa4d2062 300 }
jmoreno10 0:0159aa4d2062 301
jmoreno10 0:0159aa4d2062 302 //0x05 Query Firmware
jmoreno10 0:0159aa4d2062 303 void Stingr::query_Firmware()
jmoreno10 0:0159aa4d2062 304 {
jmoreno10 0:0159aa4d2062 305 led3=!led3;
jmoreno10 0:0159aa4d2062 306 _pc.printf("\r0x05\t\t\tQuery Firmware Version\n");
jmoreno10 0:0159aa4d2062 307 _pc.printf("Command(HEX):\t\tAA 5 5 74 93\n\r");
jmoreno10 0:0159aa4d2062 308 _pc.printf("Response(Stingr)HEX:\t");
jmoreno10 0:0159aa4d2062 309 waitCTS();
jmoreno10 0:0159aa4d2062 310 Thread::wait(10);
jmoreno10 0:0159aa4d2062 311 _stingrUART.putc(0XAA);
jmoreno10 0:0159aa4d2062 312 _stingrUART.putc(0X05);
jmoreno10 0:0159aa4d2062 313 _stingrUART.putc(0X05);
jmoreno10 0:0159aa4d2062 314 _stingrUART.putc(0X74);
jmoreno10 0:0159aa4d2062 315 _stingrUART.putc(0X93);
jmoreno10 0:0159aa4d2062 316
jmoreno10 0:0159aa4d2062 317 postCommand();
jmoreno10 0:0159aa4d2062 318 printPacket();
jmoreno10 0:0159aa4d2062 319 if (_packet[3] == 1 && // 0x01
jmoreno10 0:0159aa4d2062 320 _packet[4] == 3) // 0x03
jmoreno10 0:0159aa4d2062 321 {
jmoreno10 0:0159aa4d2062 322 clearPacket();
jmoreno10 0:0159aa4d2062 323 _pc.printf("\nResponse Processing:\tQuery Firmware is correct. VALID\n");
jmoreno10 0:0159aa4d2062 324 _pc.printf(" Firmware Version: 1.3\n");
jmoreno10 0:0159aa4d2062 325 strncpy(_resp,"Query Firmware is correct",sizeof(_resp));
jmoreno10 0:0159aa4d2062 326 }
jmoreno10 0:0159aa4d2062 327 else
jmoreno10 0:0159aa4d2062 328 {
jmoreno10 0:0159aa4d2062 329 clearPacket();
jmoreno10 0:0159aa4d2062 330 _pc.printf("NAK. INVALID");
jmoreno10 0:0159aa4d2062 331 strncpy(_resp,"NAK response (Query Firmware)",sizeof(_resp));
jmoreno10 0:0159aa4d2062 332 }
jmoreno10 0:0159aa4d2062 333 }
jmoreno10 0:0159aa4d2062 334
jmoreno10 0:0159aa4d2062 335 //0x07 Query Setup
jmoreno10 0:0159aa4d2062 336 void Stingr::query_Setup()
jmoreno10 0:0159aa4d2062 337 {
jmoreno10 0:0159aa4d2062 338 int numSetup = 0;
jmoreno10 0:0159aa4d2062 339 led4=!led4;
jmoreno10 0:0159aa4d2062 340 _pc.printf("\r0x07\t\t\tQuery Setup\n");
jmoreno10 0:0159aa4d2062 341 _pc.printf("Command(HEX):\t\tAA 5 7 66 B0\n\r");
jmoreno10 0:0159aa4d2062 342 _pc.printf("Response(Stingr)HEX:\t");
jmoreno10 0:0159aa4d2062 343 waitCTS();
jmoreno10 0:0159aa4d2062 344 Thread::wait(10);
jmoreno10 0:0159aa4d2062 345
jmoreno10 0:0159aa4d2062 346 _stingrUART.putc(0XAA);
jmoreno10 0:0159aa4d2062 347 _stingrUART.putc(0X05);
jmoreno10 0:0159aa4d2062 348 _stingrUART.putc(0X07);
jmoreno10 0:0159aa4d2062 349 _stingrUART.putc(0X66);
jmoreno10 0:0159aa4d2062 350 _stingrUART.putc(0XB0);
jmoreno10 0:0159aa4d2062 351
jmoreno10 0:0159aa4d2062 352 postCommand();
jmoreno10 0:0159aa4d2062 353
jmoreno10 0:0159aa4d2062 354 // NAK response
jmoreno10 0:0159aa4d2062 355 if (_packet[0] == 170 && // 0xAA
jmoreno10 0:0159aa4d2062 356 _packet[1] == 5 && // 0x05
jmoreno10 0:0159aa4d2062 357 _packet[2] == 255 && // 0xFF
jmoreno10 0:0159aa4d2062 358 _packet[3] == 161 && // 0xA1
jmoreno10 0:0159aa4d2062 359 _packet[4] == 203) // 0xCB
jmoreno10 0:0159aa4d2062 360 {
jmoreno10 0:0159aa4d2062 361 clearPacket();
jmoreno10 0:0159aa4d2062 362 _pc.printf("NAK");
jmoreno10 0:0159aa4d2062 363 }
jmoreno10 0:0159aa4d2062 364 // ACK response
jmoreno10 0:0159aa4d2062 365 else
jmoreno10 0:0159aa4d2062 366 {
jmoreno10 0:0159aa4d2062 367 numSetup = _packet[7]*10000000 + _packet[8]*100000 + _packet[9]*1000 + _packet[10];
jmoreno10 0:0159aa4d2062 368 clearPacket();
jmoreno10 0:0159aa4d2062 369
jmoreno10 0:0159aa4d2062 370 char qryRF[1];
jmoreno10 0:0159aa4d2062 371 char qryBursts[2];
jmoreno10 0:0159aa4d2062 372 char qryMin[3];
jmoreno10 0:0159aa4d2062 373 char qryMax[3];
jmoreno10 0:0159aa4d2062 374
jmoreno10 0:0159aa4d2062 375 char strt[] = "";
jmoreno10 0:0159aa4d2062 376
jmoreno10 0:0159aa4d2062 377 //Print Channel
jmoreno10 0:0159aa4d2062 378 _pc.printf("\n RF Channel: ");
jmoreno10 0:0159aa4d2062 379 _pc.printf("%u",numSetup/(10000000));
jmoreno10 0:0159aa4d2062 380 int rfprint = numSetup/(10000000); // RF channel
jmoreno10 0:0159aa4d2062 381 sprintf(qryRF,"%d",rfprint);
jmoreno10 0:0159aa4d2062 382 strcat(strt,qryRF);
jmoreno10 0:0159aa4d2062 383 numSetup = numSetup - (numSetup/10000000)*10000000; // Truncate RF Digits
jmoreno10 0:0159aa4d2062 384
jmoreno10 0:0159aa4d2062 385 //Print Bursts
jmoreno10 0:0159aa4d2062 386 _pc.printf("\n # of Bursts: ");
jmoreno10 0:0159aa4d2062 387 _pc.printf("%u",numSetup/(100000)); // Bursts Per Message
jmoreno10 0:0159aa4d2062 388 int burprint = numSetup/(100000);
jmoreno10 0:0159aa4d2062 389 sprintf(qryBursts,"%d",burprint);
jmoreno10 0:0159aa4d2062 390 strcat(strt,qryBursts);
jmoreno10 0:0159aa4d2062 391 numSetup = numSetup - (numSetup/100000)*100000; // Truncate Burst Digits
jmoreno10 0:0159aa4d2062 392
jmoreno10 0:0159aa4d2062 393 //Print Min Interval
jmoreno10 0:0159aa4d2062 394 _pc.printf("\n Min Burst Interval: ");
jmoreno10 0:0159aa4d2062 395 _pc.printf("%u",numSetup/1000*5); // Min Interval
jmoreno10 0:0159aa4d2062 396 int minprint = numSetup/1000*5;
jmoreno10 0:0159aa4d2062 397 sprintf(qryMin,"%d",minprint);
jmoreno10 0:0159aa4d2062 398 strcat(strt,qryMin);
jmoreno10 0:0159aa4d2062 399 numSetup = numSetup - (numSetup/1000)*1000; // Truncate Min Interval
jmoreno10 0:0159aa4d2062 400 _pc.printf(" seconds");
jmoreno10 0:0159aa4d2062 401
jmoreno10 0:0159aa4d2062 402 //Print Max Interval
jmoreno10 0:0159aa4d2062 403 _pc.printf("\n Max Burst Interval: ");
jmoreno10 0:0159aa4d2062 404 _pc.printf("%u",numSetup*5);
jmoreno10 0:0159aa4d2062 405 int maxprint = numSetup*5; // Max Interval
jmoreno10 0:0159aa4d2062 406 sprintf(qryMax,"%d",maxprint);
jmoreno10 0:0159aa4d2062 407 strcat(strt,qryMax);
jmoreno10 0:0159aa4d2062 408 _pc.printf(" seconds\n");
jmoreno10 0:0159aa4d2062 409
jmoreno10 0:0159aa4d2062 410 strncpy(_resp,strt,sizeof(_resp));
jmoreno10 0:0159aa4d2062 411 }
jmoreno10 0:0159aa4d2062 412 }
jmoreno10 0:0159aa4d2062 413
jmoreno10 0:0159aa4d2062 414 //0x09 Query Hardware
jmoreno10 0:0159aa4d2062 415 void Stingr::query_Hardware()
jmoreno10 0:0159aa4d2062 416 {
jmoreno10 0:0159aa4d2062 417 led1=!led1;
jmoreno10 0:0159aa4d2062 418 _pc.printf("\r0x09\t\t\tQuery Hardware Version\n");
jmoreno10 0:0159aa4d2062 419 _pc.printf("Command(HEX):\t\tAA 5 9 18 59\n\r");
jmoreno10 0:0159aa4d2062 420 _pc.printf("Response(Stingr)HEX:\t");
jmoreno10 0:0159aa4d2062 421 waitCTS();
jmoreno10 0:0159aa4d2062 422 Thread::wait(10);
jmoreno10 0:0159aa4d2062 423
jmoreno10 0:0159aa4d2062 424 _stingrUART.putc(0XAA);
jmoreno10 0:0159aa4d2062 425 _stingrUART.putc(0X05);
jmoreno10 0:0159aa4d2062 426 _stingrUART.putc(0X09);
jmoreno10 0:0159aa4d2062 427 _stingrUART.putc(0X18);
jmoreno10 0:0159aa4d2062 428 _stingrUART.putc(0X59);
jmoreno10 0:0159aa4d2062 429
jmoreno10 0:0159aa4d2062 430 postCommand();
jmoreno10 0:0159aa4d2062 431 printPacket();
jmoreno10 0:0159aa4d2062 432
jmoreno10 0:0159aa4d2062 433 // ACK response
jmoreno10 0:0159aa4d2062 434 if (_packet[5] == 143 && // 0x8F
jmoreno10 0:0159aa4d2062 435 _packet[6] == 98 && // 0x62
jmoreno10 0:0159aa4d2062 436 _packet[7] == 98) // 0x62
jmoreno10 0:0159aa4d2062 437 {
jmoreno10 0:0159aa4d2062 438 clearPacket();
jmoreno10 0:0159aa4d2062 439 _pc.printf("\nResponse Processing:\tQuery Hardware is correct. VALID\n");
jmoreno10 0:0159aa4d2062 440 _pc.printf(" Device Code: 01\n");
jmoreno10 0:0159aa4d2062 441 _pc.printf(" CPU Revision: 62\n");
jmoreno10 0:0159aa4d2062 442 _pc.printf(" Radio Revision: 62\n");
jmoreno10 0:0159aa4d2062 443 strncpy(_resp,"Query Hardware is correct",sizeof(_resp));
jmoreno10 0:0159aa4d2062 444 }
jmoreno10 0:0159aa4d2062 445 // NAK response
jmoreno10 0:0159aa4d2062 446 else
jmoreno10 0:0159aa4d2062 447 {
jmoreno10 0:0159aa4d2062 448 clearPacket();
jmoreno10 0:0159aa4d2062 449 _pc.printf("\n");
jmoreno10 0:0159aa4d2062 450 _pc.printf("NAK. INVALID");
jmoreno10 0:0159aa4d2062 451 strncpy(_resp,"NAK response (Query Hardware)",sizeof(_resp));
jmoreno10 0:0159aa4d2062 452 }
jmoreno10 0:0159aa4d2062 453 }
jmoreno10 0:0159aa4d2062 454
jmoreno10 0:0159aa4d2062 455 //NAK Command
jmoreno10 0:0159aa4d2062 456 void Stingr::NAK_command()
jmoreno10 0:0159aa4d2062 457 {
jmoreno10 0:0159aa4d2062 458 led2=!led2;
jmoreno10 0:0159aa4d2062 459 _pc.printf("\rXxXX\t\t\tNAK\n");
jmoreno10 0:0159aa4d2062 460 _pc.printf("Command(HEX):\t\tAA 5 7 66 B1\n\r");
jmoreno10 0:0159aa4d2062 461 _pc.printf("Response(Stingr)HEX:\t");
jmoreno10 0:0159aa4d2062 462 waitCTS();
jmoreno10 0:0159aa4d2062 463 Thread::wait(10);
jmoreno10 0:0159aa4d2062 464
jmoreno10 0:0159aa4d2062 465 _stingrUART.putc(0XAA);
jmoreno10 0:0159aa4d2062 466 _stingrUART.putc(0X05);
jmoreno10 0:0159aa4d2062 467 _stingrUART.putc(0X07);
jmoreno10 0:0159aa4d2062 468 _stingrUART.putc(0X66);
jmoreno10 0:0159aa4d2062 469 _stingrUART.putc(0XB1);
jmoreno10 0:0159aa4d2062 470
jmoreno10 0:0159aa4d2062 471 postCommand();
jmoreno10 0:0159aa4d2062 472 printPacket();
jmoreno10 0:0159aa4d2062 473 clearPacket();
jmoreno10 0:0159aa4d2062 474 _pc.printf("\n");
jmoreno10 0:0159aa4d2062 475 strncpy(_resp,"NAK response",sizeof(_resp));
jmoreno10 0:0159aa4d2062 476 }
jmoreno10 0:0159aa4d2062 477
jmoreno10 0:0159aa4d2062 478 //0x06 Setup
jmoreno10 0:0159aa4d2062 479 void Stingr::_setup(char* b)
jmoreno10 0:0159aa4d2062 480 {
jmoreno10 0:0159aa4d2062 481 led3=!led3;
jmoreno10 0:0159aa4d2062 482 _pc.printf("\r0x06\t\t\tSetup\n");
jmoreno10 0:0159aa4d2062 483 //_pc.printf("Command(HEX):\t\tAA 0E 06 00 00 00 00 00 03 18 30 00 CE 9C\n\r");
jmoreno10 0:0159aa4d2062 484
jmoreno10 0:0159aa4d2062 485 // RF Channel
jmoreno10 0:0159aa4d2062 486 int rf = b[1] - '0';
jmoreno10 0:0159aa4d2062 487 _pc.printf("rf channel: %u \n",rf);
jmoreno10 0:0159aa4d2062 488
jmoreno10 0:0159aa4d2062 489 // Bursts
jmoreno10 0:0159aa4d2062 490 int brst = b[2] - '0';
jmoreno10 0:0159aa4d2062 491 _pc.printf("bursts: %u \n", brst);
jmoreno10 0:0159aa4d2062 492
jmoreno10 0:0159aa4d2062 493 // Min Interval
jmoreno10 0:0159aa4d2062 494 int min_hund = b[3] - '0';
jmoreno10 0:0159aa4d2062 495 int min_tens = b[4] - '0';
jmoreno10 0:0159aa4d2062 496 int min_ones = b[5] - '0';
jmoreno10 0:0159aa4d2062 497 //_pc.printf("Min interval: %u%u%u seconds\n", min_hund, min_tens, min_ones);
jmoreno10 0:0159aa4d2062 498 int min_int = (min_hund*100 + min_tens*10 + min_ones)/5;
jmoreno10 0:0159aa4d2062 499 _pc.printf("Min interval: %u seconds\n", min_int*5);
jmoreno10 0:0159aa4d2062 500
jmoreno10 0:0159aa4d2062 501 // Max Interval
jmoreno10 0:0159aa4d2062 502 int max_hund = b[6] - '0';
jmoreno10 0:0159aa4d2062 503 int max_tens = b[7] - '0';
jmoreno10 0:0159aa4d2062 504 int max_ones = b[8] - '0';
jmoreno10 0:0159aa4d2062 505 //_pc.printf("Max interval: %u%u%u seconds\n", max_hund, max_tens, max_ones);
jmoreno10 0:0159aa4d2062 506 int max_int = (max_hund*100 + max_tens*10 + max_ones)/5;
jmoreno10 0:0159aa4d2062 507 _pc.printf("Max interval: %u seconds\n", max_int*5);
jmoreno10 0:0159aa4d2062 508
jmoreno10 0:0159aa4d2062 509 char header[12] = {0xAA,0x0E,0x06,0x00,0x00,0x00,0x00,rf,brst,min_int,max_int,0x00};
jmoreno10 0:0159aa4d2062 510
jmoreno10 0:0159aa4d2062 511 _pc.printf("Command(HEX):\t\t");
jmoreno10 0:0159aa4d2062 512 //Print b characters in HEX
jmoreno10 0:0159aa4d2062 513 for(int k = 0; k < 12; k++)
jmoreno10 0:0159aa4d2062 514 _pc.printf("%X ",header[k]);
jmoreno10 0:0159aa4d2062 515
jmoreno10 0:0159aa4d2062 516 // CRC calculation
jmoreno10 0:0159aa4d2062 517 char *t = (char *)header; //a
jmoreno10 0:0159aa4d2062 518 char crc1 = ModRTU_CRC(t,t[1]-2)&0xFF;
jmoreno10 0:0159aa4d2062 519 char crc2 = ModRTU_CRC(t,t[1]-2)>>8;
jmoreno10 0:0159aa4d2062 520
jmoreno10 0:0159aa4d2062 521 _pc.printf("%X ",crc1); //%X print char in HEX format
jmoreno10 0:0159aa4d2062 522 _pc.printf("%X ",crc2); //%X print char in HEX format
jmoreno10 0:0159aa4d2062 523
jmoreno10 0:0159aa4d2062 524 _pc.printf("\nResponse(Stingr)HEX:\t");
jmoreno10 0:0159aa4d2062 525
jmoreno10 0:0159aa4d2062 526 waitCTS();
jmoreno10 0:0159aa4d2062 527 Thread::wait(10);
jmoreno10 0:0159aa4d2062 528
jmoreno10 0:0159aa4d2062 529
jmoreno10 0:0159aa4d2062 530 //Send Command to STINGR
jmoreno10 0:0159aa4d2062 531 for(int k = 0; k < 12; k++)
jmoreno10 0:0159aa4d2062 532 _stingrUART.putc(header[k]);
jmoreno10 0:0159aa4d2062 533 _stingrUART.putc(crc1);
jmoreno10 0:0159aa4d2062 534 _stingrUART.putc(crc2);
jmoreno10 0:0159aa4d2062 535
jmoreno10 0:0159aa4d2062 536 /*
jmoreno10 0:0159aa4d2062 537 _stingrUART.putc(0XAA);
jmoreno10 0:0159aa4d2062 538 _stingrUART.putc(0X0E);
jmoreno10 0:0159aa4d2062 539 _stingrUART.putc(0X06);
jmoreno10 0:0159aa4d2062 540 _stingrUART.putc(0X00);
jmoreno10 0:0159aa4d2062 541 _stingrUART.putc(0X00);
jmoreno10 0:0159aa4d2062 542 _stingrUART.putc(0X00);
jmoreno10 0:0159aa4d2062 543 _stingrUART.putc(0X00);
jmoreno10 0:0159aa4d2062 544 _stingrUART.putc(0X00);
jmoreno10 0:0159aa4d2062 545 _stingrUART.putc(0X03);
jmoreno10 0:0159aa4d2062 546 _stingrUART.putc(0X18);
jmoreno10 0:0159aa4d2062 547 _stingrUART.putc(0X30);
jmoreno10 0:0159aa4d2062 548 _stingrUART.putc(0X00);
jmoreno10 0:0159aa4d2062 549 _stingrUART.putc(0XCE);
jmoreno10 0:0159aa4d2062 550 _stingrUART.putc(0X9C);
jmoreno10 0:0159aa4d2062 551 */
jmoreno10 0:0159aa4d2062 552
jmoreno10 0:0159aa4d2062 553 postCommand();
jmoreno10 0:0159aa4d2062 554 printPacket();
jmoreno10 0:0159aa4d2062 555
jmoreno10 0:0159aa4d2062 556 _pc.printf("\n");
jmoreno10 0:0159aa4d2062 557 clearPacket();
jmoreno10 0:0159aa4d2062 558 }
jmoreno10 0:0159aa4d2062 559
jmoreno10 0:0159aa4d2062 560 //0x00 Send Data
jmoreno10 0:0159aa4d2062 561 void Stingr::send_Data(char* b) //char* s //Replace "buf" with "b"
jmoreno10 0:0159aa4d2062 562 {
jmoreno10 0:0159aa4d2062 563 led4=!led4;
jmoreno10 0:0159aa4d2062 564
jmoreno10 0:0159aa4d2062 565 // Count all characters before the "Return" key in the buffer
jmoreno10 0:0159aa4d2062 566 int cnt = 0;
jmoreno10 0:0159aa4d2062 567 for (int k = 0; k < 200; k++)
jmoreno10 0:0159aa4d2062 568 {
jmoreno10 0:0159aa4d2062 569 if(b[k] == '\n')
jmoreno10 0:0159aa4d2062 570 break;
jmoreno10 0:0159aa4d2062 571 cnt++;
jmoreno10 0:0159aa4d2062 572 }
jmoreno10 0:0159aa4d2062 573
jmoreno10 0:0159aa4d2062 574 // Print command info and input
jmoreno10 0:0159aa4d2062 575 _pc.printf("0x00\t\t\tSend Data\n\r");
jmoreno10 0:0159aa4d2062 576 cnt--; //Decrement total by 1. Don't count 8/h.
jmoreno10 0:0159aa4d2062 577 _pc.printf("Data Packet length: \t%u\n",cnt);
jmoreno10 0:0159aa4d2062 578
jmoreno10 0:0159aa4d2062 579 // Port b array to str, but without first character '8/h'
jmoreno10 0:0159aa4d2062 580 char str[cnt]; //Declare str array. str will copy buf but without '8'
jmoreno10 0:0159aa4d2062 581 for (int k = 0; k < cnt; k++)
jmoreno10 0:0159aa4d2062 582 str[k] = b[k+1]; //Starts 1 index after '8'.
jmoreno10 0:0159aa4d2062 583
jmoreno10 0:0159aa4d2062 584 // str1 copies str but with correct datalength by truncating the extra characters
jmoreno10 0:0159aa4d2062 585 // found in str
jmoreno10 0:0159aa4d2062 586 char str1[cnt];
jmoreno10 0:0159aa4d2062 587 strncpy(str1,str,cnt); //truncation of extra characters
jmoreno10 0:0159aa4d2062 588 str1[cnt] = '\0'; //Null character
jmoreno10 0:0159aa4d2062 589 _pc.printf("Data Packet: \t\t%s \n",str1);
jmoreno10 0:0159aa4d2062 590
jmoreno10 0:0159aa4d2062 591 //--------------------replace b with str1
jmoreno10 0:0159aa4d2062 592
jmoreno10 0:0159aa4d2062 593 waitCTS();
jmoreno10 0:0159aa4d2062 594 Thread::wait(10);
jmoreno10 0:0159aa4d2062 595
jmoreno10 0:0159aa4d2062 596 // Put str "data" into Serial packet array (to send to STINGR)
jmoreno10 0:0159aa4d2062 597 size_t n = strlen(str1); //Measure size of b. This includes the zeros //!replaced
jmoreno10 0:0159aa4d2062 598 int number = n+3;
jmoreno10 0:0159aa4d2062 599 int len = n+5;
jmoreno10 0:0159aa4d2062 600 char header[3] = {0xAA,len,0x00}; //Define header information
jmoreno10 0:0159aa4d2062 601 char vec[number];
jmoreno10 0:0159aa4d2062 602 //pc.printf("number = %u\n",number);
jmoreno10 0:0159aa4d2062 603
jmoreno10 0:0159aa4d2062 604 //store all in vec
jmoreno10 0:0159aa4d2062 605 for(int k = 0; k < 3; k++)
jmoreno10 0:0159aa4d2062 606 vec[k] = header[k];
jmoreno10 0:0159aa4d2062 607 for(int k = 3; k < number; k++)
jmoreno10 0:0159aa4d2062 608 vec[k] = str1[k-3];//!replaced
jmoreno10 0:0159aa4d2062 609
jmoreno10 0:0159aa4d2062 610 _pc.printf("Command(HEX):\t\t");
jmoreno10 0:0159aa4d2062 611 //Print b characters in HEX
jmoreno10 0:0159aa4d2062 612 for(int k = 0; k < number; k++)
jmoreno10 0:0159aa4d2062 613 _pc.printf("%X ",vec[k]);
jmoreno10 0:0159aa4d2062 614
jmoreno10 0:0159aa4d2062 615 // CRC calculation
jmoreno10 0:0159aa4d2062 616 char *t = (char *)vec; //a
jmoreno10 0:0159aa4d2062 617 char crc1 = ModRTU_CRC(t,t[1]-2)&0xFF;
jmoreno10 0:0159aa4d2062 618 char crc2 = ModRTU_CRC(t,t[1]-2)>>8;
jmoreno10 0:0159aa4d2062 619
jmoreno10 0:0159aa4d2062 620 _pc.printf("%X ",crc1); //%X print char in HEX format
jmoreno10 0:0159aa4d2062 621 _pc.printf("%X ",crc2); //%X print char in HEX format
jmoreno10 0:0159aa4d2062 622 _pc.printf("\nResponse(Stingr)HEX:\t");
jmoreno10 0:0159aa4d2062 623
jmoreno10 0:0159aa4d2062 624 //Send Command to STINGR
jmoreno10 0:0159aa4d2062 625 for(int k = 0; k < number; k++)
jmoreno10 0:0159aa4d2062 626 _stingrUART.putc(vec[k]);
jmoreno10 0:0159aa4d2062 627 _stingrUART.putc(crc1);
jmoreno10 0:0159aa4d2062 628 _stingrUART.putc(crc2);
jmoreno10 0:0159aa4d2062 629
jmoreno10 0:0159aa4d2062 630 postCommand();
jmoreno10 0:0159aa4d2062 631 printPacket();
jmoreno10 0:0159aa4d2062 632
jmoreno10 0:0159aa4d2062 633 // NAK response
jmoreno10 0:0159aa4d2062 634 if (_packet[0] == 170 && // 0xAA
jmoreno10 0:0159aa4d2062 635 _packet[1] == 5 && // 0x05
jmoreno10 0:0159aa4d2062 636 _packet[2] == 255 && // 0xFF
jmoreno10 0:0159aa4d2062 637 _packet[3] == 161 && // 0xA1
jmoreno10 0:0159aa4d2062 638 _packet[4] == 203) // 0xCB
jmoreno10 0:0159aa4d2062 639 {
jmoreno10 0:0159aa4d2062 640 clearPacket();
jmoreno10 0:0159aa4d2062 641 _pc.printf("\n");
jmoreno10 0:0159aa4d2062 642 _pc.printf("\nNAK response. INVALID");
jmoreno10 0:0159aa4d2062 643 strncpy(_resp,"NAK response (Send Data)",sizeof(_resp));
jmoreno10 0:0159aa4d2062 644 }
jmoreno10 0:0159aa4d2062 645 // ACK response
jmoreno10 0:0159aa4d2062 646 else
jmoreno10 0:0159aa4d2062 647 {
jmoreno10 0:0159aa4d2062 648 clearPacket();
jmoreno10 0:0159aa4d2062 649 _pc.printf("\n");
jmoreno10 0:0159aa4d2062 650 _pc.printf("\nSend Data is successful. VALID\n");
jmoreno10 0:0159aa4d2062 651 strncpy(_resp,"Send Data is successful",sizeof(_resp));
jmoreno10 0:0159aa4d2062 652 }
jmoreno10 0:0159aa4d2062 653 }
jmoreno10 0:0159aa4d2062 654
jmoreno10 0:0159aa4d2062 655 //0x03 Abort Transmission
jmoreno10 0:0159aa4d2062 656 void Stingr::abort_Transmission()
jmoreno10 0:0159aa4d2062 657 {
jmoreno10 0:0159aa4d2062 658 led1=!led1;
jmoreno10 0:0159aa4d2062 659 _pc.printf("\r0x03\t\t\tAbort Transmission\n");
jmoreno10 0:0159aa4d2062 660 _pc.printf("Command(HEX):\t\tAA 5 3 42 F6\n\r");
jmoreno10 0:0159aa4d2062 661 _pc.printf("Response(Stingr)HEX:\t");
jmoreno10 0:0159aa4d2062 662 waitCTS();
jmoreno10 0:0159aa4d2062 663 Thread::wait(10);
jmoreno10 0:0159aa4d2062 664 _stingrUART.putc(0XAA);
jmoreno10 0:0159aa4d2062 665 _stingrUART.putc(0X05);
jmoreno10 0:0159aa4d2062 666 _stingrUART.putc(0X03);
jmoreno10 0:0159aa4d2062 667 _stingrUART.putc(0X42);
jmoreno10 0:0159aa4d2062 668 _stingrUART.putc(0XF6);
jmoreno10 0:0159aa4d2062 669
jmoreno10 0:0159aa4d2062 670 postCommand();
jmoreno10 0:0159aa4d2062 671 printPacket();
jmoreno10 0:0159aa4d2062 672
jmoreno10 0:0159aa4d2062 673 // ACK response
jmoreno10 0:0159aa4d2062 674 if (_packet[0] == 170 && // 0xAA
jmoreno10 0:0159aa4d2062 675 _packet[1] == 5 && // 0x05
jmoreno10 0:0159aa4d2062 676 _packet[2] == 3 && // 0x03
jmoreno10 0:0159aa4d2062 677 _packet[3] == 66 && // 0x42
jmoreno10 0:0159aa4d2062 678 _packet[4] == 246) // 0xF6
jmoreno10 0:0159aa4d2062 679 {
jmoreno10 0:0159aa4d2062 680 clearPacket();
jmoreno10 0:0159aa4d2062 681 _pc.printf("\nResponse Processing:\tTransmission successfully aborted.\n");
jmoreno10 0:0159aa4d2062 682 strncpy(_resp,"Transmission successfully aborted.",sizeof(_resp));
jmoreno10 0:0159aa4d2062 683 }
jmoreno10 0:0159aa4d2062 684 // NAK response
jmoreno10 0:0159aa4d2062 685 else
jmoreno10 0:0159aa4d2062 686 {
jmoreno10 0:0159aa4d2062 687 clearPacket();
jmoreno10 0:0159aa4d2062 688 _pc.printf("\nResponse Processing:\tNAK response. INVALID");
jmoreno10 0:0159aa4d2062 689 strncpy(_resp,"NAK response (Abort Transmission)",sizeof(_resp));
jmoreno10 0:0159aa4d2062 690 }
jmoreno10 0:0159aa4d2062 691 }
jmoreno10 0:0159aa4d2062 692
jmoreno10 0:0159aa4d2062 693 uint16_t Stingr::ModRTU_CRC(char * buf, int len)
jmoreno10 0:0159aa4d2062 694 {
jmoreno10 0:0159aa4d2062 695 unsigned char i;
jmoreno10 0:0159aa4d2062 696 unsigned short data;
jmoreno10 0:0159aa4d2062 697 uint16_t crc = 0xFFFF;
jmoreno10 0:0159aa4d2062 698
jmoreno10 0:0159aa4d2062 699 do{
jmoreno10 0:0159aa4d2062 700 data = (unsigned int)0x00FF & *buf++;
jmoreno10 0:0159aa4d2062 701 crc = crc ^ data;
jmoreno10 0:0159aa4d2062 702
jmoreno10 0:0159aa4d2062 703 for(i = 8; i > 0; i--)
jmoreno10 0:0159aa4d2062 704 {
jmoreno10 0:0159aa4d2062 705 if(crc & 0x0001)
jmoreno10 0:0159aa4d2062 706 crc = (crc >> 1) ^ 0x8408;
jmoreno10 0:0159aa4d2062 707 else
jmoreno10 0:0159aa4d2062 708 crc >>=1;
jmoreno10 0:0159aa4d2062 709 }
jmoreno10 0:0159aa4d2062 710
jmoreno10 0:0159aa4d2062 711 }while (--len);
jmoreno10 0:0159aa4d2062 712
jmoreno10 0:0159aa4d2062 713 crc = ~crc;
jmoreno10 0:0159aa4d2062 714
jmoreno10 0:0159aa4d2062 715 // Note, this number has low and high bytes swapped, so use it accordingly (or swap bytes)
jmoreno10 0:0159aa4d2062 716 return (crc);
jmoreno10 0:0159aa4d2062 717 }
jmoreno10 0:0159aa4d2062 718
jmoreno10 0:0159aa4d2062 719 void Stingr::respuesta()
jmoreno10 0:0159aa4d2062 720 {
jmoreno10 0:0159aa4d2062 721 while(1)
jmoreno10 0:0159aa4d2062 722 {
jmoreno10 0:0159aa4d2062 723 if(_stingrUART.readable())
jmoreno10 0:0159aa4d2062 724 { // Se esperan datos provenientes del TX del módulo STX3
jmoreno10 0:0159aa4d2062 725 incomingByteR = _stingrUART.getc();
jmoreno10 0:0159aa4d2062 726 //stingr.setPacketByte(incomingByteR);
jmoreno10 0:0159aa4d2062 727 setPacketByte(incomingByteR);
jmoreno10 0:0159aa4d2062 728 _pc.printf("%X",incomingByteR); // Format specifier
jmoreno10 0:0159aa4d2062 729 _pc.printf(" ");
jmoreno10 0:0159aa4d2062 730 }
jmoreno10 0:0159aa4d2062 731 }
jmoreno10 0:0159aa4d2062 732 }
jmoreno10 0:0159aa4d2062 733
jmoreno10 0:0159aa4d2062 734 // Thread 2: Reading commands from PC
jmoreno10 0:0159aa4d2062 735 void Stingr::PC_communication()
jmoreno10 0:0159aa4d2062 736 {
jmoreno10 0:0159aa4d2062 737 int a = 0;
jmoreno10 0:0159aa4d2062 738
jmoreno10 0:0159aa4d2062 739 while(1)
jmoreno10 0:0159aa4d2062 740 {
jmoreno10 0:0159aa4d2062 741 char str1[200];
jmoreno10 0:0159aa4d2062 742 if (_pc.readable())
jmoreno10 0:0159aa4d2062 743 {
jmoreno10 0:0159aa4d2062 744 incomingByteX = _pc.getc();
jmoreno10 0:0159aa4d2062 745 strx[a] = incomingByteX;
jmoreno10 0:0159aa4d2062 746 a++;
jmoreno10 0:0159aa4d2062 747
jmoreno10 0:0159aa4d2062 748 if(incomingByteX == '\n')
jmoreno10 0:0159aa4d2062 749 {
jmoreno10 0:0159aa4d2062 750 _pc.printf("\n\nPC input: \t\t");
jmoreno10 0:0159aa4d2062 751 strncpy(str1,strx,sizeof(strx));
jmoreno10 0:0159aa4d2062 752 _pc.printf("%s",str1);
jmoreno10 0:0159aa4d2062 753 _pc.printf("\n");
jmoreno10 0:0159aa4d2062 754 //stingr.command(str1);
jmoreno10 0:0159aa4d2062 755 command(str1);
jmoreno10 0:0159aa4d2062 756 memset(strx, 0, 200);
jmoreno10 0:0159aa4d2062 757 a = 0;
jmoreno10 0:0159aa4d2062 758 }
jmoreno10 0:0159aa4d2062 759 }
jmoreno10 0:0159aa4d2062 760 }
jmoreno10 0:0159aa4d2062 761 }