Kenneth Dunlop / Mbed 2 deprecated Ken_CAN_test

Dependencies:   mbed

Committer:
kendunlop
Date:
Wed Jun 22 16:04:06 2022 +0000
Revision:
8:6f096b45ca15
Parent:
7:a9150dc1e481
Child:
9:7c27efe30a77
Nice red highlighted text now shows where the mismatches are in the data!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kendunlop 1:19d183cf2689 1 //This program was created by Ken 8th June 2022 to test the CAN output of a CAN Gateway.
kendunlop 4:e8e9bc25b1ca 2 #define kMajorVersion 0 //Defines a major and minor version to be shown later in the program.
kendunlop 1:19d183cf2689 3 #define kMinorVersion 5
kendunlop 1:19d183cf2689 4
kendunlop 1:19d183cf2689 5 #include "mbed.h" //Including mbed libraries. This should be all it needs to use the Mbed and CAN.
kendunlop 1:19d183cf2689 6 #include <string> //This is needed to make text strings work
kendunlop 1:19d183cf2689 7 #include <stdio.h>
kendunlop 1:19d183cf2689 8 #include <ctype.h> //For 'is digit' condition
kendunlop 1:19d183cf2689 9 #include <sstream> //This is used to convert an integer to hex
kendunlop 2:11339018dda6 10 #include <time.h> //To measure how long a process took? Includes clock_t and CLOCKS_PER_SEC
kendunlop 1:19d183cf2689 11
kendunlop 3:79133dcea836 12 RawSerial pc(USBTX, USBRX); // USB UART Terminal, tx, rx. Is needed for 'pc.printf' functions to work.
kendunlop 3:79133dcea836 13
kendunlop 3:79133dcea836 14 // NOTE: Original pins for CAN Bus 1 are 9 and 10.
kendunlop 7:a9150dc1e481 15 CAN CanBus(p9, p10); //CANBus to use pins 9 and 10. This defines CanBus so other 'CanBus' lines will work.
kendunlop 7:a9150dc1e481 16 CAN CanBus2(p30, p29); //CANBus2 for recieving to use pins 30 and 29 (in that order!).
kendunlop 1:19d183cf2689 17 CANMessage messageIn;
kendunlop 1:19d183cf2689 18
kendunlop 1:19d183cf2689 19 //Define a CAN message to send
kendunlop 1:19d183cf2689 20 CANMessage messageOut1;
kendunlop 1:19d183cf2689 21
kendunlop 1:19d183cf2689 22 //Set messageOutText to a default message so it's defined.
kendunlop 5:bf4c6278ca8b 23 string messageOutText = "";
kendunlop 7:a9150dc1e481 24 string messageOutTextWithSpaces = "";
kendunlop 8:6f096b45ca15 25 string buttonPressMessageOutText = "";
kendunlop 1:19d183cf2689 26 string part = "";
kendunlop 1:19d183cf2689 27 int partincrement = 0;
kendunlop 3:79133dcea836 28 __int64 incrementer = 0;
kendunlop 1:19d183cf2689 29 int CANCount = 0;
kendunlop 4:e8e9bc25b1ca 30 int spamon = 0;
kendunlop 6:2882710e4f1e 31 int idspamon = 0;
kendunlop 8:6f096b45ca15 32 int idscheckedcount = 0;
kendunlop 1:19d183cf2689 33 int spamcount = 0;
kendunlop 3:79133dcea836 34 int totalspamsever = 0;
kendunlop 2:11339018dda6 35 int spamstarttime = 0;
kendunlop 2:11339018dda6 36 int spamendtime = 0;
kendunlop 3:79133dcea836 37 int vlistincrementer = 0;
kendunlop 6:2882710e4f1e 38 int idlistincrementer = 0;
kendunlop 6:2882710e4f1e 39 string spampreamble = "333";
kendunlop 7:a9150dc1e481 40 int expected1 = 0;
kendunlop 7:a9150dc1e481 41 int expected2 = 0;
kendunlop 7:a9150dc1e481 42 unsigned char expected3 = 0;
kendunlop 7:a9150dc1e481 43 unsigned char expected4 = 0;
kendunlop 7:a9150dc1e481 44 unsigned char expected5 = 0;
kendunlop 7:a9150dc1e481 45 unsigned char expected6 = 0;
kendunlop 7:a9150dc1e481 46 unsigned char expected7 = 0;
kendunlop 7:a9150dc1e481 47 unsigned char expected8 = 0;
kendunlop 7:a9150dc1e481 48 unsigned char expected9 = 0;
kendunlop 7:a9150dc1e481 49 unsigned char expected10 = 0;
kendunlop 8:6f096b45ca15 50 int expectationwasfulfilled = -1;
kendunlop 8:6f096b45ca15 51 int expectationresolved = 1;
kendunlop 8:6f096b45ca15 52 int expectationtimer = 0;
kendunlop 8:6f096b45ca15 53 int shouldntcomein = 0;
kendunlop 8:6f096b45ca15 54
kendunlop 7:a9150dc1e481 55 int failsthistest = 0;
kendunlop 8:6f096b45ca15 56 int noreplyfailsthistest = 0;
kendunlop 8:6f096b45ca15 57 int mismatchfailsthistest = 0;
kendunlop 7:a9150dc1e481 58 int addnewlinetonextmessage = 0;
kendunlop 8:6f096b45ca15 59 int checklaterbytes = 0;
kendunlop 8:6f096b45ca15 60 int aspamisgoing = 0;
kendunlop 8:6f096b45ca15 61 int CANpassthrough = 1;
kendunlop 8:6f096b45ca15 62
kendunlop 8:6f096b45ca15 63 //Array of IDs to translate can be overwritten with the 'copyarray'.
kendunlop 8:6f096b45ca15 64 //It's arranged in columns of IDs that are translated to the ID in the next column.
kendunlop 8:6f096b45ca15 65 //NOTE: Array must be a large enough size to contain all ID translations.
kendunlop 8:6f096b45ca15 66 int idsarray[2][8] = {};
kendunlop 8:6f096b45ca15 67 int arrayx = 2;
kendunlop 8:6f096b45ca15 68 int arrayy = 8;
kendunlop 8:6f096b45ca15 69
kendunlop 8:6f096b45ca15 70 //A function to handle when CAN-expectations are not fulfilled
kendunlop 8:6f096b45ca15 71 void expectationwasnotfulfilled(void)
kendunlop 8:6f096b45ca15 72 {
kendunlop 8:6f096b45ca15 73 //pc.printf("Expectation was not fulfilled for that message!\r\n");
kendunlop 8:6f096b45ca15 74 }
kendunlop 7:a9150dc1e481 75
kendunlop 7:a9150dc1e481 76 //A function to clear all test-related variables
kendunlop 7:a9150dc1e481 77 void resettest(void)
kendunlop 7:a9150dc1e481 78 {
kendunlop 7:a9150dc1e481 79 pc.printf("Resetting test.\r\n");
kendunlop 7:a9150dc1e481 80 failsthistest = 0;
kendunlop 8:6f096b45ca15 81 noreplyfailsthistest = 0;
kendunlop 8:6f096b45ca15 82 mismatchfailsthistest = 0;
kendunlop 8:6f096b45ca15 83 idscheckedcount = 0;
kendunlop 7:a9150dc1e481 84 }
kendunlop 7:a9150dc1e481 85
kendunlop 7:a9150dc1e481 86 //A function to add a carriage return in the output if the system has been writing out the CAN messages it sends.
kendunlop 7:a9150dc1e481 87 void addnewlineifneeded(void)
kendunlop 7:a9150dc1e481 88 {
kendunlop 7:a9150dc1e481 89 if (addnewlinetonextmessage == 1)
kendunlop 7:a9150dc1e481 90 {
kendunlop 7:a9150dc1e481 91 pc.printf("\r\n");
kendunlop 7:a9150dc1e481 92 addnewlinetonextmessage = 0;
kendunlop 7:a9150dc1e481 93 }
kendunlop 7:a9150dc1e481 94 }
kendunlop 7:a9150dc1e481 95
kendunlop 7:a9150dc1e481 96
kendunlop 7:a9150dc1e481 97 //A function to end a test and report on findings.
kendunlop 7:a9150dc1e481 98 void endtest()
kendunlop 7:a9150dc1e481 99 {
kendunlop 8:6f096b45ca15 100 expectationresolved = 1;
kendunlop 7:a9150dc1e481 101 if (failsthistest == 0)
kendunlop 7:a9150dc1e481 102 {
kendunlop 7:a9150dc1e481 103 pc.printf("\033[1;32mTest complete. %d failures found.\033[0m\r\n", failsthistest);
kendunlop 7:a9150dc1e481 104 }
kendunlop 7:a9150dc1e481 105 if (failsthistest > 0)
kendunlop 7:a9150dc1e481 106 {
kendunlop 8:6f096b45ca15 107 pc.printf("\033[1;31mTest complete. %d failures found. %d were mismatch failures. %d were 'no reply' failures.\033[0m\r\n", failsthistest, mismatchfailsthistest, noreplyfailsthistest);
kendunlop 7:a9150dc1e481 108 }
kendunlop 7:a9150dc1e481 109 addnewlineifneeded();
kendunlop 7:a9150dc1e481 110 pc.printf("-------------------------------------\r\n");
kendunlop 7:a9150dc1e481 111 failsthistest = 0;
kendunlop 7:a9150dc1e481 112 }
kendunlop 7:a9150dc1e481 113
kendunlop 7:a9150dc1e481 114 //A generic 'send CAN' function to send CAN with a standard message
kendunlop 7:a9150dc1e481 115 void sendCAN(void)
kendunlop 7:a9150dc1e481 116 {
kendunlop 7:a9150dc1e481 117 CanBus.write(messageOut1);
kendunlop 7:a9150dc1e481 118 //addnewlineifneeded();
kendunlop 8:6f096b45ca15 119 pc.printf("\033[1;33mSending out: %s \033[0m", messageOutTextWithSpaces);
kendunlop 8:6f096b45ca15 120 if (aspamisgoing == 0)
kendunlop 8:6f096b45ca15 121 {
kendunlop 8:6f096b45ca15 122 //pc.printf("\r\n");
kendunlop 8:6f096b45ca15 123 }
kendunlop 8:6f096b45ca15 124 expectationresolved = 0;
kendunlop 8:6f096b45ca15 125 //pc.printf("Expectationresolved now is %d.", expectationresolved);
kendunlop 8:6f096b45ca15 126 addnewlineifneeded();
kendunlop 8:6f096b45ca15 127 expectationtimer = 0;
kendunlop 7:a9150dc1e481 128 //addnewlinetonextmessage = 1;
kendunlop 7:a9150dc1e481 129 }
kendunlop 1:19d183cf2689 130
kendunlop 2:11339018dda6 131 //A function to deal with each CAN message part (e.g.: 301, 8, 01, 02...)
kendunlop 1:19d183cf2689 132 void dealwithpart(void)
kendunlop 1:19d183cf2689 133 {
kendunlop 1:19d183cf2689 134 int hextotal = 0;
kendunlop 1:19d183cf2689 135 partincrement = partincrement + 1;
kendunlop 1:19d183cf2689 136 //pc.printf("Dealing with part %d. (%s)\r\n", partincrement, part);
kendunlop 1:19d183cf2689 137 //int partincrementb = 0;
kendunlop 1:19d183cf2689 138 int textlength = part.length();
kendunlop 1:19d183cf2689 139 //int characterinc = 0;
kendunlop 1:19d183cf2689 140 //pc.printf("That's %d characters long.\r\n", textlength);
kendunlop 1:19d183cf2689 141 for (int i = 0; i < part.size(); i++)
kendunlop 1:19d183cf2689 142 {
kendunlop 1:19d183cf2689 143 //pc.printf("Examining character %d/%d.\r\n", (i+1), textlength);
kendunlop 1:19d183cf2689 144 char individualcharacter = part.at(i);
kendunlop 1:19d183cf2689 145 //pc.printf("That's '%c'.\r\n", individualcharacter);
kendunlop 1:19d183cf2689 146 int numberized = 0;
kendunlop 1:19d183cf2689 147 if(isdigit(individualcharacter))
kendunlop 1:19d183cf2689 148 {
kendunlop 1:19d183cf2689 149 //pc.printf("That character is a digit.\r\n");
kendunlop 1:19d183cf2689 150 numberized = individualcharacter - '0';
kendunlop 1:19d183cf2689 151 //pc.printf("Numberized that's '%d'.\r\n", numberized);
kendunlop 1:19d183cf2689 152 }
kendunlop 1:19d183cf2689 153 else
kendunlop 1:19d183cf2689 154 {
kendunlop 1:19d183cf2689 155 //pc.printf("That character is NOT a digit.\r\n");
kendunlop 1:19d183cf2689 156 int asciivalue = toupper(individualcharacter);
kendunlop 1:19d183cf2689 157 //pc.printf("Ascii value is %d.\r\n", asciivalue);
kendunlop 1:19d183cf2689 158 numberized = asciivalue - 55;
kendunlop 1:19d183cf2689 159 //pc.printf("Hex value is %d.\r\n", numberized);
kendunlop 1:19d183cf2689 160 }
kendunlop 1:19d183cf2689 161 //pc.printf("Eventual numberization is %d.\r\n", numberized);
kendunlop 1:19d183cf2689 162 //pc.printf("Hex total is now %d.\r\n", hextotal);
kendunlop 1:19d183cf2689 163 int powertoraise = part.size() - (i+1);
kendunlop 1:19d183cf2689 164 //pc.printf("Must multiply by 16 to the power of %d.\r\n", powertoraise);
kendunlop 1:19d183cf2689 165 int amounttoadd = numberized;
kendunlop 1:19d183cf2689 166 //pc.printf("powertoraise is '%d'.\r\n", powertoraise);
kendunlop 1:19d183cf2689 167 if (powertoraise == 1)
kendunlop 1:19d183cf2689 168 {
kendunlop 1:19d183cf2689 169 amounttoadd = numberized * 16;
kendunlop 1:19d183cf2689 170 //pc.printf("Multiplying by 16.\r\n");
kendunlop 1:19d183cf2689 171 //pc.printf("powertoraise is '%d'.\r\n", powertoraise);
kendunlop 1:19d183cf2689 172 }
kendunlop 1:19d183cf2689 173 if (powertoraise == 2)
kendunlop 1:19d183cf2689 174 {
kendunlop 1:19d183cf2689 175 amounttoadd = numberized * 256;
kendunlop 1:19d183cf2689 176 //pc.printf("Multiplying by 256.\r\n");
kendunlop 1:19d183cf2689 177 }
kendunlop 1:19d183cf2689 178 //pc.printf("Amount to add is therefore %d.\r\n", amounttoadd);
kendunlop 1:19d183cf2689 179 hextotal = hextotal + amounttoadd;
kendunlop 1:19d183cf2689 180 //pc.printf("hextotal so far for this part is therefore %d.\r\n", hextotal);
kendunlop 1:19d183cf2689 181 }
kendunlop 1:19d183cf2689 182 //pc.printf("hextotal for whole part is therefore %d.\r\n", hextotal);
kendunlop 1:19d183cf2689 183 //pc.printf("Need to convert that into true hex.\r\n");
kendunlop 1:19d183cf2689 184 std::stringstream sstream;
kendunlop 1:19d183cf2689 185 sstream << std::hex << hextotal;
kendunlop 1:19d183cf2689 186 //pc.printf("StringSteam says '%s'.\r\n", sstream.str());
kendunlop 1:19d183cf2689 187 if (partincrement == 1)
kendunlop 7:a9150dc1e481 188 {
kendunlop 7:a9150dc1e481 189 messageOut1.id = hextotal;
kendunlop 7:a9150dc1e481 190 expected1 = hextotal;
kendunlop 8:6f096b45ca15 191 //For IDs, check if the ID is represented in the idsarray.
kendunlop 8:6f096b45ca15 192 int idwasfound = 0;
kendunlop 8:6f096b45ca15 193 idwasfound = 0;
kendunlop 8:6f096b45ca15 194 shouldntcomein = 0;
kendunlop 8:6f096b45ca15 195 if (CANpassthrough == 0)
kendunlop 8:6f096b45ca15 196 {
kendunlop 8:6f096b45ca15 197 for(int i = 0; i <=arrayy-1; i++)
kendunlop 8:6f096b45ca15 198 {
kendunlop 8:6f096b45ca15 199 if(idsarray[0][i] == expected1)
kendunlop 8:6f096b45ca15 200 {
kendunlop 8:6f096b45ca15 201 idwasfound = 1;
kendunlop 8:6f096b45ca15 202 int originalexpectedvalue = expected1;
kendunlop 8:6f096b45ca15 203 expected1 = idsarray[1][i];
kendunlop 8:6f096b45ca15 204 pc.printf("\r\nFound message ID %d at 0,%d in idsarray! Will adjust expected value to %d.\r\n", originalexpectedvalue, i, expected1);
kendunlop 8:6f096b45ca15 205 }
kendunlop 8:6f096b45ca15 206 }
kendunlop 8:6f096b45ca15 207 if (idwasfound == 0 and CANpassthrough == 0)
kendunlop 8:6f096b45ca15 208 {
kendunlop 8:6f096b45ca15 209 //pc.printf("ID of %d was never found, expecting it NOT to come in.\r\n", expected1);
kendunlop 8:6f096b45ca15 210 //pc.printf("CANpassthrough is %d.\r\n", CANpassthrough);
kendunlop 8:6f096b45ca15 211 shouldntcomein = 1;
kendunlop 8:6f096b45ca15 212 }
kendunlop 8:6f096b45ca15 213 }
kendunlop 7:a9150dc1e481 214 }
kendunlop 1:19d183cf2689 215 if (partincrement == 2)
kendunlop 7:a9150dc1e481 216 {
kendunlop 7:a9150dc1e481 217 messageOut1.len = hextotal;
kendunlop 7:a9150dc1e481 218 expected2 = hextotal;
kendunlop 7:a9150dc1e481 219 }
kendunlop 1:19d183cf2689 220 if (partincrement >= 3)
kendunlop 7:a9150dc1e481 221 {
kendunlop 7:a9150dc1e481 222 messageOut1.data[partincrement-3] = hextotal;
kendunlop 7:a9150dc1e481 223 }
kendunlop 7:a9150dc1e481 224 if (partincrement == 3)
kendunlop 7:a9150dc1e481 225 {
kendunlop 7:a9150dc1e481 226 expected3 = hextotal;
kendunlop 7:a9150dc1e481 227 }
kendunlop 7:a9150dc1e481 228 if (partincrement == 4)
kendunlop 7:a9150dc1e481 229 {
kendunlop 7:a9150dc1e481 230 expected4 = hextotal;
kendunlop 7:a9150dc1e481 231 }
kendunlop 7:a9150dc1e481 232 if (partincrement == 5)
kendunlop 7:a9150dc1e481 233 {
kendunlop 7:a9150dc1e481 234 expected5 = hextotal;
kendunlop 7:a9150dc1e481 235 }
kendunlop 7:a9150dc1e481 236 if (partincrement == 6)
kendunlop 7:a9150dc1e481 237 {
kendunlop 7:a9150dc1e481 238 expected6 = hextotal;
kendunlop 7:a9150dc1e481 239 }
kendunlop 7:a9150dc1e481 240 if (partincrement == 7)
kendunlop 7:a9150dc1e481 241 {
kendunlop 7:a9150dc1e481 242 expected7 = hextotal;
kendunlop 7:a9150dc1e481 243 }
kendunlop 7:a9150dc1e481 244 if (partincrement == 8)
kendunlop 7:a9150dc1e481 245 {
kendunlop 7:a9150dc1e481 246 expected8 = hextotal;
kendunlop 7:a9150dc1e481 247 }
kendunlop 7:a9150dc1e481 248 if (partincrement == 9)
kendunlop 7:a9150dc1e481 249 {
kendunlop 7:a9150dc1e481 250 expected9 = hextotal;
kendunlop 7:a9150dc1e481 251 }
kendunlop 7:a9150dc1e481 252 if (partincrement == 10)
kendunlop 7:a9150dc1e481 253 {
kendunlop 7:a9150dc1e481 254 expected10 = hextotal;
kendunlop 7:a9150dc1e481 255 }
kendunlop 1:19d183cf2689 256 //pc.printf("Part %d complete.\r\n", partincrement);
kendunlop 1:19d183cf2689 257 //pc.printf("--------------------------------------\r\n");
kendunlop 1:19d183cf2689 258 }
kendunlop 1:19d183cf2689 259
kendunlop 3:79133dcea836 260 //A function to get a coherent CAN message from one, uninterrupted string
kendunlop 6:2882710e4f1e 261 void getCANfrommessageOutText(void)
kendunlop 1:19d183cf2689 262 {
kendunlop 1:19d183cf2689 263 //pc.printf("messageOutText is '%s'\r\n", messageOutText);
kendunlop 7:a9150dc1e481 264 messageOutTextWithSpaces = messageOutText;
kendunlop 1:19d183cf2689 265 remove(messageOutText.begin(), messageOutText.end(), ' '); //Remove the spaces from the text to send out so it can be parsed.
kendunlop 1:19d183cf2689 266 //pc.printf("After removing spaces, messageOutText is '%s'\r\n", messageOutText);
kendunlop 1:19d183cf2689 267 string startofstring = messageOutText.substr(0,20); //Take the first 20 characters of the newly-formed string to get a spaceless CAN message.
kendunlop 1:19d183cf2689 268 //pc.printf("String to parse is '%s'.\r\n", startofstring);
kendunlop 8:6f096b45ca15 269 expectationresolved = 0;
kendunlop 8:6f096b45ca15 270 if (expectationwasfulfilled == 0)
kendunlop 8:6f096b45ca15 271 {
kendunlop 8:6f096b45ca15 272 expectationwasnotfulfilled();
kendunlop 8:6f096b45ca15 273 }
kendunlop 8:6f096b45ca15 274 if (expectationwasfulfilled == -1)
kendunlop 8:6f096b45ca15 275 {
kendunlop 8:6f096b45ca15 276 expectationwasfulfilled = 0;
kendunlop 8:6f096b45ca15 277 }
kendunlop 8:6f096b45ca15 278 expectationwasfulfilled = 0;
kendunlop 1:19d183cf2689 279 partincrement = 0;
kendunlop 1:19d183cf2689 280 part = startofstring.substr(0,3);
kendunlop 1:19d183cf2689 281 dealwithpart();
kendunlop 1:19d183cf2689 282 part = startofstring.substr(3,1);
kendunlop 1:19d183cf2689 283 dealwithpart();
kendunlop 1:19d183cf2689 284 part = startofstring.substr(4,2);
kendunlop 1:19d183cf2689 285 dealwithpart();
kendunlop 1:19d183cf2689 286 part = startofstring.substr(6,2);
kendunlop 1:19d183cf2689 287 dealwithpart();
kendunlop 1:19d183cf2689 288 part = startofstring.substr(8,2);
kendunlop 1:19d183cf2689 289 dealwithpart();
kendunlop 1:19d183cf2689 290 part = startofstring.substr(10,2);
kendunlop 1:19d183cf2689 291 dealwithpart();
kendunlop 1:19d183cf2689 292 part = startofstring.substr(12,2);
kendunlop 1:19d183cf2689 293 dealwithpart();
kendunlop 1:19d183cf2689 294 part = startofstring.substr(14,2);
kendunlop 1:19d183cf2689 295 dealwithpart();
kendunlop 1:19d183cf2689 296 part = startofstring.substr(16,2);
kendunlop 1:19d183cf2689 297 dealwithpart();
kendunlop 1:19d183cf2689 298 part = startofstring.substr(18,2);
kendunlop 1:19d183cf2689 299 dealwithpart();
kendunlop 1:19d183cf2689 300 }
kendunlop 1:19d183cf2689 301
kendunlop 1:19d183cf2689 302 void defineCANmessage(void)
kendunlop 1:19d183cf2689 303 {
kendunlop 1:19d183cf2689 304 //pc.printf("Defining CAN message 1.\r\n");
kendunlop 1:19d183cf2689 305 //messageOut1.format = CANStandard;
kendunlop 1:19d183cf2689 306 //messageOut1.id = 0x301;
kendunlop 1:19d183cf2689 307 //messageOut1.len = 8;
kendunlop 1:19d183cf2689 308 //messageOut1.data[0] = 0x06;
kendunlop 1:19d183cf2689 309 //messageOut1.data[1] = 0x3f;
kendunlop 1:19d183cf2689 310 //messageOut1.data[2] = 0xb2;
kendunlop 1:19d183cf2689 311 //messageOut1.data[3] = 0x29;
kendunlop 1:19d183cf2689 312 //messageOut1.data[4] = 0x19;
kendunlop 1:19d183cf2689 313 //messageOut1.data[5] = 0x97;
kendunlop 1:19d183cf2689 314 //messageOut1.data[6] = 0x67;
kendunlop 1:19d183cf2689 315 //messageOut1.data[7] = 0x37;
kendunlop 1:19d183cf2689 316 }
kendunlop 1:19d183cf2689 317
kendunlop 3:79133dcea836 318 void printMessageOut (void)
kendunlop 3:79133dcea836 319 {
kendunlop 1:19d183cf2689 320 //This function will print out whatever the CAN bus is sending out. Can't be used constantly as it sends '000 8 00 00 00 00 00 00 00 00' all the time.
kendunlop 1:19d183cf2689 321 pc.printf("Message OUT: %03X %01X %02X %02X %02X %02X %02X %02X %02X %02X\r\n",messageOut1.id,messageOut1.len,messageOut1.data[0],messageOut1.data[1],messageOut1.data[2],messageOut1.data[3],messageOut1.data[4],messageOut1.data[5],messageOut1.data[6],messageOut1.data[7]);
kendunlop 1:19d183cf2689 322 }
kendunlop 1:19d183cf2689 323
kendunlop 3:79133dcea836 324 void printMessageIn (void)
kendunlop 3:79133dcea836 325 {
kendunlop 8:6f096b45ca15 326 //This function will print out whatever the CAN bus is receiving.
kendunlop 8:6f096b45ca15 327 //addnewlineifneeded();
kendunlop 8:6f096b45ca15 328 //pc.printf("\033[0;36mMessage IN: %03X %01X %02X %02X %02X %02X %02X %02X %02X %02X\033[0m\r\n",messageIn.id,messageIn.len,messageIn.data[0],messageIn.data[1],messageIn.data[2],messageIn.data[3],messageIn.data[4],messageIn.data[5],messageIn.data[6],messageIn.data[7]);
kendunlop 1:19d183cf2689 329 }
kendunlop 0:7a500ebaa7a6 330
kendunlop 0:7a500ebaa7a6 331 //The 'main' function will run as soon as the program starts.
kendunlop 0:7a500ebaa7a6 332 int main()
kendunlop 1:19d183cf2689 333 {
kendunlop 1:19d183cf2689 334 pc.baud(115200); // serial port at 115200
kendunlop 1:19d183cf2689 335 CanBus.frequency(500 * 1000); // CAN bus at 500k
kendunlop 3:79133dcea836 336 CanBus2.frequency(500 * 1000); // CAN bus at 500k
kendunlop 1:19d183cf2689 337 CanBus.reset(); // clear any bus errors
kendunlop 3:79133dcea836 338 CanBus2.reset(); // clear any bus errors
kendunlop 1:19d183cf2689 339 //NOTE: Print messages must be below this line to work.
kendunlop 8:6f096b45ca15 340 pc.printf("\033[0m------------------------------------------\r\n");
kendunlop 1:19d183cf2689 341 pc.printf("Welcome to Ken CAN test.\r\n");
kendunlop 1:19d183cf2689 342 pc.printf("Setting CAN bus to 500k.\r\n");
kendunlop 6:2882710e4f1e 343 pc.printf("Setting serial port to baud rate 115200.\r\n");
kendunlop 8:6f096b45ca15 344 pc.printf("Using pins 9 and 10 for CANBus 1 (out) and 30 and 29 for CANBus 2 (in).\r\n");
kendunlop 1:19d183cf2689 345 pc.printf("Version %d.%d\r\n",kMajorVersion,kMinorVersion);
kendunlop 1:19d183cf2689 346 pc.printf("Build date %s %s\r\n",__DATE__,__TIME__);
kendunlop 1:19d183cf2689 347 pc.printf("------------------------------------------\r\n");
kendunlop 1:19d183cf2689 348 char c;
kendunlop 1:19d183cf2689 349
kendunlop 7:a9150dc1e481 350 //Define the list of expected hex values for use later
kendunlop 7:a9150dc1e481 351 //expectedlist[0] = 5;
kendunlop 7:a9150dc1e481 352 //for (int item : expectedlist)
kendunlop 7:a9150dc1e481 353 // pc.printf("%d\r\n", item);
kendunlop 7:a9150dc1e481 354 //int integer = expectedlist[1];
kendunlop 7:a9150dc1e481 355
kendunlop 1:19d183cf2689 356 //Check for button presses
kendunlop 1:19d183cf2689 357 while (1)
kendunlop 0:7a500ebaa7a6 358 {
kendunlop 8:6f096b45ca15 359 if (expectationtimer < 100 and expectationresolved == 0)
kendunlop 8:6f096b45ca15 360 {
kendunlop 8:6f096b45ca15 361 if (expectationresolved == 0)
kendunlop 8:6f096b45ca15 362 {
kendunlop 8:6f096b45ca15 363 //pc.printf("It's real!\r\n");
kendunlop 8:6f096b45ca15 364 if (expectationtimer == 0)
kendunlop 8:6f096b45ca15 365 {
kendunlop 8:6f096b45ca15 366 //pc.printf("Expectation timer is %d\r\n", expectationtimer);
kendunlop 8:6f096b45ca15 367 }
kendunlop 8:6f096b45ca15 368 expectationtimer++;
kendunlop 8:6f096b45ca15 369 //pc.printf("Expectation timer is now %d!\r\n", expectationtimer);
kendunlop 8:6f096b45ca15 370 if (expectationtimer >= 5)
kendunlop 8:6f096b45ca15 371 {
kendunlop 8:6f096b45ca15 372 if (shouldntcomein == 0)
kendunlop 8:6f096b45ca15 373 {
kendunlop 8:6f096b45ca15 374 expectationwasfulfilled = 0;
kendunlop 8:6f096b45ca15 375 failsthistest++;
kendunlop 8:6f096b45ca15 376 noreplyfailsthistest++;
kendunlop 8:6f096b45ca15 377 if (aspamisgoing == 0)
kendunlop 8:6f096b45ca15 378 {
kendunlop 8:6f096b45ca15 379 pc.printf("\r\n");
kendunlop 8:6f096b45ca15 380 }
kendunlop 8:6f096b45ca15 381 pc.printf("\033[38;5;220mNo reply message detected from ID %d. Ensure that CAN is connected to port 2. 'No reply' fails this test are now %d.\033[0m\r", expected1, noreplyfailsthistest);
kendunlop 8:6f096b45ca15 382 }
kendunlop 8:6f096b45ca15 383 if (shouldntcomein == 1)
kendunlop 8:6f096b45ca15 384 {
kendunlop 8:6f096b45ca15 385 expectationwasfulfilled = 1;
kendunlop 8:6f096b45ca15 386 //pc.printf("\rIt didn't come in! Good.\r\n", shouldntcomein);
kendunlop 8:6f096b45ca15 387 }
kendunlop 8:6f096b45ca15 388 expectationresolved = 1;
kendunlop 8:6f096b45ca15 389 //pc.printf("Expectation timer reached %d!\r\n", expectationtimer);
kendunlop 8:6f096b45ca15 390 expectationtimer = 0;
kendunlop 8:6f096b45ca15 391 if (idspamon == 1)
kendunlop 8:6f096b45ca15 392 {
kendunlop 8:6f096b45ca15 393 idscheckedcount++;
kendunlop 8:6f096b45ca15 394 }
kendunlop 8:6f096b45ca15 395 }
kendunlop 8:6f096b45ca15 396 wait(0.01);
kendunlop 8:6f096b45ca15 397 }
kendunlop 8:6f096b45ca15 398 }
kendunlop 1:19d183cf2689 399 if (pc.readable())
kendunlop 1:19d183cf2689 400 {
kendunlop 1:19d183cf2689 401 c = pc.getc();
kendunlop 3:79133dcea836 402 if (c != NULL)
kendunlop 1:19d183cf2689 403 {
kendunlop 3:79133dcea836 404 //When the a key is pressed, define a CAN message and send it.
kendunlop 1:19d183cf2689 405 //pc.printf("A key was pressed! (%c)\r\n", c);
kendunlop 1:19d183cf2689 406 messageOutText = "";
kendunlop 8:6f096b45ca15 407 buttonPressMessageOutText = "";
kendunlop 1:19d183cf2689 408 if (c == '1')
kendunlop 7:a9150dc1e481 409 {
kendunlop 8:6f096b45ca15 410 buttonPressMessageOutText = "301 8 01 01 01 01 01 01 01 01";
kendunlop 7:a9150dc1e481 411 }
kendunlop 1:19d183cf2689 412 if (c == '2')
kendunlop 7:a9150dc1e481 413 {
kendunlop 8:6f096b45ca15 414 buttonPressMessageOutText = "302 8 02 02 02 02 02 02 02 02";
kendunlop 7:a9150dc1e481 415 }
kendunlop 1:19d183cf2689 416 if (c == '3')
kendunlop 7:a9150dc1e481 417 {
kendunlop 8:6f096b45ca15 418 buttonPressMessageOutText = "303 8 00 00 00 00 00 00 00 33";
kendunlop 7:a9150dc1e481 419 }
kendunlop 1:19d183cf2689 420 if (c == '4')
kendunlop 7:a9150dc1e481 421 {
kendunlop 8:6f096b45ca15 422 buttonPressMessageOutText = "304 8 04 04 04 04 04 04 04 04";
kendunlop 7:a9150dc1e481 423 }
kendunlop 1:19d183cf2689 424 if (c == '5')
kendunlop 7:a9150dc1e481 425 {
kendunlop 8:6f096b45ca15 426 buttonPressMessageOutText = "305 8 05 05 05 05 05 05 05 05";
kendunlop 7:a9150dc1e481 427 }
kendunlop 1:19d183cf2689 428 if (c == '6')
kendunlop 7:a9150dc1e481 429 {
kendunlop 8:6f096b45ca15 430 buttonPressMessageOutText = "306 8 06 06 06 06 06 06 06 06";
kendunlop 7:a9150dc1e481 431 }
kendunlop 1:19d183cf2689 432 if (c == '7')
kendunlop 7:a9150dc1e481 433 {
kendunlop 8:6f096b45ca15 434 buttonPressMessageOutText = "307 8 07 07 07 07 07 07 07 07";
kendunlop 7:a9150dc1e481 435 }
kendunlop 1:19d183cf2689 436 if (c == '8')
kendunlop 7:a9150dc1e481 437 {
kendunlop 8:6f096b45ca15 438 buttonPressMessageOutText = "308 8 08 08 08 08 08 08 08 08";
kendunlop 7:a9150dc1e481 439 }
kendunlop 1:19d183cf2689 440 if (c == '9')
kendunlop 7:a9150dc1e481 441 {
kendunlop 8:6f096b45ca15 442 buttonPressMessageOutText = "309 8 09 09 09 09 09 09 09 09";
kendunlop 7:a9150dc1e481 443 }
kendunlop 1:19d183cf2689 444 if (c == 'f')
kendunlop 7:a9150dc1e481 445 {
kendunlop 8:6f096b45ca15 446 buttonPressMessageOutText = "7FF 8 FF FF FF FF FF FF FF FF";//NOTE: CAN IDs only go p to 7FF, not FFF.
kendunlop 8:6f096b45ca15 447 }
kendunlop 8:6f096b45ca15 448 if (buttonPressMessageOutText != "")//Centralized 'button press MessageOut' routine that applies to all button presses.
kendunlop 8:6f096b45ca15 449 {
kendunlop 8:6f096b45ca15 450 if (aspamisgoing == 0)
kendunlop 8:6f096b45ca15 451 {
kendunlop 8:6f096b45ca15 452 addnewlineifneeded();
kendunlop 8:6f096b45ca15 453 messageOutText = buttonPressMessageOutText;
kendunlop 8:6f096b45ca15 454 addnewlinetonextmessage = 1;
kendunlop 8:6f096b45ca15 455 checklaterbytes = 1;//Check all bytes for button-press messages.
kendunlop 8:6f096b45ca15 456 }
kendunlop 8:6f096b45ca15 457 buttonPressMessageOutText = "";
kendunlop 7:a9150dc1e481 458 }
kendunlop 1:19d183cf2689 459 if (c == 'q')
kendunlop 3:79133dcea836 460 {
kendunlop 3:79133dcea836 461 pc.printf("Changing CAN bus speed to 125.\r\n");
kendunlop 3:79133dcea836 462 CanBus.frequency(125 * 1000); // CAN bus at 125k
kendunlop 5:bf4c6278ca8b 463 CanBus2.frequency(125 * 1000); // CAN bus at 125k
kendunlop 3:79133dcea836 464 }
kendunlop 1:19d183cf2689 465 if (c == 'w')
kendunlop 3:79133dcea836 466 {
kendunlop 3:79133dcea836 467 pc.printf("Changing CAN bus speed to 250.\r\n");
kendunlop 3:79133dcea836 468 CanBus.frequency(250 * 1000); // CAN bus at 250k
kendunlop 5:bf4c6278ca8b 469 CanBus2.frequency(250 * 1000); // CAN bus at 250k
kendunlop 3:79133dcea836 470 }
kendunlop 1:19d183cf2689 471 if (c == 'e')
kendunlop 3:79133dcea836 472 {
kendunlop 3:79133dcea836 473 pc.printf("Changing CAN bus speed to 500.\r\n");
kendunlop 3:79133dcea836 474 CanBus.frequency(500 * 1000); // CAN bus at 500k
kendunlop 3:79133dcea836 475 CanBus2.frequency(500 * 1000);// CAN bus at 500k
kendunlop 3:79133dcea836 476 }
kendunlop 1:19d183cf2689 477 if (c == 'r')
kendunlop 3:79133dcea836 478 {
kendunlop 3:79133dcea836 479 pc.printf("Changing CAN bus speed to 1000.\r\n");
kendunlop 3:79133dcea836 480 CanBus.frequency(1000 * 1000); // CAN bus at 1000k
kendunlop 3:79133dcea836 481 CanBus2.frequency(1000 * 1000); // CAN bus at 1000k
kendunlop 3:79133dcea836 482 }
kendunlop 8:6f096b45ca15 483 if (c == 'm')
kendunlop 8:6f096b45ca15 484 {
kendunlop 8:6f096b45ca15 485 pc.printf("Will try to make an array.\r\n");
kendunlop 8:6f096b45ca15 486 //Making a two-dimensional array with 301-309 in the first column, then 401-409 in the second column.
kendunlop 8:6f096b45ca15 487 int copyarray[2][8] = {{769, 770, 771, 772, 773, 774, 776, 777}, {1025, 1026, 1027, 1028, 1029, 1030, 1032, 1033}};
kendunlop 8:6f096b45ca15 488 //int copyarray[2][1] = {{2047}, {1025}};
kendunlop 8:6f096b45ca15 489 memcpy(idsarray, copyarray, sizeof(copyarray)); //Copy the new array over the old 'idsarray'.
kendunlop 8:6f096b45ca15 490 arrayx = 2;
kendunlop 8:6f096b45ca15 491 arrayy = 8;
kendunlop 8:6f096b45ca15 492 pc.printf("Array is %dx%d.\r\n", arrayx, arrayy);
kendunlop 8:6f096b45ca15 493 pc.printf("1st column of IDs array says %d, %d, %d, %d, %d, %d, %d, %d\r\n", idsarray[0][0], idsarray[0][1], idsarray[0][2], idsarray[0][3], idsarray[0][4], idsarray[0][5], idsarray[0][6], idsarray[0][7]);
kendunlop 8:6f096b45ca15 494 pc.printf("2nd column of IDs array says %d, %d, %d, %d, %d, %d, %d, %d\r\n", idsarray[1][0], idsarray[1][1], idsarray[1][2], idsarray[1][3], idsarray[1][4], idsarray[1][5], idsarray[1][6], idsarray[1][7]);
kendunlop 8:6f096b45ca15 495 //pc.printf("1st column of IDs array says %d.\r\n", idsarray[0][0]);
kendunlop 8:6f096b45ca15 496 //pc.printf("2nd column of IDs array says %d.\r\n", idsarray[1][0]);
kendunlop 8:6f096b45ca15 497 CANpassthrough = 0;
kendunlop 8:6f096b45ca15 498 pc.printf("CANpassthrough is now set to %d.\r\n", CANpassthrough);
kendunlop 8:6f096b45ca15 499 /*
kendunlop 8:6f096b45ca15 500 pc.printf("Will search for '305' in row 1.\r\n");
kendunlop 8:6f096b45ca15 501 //Search for '305' in the array.
kendunlop 8:6f096b45ca15 502 for(int i = 0; i <=arrayy; i++)
kendunlop 8:6f096b45ca15 503 {
kendunlop 8:6f096b45ca15 504 if(idsarray[0][i] == 305)
kendunlop 8:6f096b45ca15 505 {
kendunlop 8:6f096b45ca15 506 pc.printf("Element is found at 0,%d.\r\n", i);
kendunlop 8:6f096b45ca15 507 }
kendunlop 8:6f096b45ca15 508 }
kendunlop 8:6f096b45ca15 509 */
kendunlop 8:6f096b45ca15 510 //pc.printf("It got here.\r\n"); //To check if it got stuck at this point.
kendunlop 8:6f096b45ca15 511 }
kendunlop 7:a9150dc1e481 512 //if (c == 's')
kendunlop 7:a9150dc1e481 513 // {
kendunlop 7:a9150dc1e481 514 // pc.printf("Sending authentic sats message.\r\n");
kendunlop 7:a9150dc1e481 515 // messageOutText = "301 8 06 3F B2 29 12 97 67 37";
kendunlop 7:a9150dc1e481 516 // }
kendunlop 8:6f096b45ca15 517 if (c == 'o' and idspamon == 0 and spamon == 1)
kendunlop 3:79133dcea836 518 {
kendunlop 3:79133dcea836 519 incrementer = (incrementer * 16);
kendunlop 3:79133dcea836 520 pc.printf("Multiplied Incrementer by 16. It's now now %d.\r\n", incrementer);
kendunlop 3:79133dcea836 521 }
kendunlop 8:6f096b45ca15 522 if (c == 'i' and idspamon == 1)
kendunlop 1:19d183cf2689 523 {
kendunlop 1:19d183cf2689 524 incrementer = incrementer + 16;
kendunlop 1:19d183cf2689 525 std::stringstream sstream;
kendunlop 1:19d183cf2689 526 sstream << std::hex << incrementer;
kendunlop 1:19d183cf2689 527 string stringsofar = sstream.str();
kendunlop 1:19d183cf2689 528 //pc.printf("Incrementer is now %d.\r\n", incrementer);
kendunlop 1:19d183cf2689 529 //pc.printf("StringStream says '%s'.\r\n", stringsofar);
kendunlop 1:19d183cf2689 530 int length = stringsofar.length();
kendunlop 1:19d183cf2689 531 //pc.printf("Length is %d/16.\r\n", length);
kendunlop 1:19d183cf2689 532 for (int i = 0; i < (16-length); i++)
kendunlop 1:19d183cf2689 533 stringsofar = "0" + stringsofar;
kendunlop 1:19d183cf2689 534 //pc.printf("stringsofar says '%s'.\r\n", stringsofar);
kendunlop 1:19d183cf2689 535 messageOutText = "305 8 " + stringsofar;
kendunlop 1:19d183cf2689 536 //pc.printf("Will try to send '%s'.\r\n", messageOutText);
kendunlop 1:19d183cf2689 537 }
kendunlop 1:19d183cf2689 538 messageOut1.format = CANStandard;
kendunlop 8:6f096b45ca15 539 if (c == 'z' and aspamisgoing == 0)
kendunlop 1:19d183cf2689 540 {
kendunlop 7:a9150dc1e481 541 resettest();
kendunlop 8:6f096b45ca15 542 pc.printf("Starting spam sequence for ID %s. Press 'x' to end. Press 'o' to increase incrementer.\r\n", spampreamble);
kendunlop 1:19d183cf2689 543 spamon = 1;
kendunlop 8:6f096b45ca15 544 aspamisgoing = 1;
kendunlop 8:6f096b45ca15 545 checklaterbytes = 1;
kendunlop 1:19d183cf2689 546 incrementer = 0;
kendunlop 2:11339018dda6 547 spamcount = 0;
kendunlop 3:79133dcea836 548 spamstarttime = clock();
kendunlop 1:19d183cf2689 549 }
kendunlop 1:19d183cf2689 550 //Pressing 'x' switches off spam mode
kendunlop 1:19d183cf2689 551 if (c == 'x' and spamon == 1)
kendunlop 1:19d183cf2689 552 {
kendunlop 8:6f096b45ca15 553 spamon = 2;
kendunlop 8:6f096b45ca15 554 aspamisgoing = 0;
kendunlop 3:79133dcea836 555 int spamendtime = 0;
kendunlop 3:79133dcea836 556 spamendtime = clock();
kendunlop 2:11339018dda6 557 int spamtime = 0;
kendunlop 2:11339018dda6 558 spamtime = spamendtime - spamstarttime;
kendunlop 3:79133dcea836 559 int spamtimeseconds = 0;
kendunlop 3:79133dcea836 560 spamtimeseconds = (spamtime / CLOCKS_PER_SEC);
kendunlop 8:6f096b45ca15 561 addnewlinetonextmessage = 1;
kendunlop 7:a9150dc1e481 562 addnewlineifneeded();
kendunlop 3:79133dcea836 563 pc.printf("Ending spam mode. Spammed %d times for %d ticks (%d seconds) at %d ticks per second.\r\n", spamcount, spamtime, spamtimeseconds, CLOCKS_PER_SEC);
kendunlop 3:79133dcea836 564 //pc.printf("Clock right now is %d.\r\n", (spamtime));
kendunlop 3:79133dcea836 565 //pc.printf("Ticks per second are %d.\r\n", CLOCKS_PER_SEC);
kendunlop 3:79133dcea836 566 //pc.printf("Spam start/end is %d/%d.\r\n", spamstarttime, spamendtime);
kendunlop 3:79133dcea836 567 //pc.printf("Spamtime was %d ticks.\r\n", spamtime);
kendunlop 2:11339018dda6 568 int spamspersecond = 0;
kendunlop 3:79133dcea836 569 spamspersecond = (10000 * spamcount);
kendunlop 3:79133dcea836 570 //pc.printf("spamcount times 10,000 is %d.\r\n", spamspersecond);
kendunlop 3:79133dcea836 571 spamspersecond = spamspersecond / spamtime;
kendunlop 3:79133dcea836 572 //pc.printf("10,000 times spamcount divided by spamtime is %d spams per tick.\r\n", spamspersecond);
kendunlop 3:79133dcea836 573 spamspersecond = ((spamspersecond * CLOCKS_PER_SEC))/10000;
kendunlop 3:79133dcea836 574 pc.printf("Spams per second are %d.\r\n", spamspersecond);
kendunlop 3:79133dcea836 575 pc.printf("Total spams ever are %d.\r\n", totalspamsever);
kendunlop 7:a9150dc1e481 576 endtest();
kendunlop 7:a9150dc1e481 577 //pc.printf("-------------------------\r\n");
kendunlop 3:79133dcea836 578 }
kendunlop 8:6f096b45ca15 579 if (c == 'v' and aspamisgoing == 0)
kendunlop 3:79133dcea836 580 {
kendunlop 7:a9150dc1e481 581 resettest();
kendunlop 6:2882710e4f1e 582 idspamon = 1; //Set the 'idspamon' integer to 1 so ID spam happens.
kendunlop 8:6f096b45ca15 583 aspamisgoing = 1;
kendunlop 8:6f096b45ca15 584 checklaterbytes = 0;
kendunlop 6:2882710e4f1e 585 idlistincrementer = 0;
kendunlop 7:a9150dc1e481 586 pc.printf("Beginning spam of all possible CAN IDs (000 - 7FF).\r\n");
kendunlop 8:6f096b45ca15 587 if (checklaterbytes == 0)
kendunlop 8:6f096b45ca15 588 {
kendunlop 8:6f096b45ca15 589 pc.printf("Will only check CAN IDs, not later bytes.\r\n");
kendunlop 8:6f096b45ca15 590 }
kendunlop 1:19d183cf2689 591 }
kendunlop 1:19d183cf2689 592 if (messageOutText != "")
kendunlop 1:19d183cf2689 593 {
kendunlop 6:2882710e4f1e 594 getCANfrommessageOutText();
kendunlop 7:a9150dc1e481 595 sendCAN();
kendunlop 7:a9150dc1e481 596 //CanBus.write(messageOut1);
kendunlop 7:a9150dc1e481 597 //CanBus2.write(messageOut1);
kendunlop 6:2882710e4f1e 598 if (spamon == 0 and idspamon == 0)
kendunlop 3:79133dcea836 599 {
kendunlop 7:a9150dc1e481 600 //printMessageOut();
kendunlop 3:79133dcea836 601 }
kendunlop 3:79133dcea836 602 messageOutText = "";
kendunlop 1:19d183cf2689 603 }
kendunlop 5:bf4c6278ca8b 604 }
kendunlop 4:e8e9bc25b1ca 605 }
kendunlop 5:bf4c6278ca8b 606
kendunlop 4:e8e9bc25b1ca 607
kendunlop 7:a9150dc1e481 608 //If spam mode is on, spam an incrementing CAN message
kendunlop 8:6f096b45ca15 609 if (spamon == 1 and expectationresolved == 1)
kendunlop 5:bf4c6278ca8b 610 {
kendunlop 5:bf4c6278ca8b 611 spamcount ++;
kendunlop 5:bf4c6278ca8b 612 totalspamsever ++;
kendunlop 5:bf4c6278ca8b 613 incrementer ++;
kendunlop 5:bf4c6278ca8b 614 std::stringstream sstream;
kendunlop 5:bf4c6278ca8b 615 sstream << std::hex << incrementer;
kendunlop 5:bf4c6278ca8b 616 string stringsofar = sstream.str();
kendunlop 5:bf4c6278ca8b 617 int length = stringsofar.length();
kendunlop 5:bf4c6278ca8b 618 for (int i = 0; i < (16-length); i++)
kendunlop 5:bf4c6278ca8b 619 stringsofar = "0" + stringsofar;
kendunlop 6:2882710e4f1e 620 messageOutText = spampreamble + " 8 " + stringsofar;
kendunlop 6:2882710e4f1e 621 getCANfrommessageOutText();
kendunlop 7:a9150dc1e481 622 sendCAN();
kendunlop 7:a9150dc1e481 623 //CanBus.write(messageOut1);
kendunlop 7:a9150dc1e481 624 //CanBus2.write(messageOut1);
kendunlop 5:bf4c6278ca8b 625 wait(0.01);
kendunlop 5:bf4c6278ca8b 626 }
kendunlop 8:6f096b45ca15 627 if (idspamon == 1 and idlistincrementer >= 2048 and expectationresolved == 1) //If the spam value gets to 2048 (7FF), end the spam sequence. RL only go up to 7FF.
kendunlop 8:6f096b45ca15 628 {
kendunlop 8:6f096b45ca15 629 idspamon = 0;
kendunlop 8:6f096b45ca15 630 aspamisgoing = 0;
kendunlop 8:6f096b45ca15 631 addnewlinetonextmessage = 1;
kendunlop 8:6f096b45ca15 632 addnewlineifneeded();
kendunlop 8:6f096b45ca15 633 pc.printf("Ending ID spam sequence. %d IDs checked total.\r\n", idscheckedcount);
kendunlop 8:6f096b45ca15 634 idlistincrementer = 0;
kendunlop 8:6f096b45ca15 635 endtest();
kendunlop 8:6f096b45ca15 636 }
kendunlop 8:6f096b45ca15 637 if (idspamon == 1 and expectationresolved == 1)
kendunlop 6:2882710e4f1e 638 {
kendunlop 6:2882710e4f1e 639 std::stringstream sstream;
kendunlop 6:2882710e4f1e 640 sstream << std::hex << idlistincrementer;
kendunlop 6:2882710e4f1e 641 string idstring = "";
kendunlop 6:2882710e4f1e 642 idstring = sstream.str();
kendunlop 6:2882710e4f1e 643 //pc.printf("idstring says '%s'\r\n", idstring);
kendunlop 6:2882710e4f1e 644 if (idstring.length() < 3)
kendunlop 6:2882710e4f1e 645 {
kendunlop 6:2882710e4f1e 646 for (int i = 0; i < (4 - idstring.length()); i++)
kendunlop 6:2882710e4f1e 647 {
kendunlop 6:2882710e4f1e 648 idstring = "0" + idstring;
kendunlop 6:2882710e4f1e 649 }
kendunlop 6:2882710e4f1e 650 }
kendunlop 6:2882710e4f1e 651 //pc.printf("idstring now says '%s'\r\n", idstring);
kendunlop 6:2882710e4f1e 652 //pc.printf("%s\r\n", idstring);
kendunlop 7:a9150dc1e481 653 idstring = idstring + " 8 01 02 03 04 05 06 07 08";
kendunlop 6:2882710e4f1e 654 //pc.printf("...and now idstring now says '%s'\r\n", idstring);
kendunlop 6:2882710e4f1e 655 messageOutText = idstring;
kendunlop 6:2882710e4f1e 656 getCANfrommessageOutText();
kendunlop 7:a9150dc1e481 657 sendCAN();
kendunlop 7:a9150dc1e481 658 //CanBus.write(messageOut1);
kendunlop 7:a9150dc1e481 659 //CanBus2.write(messageOut1);
kendunlop 6:2882710e4f1e 660 idlistincrementer++; //Only add 1 to the idlist incrementer at the end so the first ID it sends is '000'.
kendunlop 6:2882710e4f1e 661 //pc.printf("ID list incrementer is now %d\r\n", idlistincrementer);
kendunlop 8:6f096b45ca15 662
kendunlop 6:2882710e4f1e 663 wait(0.01);//ID spam at 100Hz
kendunlop 6:2882710e4f1e 664 }
kendunlop 1:19d183cf2689 665 //Check for CAN messages coming in
kendunlop 7:a9150dc1e481 666 if (CanBus2.read(messageIn))
kendunlop 7:a9150dc1e481 667 {
kendunlop 8:6f096b45ca15 668 if (idspamon == 1)
kendunlop 8:6f096b45ca15 669 {
kendunlop 8:6f096b45ca15 670 idscheckedcount++;
kendunlop 8:6f096b45ca15 671 }
kendunlop 1:19d183cf2689 672 CANCount ++;
kendunlop 1:19d183cf2689 673 printMessageIn();
kendunlop 7:a9150dc1e481 674 if (1 == 1)
kendunlop 7:a9150dc1e481 675 {
kendunlop 7:a9150dc1e481 676 int itsamatch = 1;
kendunlop 8:6f096b45ca15 677 //addnewlineifneeded();
kendunlop 8:6f096b45ca15 678 pc.printf("Message received was: ");
kendunlop 7:a9150dc1e481 679 if (expected1 != messageIn.id)
kendunlop 7:a9150dc1e481 680 {
kendunlop 8:6f096b45ca15 681 if (CANpassthrough == 1)
kendunlop 8:6f096b45ca15 682 {
kendunlop 8:6f096b45ca15 683 //pc.printf("\r\nID of %d does NOT match %d.\r\n", messageIn.id, expected1);
kendunlop 8:6f096b45ca15 684 pc.printf("\033[1;31m%03X\033[0m ", messageIn.id);
kendunlop 8:6f096b45ca15 685 }
kendunlop 8:6f096b45ca15 686 itsamatch = 0;
kendunlop 8:6f096b45ca15 687 }
kendunlop 8:6f096b45ca15 688 else
kendunlop 8:6f096b45ca15 689 {
kendunlop 8:6f096b45ca15 690 pc.printf("%03X ", messageIn.id);
kendunlop 8:6f096b45ca15 691 }
kendunlop 8:6f096b45ca15 692 if (expected2 != messageIn.len and checklaterbytes >= 1)
kendunlop 8:6f096b45ca15 693 {
kendunlop 8:6f096b45ca15 694 //pc.printf("Length of %d does NOT match %d.\r\n", messageIn.len, expected2);
kendunlop 8:6f096b45ca15 695 pc.printf("\033[1;31m%01X\033[0m ", messageIn.len);
kendunlop 7:a9150dc1e481 696 itsamatch = 0;
kendunlop 7:a9150dc1e481 697 }
kendunlop 8:6f096b45ca15 698 else
kendunlop 7:a9150dc1e481 699 {
kendunlop 8:6f096b45ca15 700 pc.printf("%01X ", messageIn.len);
kendunlop 8:6f096b45ca15 701 }
kendunlop 8:6f096b45ca15 702 if (expected3 != messageIn.data[0] and checklaterbytes >= 1)
kendunlop 8:6f096b45ca15 703 {
kendunlop 8:6f096b45ca15 704 //pc.printf("Data 0 of %d does NOT match %d.\r\n", messageIn.data[0], expected3);
kendunlop 8:6f096b45ca15 705 pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[0]);
kendunlop 7:a9150dc1e481 706 itsamatch = 0;
kendunlop 7:a9150dc1e481 707 }
kendunlop 8:6f096b45ca15 708 else
kendunlop 7:a9150dc1e481 709 {
kendunlop 8:6f096b45ca15 710 pc.printf("%02X ", messageIn.data[0]);
kendunlop 8:6f096b45ca15 711 }
kendunlop 8:6f096b45ca15 712 if (expected4 != messageIn.data[1] and checklaterbytes >= 1)
kendunlop 8:6f096b45ca15 713 {
kendunlop 8:6f096b45ca15 714 //pc.printf("Data 1 of %d does NOT match %d.\r\n", messageIn.data[1], expected4);
kendunlop 8:6f096b45ca15 715 pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[1]);
kendunlop 7:a9150dc1e481 716 itsamatch = 0;
kendunlop 7:a9150dc1e481 717 }
kendunlop 8:6f096b45ca15 718 else
kendunlop 7:a9150dc1e481 719 {
kendunlop 8:6f096b45ca15 720 pc.printf("%02X ", messageIn.data[1]);
kendunlop 8:6f096b45ca15 721 }
kendunlop 8:6f096b45ca15 722 if (expected5 != messageIn.data[2] and checklaterbytes >= 1)
kendunlop 8:6f096b45ca15 723 {
kendunlop 8:6f096b45ca15 724 //pc.printf("Data 2 of %d does NOT match %d.\r\n", messageIn.data[2], expected5);
kendunlop 8:6f096b45ca15 725 pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[2]);
kendunlop 7:a9150dc1e481 726 itsamatch = 0;
kendunlop 7:a9150dc1e481 727 }
kendunlop 8:6f096b45ca15 728 else
kendunlop 7:a9150dc1e481 729 {
kendunlop 8:6f096b45ca15 730 pc.printf("%02X ", messageIn.data[2]);
kendunlop 8:6f096b45ca15 731 }
kendunlop 8:6f096b45ca15 732 if (expected6 != messageIn.data[3] and checklaterbytes >= 1)
kendunlop 7:a9150dc1e481 733 {
kendunlop 8:6f096b45ca15 734 //pc.printf("Data 3 of %d does NOT match %d.\r\n", messageIn.data[3], expected6);
kendunlop 8:6f096b45ca15 735 pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[3]);
kendunlop 7:a9150dc1e481 736 itsamatch = 0;
kendunlop 7:a9150dc1e481 737 }
kendunlop 8:6f096b45ca15 738
kendunlop 8:6f096b45ca15 739 else
kendunlop 8:6f096b45ca15 740 {
kendunlop 8:6f096b45ca15 741 pc.printf("%02X ", messageIn.data[3]);
kendunlop 8:6f096b45ca15 742 }
kendunlop 8:6f096b45ca15 743 if (expected7 != messageIn.data[4] and checklaterbytes >= 1)
kendunlop 7:a9150dc1e481 744 {
kendunlop 8:6f096b45ca15 745 //pc.printf("Data 4 of %d does NOT match %d.\r\n", messageIn.data[4], expected7);
kendunlop 8:6f096b45ca15 746 pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[4]);
kendunlop 8:6f096b45ca15 747 itsamatch = 0;
kendunlop 8:6f096b45ca15 748 }
kendunlop 8:6f096b45ca15 749 else
kendunlop 8:6f096b45ca15 750 {
kendunlop 8:6f096b45ca15 751 pc.printf("%02X ", messageIn.data[4]);
kendunlop 8:6f096b45ca15 752 }
kendunlop 8:6f096b45ca15 753 if (expected8 != messageIn.data[5] and checklaterbytes >= 1)
kendunlop 8:6f096b45ca15 754 {
kendunlop 8:6f096b45ca15 755 //pc.printf("Data 5 of %d does NOT match %d.\r\n", messageIn.data[5], expected8);
kendunlop 8:6f096b45ca15 756 pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[5]);
kendunlop 7:a9150dc1e481 757 itsamatch = 0;
kendunlop 7:a9150dc1e481 758 }
kendunlop 8:6f096b45ca15 759 else
kendunlop 7:a9150dc1e481 760 {
kendunlop 8:6f096b45ca15 761 pc.printf("%02X ", messageIn.data[5]);
kendunlop 7:a9150dc1e481 762 }
kendunlop 8:6f096b45ca15 763 if (expected9 != messageIn.data[6] and checklaterbytes >= 1)
kendunlop 7:a9150dc1e481 764 {
kendunlop 8:6f096b45ca15 765 //pc.printf("Data 6 of %d does NOT match %d.\r\n", messageIn.data[6], expected9);
kendunlop 8:6f096b45ca15 766 pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[6]);
kendunlop 7:a9150dc1e481 767 itsamatch = 0;
kendunlop 7:a9150dc1e481 768 }
kendunlop 8:6f096b45ca15 769 else
kendunlop 7:a9150dc1e481 770 {
kendunlop 8:6f096b45ca15 771 pc.printf("%02X ", messageIn.data[6]);
kendunlop 8:6f096b45ca15 772 }
kendunlop 8:6f096b45ca15 773 if (expected10 != messageIn.data[7] and checklaterbytes >= 1)
kendunlop 8:6f096b45ca15 774 {
kendunlop 8:6f096b45ca15 775 //pc.printf("Data 7 of %d does NOT match %d.\r\n", messageIn.data[7], expected10);
kendunlop 8:6f096b45ca15 776 pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[7]);
kendunlop 7:a9150dc1e481 777 itsamatch = 0;
kendunlop 7:a9150dc1e481 778 }
kendunlop 8:6f096b45ca15 779 else
kendunlop 8:6f096b45ca15 780 {
kendunlop 8:6f096b45ca15 781 pc.printf("%02X ", messageIn.data[7]);
kendunlop 8:6f096b45ca15 782 }
kendunlop 8:6f096b45ca15 783 //pc.printf("\r\n");
kendunlop 7:a9150dc1e481 784 if (itsamatch == 0)
kendunlop 7:a9150dc1e481 785 {
kendunlop 8:6f096b45ca15 786 mismatchfailsthistest++;
kendunlop 8:6f096b45ca15 787 pc.printf("\r\nDoes NOT match : %03X %01X %02X %02X %02X %02X %02X %02X %02X %02X\r\n", expected1, expected2, expected3, expected4, expected5, expected6, expected7, expected8, expected9, expected10);
kendunlop 7:a9150dc1e481 788 failsthistest++; //Add 1 to failsthistest to mark the fail
kendunlop 8:6f096b45ca15 789 expectationwasfulfilled = 0;
kendunlop 8:6f096b45ca15 790 expectationresolved = 1;
kendunlop 8:6f096b45ca15 791 }
kendunlop 8:6f096b45ca15 792 if (itsamatch == 1)
kendunlop 8:6f096b45ca15 793 {
kendunlop 8:6f096b45ca15 794 expectationwasfulfilled = 1;
kendunlop 8:6f096b45ca15 795 expectationresolved = 1;
kendunlop 8:6f096b45ca15 796 //pc.printf("It's a match. Expectation fulfilled.\r\n");
kendunlop 7:a9150dc1e481 797 }
kendunlop 7:a9150dc1e481 798 }
kendunlop 1:19d183cf2689 799 }
kendunlop 8:6f096b45ca15 800 //if (expected1 != -1) //If CANBus does not read a message in
kendunlop 8:6f096b45ca15 801 //{
kendunlop 8:6f096b45ca15 802 //
kendunlop 8:6f096b45ca15 803 //}
kendunlop 8:6f096b45ca15 804 }
kendunlop 5:bf4c6278ca8b 805 }