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