AguilaBoard program. No library.

Dependencies:   mbed-rtos mbed

Fork of PYL_v6 by Jonathan Moreno

Committer:
jmoreno10
Date:
Mon Oct 29 05:37:02 2018 +0000
Revision:
7:a42095d457a0
Parent:
6:172a750bbf33
AguilaBoard code (no library)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jmoreno10 6:172a750bbf33 1 #include "mbed.h" //Se declara la librería mbed.
Sumobot 0:88a700f174a3 2 #include "rtos.h"
Sumobot 0:88a700f174a3 3
jmoreno10 6:172a750bbf33 4 I2CSlave slave(p9, p10); //sda,scl
jmoreno10 6:172a750bbf33 5 DigitalOut led1(LED1);
jmoreno10 6:172a750bbf33 6 DigitalOut led2(LED2);
jmoreno10 6:172a750bbf33 7 DigitalOut led3(LED3);
jmoreno10 6:172a750bbf33 8 DigitalOut led4(LED4);
jmoreno10 6:172a750bbf33 9 Serial pc(USBTX, USBRX, 9600); // tx, rx Conunicación Serial con la PC
jmoreno10 6:172a750bbf33 10
jmoreno10 6:172a750bbf33 11 // Pin Digital de entrada "CTS" en modo Pull-Up, para encontrarse normalmente a VCC cuando no haya un pulso.
jmoreno10 6:172a750bbf33 12 DigitalIn CTS(p11, PullUp); //p7 proto // p11 AguilaBoard
jmoreno10 5:c52a2d06bd93 13
jmoreno10 6:172a750bbf33 14 // Pin Digital de Salida "RTS"; Predefinido para valer 1 en su estado inactivo dentro del código.
jmoreno10 6:172a750bbf33 15 DigitalOut RTS(p12, 1); //p8 proto // p12 AguilaBoard
jmoreno10 6:172a750bbf33 16
jmoreno10 6:172a750bbf33 17 Serial stingr(p13, p14, 9600); // tx, rx Comunicación Serial con el Módulo STX3
Sumobot 0:88a700f174a3 18
jmoreno10 6:172a750bbf33 19 int flag=1; // Declaración de la Bandera.
jmoreno10 6:172a750bbf33 20 int incomingByte=0;
jmoreno10 6:172a750bbf33 21 Thread thread;
jmoreno10 6:172a750bbf33 22 int packet[15]; //int or char
jmoreno10 6:172a750bbf33 23 int num = 0;
jmoreno10 6:172a750bbf33 24
jmoreno10 6:172a750bbf33 25 void clearPacket();
jmoreno10 6:172a750bbf33 26 void printPacket();
jmoreno10 3:5d959275ac05 27 void respuesta();
jmoreno10 6:172a750bbf33 28 void waitCTS();
jmoreno10 6:172a750bbf33 29 void postCommand();
jmoreno10 3:5d959275ac05 30
jmoreno10 6:172a750bbf33 31 bool queryESN();
jmoreno10 6:172a750bbf33 32 bool abortTransmission();
jmoreno10 6:172a750bbf33 33 bool queryFirmware();
jmoreno10 6:172a750bbf33 34 bool queryHardware();
jmoreno10 6:172a750bbf33 35 int queryBursts();
jmoreno10 6:172a750bbf33 36 long int querySetup();
jmoreno10 6:172a750bbf33 37 bool sendData(char* s);
jmoreno10 6:172a750bbf33 38 void _setup();
jmoreno10 6:172a750bbf33 39
jmoreno10 6:172a750bbf33 40 void NAKcommand();
jmoreno10 6:172a750bbf33 41 uint16_t ModRTU_CRC(char * buf, int len);
jmoreno10 6:172a750bbf33 42 void combine(char* s, char c);
jmoreno10 3:5d959275ac05 43
jmoreno10 3:5d959275ac05 44 int main()
jmoreno10 6:172a750bbf33 45 {
jmoreno10 6:172a750bbf33 46 //i2c variables
jmoreno10 6:172a750bbf33 47 slave.frequency(400000);
jmoreno10 6:172a750bbf33 48 char buf[200];
jmoreno10 6:172a750bbf33 49 char msg[200];
jmoreno10 6:172a750bbf33 50 slave.address(0x1E);
jmoreno10 3:5d959275ac05 51
jmoreno10 6:172a750bbf33 52 int bursts = 0;
jmoreno10 6:172a750bbf33 53 int long numSetup = 0;
jmoreno10 6:172a750bbf33 54 thread.start(respuesta);
jmoreno10 3:5d959275ac05 55
jmoreno10 6:172a750bbf33 56 while(1)
jmoreno10 6:172a750bbf33 57 {
jmoreno10 6:172a750bbf33 58 int i = slave.receive();
jmoreno10 6:172a750bbf33 59
jmoreno10 6:172a750bbf33 60 switch(i)
jmoreno10 6:172a750bbf33 61 {
jmoreno10 6:172a750bbf33 62 // WRITING RESPONSES
jmoreno10 6:172a750bbf33 63 // Slave writes to Master. Master reads.
jmoreno10 6:172a750bbf33 64 case I2CSlave::ReadAddressed:
jmoreno10 6:172a750bbf33 65 slave.write(msg, strlen(msg) + 1); // Includes null char
jmoreno10 7:a42095d457a0 66 slave.stop();
jmoreno10 6:172a750bbf33 67 printf("Writing to Master:\t<<- %s\n", msg);
jmoreno10 6:172a750bbf33 68 break;
jmoreno10 6:172a750bbf33 69
jmoreno10 6:172a750bbf33 70 // READING COMMANDS
jmoreno10 6:172a750bbf33 71 // Master writes to Slave. Slave reads.
jmoreno10 6:172a750bbf33 72 case I2CSlave::WriteAddressed:
jmoreno10 6:172a750bbf33 73 slave.read(buf, 200); //Read up to 30 characters from Master
jmoreno10 7:a42095d457a0 74 //slave.stop();
jmoreno10 6:172a750bbf33 75 printf("\n\nReading from Master:\t->> %s\n", buf);
jmoreno10 6:172a750bbf33 76 char j = buf[0];
jmoreno10 6:172a750bbf33 77
jmoreno10 6:172a750bbf33 78 switch(j)
jmoreno10 6:172a750bbf33 79 {
jmoreno10 6:172a750bbf33 80 case '0': //Handshake
jmoreno10 6:172a750bbf33 81 {
jmoreno10 6:172a750bbf33 82 // if 'a' is received
jmoreno10 6:172a750bbf33 83 pc.printf("Received I2C handshake transmission from Master\n");
jmoreno10 6:172a750bbf33 84 strncpy(msg,"I2C Success",sizeof(msg));
jmoreno10 6:172a750bbf33 85 break;
jmoreno10 6:172a750bbf33 86 }
jmoreno10 6:172a750bbf33 87 case 'a':
jmoreno10 6:172a750bbf33 88 {
jmoreno10 6:172a750bbf33 89 if (queryESN() == true)
jmoreno10 6:172a750bbf33 90 {
jmoreno10 6:172a750bbf33 91 pc.printf("\nQuery ESN is correct. VALID\n");
jmoreno10 6:172a750bbf33 92 strncpy(msg,"Query ESN is correct",sizeof(msg));
jmoreno10 6:172a750bbf33 93 }
jmoreno10 6:172a750bbf33 94 else
jmoreno10 6:172a750bbf33 95 {
jmoreno10 6:172a750bbf33 96 pc.printf("\nNAK response. INVALID\n");
jmoreno10 6:172a750bbf33 97 strncpy(msg,"NAK response (Query ESN)",sizeof(msg));
jmoreno10 6:172a750bbf33 98 }
jmoreno10 6:172a750bbf33 99 break;
jmoreno10 6:172a750bbf33 100 }
jmoreno10 6:172a750bbf33 101 case 'b':
jmoreno10 6:172a750bbf33 102 {
jmoreno10 6:172a750bbf33 103 bursts = queryBursts();
jmoreno10 6:172a750bbf33 104 if (bursts != 99)
jmoreno10 6:172a750bbf33 105 {
jmoreno10 6:172a750bbf33 106 char qry[2];
jmoreno10 6:172a750bbf33 107 sprintf(qry,"%d",bursts);
jmoreno10 6:172a750bbf33 108 char strt[] = "Bursts Remaining is ";
jmoreno10 6:172a750bbf33 109 strcat(strt,qry);
jmoreno10 6:172a750bbf33 110
jmoreno10 6:172a750bbf33 111 pc.printf("\nBursts Remaining: \t");
jmoreno10 6:172a750bbf33 112 pc.printf("%u",bursts);
jmoreno10 6:172a750bbf33 113 pc.printf("\n");
jmoreno10 6:172a750bbf33 114
jmoreno10 6:172a750bbf33 115 strncpy(msg,strt,sizeof(msg));
jmoreno10 6:172a750bbf33 116 }
jmoreno10 6:172a750bbf33 117 else
jmoreno10 6:172a750bbf33 118 {
jmoreno10 6:172a750bbf33 119 pc.printf("\nNAK response. INVALID");
jmoreno10 6:172a750bbf33 120 strncpy(msg,"NAK response (Query Bursts)",sizeof(msg));
jmoreno10 6:172a750bbf33 121 }
jmoreno10 6:172a750bbf33 122 break;
jmoreno10 6:172a750bbf33 123 }
jmoreno10 6:172a750bbf33 124 case 'c':
jmoreno10 6:172a750bbf33 125 {
jmoreno10 6:172a750bbf33 126 if (queryFirmware() == true)
jmoreno10 6:172a750bbf33 127 {
jmoreno10 6:172a750bbf33 128 pc.printf("\nResponse Processing:\tQuery Firmware is correct. VALID\n");
jmoreno10 6:172a750bbf33 129 pc.printf(" Firmware Version: 1.3\n");
jmoreno10 6:172a750bbf33 130 strncpy(msg,"Query Firmware is correct",sizeof(msg));
jmoreno10 6:172a750bbf33 131 }
jmoreno10 6:172a750bbf33 132 else
jmoreno10 6:172a750bbf33 133 {
jmoreno10 6:172a750bbf33 134 pc.printf("NAK. INVALID");
jmoreno10 6:172a750bbf33 135 strncpy(msg,"NAK response (Query Firmware)",sizeof(msg));
jmoreno10 6:172a750bbf33 136 }
jmoreno10 6:172a750bbf33 137 break;
jmoreno10 6:172a750bbf33 138 }
jmoreno10 6:172a750bbf33 139 case 'd':
jmoreno10 6:172a750bbf33 140 {
jmoreno10 6:172a750bbf33 141 numSetup = querySetup();
jmoreno10 6:172a750bbf33 142 if (numSetup == NULL)
jmoreno10 6:172a750bbf33 143 pc.printf("NAK");
jmoreno10 6:172a750bbf33 144
jmoreno10 6:172a750bbf33 145 char qryRF[1];
jmoreno10 6:172a750bbf33 146 char qryBursts[2];
jmoreno10 6:172a750bbf33 147 char qryMin[3];
jmoreno10 6:172a750bbf33 148 char qryMax[3];
jmoreno10 6:172a750bbf33 149
jmoreno10 6:172a750bbf33 150 char strt[] = "";
jmoreno10 6:172a750bbf33 151
jmoreno10 6:172a750bbf33 152 //Print Channel
jmoreno10 6:172a750bbf33 153 pc.printf("\n RF Channel: ");
jmoreno10 6:172a750bbf33 154 pc.printf("%u",numSetup/(10000000));
jmoreno10 6:172a750bbf33 155 int rfprint = numSetup/(10000000); // RF channel
jmoreno10 6:172a750bbf33 156 sprintf(qryRF,"%d",rfprint);
jmoreno10 6:172a750bbf33 157 strcat(strt,qryRF);
jmoreno10 6:172a750bbf33 158 numSetup = numSetup - (numSetup/10000000)*10000000; // Truncate RF Digits
jmoreno10 6:172a750bbf33 159
jmoreno10 6:172a750bbf33 160 //Print Bursts
jmoreno10 6:172a750bbf33 161 pc.printf("\n # of Bursts: ");
jmoreno10 6:172a750bbf33 162 pc.printf("%u",numSetup/(100000)); // Bursts Per Message
jmoreno10 6:172a750bbf33 163 int burprint = numSetup/(100000);
jmoreno10 6:172a750bbf33 164 sprintf(qryBursts,"%d",burprint);
jmoreno10 6:172a750bbf33 165 strcat(strt,qryBursts);
jmoreno10 6:172a750bbf33 166 numSetup = numSetup - (numSetup/100000)*100000; // Truncate Burst Digits
jmoreno10 6:172a750bbf33 167
jmoreno10 6:172a750bbf33 168 //Print Min Interval
jmoreno10 6:172a750bbf33 169 pc.printf("\n Min Burst Interval: ");
jmoreno10 6:172a750bbf33 170 pc.printf("%u",numSetup/1000*5); // Min Interval
jmoreno10 6:172a750bbf33 171 int minprint = numSetup/1000*5;
jmoreno10 6:172a750bbf33 172 sprintf(qryMin,"%d",minprint);
jmoreno10 6:172a750bbf33 173 strcat(strt,qryMin);
jmoreno10 6:172a750bbf33 174 numSetup = numSetup - (numSetup/1000)*1000; // Truncate Min Interval
jmoreno10 6:172a750bbf33 175 pc.printf(" seconds");
jmoreno10 6:172a750bbf33 176
jmoreno10 6:172a750bbf33 177 //Print Max Interval
jmoreno10 6:172a750bbf33 178 pc.printf("\n Max Burst Interval: ");
jmoreno10 6:172a750bbf33 179 pc.printf("%u",numSetup*5);
jmoreno10 6:172a750bbf33 180 int maxprint = numSetup*5; // Max Interval
jmoreno10 6:172a750bbf33 181 sprintf(qryMax,"%d",maxprint);
jmoreno10 6:172a750bbf33 182 strcat(strt,qryMax);
jmoreno10 6:172a750bbf33 183 pc.printf(" seconds\n");
jmoreno10 6:172a750bbf33 184
jmoreno10 6:172a750bbf33 185 strncpy(msg,strt,sizeof(msg));
jmoreno10 6:172a750bbf33 186
jmoreno10 6:172a750bbf33 187 break;
jmoreno10 6:172a750bbf33 188 }
jmoreno10 6:172a750bbf33 189 case 'e':
jmoreno10 6:172a750bbf33 190 {
jmoreno10 6:172a750bbf33 191 if (queryHardware() == true)
jmoreno10 6:172a750bbf33 192 {
jmoreno10 6:172a750bbf33 193 pc.printf("\nResponse Processing:\tQuery Hardware is correct. VALID\n");
jmoreno10 6:172a750bbf33 194 pc.printf(" Device Code: 01\n");
jmoreno10 6:172a750bbf33 195 pc.printf(" CPU Revision: 62\n");
jmoreno10 6:172a750bbf33 196 pc.printf(" Radio Revision: 62\n");
jmoreno10 6:172a750bbf33 197 strncpy(msg,"Query Hardware is correct",sizeof(msg));
jmoreno10 6:172a750bbf33 198 }
jmoreno10 6:172a750bbf33 199 else
jmoreno10 6:172a750bbf33 200 {
jmoreno10 6:172a750bbf33 201 pc.printf("NAK. INVALID");
jmoreno10 6:172a750bbf33 202 strncpy(msg,"NAK response (Query Hardware)",sizeof(msg));
jmoreno10 6:172a750bbf33 203 }
jmoreno10 6:172a750bbf33 204 break;
jmoreno10 6:172a750bbf33 205 }
jmoreno10 6:172a750bbf33 206 case 'f':
jmoreno10 6:172a750bbf33 207 {
jmoreno10 6:172a750bbf33 208 NAKcommand();
jmoreno10 6:172a750bbf33 209 strncpy(msg,"NAK response",sizeof(msg));
jmoreno10 6:172a750bbf33 210 break;
jmoreno10 6:172a750bbf33 211 }
jmoreno10 6:172a750bbf33 212 case 'g':
jmoreno10 6:172a750bbf33 213 {
jmoreno10 6:172a750bbf33 214 _setup();
jmoreno10 6:172a750bbf33 215 strncpy(msg,"Setup changed",sizeof(msg));
jmoreno10 6:172a750bbf33 216 break;
jmoreno10 6:172a750bbf33 217 }
jmoreno10 6:172a750bbf33 218 case 'h':
jmoreno10 6:172a750bbf33 219 {
jmoreno10 6:172a750bbf33 220 int cnt = 0; //Counter for all the characters before pressing "Return" key
jmoreno10 6:172a750bbf33 221
jmoreno10 6:172a750bbf33 222 //This loop counts all characters before the "Return" key in the buffer
jmoreno10 6:172a750bbf33 223 for (int k = 0; k < 200; k++)
jmoreno10 6:172a750bbf33 224 {
jmoreno10 6:172a750bbf33 225 if(buf[k] == '\n')
jmoreno10 6:172a750bbf33 226 break;
jmoreno10 6:172a750bbf33 227 cnt++;
jmoreno10 6:172a750bbf33 228 }
jmoreno10 6:172a750bbf33 229
jmoreno10 6:172a750bbf33 230 pc.printf("0x00\t\t\tSend Data\n\r");
jmoreno10 6:172a750bbf33 231 cnt--; //Decrement total by 1. Don't count 8.
jmoreno10 6:172a750bbf33 232 pc.printf("Data Packet length: \t%u\n",cnt); //Decrement total by 1
jmoreno10 6:172a750bbf33 233
jmoreno10 6:172a750bbf33 234 char str[cnt]; //Declare str array. str will copy buf but without '8'
jmoreno10 6:172a750bbf33 235 //Define str array. str will copy buf but without '8'
jmoreno10 6:172a750bbf33 236 for (int k = 0; k < cnt; k++)
jmoreno10 6:172a750bbf33 237 str[k] = buf[k+1]; //Starts 1 index after '8'.
jmoreno10 6:172a750bbf33 238
jmoreno10 6:172a750bbf33 239 //Declare str1 array. str1 copies str but with
jmoreno10 6:172a750bbf33 240 //the correct datalength by truncating the extra characters
jmoreno10 6:172a750bbf33 241 //found in str
jmoreno10 6:172a750bbf33 242 char str1[cnt];
jmoreno10 6:172a750bbf33 243 strncpy(str1,str,cnt); //truncation of extra characters
jmoreno10 6:172a750bbf33 244 str1[cnt] = '\0'; //Null character
jmoreno10 6:172a750bbf33 245
jmoreno10 6:172a750bbf33 246 pc.printf("Data Packet: \t\t%s \n",str1);
jmoreno10 6:172a750bbf33 247
jmoreno10 6:172a750bbf33 248 //Execute Command "Send Data"
jmoreno10 6:172a750bbf33 249 //sendData(str1);
jmoreno10 6:172a750bbf33 250 if (sendData(str1) == true)
jmoreno10 6:172a750bbf33 251 {
jmoreno10 6:172a750bbf33 252 pc.printf("\nSend Data is successful. VALID\n");
jmoreno10 6:172a750bbf33 253 strncpy(msg,"Send Data is successful",sizeof(msg));
jmoreno10 6:172a750bbf33 254 }
jmoreno10 6:172a750bbf33 255 else
jmoreno10 6:172a750bbf33 256 {
jmoreno10 6:172a750bbf33 257 pc.printf("\nNAK response. INVALID");
jmoreno10 6:172a750bbf33 258 strncpy(msg,"NAK response (Send Data)",sizeof(msg));
jmoreno10 6:172a750bbf33 259 }
jmoreno10 6:172a750bbf33 260 break;
jmoreno10 6:172a750bbf33 261 }
jmoreno10 6:172a750bbf33 262 case 'i':
jmoreno10 6:172a750bbf33 263 {
jmoreno10 6:172a750bbf33 264 if (abortTransmission() == true)
jmoreno10 6:172a750bbf33 265 {
jmoreno10 6:172a750bbf33 266 pc.printf("\nResponse Processing:\tTransmission successfully aborted.\n");
jmoreno10 6:172a750bbf33 267 strncpy(msg,"Transmission successfully aborted.",sizeof(msg));
jmoreno10 6:172a750bbf33 268 }
jmoreno10 6:172a750bbf33 269 else
jmoreno10 6:172a750bbf33 270 {
jmoreno10 6:172a750bbf33 271 pc.printf("\nResponse Processing:\tNAK response. INVALID");
jmoreno10 6:172a750bbf33 272 strncpy(msg,"NAK response (Abort Transmission)",sizeof(msg));
jmoreno10 6:172a750bbf33 273 }
jmoreno10 6:172a750bbf33 274 break; //break case '9'
jmoreno10 6:172a750bbf33 275 }
jmoreno10 6:172a750bbf33 276 } // switch(j) loop ends
jmoreno10 6:172a750bbf33 277 break; //break write addressed
jmoreno10 6:172a750bbf33 278 } // switch(i) loop ends
jmoreno10 6:172a750bbf33 279 //for(int i = 0; i < 30; i++) buf[i] = 0; // Clear buffer
jmoreno10 6:172a750bbf33 280 } // while(1) loop ends
jmoreno10 6:172a750bbf33 281 } // main loop ends
jmoreno10 6:172a750bbf33 282
jmoreno10 6:172a750bbf33 283 void clearPacket()
jmoreno10 6:172a750bbf33 284 {
jmoreno10 6:172a750bbf33 285 num = 0;
jmoreno10 6:172a750bbf33 286 for(int i = 0; i < 15 ; i++)
jmoreno10 6:172a750bbf33 287 packet[i] = 0;
jmoreno10 6:172a750bbf33 288 }
jmoreno10 6:172a750bbf33 289
jmoreno10 6:172a750bbf33 290 void printPacket()
jmoreno10 6:172a750bbf33 291 {
jmoreno10 6:172a750bbf33 292 pc.printf("\nResponse(Decimal): \t");
jmoreno10 6:172a750bbf33 293 for(int i = 0; i < 15 ; i++)
jmoreno10 6:172a750bbf33 294 {
jmoreno10 6:172a750bbf33 295 pc.printf("%u",packet[i]); // Format specifier
jmoreno10 6:172a750bbf33 296 pc.printf(" ");
jmoreno10 6:172a750bbf33 297 }
jmoreno10 6:172a750bbf33 298 }
jmoreno10 6:172a750bbf33 299
jmoreno10 6:172a750bbf33 300 void respuesta()
jmoreno10 6:172a750bbf33 301 {
jmoreno10 6:172a750bbf33 302 while(1)
jmoreno10 6:172a750bbf33 303 {
jmoreno10 6:172a750bbf33 304 if(stingr.readable())
jmoreno10 6:172a750bbf33 305 { // Se esperan datos provenientes del TX del módulo STX3
jmoreno10 6:172a750bbf33 306 incomingByte = stingr.getc();
jmoreno10 6:172a750bbf33 307 packet[num] = incomingByte;
jmoreno10 6:172a750bbf33 308 pc.printf("%X",incomingByte); // Format specifier
jmoreno10 6:172a750bbf33 309 pc.printf(" ");
jmoreno10 6:172a750bbf33 310 num++;
jmoreno10 6:172a750bbf33 311 }
jmoreno10 6:172a750bbf33 312 }
jmoreno10 6:172a750bbf33 313 }
jmoreno10 6:172a750bbf33 314
jmoreno10 6:172a750bbf33 315 void waitCTS()
jmoreno10 6:172a750bbf33 316 {
jmoreno10 6:172a750bbf33 317 Thread::wait(200); // Se da un tiempo para que el analizador se estabilice
jmoreno10 6:172a750bbf33 318 incomingByte=0;
jmoreno10 6:172a750bbf33 319 //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 6:172a750bbf33 320 //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 6:172a750bbf33 321 RTS=0; // Se manda un pulso en bajo en RTS, para inicial el proceso de transmisión
jmoreno10 3:5d959275ac05 322
jmoreno10 6:172a750bbf33 323 while(flag==1)
jmoreno10 6:172a750bbf33 324 {// Flag inicialmente vale 1, así que el ciclo while cambiará hasta que esa condición no se cumpla
jmoreno10 6:172a750bbf33 325 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 6:172a750bbf33 326 //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 6:172a750bbf33 327 }
jmoreno10 6:172a750bbf33 328 }
jmoreno10 6:172a750bbf33 329
jmoreno10 6:172a750bbf33 330 void postCommand()
jmoreno10 6:172a750bbf33 331 {
jmoreno10 6:172a750bbf33 332 Thread::wait(10); // Se esperan .1 segundos una vez que se terminaron de hacer las transmisiones
jmoreno10 3:5d959275ac05 333 //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 3:5d959275ac05 334 RTS=1;
jmoreno10 6:172a750bbf33 335 Thread::wait(150);
jmoreno10 6:172a750bbf33 336 //pc.printf("\n\rCTS: %d\n\r",CTS.read());
jmoreno10 3:5d959275ac05 337 flag=1;
jmoreno10 6:172a750bbf33 338 }
jmoreno10 6:172a750bbf33 339
jmoreno10 6:172a750bbf33 340 //0x00 Send Data
jmoreno10 6:172a750bbf33 341 bool sendData(char* b) //char* s
jmoreno10 6:172a750bbf33 342 {
jmoreno10 6:172a750bbf33 343 led4=!led4;
jmoreno10 6:172a750bbf33 344 waitCTS();
jmoreno10 6:172a750bbf33 345 Thread::wait(10);
jmoreno10 6:172a750bbf33 346
jmoreno10 6:172a750bbf33 347 size_t n = strlen(b); //Measure size of b. This includes the zeros
jmoreno10 6:172a750bbf33 348 int number = n+3;
jmoreno10 6:172a750bbf33 349 int len = n+5;
jmoreno10 6:172a750bbf33 350 char header[3] = {0xAA,len,0x00}; //Define header information
jmoreno10 6:172a750bbf33 351 char vec[number];
jmoreno10 6:172a750bbf33 352 //pc.printf("number = %u\n",number);
jmoreno10 6:172a750bbf33 353
jmoreno10 6:172a750bbf33 354 //store all in vec
jmoreno10 6:172a750bbf33 355 for(int k = 0; k < 3; k++)
jmoreno10 6:172a750bbf33 356 vec[k] = header[k];
jmoreno10 6:172a750bbf33 357 for(int k = 3; k < number; k++)
jmoreno10 6:172a750bbf33 358 vec[k] = b[k-3];
jmoreno10 6:172a750bbf33 359
jmoreno10 6:172a750bbf33 360 pc.printf("Command(HEX):\t\t");
jmoreno10 6:172a750bbf33 361 //Print b characters in HEX
jmoreno10 6:172a750bbf33 362 for(int k = 0; k < number; k++)
jmoreno10 6:172a750bbf33 363 pc.printf("%X ",vec[k]);
jmoreno10 6:172a750bbf33 364
jmoreno10 6:172a750bbf33 365 char *t = (char *)vec; //a
jmoreno10 6:172a750bbf33 366 char crc1 = ModRTU_CRC(t,t[1]-2)&0xFF;
jmoreno10 6:172a750bbf33 367 char crc2 = ModRTU_CRC(t,t[1]-2)>>8;
jmoreno10 6:172a750bbf33 368
jmoreno10 6:172a750bbf33 369 pc.printf("%X ",crc1); //%X print char in HEX format
jmoreno10 6:172a750bbf33 370 pc.printf("%X ",crc2); //%X print char in HEX format
jmoreno10 6:172a750bbf33 371 pc.printf("\nResponse(HEX):\t\t");
jmoreno10 6:172a750bbf33 372
jmoreno10 6:172a750bbf33 373 //Send Command to STINGR
jmoreno10 6:172a750bbf33 374 for(int k = 0; k < number; k++)
jmoreno10 6:172a750bbf33 375 stingr.putc(vec[k]);
jmoreno10 6:172a750bbf33 376 stingr.putc(crc1);
jmoreno10 6:172a750bbf33 377 stingr.putc(crc2);
jmoreno10 6:172a750bbf33 378
jmoreno10 6:172a750bbf33 379
jmoreno10 6:172a750bbf33 380 postCommand();
jmoreno10 6:172a750bbf33 381 printPacket();
jmoreno10 6:172a750bbf33 382
jmoreno10 6:172a750bbf33 383 //If NAK response
jmoreno10 6:172a750bbf33 384 if (packet[0] == 170 && // 0xAA
jmoreno10 6:172a750bbf33 385 packet[1] == 5 && // 0x05
jmoreno10 6:172a750bbf33 386 packet[2] == 255 && // 0xFF
jmoreno10 6:172a750bbf33 387 packet[3] == 161 && // 0xA1
jmoreno10 6:172a750bbf33 388 packet[4] == 203) // 0xCB
jmoreno10 6:172a750bbf33 389 {
jmoreno10 6:172a750bbf33 390 clearPacket();
jmoreno10 6:172a750bbf33 391 pc.printf("\n");
jmoreno10 6:172a750bbf33 392 return false;
jmoreno10 6:172a750bbf33 393 }
jmoreno10 6:172a750bbf33 394 clearPacket();
jmoreno10 6:172a750bbf33 395 pc.printf("\n");
jmoreno10 6:172a750bbf33 396 return true;
jmoreno10 6:172a750bbf33 397 }
jmoreno10 6:172a750bbf33 398
jmoreno10 6:172a750bbf33 399 //0x01 Query ESN
jmoreno10 6:172a750bbf33 400 bool queryESN()
jmoreno10 6:172a750bbf33 401 {
jmoreno10 6:172a750bbf33 402 led1=!led1;
jmoreno10 6:172a750bbf33 403 pc.printf("\r0x01\t\t\tQuery ESN\n");
jmoreno10 6:172a750bbf33 404 pc.printf("Command(HEX):\t\tAA 5 1 50 D5\n\r");
jmoreno10 6:172a750bbf33 405 pc.printf("Response(HEX):\t\t");
jmoreno10 6:172a750bbf33 406 waitCTS();
jmoreno10 6:172a750bbf33 407 Thread::wait(10);
jmoreno10 6:172a750bbf33 408
jmoreno10 6:172a750bbf33 409 stingr.putc(0XAA);
jmoreno10 6:172a750bbf33 410 stingr.putc(0X05);
jmoreno10 6:172a750bbf33 411 stingr.putc(0X01);
jmoreno10 6:172a750bbf33 412 stingr.putc(0X50);
jmoreno10 6:172a750bbf33 413 stingr.putc(0XD5);
jmoreno10 6:172a750bbf33 414
jmoreno10 6:172a750bbf33 415 postCommand();
jmoreno10 6:172a750bbf33 416 printPacket();
jmoreno10 6:172a750bbf33 417 if (packet[3] == 0 && // 0x00
jmoreno10 6:172a750bbf33 418 packet[4] == 41 && // 0x29
jmoreno10 6:172a750bbf33 419 packet[5] == 72 && // 0x48 //0x43 STINGR TLE
jmoreno10 6:172a750bbf33 420 packet[6] == 254) // 0xFE //0xB3 STINGR TLE
jmoreno10 6:172a750bbf33 421 {
jmoreno10 6:172a750bbf33 422 clearPacket();
jmoreno10 6:172a750bbf33 423 return true;
jmoreno10 6:172a750bbf33 424 }
jmoreno10 6:172a750bbf33 425 clearPacket();
jmoreno10 6:172a750bbf33 426 return false;
jmoreno10 6:172a750bbf33 427 }
jmoreno10 6:172a750bbf33 428
jmoreno10 6:172a750bbf33 429 //0x03 Abort Transmission
jmoreno10 6:172a750bbf33 430 bool abortTransmission()
jmoreno10 6:172a750bbf33 431 {
jmoreno10 6:172a750bbf33 432 led1=!led1;
jmoreno10 6:172a750bbf33 433 pc.printf("\r0x03\t\t\tAbort Transmission\n");
jmoreno10 6:172a750bbf33 434 pc.printf("Command(HEX):\t\tAA 5 3 42 F6\n\r");
jmoreno10 6:172a750bbf33 435 pc.printf("Response(HEX):\t\t");
jmoreno10 6:172a750bbf33 436 waitCTS();
jmoreno10 6:172a750bbf33 437 Thread::wait(10);
jmoreno10 6:172a750bbf33 438 stingr.putc(0XAA);
jmoreno10 6:172a750bbf33 439 stingr.putc(0X05);
jmoreno10 6:172a750bbf33 440 stingr.putc(0X03);
jmoreno10 6:172a750bbf33 441 stingr.putc(0X42);
jmoreno10 6:172a750bbf33 442 stingr.putc(0XF6);
jmoreno10 6:172a750bbf33 443
jmoreno10 6:172a750bbf33 444 postCommand();
jmoreno10 6:172a750bbf33 445 printPacket();
jmoreno10 6:172a750bbf33 446 if (packet[0] == 170 && // 0xAA
jmoreno10 6:172a750bbf33 447 packet[1] == 5 && // 0x05
jmoreno10 6:172a750bbf33 448 packet[2] == 3 && // 0x03
jmoreno10 6:172a750bbf33 449 packet[3] == 66 && // 0x42
jmoreno10 6:172a750bbf33 450 packet[4] == 246) // 0xF6
jmoreno10 6:172a750bbf33 451 {
jmoreno10 6:172a750bbf33 452 clearPacket();
jmoreno10 6:172a750bbf33 453 return true;
jmoreno10 6:172a750bbf33 454 }
jmoreno10 6:172a750bbf33 455 clearPacket();
jmoreno10 6:172a750bbf33 456 return false;
jmoreno10 6:172a750bbf33 457 }
jmoreno10 6:172a750bbf33 458
jmoreno10 6:172a750bbf33 459 //0x04 Query Bursts
jmoreno10 6:172a750bbf33 460 int queryBursts()
jmoreno10 6:172a750bbf33 461 {
jmoreno10 6:172a750bbf33 462 int bursts = 99;
jmoreno10 6:172a750bbf33 463
jmoreno10 6:172a750bbf33 464 led2=!led2;
jmoreno10 6:172a750bbf33 465 pc.printf("\r0x04\t\t\tQuery Burst Remaining\n");
jmoreno10 6:172a750bbf33 466 pc.printf("Command(HEX):\t\tAA 5 4 FD 82\n\r");
jmoreno10 6:172a750bbf33 467 pc.printf("Response(HEX):\t\t");
jmoreno10 6:172a750bbf33 468 waitCTS();
jmoreno10 6:172a750bbf33 469 Thread::wait(10);
jmoreno10 6:172a750bbf33 470
jmoreno10 6:172a750bbf33 471 stingr.putc(0XAA);
jmoreno10 6:172a750bbf33 472 stingr.putc(0X05);
jmoreno10 6:172a750bbf33 473 stingr.putc(0X04);
jmoreno10 6:172a750bbf33 474 stingr.putc(0XFD);
jmoreno10 6:172a750bbf33 475 stingr.putc(0X82);
jmoreno10 6:172a750bbf33 476
jmoreno10 6:172a750bbf33 477 postCommand();
jmoreno10 6:172a750bbf33 478 printPacket();
jmoreno10 3:5d959275ac05 479
jmoreno10 6:172a750bbf33 480 //If NAK response
jmoreno10 6:172a750bbf33 481 if (packet[0] == 170 && // 0xAA
jmoreno10 6:172a750bbf33 482 packet[1] == 5 && // 0x05
jmoreno10 6:172a750bbf33 483 packet[2] == 255 && // 0xFF
jmoreno10 6:172a750bbf33 484 packet[3] == 161 && // 0xA1
jmoreno10 6:172a750bbf33 485 packet[4] == 203) // 0xCB
jmoreno10 6:172a750bbf33 486 {
jmoreno10 6:172a750bbf33 487 clearPacket();
jmoreno10 6:172a750bbf33 488 return bursts;
jmoreno10 6:172a750bbf33 489 }
jmoreno10 6:172a750bbf33 490 bursts = packet[3];
jmoreno10 6:172a750bbf33 491 clearPacket();
jmoreno10 6:172a750bbf33 492 return bursts;
jmoreno10 6:172a750bbf33 493 }
jmoreno10 6:172a750bbf33 494
jmoreno10 6:172a750bbf33 495 //0x05 Query Firmware
jmoreno10 6:172a750bbf33 496 bool queryFirmware()
jmoreno10 6:172a750bbf33 497 {
jmoreno10 6:172a750bbf33 498 led3=!led3;
jmoreno10 6:172a750bbf33 499 pc.printf("\r0x05\t\t\tQuery Firmware Version\n");
jmoreno10 6:172a750bbf33 500 pc.printf("Command(HEX):\t\tAA 5 5 74 93\n\r");
jmoreno10 6:172a750bbf33 501 pc.printf("Response(HEX):\t\t");
jmoreno10 6:172a750bbf33 502 waitCTS();
jmoreno10 6:172a750bbf33 503 Thread::wait(10);
jmoreno10 6:172a750bbf33 504 stingr.putc(0XAA);
jmoreno10 6:172a750bbf33 505 stingr.putc(0X05);
jmoreno10 6:172a750bbf33 506 stingr.putc(0X05);
jmoreno10 6:172a750bbf33 507 stingr.putc(0X74);
jmoreno10 6:172a750bbf33 508 stingr.putc(0X93);
jmoreno10 6:172a750bbf33 509
jmoreno10 6:172a750bbf33 510 postCommand();
jmoreno10 6:172a750bbf33 511 printPacket();
jmoreno10 6:172a750bbf33 512 if (packet[3] == 1 && // 0x01
jmoreno10 6:172a750bbf33 513 packet[4] == 3) // 0x03
jmoreno10 6:172a750bbf33 514 {
jmoreno10 6:172a750bbf33 515 clearPacket();
jmoreno10 6:172a750bbf33 516 return true;
jmoreno10 6:172a750bbf33 517 }
jmoreno10 6:172a750bbf33 518 clearPacket();
jmoreno10 6:172a750bbf33 519 return false;
jmoreno10 6:172a750bbf33 520 }
jmoreno10 6:172a750bbf33 521
jmoreno10 6:172a750bbf33 522 //0x06 Setup
jmoreno10 6:172a750bbf33 523 void _setup()
jmoreno10 6:172a750bbf33 524 {
jmoreno10 6:172a750bbf33 525 led3=!led3;
jmoreno10 6:172a750bbf33 526 pc.printf("\r0x06\t\t\tSetup\n");
jmoreno10 6:172a750bbf33 527 pc.printf("Command(HEX):\t\tAA 0E 06 00 00 00 00 00 03 18 30 00 CE 9C\n\r");
jmoreno10 6:172a750bbf33 528 pc.printf("Response(HEX):\t\t");
jmoreno10 6:172a750bbf33 529 waitCTS();
jmoreno10 6:172a750bbf33 530 Thread::wait(10);
jmoreno10 6:172a750bbf33 531 stingr.putc(0XAA);
jmoreno10 6:172a750bbf33 532 stingr.putc(0X0E);
jmoreno10 6:172a750bbf33 533 stingr.putc(0X06);
jmoreno10 6:172a750bbf33 534 stingr.putc(0X00);
jmoreno10 6:172a750bbf33 535 stingr.putc(0X00);
jmoreno10 6:172a750bbf33 536 stingr.putc(0X00);
jmoreno10 6:172a750bbf33 537 stingr.putc(0X00);
jmoreno10 6:172a750bbf33 538 stingr.putc(0X00);
jmoreno10 6:172a750bbf33 539 stingr.putc(0X03);
jmoreno10 6:172a750bbf33 540 stingr.putc(0X18);
jmoreno10 6:172a750bbf33 541 stingr.putc(0X30);
jmoreno10 6:172a750bbf33 542 stingr.putc(0X00);
jmoreno10 6:172a750bbf33 543 stingr.putc(0XCE);
jmoreno10 6:172a750bbf33 544 stingr.putc(0X9C);
jmoreno10 6:172a750bbf33 545
jmoreno10 6:172a750bbf33 546 postCommand();
jmoreno10 6:172a750bbf33 547 printPacket();
jmoreno10 3:5d959275ac05 548
jmoreno10 6:172a750bbf33 549 pc.printf("\n");
jmoreno10 6:172a750bbf33 550 clearPacket();
jmoreno10 6:172a750bbf33 551 }
jmoreno10 6:172a750bbf33 552
jmoreno10 6:172a750bbf33 553 //0x07 Query Setup
jmoreno10 6:172a750bbf33 554 long int querySetup()
jmoreno10 6:172a750bbf33 555 {
jmoreno10 6:172a750bbf33 556 int numSetup = 0;
jmoreno10 6:172a750bbf33 557 led4=!led4;
jmoreno10 6:172a750bbf33 558 pc.printf("\r0x07\t\t\tQuery Setup\n");
jmoreno10 6:172a750bbf33 559 pc.printf("Command(HEX):\t\tAA 5 7 66 B0\n\r");
jmoreno10 6:172a750bbf33 560 pc.printf("Response(HEX):\t\t");
jmoreno10 6:172a750bbf33 561 waitCTS();
jmoreno10 6:172a750bbf33 562 Thread::wait(10);
jmoreno10 6:172a750bbf33 563
jmoreno10 6:172a750bbf33 564 stingr.putc(0XAA);
jmoreno10 6:172a750bbf33 565 stingr.putc(0X05);
jmoreno10 6:172a750bbf33 566 stingr.putc(0X07);
jmoreno10 6:172a750bbf33 567 stingr.putc(0X66);
jmoreno10 6:172a750bbf33 568 stingr.putc(0XB0);
jmoreno10 6:172a750bbf33 569
jmoreno10 6:172a750bbf33 570 postCommand();
jmoreno10 5:c52a2d06bd93 571
jmoreno10 6:172a750bbf33 572 //If NAK response
jmoreno10 6:172a750bbf33 573 if (packet[0] == 170 && // 0xAA
jmoreno10 6:172a750bbf33 574 packet[1] == 5 && // 0x05
jmoreno10 6:172a750bbf33 575 packet[2] == 255 && // 0xFF
jmoreno10 6:172a750bbf33 576 packet[3] == 161 && // 0xA1
jmoreno10 6:172a750bbf33 577 packet[4] == 203) // 0xCB
jmoreno10 6:172a750bbf33 578 {
jmoreno10 6:172a750bbf33 579 clearPacket();
jmoreno10 6:172a750bbf33 580 return NULL;
jmoreno10 6:172a750bbf33 581 }
jmoreno10 6:172a750bbf33 582 numSetup = packet[7]*10000000 + packet[8]*100000 + packet[9]*1000 + packet[10];
jmoreno10 6:172a750bbf33 583 clearPacket();
jmoreno10 6:172a750bbf33 584 return numSetup;
jmoreno10 6:172a750bbf33 585 }
jmoreno10 6:172a750bbf33 586
jmoreno10 6:172a750bbf33 587 //0x09 Query Hardware
jmoreno10 6:172a750bbf33 588 bool queryHardware()
jmoreno10 6:172a750bbf33 589 {
jmoreno10 6:172a750bbf33 590 led1=!led1;
jmoreno10 6:172a750bbf33 591 pc.printf("\r0x09\t\t\tQuery Hardware Version\n");
jmoreno10 6:172a750bbf33 592 pc.printf("Command(HEX):\t\tAA 5 9 18 59\n\r");
jmoreno10 6:172a750bbf33 593 pc.printf("Response(HEX):\t\t");
jmoreno10 6:172a750bbf33 594 waitCTS();
jmoreno10 6:172a750bbf33 595 Thread::wait(10);
jmoreno10 6:172a750bbf33 596
jmoreno10 6:172a750bbf33 597 stingr.putc(0XAA);
jmoreno10 6:172a750bbf33 598 stingr.putc(0X05);
jmoreno10 6:172a750bbf33 599 stingr.putc(0X09);
jmoreno10 6:172a750bbf33 600 stingr.putc(0X18);
jmoreno10 6:172a750bbf33 601 stingr.putc(0X59);
jmoreno10 6:172a750bbf33 602
jmoreno10 6:172a750bbf33 603 postCommand();
jmoreno10 6:172a750bbf33 604 printPacket();
jmoreno10 6:172a750bbf33 605
jmoreno10 5:c52a2d06bd93 606
jmoreno10 6:172a750bbf33 607 if (packet[5] == 143 && // 0x8F
jmoreno10 6:172a750bbf33 608 packet[6] == 98 && // 0x62
jmoreno10 6:172a750bbf33 609 packet[7] == 98) // 0x62
jmoreno10 6:172a750bbf33 610 {
jmoreno10 6:172a750bbf33 611 clearPacket();
jmoreno10 6:172a750bbf33 612 return true;
jmoreno10 6:172a750bbf33 613 }
jmoreno10 6:172a750bbf33 614
jmoreno10 6:172a750bbf33 615
jmoreno10 6:172a750bbf33 616 clearPacket();
jmoreno10 6:172a750bbf33 617 pc.printf("\n");
jmoreno10 6:172a750bbf33 618 return false;
jmoreno10 6:172a750bbf33 619 }
jmoreno10 3:5d959275ac05 620
jmoreno10 6:172a750bbf33 621 //NAK Command
jmoreno10 6:172a750bbf33 622 void NAKcommand()
jmoreno10 3:5d959275ac05 623 {
jmoreno10 6:172a750bbf33 624 led2=!led2;
jmoreno10 6:172a750bbf33 625 pc.printf("\rXxXX\t\t\tNAK\n");
jmoreno10 6:172a750bbf33 626 pc.printf("Command(HEX):\t\tAA 5 7 66 B1\n\r");
jmoreno10 6:172a750bbf33 627 pc.printf("Response(HEX):\t\t");
jmoreno10 6:172a750bbf33 628 waitCTS();
jmoreno10 6:172a750bbf33 629 Thread::wait(10);
jmoreno10 6:172a750bbf33 630
jmoreno10 6:172a750bbf33 631 stingr.putc(0XAA);
jmoreno10 6:172a750bbf33 632 stingr.putc(0X05);
jmoreno10 6:172a750bbf33 633 stingr.putc(0X07);
jmoreno10 6:172a750bbf33 634 stingr.putc(0X66);
jmoreno10 6:172a750bbf33 635 stingr.putc(0XB1);
jmoreno10 6:172a750bbf33 636
jmoreno10 6:172a750bbf33 637 postCommand();
jmoreno10 6:172a750bbf33 638 printPacket();
jmoreno10 6:172a750bbf33 639 clearPacket();
jmoreno10 6:172a750bbf33 640 pc.printf("\n");
jmoreno10 3:5d959275ac05 641 }
jmoreno10 3:5d959275ac05 642
jmoreno10 3:5d959275ac05 643 uint16_t ModRTU_CRC(char * buf, int len)
jmoreno10 3:5d959275ac05 644 {
jmoreno10 3:5d959275ac05 645 unsigned char i;
jmoreno10 3:5d959275ac05 646 unsigned short data;
jmoreno10 3:5d959275ac05 647 uint16_t crc = 0xFFFF;
jmoreno10 3:5d959275ac05 648
jmoreno10 3:5d959275ac05 649 do{
jmoreno10 3:5d959275ac05 650 data = (unsigned int)0x00FF & *buf++;
jmoreno10 3:5d959275ac05 651 crc = crc ^ data;
jmoreno10 3:5d959275ac05 652
jmoreno10 3:5d959275ac05 653 for(i = 8; i > 0; i--)
jmoreno10 3:5d959275ac05 654 {
jmoreno10 3:5d959275ac05 655 if(crc & 0x0001)
jmoreno10 3:5d959275ac05 656 crc = (crc >> 1) ^ 0x8408;
jmoreno10 3:5d959275ac05 657 else
jmoreno10 3:5d959275ac05 658 crc >>=1;
jmoreno10 3:5d959275ac05 659 }
jmoreno10 3:5d959275ac05 660
jmoreno10 3:5d959275ac05 661 }while (--len);
jmoreno10 3:5d959275ac05 662
jmoreno10 3:5d959275ac05 663 crc = ~crc;
jmoreno10 3:5d959275ac05 664
jmoreno10 3:5d959275ac05 665 // Note, this number has low and high bytes swapped, so use it accordingly (or swap bytes)
jmoreno10 3:5d959275ac05 666 return (crc);
jmoreno10 3:5d959275ac05 667 }
jmoreno10 3:5d959275ac05 668
jmoreno10 6:172a750bbf33 669 void combine(char* s, char c)
jmoreno10 3:5d959275ac05 670 {
jmoreno10 6:172a750bbf33 671 int len = strlen(s);
jmoreno10 6:172a750bbf33 672 s[len] = c;
jmoreno10 6:172a750bbf33 673 s[len+1] = '\0';
jmoreno10 5:c52a2d06bd93 674 }