Kenneth Dunlop / Mbed 2 deprecated Ken_CAN_test

Dependencies:   mbed

Committer:
kendunlop
Date:
Mon Jun 27 13:14:20 2022 +0000
Revision:
9:7c27efe30a77
Parent:
8:6f096b45ca15
Child:
10:3e15baff40e7
Reports are a lot tidier now, spaces between lines works better.

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 9:7c27efe30a77 30
kendunlop 4:e8e9bc25b1ca 31 int spamon = 0;
kendunlop 6:2882710e4f1e 32 int idspamon = 0;
kendunlop 8:6f096b45ca15 33 int idscheckedcount = 0;
kendunlop 1:19d183cf2689 34 int spamcount = 0;
kendunlop 3:79133dcea836 35 int totalspamsever = 0;
kendunlop 2:11339018dda6 36 int spamstarttime = 0;
kendunlop 2:11339018dda6 37 int spamendtime = 0;
kendunlop 9:7c27efe30a77 38 int spamsecondstotal = 0;
kendunlop 9:7c27efe30a77 39 int spamspersecond = 0;
kendunlop 9:7c27efe30a77 40
kendunlop 3:79133dcea836 41 int vlistincrementer = 0;
kendunlop 6:2882710e4f1e 42 int idlistincrementer = 0;
kendunlop 6:2882710e4f1e 43 string spampreamble = "333";
kendunlop 7:a9150dc1e481 44 int expected1 = 0;
kendunlop 7:a9150dc1e481 45 int expected2 = 0;
kendunlop 7:a9150dc1e481 46 unsigned char expected3 = 0;
kendunlop 7:a9150dc1e481 47 unsigned char expected4 = 0;
kendunlop 7:a9150dc1e481 48 unsigned char expected5 = 0;
kendunlop 7:a9150dc1e481 49 unsigned char expected6 = 0;
kendunlop 7:a9150dc1e481 50 unsigned char expected7 = 0;
kendunlop 7:a9150dc1e481 51 unsigned char expected8 = 0;
kendunlop 7:a9150dc1e481 52 unsigned char expected9 = 0;
kendunlop 7:a9150dc1e481 53 unsigned char expected10 = 0;
kendunlop 8:6f096b45ca15 54 int expectationwasfulfilled = -1;
kendunlop 8:6f096b45ca15 55 int expectationresolved = 1;
kendunlop 8:6f096b45ca15 56 int expectationtimer = 0;
kendunlop 8:6f096b45ca15 57 int shouldntcomein = 0;
kendunlop 9:7c27efe30a77 58 int goodnoreplies = 0;
kendunlop 9:7c27efe30a77 59 int goodnoreplystreak = 0;
kendunlop 9:7c27efe30a77 60 int goodnoreplystreakstartid = 0;
kendunlop 9:7c27efe30a77 61 int CANmatchstreak = 0;
kendunlop 9:7c27efe30a77 62 int CANmatchstreakstartID = 0;
kendunlop 9:7c27efe30a77 63 int unexpectedCANfails = 0;
kendunlop 9:7c27efe30a77 64 int unexpectedCANstreak = 0;
kendunlop 9:7c27efe30a77 65 int unexpectedCANstreakstartid = 0;
kendunlop 7:a9150dc1e481 66 int failsthistest = 0;
kendunlop 8:6f096b45ca15 67 int noreplyfailsthistest = 0;
kendunlop 9:7c27efe30a77 68 int noreplystreak = 0;
kendunlop 9:7c27efe30a77 69 int noreplystreakstartid = 0;
kendunlop 9:7c27efe30a77 70
kendunlop 8:6f096b45ca15 71 int mismatchfailsthistest = 0;
kendunlop 9:7c27efe30a77 72 int matchesthistest = 0;
kendunlop 7:a9150dc1e481 73 int addnewlinetonextmessage = 0;
kendunlop 8:6f096b45ca15 74 int checklaterbytes = 0;
kendunlop 8:6f096b45ca15 75 int aspamisgoing = 0;
kendunlop 8:6f096b45ca15 76 int CANpassthrough = 1;
kendunlop 8:6f096b45ca15 77
kendunlop 9:7c27efe30a77 78 int mbedCANbusspeed = 500 * 1000;
kendunlop 9:7c27efe30a77 79 int CANGatewayCANbusspeed = 500 * 1000;
kendunlop 9:7c27efe30a77 80
kendunlop 8:6f096b45ca15 81 //Array of IDs to translate can be overwritten with the 'copyarray'.
kendunlop 8:6f096b45ca15 82 //It's arranged in columns of IDs that are translated to the ID in the next column.
kendunlop 8:6f096b45ca15 83 //NOTE: Array must be a large enough size to contain all ID translations.
kendunlop 8:6f096b45ca15 84 int idsarray[2][8] = {};
kendunlop 8:6f096b45ca15 85 int arrayx = 2;
kendunlop 8:6f096b45ca15 86 int arrayy = 8;
kendunlop 8:6f096b45ca15 87
kendunlop 9:7c27efe30a77 88 //A function to easily get the total of seconds a spam has taken
kendunlop 9:7c27efe30a77 89 int getspamsecondstotal(void)
kendunlop 9:7c27efe30a77 90 {
kendunlop 9:7c27efe30a77 91 //pc.printf("\r\nHere I'll get the spam seconds total!\r\n");
kendunlop 9:7c27efe30a77 92 //pc.printf("Spam start/end time was %d / %d.\r\n", spamstarttime, spamendtime);
kendunlop 9:7c27efe30a77 93 int spamtickstotal = (spamendtime - spamstarttime);
kendunlop 9:7c27efe30a77 94 //pc.printf("Spam ticks total is %d.\r\n", spamtickstotal);
kendunlop 9:7c27efe30a77 95 //pc.printf("CLOCKS_PER_SEC are %d.\r\n", CLOCKS_PER_SEC);
kendunlop 9:7c27efe30a77 96 //pc.printf("Spamcount this test is %d.\r\n", spamcount);
kendunlop 9:7c27efe30a77 97 spamsecondstotal = ((spamendtime - spamstarttime) / CLOCKS_PER_SEC);
kendunlop 9:7c27efe30a77 98 //pc.printf("Spam seconds total is %d.\r\n", spamsecondstotal);
kendunlop 9:7c27efe30a77 99 spamspersecond = (10000 * spamcount);
kendunlop 9:7c27efe30a77 100 //pc.printf("Spams biggened x 10,000 is %d.\r\n", spamspersecond);
kendunlop 9:7c27efe30a77 101 spamspersecond = (spamspersecond / spamtickstotal);
kendunlop 9:7c27efe30a77 102 //pc.printf("Spams per tick, still biggened, is %d.\r\n", spamspersecond);
kendunlop 9:7c27efe30a77 103 spamspersecond = (spamspersecond * CLOCKS_PER_SEC);
kendunlop 9:7c27efe30a77 104 //pc.printf("Spams per second, still biggened, given %d ticks per second, is %d.\r\n", CLOCKS_PER_SEC, spamspersecond);
kendunlop 9:7c27efe30a77 105 spamspersecond = spamspersecond / 10000;
kendunlop 9:7c27efe30a77 106 //pc.printf("Spams per second, un-biggened, is %d.\r\n", spamspersecond);
kendunlop 9:7c27efe30a77 107 pc.printf("\r\nSent %d messages for %d ticks (%d seconds) at %d ticks per second. Messages per second is %d.\r\n", spamcount, spamtickstotal, spamsecondstotal, CLOCKS_PER_SEC, spamspersecond);
kendunlop 9:7c27efe30a77 108 return(spamsecondstotal, spamspersecond);
kendunlop 9:7c27efe30a77 109 }
kendunlop 9:7c27efe30a77 110
kendunlop 8:6f096b45ca15 111 //A function to handle when CAN-expectations are not fulfilled
kendunlop 8:6f096b45ca15 112 void expectationwasnotfulfilled(void)
kendunlop 8:6f096b45ca15 113 {
kendunlop 8:6f096b45ca15 114 //pc.printf("Expectation was not fulfilled for that message!\r\n");
kendunlop 8:6f096b45ca15 115 }
kendunlop 7:a9150dc1e481 116
kendunlop 7:a9150dc1e481 117 //A function to clear all test-related variables
kendunlop 7:a9150dc1e481 118 void resettest(void)
kendunlop 7:a9150dc1e481 119 {
kendunlop 9:7c27efe30a77 120 //pc.printf("Resetting test.\r\n");
kendunlop 7:a9150dc1e481 121 failsthistest = 0;
kendunlop 9:7c27efe30a77 122 spamcount = 0;
kendunlop 8:6f096b45ca15 123 noreplyfailsthistest = 0;
kendunlop 9:7c27efe30a77 124 goodnoreplies = 0;
kendunlop 9:7c27efe30a77 125 goodnoreplystreak = 0;
kendunlop 9:7c27efe30a77 126 goodnoreplystreakstartid = 0;
kendunlop 9:7c27efe30a77 127 noreplystreak = 0;
kendunlop 9:7c27efe30a77 128 noreplystreakstartid = 0;
kendunlop 9:7c27efe30a77 129 CANmatchstreak = 0;
kendunlop 9:7c27efe30a77 130 CANmatchstreakstartID = 0;
kendunlop 8:6f096b45ca15 131 mismatchfailsthistest = 0;
kendunlop 9:7c27efe30a77 132 matchesthistest = 0;
kendunlop 8:6f096b45ca15 133 idscheckedcount = 0;
kendunlop 9:7c27efe30a77 134 unexpectedCANfails = 0;
kendunlop 9:7c27efe30a77 135 unexpectedCANstreak = 0;
kendunlop 9:7c27efe30a77 136 unexpectedCANstreakstartid = 0;
kendunlop 7:a9150dc1e481 137 }
kendunlop 7:a9150dc1e481 138
kendunlop 7:a9150dc1e481 139 //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 140 void addnewlineifneeded(void)
kendunlop 7:a9150dc1e481 141 {
kendunlop 7:a9150dc1e481 142 if (addnewlinetonextmessage == 1)
kendunlop 7:a9150dc1e481 143 {
kendunlop 7:a9150dc1e481 144 pc.printf("\r\n");
kendunlop 7:a9150dc1e481 145 addnewlinetonextmessage = 0;
kendunlop 7:a9150dc1e481 146 }
kendunlop 7:a9150dc1e481 147 }
kendunlop 7:a9150dc1e481 148
kendunlop 9:7c27efe30a77 149 void reportsentCAN() //A function to print out the CAN that was sent. Messages can only be positioned correctly once their results are known.
kendunlop 9:7c27efe30a77 150 {
kendunlop 9:7c27efe30a77 151 pc.printf("\033[1;33m\rSent out : %s \033[0m", messageOutTextWithSpaces);
kendunlop 9:7c27efe30a77 152 }
kendunlop 9:7c27efe30a77 153
kendunlop 7:a9150dc1e481 154
kendunlop 7:a9150dc1e481 155 //A function to end a test and report on findings.
kendunlop 7:a9150dc1e481 156 void endtest()
kendunlop 7:a9150dc1e481 157 {
kendunlop 8:6f096b45ca15 158 expectationresolved = 1;
kendunlop 7:a9150dc1e481 159 if (failsthistest == 0)
kendunlop 7:a9150dc1e481 160 {
kendunlop 9:7c27efe30a77 161 pc.printf("\033[1;32mTest passed.\r\n");
kendunlop 9:7c27efe30a77 162 pc.printf("\033[1;32m%d IDs sent out. ", spamcount);
kendunlop 9:7c27efe30a77 163 pc.printf("\033[1;32m%d failures found.\r\n", failsthistest);
kendunlop 9:7c27efe30a77 164 if (CANpassthrough == 0)
kendunlop 9:7c27efe30a77 165 {
kendunlop 9:7c27efe30a77 166 pc.printf("%d expected non-replies found. ", goodnoreplies);
kendunlop 9:7c27efe30a77 167 }
kendunlop 9:7c27efe30a77 168 pc.printf("%d CAN matches found.\033[0m\r\n", matchesthistest);
kendunlop 7:a9150dc1e481 169 }
kendunlop 7:a9150dc1e481 170 if (failsthistest > 0)
kendunlop 7:a9150dc1e481 171 {
kendunlop 9:7c27efe30a77 172 pc.printf("\033[1;31mTest failed.\r\n%d failures found.\r\n", failsthistest);
kendunlop 9:7c27efe30a77 173 pc.printf("\033[1;31m%d mismatch failures, %d 'no reply' failures, %d unexpected CAN messages.\033[0m\r\n", mismatchfailsthistest, noreplyfailsthistest, unexpectedCANfails);
kendunlop 9:7c27efe30a77 174 if (goodnoreplies > 0)
kendunlop 9:7c27efe30a77 175 {
kendunlop 9:7c27efe30a77 176 pc.printf("\033[1;31m%d expected non-replies occurred.\033[0m\r\n", goodnoreplies);
kendunlop 9:7c27efe30a77 177 }
kendunlop 7:a9150dc1e481 178 }
kendunlop 9:7c27efe30a77 179 //addnewlineifneeded();
kendunlop 7:a9150dc1e481 180 pc.printf("-------------------------------------\r\n");
kendunlop 7:a9150dc1e481 181 failsthistest = 0;
kendunlop 7:a9150dc1e481 182 }
kendunlop 7:a9150dc1e481 183
kendunlop 7:a9150dc1e481 184 //A generic 'send CAN' function to send CAN with a standard message
kendunlop 7:a9150dc1e481 185 void sendCAN(void)
kendunlop 7:a9150dc1e481 186 {
kendunlop 7:a9150dc1e481 187 CanBus.write(messageOut1);
kendunlop 7:a9150dc1e481 188 //addnewlineifneeded();
kendunlop 8:6f096b45ca15 189 if (aspamisgoing == 0)
kendunlop 8:6f096b45ca15 190 {
kendunlop 8:6f096b45ca15 191 //pc.printf("\r\n");
kendunlop 8:6f096b45ca15 192 }
kendunlop 8:6f096b45ca15 193 expectationresolved = 0;
kendunlop 8:6f096b45ca15 194 //pc.printf("Expectationresolved now is %d.", expectationresolved);
kendunlop 8:6f096b45ca15 195 expectationtimer = 0;
kendunlop 7:a9150dc1e481 196 //addnewlinetonextmessage = 1;
kendunlop 7:a9150dc1e481 197 }
kendunlop 1:19d183cf2689 198
kendunlop 2:11339018dda6 199 //A function to deal with each CAN message part (e.g.: 301, 8, 01, 02...)
kendunlop 1:19d183cf2689 200 void dealwithpart(void)
kendunlop 1:19d183cf2689 201 {
kendunlop 1:19d183cf2689 202 int hextotal = 0;
kendunlop 1:19d183cf2689 203 partincrement = partincrement + 1;
kendunlop 1:19d183cf2689 204 //pc.printf("Dealing with part %d. (%s)\r\n", partincrement, part);
kendunlop 1:19d183cf2689 205 //int partincrementb = 0;
kendunlop 1:19d183cf2689 206 int textlength = part.length();
kendunlop 1:19d183cf2689 207 //int characterinc = 0;
kendunlop 1:19d183cf2689 208 //pc.printf("That's %d characters long.\r\n", textlength);
kendunlop 1:19d183cf2689 209 for (int i = 0; i < part.size(); i++)
kendunlop 1:19d183cf2689 210 {
kendunlop 1:19d183cf2689 211 //pc.printf("Examining character %d/%d.\r\n", (i+1), textlength);
kendunlop 1:19d183cf2689 212 char individualcharacter = part.at(i);
kendunlop 1:19d183cf2689 213 //pc.printf("That's '%c'.\r\n", individualcharacter);
kendunlop 1:19d183cf2689 214 int numberized = 0;
kendunlop 1:19d183cf2689 215 if(isdigit(individualcharacter))
kendunlop 1:19d183cf2689 216 {
kendunlop 1:19d183cf2689 217 //pc.printf("That character is a digit.\r\n");
kendunlop 1:19d183cf2689 218 numberized = individualcharacter - '0';
kendunlop 1:19d183cf2689 219 //pc.printf("Numberized that's '%d'.\r\n", numberized);
kendunlop 1:19d183cf2689 220 }
kendunlop 1:19d183cf2689 221 else
kendunlop 1:19d183cf2689 222 {
kendunlop 1:19d183cf2689 223 //pc.printf("That character is NOT a digit.\r\n");
kendunlop 1:19d183cf2689 224 int asciivalue = toupper(individualcharacter);
kendunlop 1:19d183cf2689 225 //pc.printf("Ascii value is %d.\r\n", asciivalue);
kendunlop 1:19d183cf2689 226 numberized = asciivalue - 55;
kendunlop 1:19d183cf2689 227 //pc.printf("Hex value is %d.\r\n", numberized);
kendunlop 1:19d183cf2689 228 }
kendunlop 1:19d183cf2689 229 //pc.printf("Eventual numberization is %d.\r\n", numberized);
kendunlop 1:19d183cf2689 230 //pc.printf("Hex total is now %d.\r\n", hextotal);
kendunlop 1:19d183cf2689 231 int powertoraise = part.size() - (i+1);
kendunlop 1:19d183cf2689 232 //pc.printf("Must multiply by 16 to the power of %d.\r\n", powertoraise);
kendunlop 1:19d183cf2689 233 int amounttoadd = numberized;
kendunlop 1:19d183cf2689 234 //pc.printf("powertoraise is '%d'.\r\n", powertoraise);
kendunlop 1:19d183cf2689 235 if (powertoraise == 1)
kendunlop 1:19d183cf2689 236 {
kendunlop 1:19d183cf2689 237 amounttoadd = numberized * 16;
kendunlop 1:19d183cf2689 238 //pc.printf("Multiplying by 16.\r\n");
kendunlop 1:19d183cf2689 239 //pc.printf("powertoraise is '%d'.\r\n", powertoraise);
kendunlop 1:19d183cf2689 240 }
kendunlop 1:19d183cf2689 241 if (powertoraise == 2)
kendunlop 1:19d183cf2689 242 {
kendunlop 1:19d183cf2689 243 amounttoadd = numberized * 256;
kendunlop 1:19d183cf2689 244 //pc.printf("Multiplying by 256.\r\n");
kendunlop 1:19d183cf2689 245 }
kendunlop 1:19d183cf2689 246 //pc.printf("Amount to add is therefore %d.\r\n", amounttoadd);
kendunlop 1:19d183cf2689 247 hextotal = hextotal + amounttoadd;
kendunlop 1:19d183cf2689 248 //pc.printf("hextotal so far for this part is therefore %d.\r\n", hextotal);
kendunlop 1:19d183cf2689 249 }
kendunlop 1:19d183cf2689 250 //pc.printf("hextotal for whole part is therefore %d.\r\n", hextotal);
kendunlop 1:19d183cf2689 251 //pc.printf("Need to convert that into true hex.\r\n");
kendunlop 1:19d183cf2689 252 std::stringstream sstream;
kendunlop 1:19d183cf2689 253 sstream << std::hex << hextotal;
kendunlop 1:19d183cf2689 254 //pc.printf("StringSteam says '%s'.\r\n", sstream.str());
kendunlop 1:19d183cf2689 255 if (partincrement == 1)
kendunlop 7:a9150dc1e481 256 {
kendunlop 7:a9150dc1e481 257 messageOut1.id = hextotal;
kendunlop 7:a9150dc1e481 258 expected1 = hextotal;
kendunlop 8:6f096b45ca15 259 //For IDs, check if the ID is represented in the idsarray.
kendunlop 8:6f096b45ca15 260 int idwasfound = 0;
kendunlop 8:6f096b45ca15 261 idwasfound = 0;
kendunlop 8:6f096b45ca15 262 shouldntcomein = 0;
kendunlop 9:7c27efe30a77 263 if (mbedCANbusspeed != CANGatewayCANbusspeed)
kendunlop 9:7c27efe30a77 264 {
kendunlop 9:7c27efe30a77 265 shouldntcomein = 1;
kendunlop 9:7c27efe30a77 266 if (aspamisgoing == 0)
kendunlop 9:7c27efe30a77 267 {
kendunlop 9:7c27efe30a77 268 pc.printf("Mbed CAN bus is at baud %d but CAN Gateway is at %d, should get no CAN.\r\n", mbedCANbusspeed, CANGatewayCANbusspeed);
kendunlop 9:7c27efe30a77 269 }
kendunlop 9:7c27efe30a77 270 }
kendunlop 8:6f096b45ca15 271 if (CANpassthrough == 0)
kendunlop 8:6f096b45ca15 272 {
kendunlop 8:6f096b45ca15 273 for(int i = 0; i <=arrayy-1; i++)
kendunlop 8:6f096b45ca15 274 {
kendunlop 8:6f096b45ca15 275 if(idsarray[0][i] == expected1)
kendunlop 8:6f096b45ca15 276 {
kendunlop 8:6f096b45ca15 277 idwasfound = 1;
kendunlop 8:6f096b45ca15 278 int originalexpectedvalue = expected1;
kendunlop 8:6f096b45ca15 279 expected1 = idsarray[1][i];
kendunlop 9:7c27efe30a77 280 //Get string of hex code for original expected value (ID code found in array)
kendunlop 9:7c27efe30a77 281 std::stringstream sstream;
kendunlop 9:7c27efe30a77 282 sstream << std::hex << originalexpectedvalue;
kendunlop 9:7c27efe30a77 283 string tempstring = sstream.str();
kendunlop 9:7c27efe30a77 284 //Get string of hex code for value to adjust to
kendunlop 9:7c27efe30a77 285 std::stringstream sstream2;
kendunlop 9:7c27efe30a77 286 sstream2 << std::hex << expected1;
kendunlop 9:7c27efe30a77 287 string tempstring2 = sstream2.str();
kendunlop 9:7c27efe30a77 288 //pc.printf("\r\nFound message ID %s at 0,%d in idsarray! Will adjust expected value to %s.\r\n", tempstring, i, tempstring2);
kendunlop 8:6f096b45ca15 289 }
kendunlop 8:6f096b45ca15 290 }
kendunlop 8:6f096b45ca15 291 if (idwasfound == 0 and CANpassthrough == 0)
kendunlop 8:6f096b45ca15 292 {
kendunlop 8:6f096b45ca15 293 //pc.printf("ID of %d was never found, expecting it NOT to come in.\r\n", expected1);
kendunlop 8:6f096b45ca15 294 //pc.printf("CANpassthrough is %d.\r\n", CANpassthrough);
kendunlop 8:6f096b45ca15 295 shouldntcomein = 1;
kendunlop 8:6f096b45ca15 296 }
kendunlop 9:7c27efe30a77 297 if (shouldntcomein == 0 and unexpectedCANstreak > 0) //If data is expected to come in, this ends any unexpected-CAN streak.
kendunlop 9:7c27efe30a77 298 {
kendunlop 9:7c27efe30a77 299 if (aspamisgoing == 1) //Add a new line if it's in spam mode here to report new non-unexpected-CAN-streak stuff.
kendunlop 9:7c27efe30a77 300 {
kendunlop 9:7c27efe30a77 301 pc.printf("\r\n\n");
kendunlop 9:7c27efe30a77 302 }
kendunlop 9:7c27efe30a77 303 unexpectedCANstreak = 0;
kendunlop 9:7c27efe30a77 304 unexpectedCANstreakstartid = 0;
kendunlop 9:7c27efe30a77 305 }
kendunlop 8:6f096b45ca15 306 }
kendunlop 7:a9150dc1e481 307 }
kendunlop 1:19d183cf2689 308 if (partincrement == 2)
kendunlop 7:a9150dc1e481 309 {
kendunlop 7:a9150dc1e481 310 messageOut1.len = hextotal;
kendunlop 7:a9150dc1e481 311 expected2 = hextotal;
kendunlop 7:a9150dc1e481 312 }
kendunlop 1:19d183cf2689 313 if (partincrement >= 3)
kendunlop 7:a9150dc1e481 314 {
kendunlop 7:a9150dc1e481 315 messageOut1.data[partincrement-3] = hextotal;
kendunlop 7:a9150dc1e481 316 }
kendunlop 7:a9150dc1e481 317 if (partincrement == 3)
kendunlop 7:a9150dc1e481 318 {
kendunlop 7:a9150dc1e481 319 expected3 = hextotal;
kendunlop 7:a9150dc1e481 320 }
kendunlop 7:a9150dc1e481 321 if (partincrement == 4)
kendunlop 7:a9150dc1e481 322 {
kendunlop 7:a9150dc1e481 323 expected4 = hextotal;
kendunlop 7:a9150dc1e481 324 }
kendunlop 7:a9150dc1e481 325 if (partincrement == 5)
kendunlop 7:a9150dc1e481 326 {
kendunlop 7:a9150dc1e481 327 expected5 = hextotal;
kendunlop 7:a9150dc1e481 328 }
kendunlop 7:a9150dc1e481 329 if (partincrement == 6)
kendunlop 7:a9150dc1e481 330 {
kendunlop 7:a9150dc1e481 331 expected6 = hextotal;
kendunlop 7:a9150dc1e481 332 }
kendunlop 7:a9150dc1e481 333 if (partincrement == 7)
kendunlop 7:a9150dc1e481 334 {
kendunlop 7:a9150dc1e481 335 expected7 = hextotal;
kendunlop 7:a9150dc1e481 336 }
kendunlop 7:a9150dc1e481 337 if (partincrement == 8)
kendunlop 7:a9150dc1e481 338 {
kendunlop 7:a9150dc1e481 339 expected8 = hextotal;
kendunlop 7:a9150dc1e481 340 }
kendunlop 7:a9150dc1e481 341 if (partincrement == 9)
kendunlop 7:a9150dc1e481 342 {
kendunlop 7:a9150dc1e481 343 expected9 = hextotal;
kendunlop 7:a9150dc1e481 344 }
kendunlop 7:a9150dc1e481 345 if (partincrement == 10)
kendunlop 7:a9150dc1e481 346 {
kendunlop 7:a9150dc1e481 347 expected10 = hextotal;
kendunlop 7:a9150dc1e481 348 }
kendunlop 1:19d183cf2689 349 //pc.printf("Part %d complete.\r\n", partincrement);
kendunlop 1:19d183cf2689 350 //pc.printf("--------------------------------------\r\n");
kendunlop 1:19d183cf2689 351 }
kendunlop 1:19d183cf2689 352
kendunlop 3:79133dcea836 353 //A function to get a coherent CAN message from one, uninterrupted string
kendunlop 6:2882710e4f1e 354 void getCANfrommessageOutText(void)
kendunlop 1:19d183cf2689 355 {
kendunlop 1:19d183cf2689 356 //pc.printf("messageOutText is '%s'\r\n", messageOutText);
kendunlop 7:a9150dc1e481 357 messageOutTextWithSpaces = messageOutText;
kendunlop 1:19d183cf2689 358 remove(messageOutText.begin(), messageOutText.end(), ' '); //Remove the spaces from the text to send out so it can be parsed.
kendunlop 1:19d183cf2689 359 //pc.printf("After removing spaces, messageOutText is '%s'\r\n", messageOutText);
kendunlop 1:19d183cf2689 360 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 361 //pc.printf("String to parse is '%s'.\r\n", startofstring);
kendunlop 8:6f096b45ca15 362 expectationresolved = 0;
kendunlop 8:6f096b45ca15 363 if (expectationwasfulfilled == 0)
kendunlop 8:6f096b45ca15 364 {
kendunlop 8:6f096b45ca15 365 expectationwasnotfulfilled();
kendunlop 8:6f096b45ca15 366 }
kendunlop 8:6f096b45ca15 367 if (expectationwasfulfilled == -1)
kendunlop 8:6f096b45ca15 368 {
kendunlop 8:6f096b45ca15 369 expectationwasfulfilled = 0;
kendunlop 8:6f096b45ca15 370 }
kendunlop 8:6f096b45ca15 371 expectationwasfulfilled = 0;
kendunlop 1:19d183cf2689 372 partincrement = 0;
kendunlop 1:19d183cf2689 373 part = startofstring.substr(0,3);
kendunlop 1:19d183cf2689 374 dealwithpart();
kendunlop 1:19d183cf2689 375 part = startofstring.substr(3,1);
kendunlop 1:19d183cf2689 376 dealwithpart();
kendunlop 1:19d183cf2689 377 part = startofstring.substr(4,2);
kendunlop 1:19d183cf2689 378 dealwithpart();
kendunlop 1:19d183cf2689 379 part = startofstring.substr(6,2);
kendunlop 1:19d183cf2689 380 dealwithpart();
kendunlop 1:19d183cf2689 381 part = startofstring.substr(8,2);
kendunlop 1:19d183cf2689 382 dealwithpart();
kendunlop 1:19d183cf2689 383 part = startofstring.substr(10,2);
kendunlop 1:19d183cf2689 384 dealwithpart();
kendunlop 1:19d183cf2689 385 part = startofstring.substr(12,2);
kendunlop 1:19d183cf2689 386 dealwithpart();
kendunlop 1:19d183cf2689 387 part = startofstring.substr(14,2);
kendunlop 1:19d183cf2689 388 dealwithpart();
kendunlop 1:19d183cf2689 389 part = startofstring.substr(16,2);
kendunlop 1:19d183cf2689 390 dealwithpart();
kendunlop 1:19d183cf2689 391 part = startofstring.substr(18,2);
kendunlop 1:19d183cf2689 392 dealwithpart();
kendunlop 1:19d183cf2689 393 }
kendunlop 1:19d183cf2689 394
kendunlop 1:19d183cf2689 395 void defineCANmessage(void)
kendunlop 1:19d183cf2689 396 {
kendunlop 1:19d183cf2689 397 //pc.printf("Defining CAN message 1.\r\n");
kendunlop 1:19d183cf2689 398 //messageOut1.format = CANStandard;
kendunlop 1:19d183cf2689 399 //messageOut1.id = 0x301;
kendunlop 1:19d183cf2689 400 //messageOut1.len = 8;
kendunlop 1:19d183cf2689 401 //messageOut1.data[0] = 0x06;
kendunlop 1:19d183cf2689 402 //messageOut1.data[1] = 0x3f;
kendunlop 1:19d183cf2689 403 //messageOut1.data[2] = 0xb2;
kendunlop 1:19d183cf2689 404 //messageOut1.data[3] = 0x29;
kendunlop 1:19d183cf2689 405 //messageOut1.data[4] = 0x19;
kendunlop 1:19d183cf2689 406 //messageOut1.data[5] = 0x97;
kendunlop 1:19d183cf2689 407 //messageOut1.data[6] = 0x67;
kendunlop 1:19d183cf2689 408 //messageOut1.data[7] = 0x37;
kendunlop 1:19d183cf2689 409 }
kendunlop 1:19d183cf2689 410
kendunlop 3:79133dcea836 411 void printMessageOut (void)
kendunlop 3:79133dcea836 412 {
kendunlop 1:19d183cf2689 413 //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 414 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 415 }
kendunlop 1:19d183cf2689 416
kendunlop 3:79133dcea836 417 void printMessageIn (void)
kendunlop 3:79133dcea836 418 {
kendunlop 8:6f096b45ca15 419 //This function will print out whatever the CAN bus is receiving.
kendunlop 8:6f096b45ca15 420 //addnewlineifneeded();
kendunlop 8:6f096b45ca15 421 //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 422 }
kendunlop 0:7a500ebaa7a6 423
kendunlop 0:7a500ebaa7a6 424 //The 'main' function will run as soon as the program starts.
kendunlop 0:7a500ebaa7a6 425 int main()
kendunlop 1:19d183cf2689 426 {
kendunlop 1:19d183cf2689 427 pc.baud(115200); // serial port at 115200
kendunlop 1:19d183cf2689 428 CanBus.frequency(500 * 1000); // CAN bus at 500k
kendunlop 3:79133dcea836 429 CanBus2.frequency(500 * 1000); // CAN bus at 500k
kendunlop 1:19d183cf2689 430 CanBus.reset(); // clear any bus errors
kendunlop 3:79133dcea836 431 CanBus2.reset(); // clear any bus errors
kendunlop 1:19d183cf2689 432 //NOTE: Print messages must be below this line to work.
kendunlop 8:6f096b45ca15 433 pc.printf("\033[0m------------------------------------------\r\n");
kendunlop 1:19d183cf2689 434 pc.printf("Welcome to Ken CAN test.\r\n");
kendunlop 1:19d183cf2689 435 pc.printf("Setting CAN bus to 500k.\r\n");
kendunlop 6:2882710e4f1e 436 pc.printf("Setting serial port to baud rate 115200.\r\n");
kendunlop 8:6f096b45ca15 437 pc.printf("Using pins 9 and 10 for CANBus 1 (out) and 30 and 29 for CANBus 2 (in).\r\n");
kendunlop 1:19d183cf2689 438 pc.printf("Version %d.%d\r\n",kMajorVersion,kMinorVersion);
kendunlop 1:19d183cf2689 439 pc.printf("Build date %s %s\r\n",__DATE__,__TIME__);
kendunlop 1:19d183cf2689 440 pc.printf("------------------------------------------\r\n");
kendunlop 1:19d183cf2689 441 char c;
kendunlop 1:19d183cf2689 442
kendunlop 1:19d183cf2689 443 //Check for button presses
kendunlop 1:19d183cf2689 444 while (1)
kendunlop 0:7a500ebaa7a6 445 {
kendunlop 8:6f096b45ca15 446 if (expectationtimer < 100 and expectationresolved == 0)
kendunlop 8:6f096b45ca15 447 {
kendunlop 8:6f096b45ca15 448 if (expectationresolved == 0)
kendunlop 8:6f096b45ca15 449 {
kendunlop 9:7c27efe30a77 450 expectationtimer++;
kendunlop 9:7c27efe30a77 451 if (expectationtimer >= 2)//If expectation timer gets too high, the CAN reply is deemed not to have arrived.
kendunlop 8:6f096b45ca15 452 {
kendunlop 9:7c27efe30a77 453 if (CANmatchstreak > 0) //CAN not arriving breaks any CAN match streak
kendunlop 9:7c27efe30a77 454 {
kendunlop 9:7c27efe30a77 455 CANmatchstreak = 0;
kendunlop 9:7c27efe30a77 456 CANmatchstreakstartID = 0;
kendunlop 9:7c27efe30a77 457 if (aspamisgoing == 1)
kendunlop 9:7c27efe30a77 458 {
kendunlop 9:7c27efe30a77 459 pc.printf("\r\n\n");//Add a return into the terminal to show the old streak on a line by itself.
kendunlop 9:7c27efe30a77 460 }
kendunlop 9:7c27efe30a77 461 }
kendunlop 9:7c27efe30a77 462 if (goodnoreplystreak > 0 and shouldntcomein == 0) //A missing expected reply breaks any combos for expected no-replies
kendunlop 9:7c27efe30a77 463 {
kendunlop 9:7c27efe30a77 464 if (aspamisgoing == 1)
kendunlop 9:7c27efe30a77 465 {
kendunlop 9:7c27efe30a77 466 //pc.printf("\r\n(From breaking goodnoreplystreak)\n\r");
kendunlop 9:7c27efe30a77 467 pc.printf("\r\n\n");
kendunlop 9:7c27efe30a77 468 }
kendunlop 9:7c27efe30a77 469 goodnoreplystreak = 0;
kendunlop 9:7c27efe30a77 470 goodnoreplystreakstartid = 0;
kendunlop 9:7c27efe30a77 471 }
kendunlop 9:7c27efe30a77 472 // Add a line here if it's breaking an 'unexpected data...' combo
kendunlop 8:6f096b45ca15 473 if (shouldntcomein == 0)
kendunlop 8:6f096b45ca15 474 {
kendunlop 8:6f096b45ca15 475 expectationwasfulfilled = 0;
kendunlop 8:6f096b45ca15 476 failsthistest++;
kendunlop 8:6f096b45ca15 477 noreplyfailsthistest++;
kendunlop 8:6f096b45ca15 478 if (aspamisgoing == 0)
kendunlop 8:6f096b45ca15 479 {
kendunlop 9:7c27efe30a77 480 noreplystreakstartid = idlistincrementer;
kendunlop 9:7c27efe30a77 481 }
kendunlop 9:7c27efe30a77 482 if (goodnoreplystreak > 0)
kendunlop 9:7c27efe30a77 483 {
kendunlop 9:7c27efe30a77 484 goodnoreplystreak = 0;
kendunlop 9:7c27efe30a77 485 if (aspamisgoing == 1)
kendunlop 9:7c27efe30a77 486 {
kendunlop 9:7c27efe30a77 487 pc.printf("(From ending goodnoreplystreak)\r\n");//Add an extra line if breaking an 'as expected' no reply streak.
kendunlop 9:7c27efe30a77 488 }
kendunlop 8:6f096b45ca15 489 }
kendunlop 9:7c27efe30a77 490 if (aspamisgoing == 1)
kendunlop 9:7c27efe30a77 491 {
kendunlop 9:7c27efe30a77 492 if (noreplystreak == 0)
kendunlop 9:7c27efe30a77 493 {
kendunlop 9:7c27efe30a77 494 noreplystreakstartid = idlistincrementer;
kendunlop 9:7c27efe30a77 495 }
kendunlop 9:7c27efe30a77 496 noreplystreak++;
kendunlop 9:7c27efe30a77 497 }
kendunlop 9:7c27efe30a77 498 reportsentCAN();//Now that it's known the CAN has no reply, you can safely write out the sent CAN. (The needed line-returns have been added.)
kendunlop 9:7c27efe30a77 499 if (noreplystreakstartid != idlistincrementer)
kendunlop 9:7c27efe30a77 500 {
kendunlop 9:7c27efe30a77 501 pc.printf("\033[38;5;220mNo reply message detected for IDs %03X to %03X.\033[0m\r", noreplystreakstartid, idlistincrementer);
kendunlop 9:7c27efe30a77 502 }
kendunlop 9:7c27efe30a77 503 if (noreplystreakstartid == idlistincrementer)
kendunlop 9:7c27efe30a77 504 {
kendunlop 9:7c27efe30a77 505 pc.printf("\033[38;5;220mNo reply message detected for ID %03X.\033[0m\r", idlistincrementer);
kendunlop 9:7c27efe30a77 506 }
kendunlop 9:7c27efe30a77 507 if (aspamisgoing == 0)
kendunlop 9:7c27efe30a77 508 {
kendunlop 9:7c27efe30a77 509 pc.printf("\r\n\n"); //If it's not spamming, you can safely add a new line after the fail message.
kendunlop 9:7c27efe30a77 510 }
kendunlop 8:6f096b45ca15 511 }
kendunlop 8:6f096b45ca15 512 if (shouldntcomein == 1)
kendunlop 8:6f096b45ca15 513 {
kendunlop 8:6f096b45ca15 514 expectationwasfulfilled = 1;
kendunlop 9:7c27efe30a77 515 //Getting an expected non-reply breaks any unexpected reply streaks
kendunlop 9:7c27efe30a77 516 if (unexpectedCANstreak > 0)
kendunlop 9:7c27efe30a77 517 {
kendunlop 9:7c27efe30a77 518 if (aspamisgoing == 1) //Add a new line if it's in spam mode here to report new expected-non-message stuff.
kendunlop 9:7c27efe30a77 519 {
kendunlop 9:7c27efe30a77 520 pc.printf("\r\n\n");
kendunlop 9:7c27efe30a77 521 }
kendunlop 9:7c27efe30a77 522 unexpectedCANstreak = 0;
kendunlop 9:7c27efe30a77 523 unexpectedCANstreakstartid = 0;
kendunlop 9:7c27efe30a77 524 }
kendunlop 9:7c27efe30a77 525 //Getting an expected non-reply also breaks any unexpected non-reply streaks
kendunlop 9:7c27efe30a77 526 if (noreplystreak > 0)
kendunlop 9:7c27efe30a77 527 {
kendunlop 9:7c27efe30a77 528 if (aspamisgoing == 1) //Add a new line if it's in spam mode here to report new expected-non-message stuff.
kendunlop 9:7c27efe30a77 529 {
kendunlop 9:7c27efe30a77 530 //pc.printf("\r\n(From breaking noreplystreak)\n");
kendunlop 9:7c27efe30a77 531 pc.printf("\r\n\n");
kendunlop 9:7c27efe30a77 532 }
kendunlop 9:7c27efe30a77 533 noreplystreak = 0;
kendunlop 9:7c27efe30a77 534 noreplystreakstartid = 0;
kendunlop 9:7c27efe30a77 535 }
kendunlop 9:7c27efe30a77 536 if (goodnoreplystreak == 0) //If there isn't yet a good no-reply streak, set the starting no-reply ID to this one
kendunlop 9:7c27efe30a77 537 {
kendunlop 9:7c27efe30a77 538 goodnoreplystreakstartid = idlistincrementer;
kendunlop 9:7c27efe30a77 539 if (CANmatchstreak > 0 and aspamisgoing == 1)//Also add an extra return for the breaking of the old CANmatchstreak if needed
kendunlop 9:7c27efe30a77 540 {
kendunlop 9:7c27efe30a77 541 CANmatchstreak = 0;
kendunlop 9:7c27efe30a77 542 pc.printf("(From breaking of CANmatchstreak)\r\n");
kendunlop 9:7c27efe30a77 543 }
kendunlop 9:7c27efe30a77 544 }
kendunlop 9:7c27efe30a77 545 reportsentCAN();//Now that it's known the CAN has no reply, you can safely write out the sent CAN. (The needed line-returns have been added.)
kendunlop 9:7c27efe30a77 546 if (aspamisgoing == 0)
kendunlop 9:7c27efe30a77 547 {
kendunlop 9:7c27efe30a77 548 goodnoreplystreakstartid = idlistincrementer;
kendunlop 9:7c27efe30a77 549 }
kendunlop 9:7c27efe30a77 550 if (goodnoreplystreakstartid != idlistincrementer)
kendunlop 9:7c27efe30a77 551 {
kendunlop 9:7c27efe30a77 552 pc.printf("\033[0;32mNo reply message detected from %03X to %03X, as expected.\033[0m\r", goodnoreplystreakstartid, idlistincrementer);
kendunlop 9:7c27efe30a77 553 }
kendunlop 9:7c27efe30a77 554 if (goodnoreplystreakstartid == idlistincrementer)
kendunlop 9:7c27efe30a77 555 {
kendunlop 9:7c27efe30a77 556 pc.printf("\033[0;32mNo reply message detected for ID %03X, as expected.\033[0m\r", goodnoreplystreakstartid);
kendunlop 9:7c27efe30a77 557 }
kendunlop 9:7c27efe30a77 558 if (aspamisgoing == 0)
kendunlop 9:7c27efe30a77 559 {
kendunlop 9:7c27efe30a77 560 pc.printf("\r\n"); //If it's not spamming, you can safely add a new line after the fail message.
kendunlop 9:7c27efe30a77 561 }
kendunlop 9:7c27efe30a77 562 goodnoreplies++;
kendunlop 9:7c27efe30a77 563 goodnoreplystreak++;
kendunlop 8:6f096b45ca15 564 }
kendunlop 8:6f096b45ca15 565 expectationresolved = 1;
kendunlop 8:6f096b45ca15 566 //pc.printf("Expectation timer reached %d!\r\n", expectationtimer);
kendunlop 8:6f096b45ca15 567 expectationtimer = 0;
kendunlop 8:6f096b45ca15 568 if (idspamon == 1)
kendunlop 8:6f096b45ca15 569 {
kendunlop 8:6f096b45ca15 570 idscheckedcount++;
kendunlop 9:7c27efe30a77 571 idlistincrementer++; //Only add 1 to the idlist incrementer at the end so the first ID it sends is '000'.
kendunlop 8:6f096b45ca15 572 }
kendunlop 8:6f096b45ca15 573 }
kendunlop 8:6f096b45ca15 574 wait(0.01);
kendunlop 8:6f096b45ca15 575 }
kendunlop 8:6f096b45ca15 576 }
kendunlop 1:19d183cf2689 577 if (pc.readable())
kendunlop 1:19d183cf2689 578 {
kendunlop 9:7c27efe30a77 579 c = tolower(pc.getc());
kendunlop 3:79133dcea836 580 if (c != NULL)
kendunlop 1:19d183cf2689 581 {
kendunlop 3:79133dcea836 582 //When the a key is pressed, define a CAN message and send it.
kendunlop 1:19d183cf2689 583 //pc.printf("A key was pressed! (%c)\r\n", c);
kendunlop 1:19d183cf2689 584 messageOutText = "";
kendunlop 8:6f096b45ca15 585 buttonPressMessageOutText = "";
kendunlop 1:19d183cf2689 586 if (c == '1')
kendunlop 7:a9150dc1e481 587 {
kendunlop 8:6f096b45ca15 588 buttonPressMessageOutText = "301 8 01 01 01 01 01 01 01 01";
kendunlop 9:7c27efe30a77 589 idlistincrementer = 0x301;
kendunlop 7:a9150dc1e481 590 }
kendunlop 1:19d183cf2689 591 if (c == '2')
kendunlop 7:a9150dc1e481 592 {
kendunlop 8:6f096b45ca15 593 buttonPressMessageOutText = "302 8 02 02 02 02 02 02 02 02";
kendunlop 9:7c27efe30a77 594 idlistincrementer = 0x302;
kendunlop 7:a9150dc1e481 595 }
kendunlop 1:19d183cf2689 596 if (c == '3')
kendunlop 7:a9150dc1e481 597 {
kendunlop 8:6f096b45ca15 598 buttonPressMessageOutText = "303 8 00 00 00 00 00 00 00 33";
kendunlop 9:7c27efe30a77 599 idlistincrementer = 0x303;
kendunlop 7:a9150dc1e481 600 }
kendunlop 1:19d183cf2689 601 if (c == '4')
kendunlop 7:a9150dc1e481 602 {
kendunlop 8:6f096b45ca15 603 buttonPressMessageOutText = "304 8 04 04 04 04 04 04 04 04";
kendunlop 9:7c27efe30a77 604 idlistincrementer = 0x304;
kendunlop 7:a9150dc1e481 605 }
kendunlop 1:19d183cf2689 606 if (c == '5')
kendunlop 7:a9150dc1e481 607 {
kendunlop 8:6f096b45ca15 608 buttonPressMessageOutText = "305 8 05 05 05 05 05 05 05 05";
kendunlop 9:7c27efe30a77 609 idlistincrementer = 0x305;
kendunlop 7:a9150dc1e481 610 }
kendunlop 1:19d183cf2689 611 if (c == '6')
kendunlop 7:a9150dc1e481 612 {
kendunlop 8:6f096b45ca15 613 buttonPressMessageOutText = "306 8 06 06 06 06 06 06 06 06";
kendunlop 9:7c27efe30a77 614 idlistincrementer = 0x306;
kendunlop 7:a9150dc1e481 615 }
kendunlop 1:19d183cf2689 616 if (c == '7')
kendunlop 7:a9150dc1e481 617 {
kendunlop 8:6f096b45ca15 618 buttonPressMessageOutText = "307 8 07 07 07 07 07 07 07 07";
kendunlop 9:7c27efe30a77 619 idlistincrementer = 0x307;
kendunlop 7:a9150dc1e481 620 }
kendunlop 1:19d183cf2689 621 if (c == '8')
kendunlop 7:a9150dc1e481 622 {
kendunlop 8:6f096b45ca15 623 buttonPressMessageOutText = "308 8 08 08 08 08 08 08 08 08";
kendunlop 9:7c27efe30a77 624 idlistincrementer = 0x308;
kendunlop 7:a9150dc1e481 625 }
kendunlop 1:19d183cf2689 626 if (c == '9')
kendunlop 7:a9150dc1e481 627 {
kendunlop 8:6f096b45ca15 628 buttonPressMessageOutText = "309 8 09 09 09 09 09 09 09 09";
kendunlop 9:7c27efe30a77 629 idlistincrementer = 0x309;
kendunlop 7:a9150dc1e481 630 }
kendunlop 1:19d183cf2689 631 if (c == 'f')
kendunlop 7:a9150dc1e481 632 {
kendunlop 9:7c27efe30a77 633 buttonPressMessageOutText = "7FF 8 FF FF FF FF FF FF FF FF";//NOTE: CAN IDs only go to 7FF, not FFF.
kendunlop 8:6f096b45ca15 634 }
kendunlop 8:6f096b45ca15 635 if (buttonPressMessageOutText != "")//Centralized 'button press MessageOut' routine that applies to all button presses.
kendunlop 8:6f096b45ca15 636 {
kendunlop 8:6f096b45ca15 637 if (aspamisgoing == 0)
kendunlop 8:6f096b45ca15 638 {
kendunlop 9:7c27efe30a77 639 //addnewlineifneeded();
kendunlop 8:6f096b45ca15 640 messageOutText = buttonPressMessageOutText;
kendunlop 8:6f096b45ca15 641 addnewlinetonextmessage = 1;
kendunlop 8:6f096b45ca15 642 checklaterbytes = 1;//Check all bytes for button-press messages.
kendunlop 8:6f096b45ca15 643 }
kendunlop 8:6f096b45ca15 644 buttonPressMessageOutText = "";
kendunlop 7:a9150dc1e481 645 }
kendunlop 9:7c27efe30a77 646 if (c == 'q' and aspamisgoing == 0)
kendunlop 3:79133dcea836 647 {
kendunlop 9:7c27efe30a77 648 pc.printf("Changing Mbed CAN bus speed to 125.\r\n");
kendunlop 3:79133dcea836 649 CanBus.frequency(125 * 1000); // CAN bus at 125k
kendunlop 5:bf4c6278ca8b 650 CanBus2.frequency(125 * 1000); // CAN bus at 125k
kendunlop 9:7c27efe30a77 651 mbedCANbusspeed = (125 * 1000);
kendunlop 3:79133dcea836 652 }
kendunlop 9:7c27efe30a77 653 if (c == 'w' and aspamisgoing == 0)
kendunlop 3:79133dcea836 654 {
kendunlop 9:7c27efe30a77 655 pc.printf("Changing Mbed CAN bus speed to 250.\r\n");
kendunlop 3:79133dcea836 656 CanBus.frequency(250 * 1000); // CAN bus at 250k
kendunlop 5:bf4c6278ca8b 657 CanBus2.frequency(250 * 1000); // CAN bus at 250k
kendunlop 9:7c27efe30a77 658 mbedCANbusspeed = (250 * 1000);
kendunlop 3:79133dcea836 659 }
kendunlop 9:7c27efe30a77 660 if (c == 'e' and aspamisgoing == 0)
kendunlop 3:79133dcea836 661 {
kendunlop 9:7c27efe30a77 662 pc.printf("Changing Mbed CAN bus speed to 500.\r\n");
kendunlop 3:79133dcea836 663 CanBus.frequency(500 * 1000); // CAN bus at 500k
kendunlop 3:79133dcea836 664 CanBus2.frequency(500 * 1000);// CAN bus at 500k
kendunlop 9:7c27efe30a77 665 mbedCANbusspeed = (500 * 1000);
kendunlop 3:79133dcea836 666 }
kendunlop 9:7c27efe30a77 667 if (c == 'r' and aspamisgoing == 0)
kendunlop 3:79133dcea836 668 {
kendunlop 9:7c27efe30a77 669 pc.printf("Changing Mbed CAN bus speed to 1000.\r\n");
kendunlop 3:79133dcea836 670 CanBus.frequency(1000 * 1000); // CAN bus at 1000k
kendunlop 3:79133dcea836 671 CanBus2.frequency(1000 * 1000); // CAN bus at 1000k
kendunlop 9:7c27efe30a77 672 mbedCANbusspeed = (1000 * 1000);
kendunlop 3:79133dcea836 673 }
kendunlop 9:7c27efe30a77 674 if (c == 't' and aspamisgoing == 0)
kendunlop 9:7c27efe30a77 675 {
kendunlop 9:7c27efe30a77 676 CANGatewayCANbusspeed = (125 * 1000);
kendunlop 9:7c27efe30a77 677 pc.printf("CAN Gateway is now presumed to be at CAN bus speed %d.\r\n", CANGatewayCANbusspeed / 1000);
kendunlop 9:7c27efe30a77 678 }
kendunlop 9:7c27efe30a77 679 if (c == 'y' and aspamisgoing == 0)
kendunlop 9:7c27efe30a77 680 {
kendunlop 9:7c27efe30a77 681 CANGatewayCANbusspeed = (250 * 1000);
kendunlop 9:7c27efe30a77 682 pc.printf("CAN Gateway is now presumed to be at CAN bus speed %d.\r\n", CANGatewayCANbusspeed / 1000);
kendunlop 9:7c27efe30a77 683 }
kendunlop 9:7c27efe30a77 684 if (c == 'u' and aspamisgoing == 0)
kendunlop 8:6f096b45ca15 685 {
kendunlop 9:7c27efe30a77 686 CANGatewayCANbusspeed = (500 * 1000);
kendunlop 9:7c27efe30a77 687 pc.printf("CAN Gateway is now presumed to be at CAN bus speed %d.\r\n", CANGatewayCANbusspeed / 1000);
kendunlop 9:7c27efe30a77 688 }
kendunlop 9:7c27efe30a77 689 if (c == 'i' and aspamisgoing == 0)
kendunlop 9:7c27efe30a77 690 {
kendunlop 9:7c27efe30a77 691 CANGatewayCANbusspeed = (1000 * 1000);
kendunlop 9:7c27efe30a77 692 pc.printf("CAN Gateway is now presumed to be at CAN bus speed %d.\r\n", CANGatewayCANbusspeed / 1000);
kendunlop 9:7c27efe30a77 693 }
kendunlop 9:7c27efe30a77 694 if (c == 'n' and CANpassthrough == 0 and aspamisgoing == 0)
kendunlop 9:7c27efe30a77 695 {
kendunlop 9:7c27efe30a77 696 pc.printf("Starting CAN passthrough mode. Will expect to recieve the same CAN that is sent.\r\n");
kendunlop 9:7c27efe30a77 697 CANpassthrough = 1;
kendunlop 9:7c27efe30a77 698 }
kendunlop 9:7c27efe30a77 699 if (c == 'm' and CANpassthrough == 1 and aspamisgoing == 0)
kendunlop 9:7c27efe30a77 700 {
kendunlop 9:7c27efe30a77 701 pc.printf("Will make a CAN ID translation array.\r\n");
kendunlop 8:6f096b45ca15 702 //Making a two-dimensional array with 301-309 in the first column, then 401-409 in the second column.
kendunlop 8:6f096b45ca15 703 int copyarray[2][8] = {{769, 770, 771, 772, 773, 774, 776, 777}, {1025, 1026, 1027, 1028, 1029, 1030, 1032, 1033}};
kendunlop 8:6f096b45ca15 704 //int copyarray[2][1] = {{2047}, {1025}};
kendunlop 8:6f096b45ca15 705 memcpy(idsarray, copyarray, sizeof(copyarray)); //Copy the new array over the old 'idsarray'.
kendunlop 8:6f096b45ca15 706 arrayx = 2;
kendunlop 8:6f096b45ca15 707 arrayy = 8;
kendunlop 9:7c27efe30a77 708 //pc.printf("Array is %dx%d.\r\n", arrayx, arrayy); //Report the size variables of the new array.
kendunlop 9:7c27efe30a77 709 pc.printf("1st column of IDs array says \033[0;30;47m%03X, %03X, %03X, %03X, %03X, %03X, %03X, %03X\033[0m\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 9:7c27efe30a77 710 pc.printf("2nd column of IDs array says \033[0;30;47m%03X, %03X, %03X, %03X, %03X, %03X, %03X, %03X\033[0m\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 711 //pc.printf("1st column of IDs array says %d.\r\n", idsarray[0][0]);
kendunlop 8:6f096b45ca15 712 //pc.printf("2nd column of IDs array says %d.\r\n", idsarray[1][0]);
kendunlop 8:6f096b45ca15 713 CANpassthrough = 0;
kendunlop 8:6f096b45ca15 714 pc.printf("CANpassthrough is now set to %d.\r\n", CANpassthrough);
kendunlop 8:6f096b45ca15 715 /*
kendunlop 8:6f096b45ca15 716 pc.printf("Will search for '305' in row 1.\r\n");
kendunlop 8:6f096b45ca15 717 //Search for '305' in the array.
kendunlop 8:6f096b45ca15 718 for(int i = 0; i <=arrayy; i++)
kendunlop 8:6f096b45ca15 719 {
kendunlop 8:6f096b45ca15 720 if(idsarray[0][i] == 305)
kendunlop 8:6f096b45ca15 721 {
kendunlop 8:6f096b45ca15 722 pc.printf("Element is found at 0,%d.\r\n", i);
kendunlop 8:6f096b45ca15 723 }
kendunlop 8:6f096b45ca15 724 }
kendunlop 8:6f096b45ca15 725 */
kendunlop 8:6f096b45ca15 726 //pc.printf("It got here.\r\n"); //To check if it got stuck at this point.
kendunlop 8:6f096b45ca15 727 }
kendunlop 7:a9150dc1e481 728 //if (c == 's')
kendunlop 7:a9150dc1e481 729 // {
kendunlop 7:a9150dc1e481 730 // pc.printf("Sending authentic sats message.\r\n");
kendunlop 7:a9150dc1e481 731 // messageOutText = "301 8 06 3F B2 29 12 97 67 37";
kendunlop 7:a9150dc1e481 732 // }
kendunlop 8:6f096b45ca15 733 if (c == 'o' and idspamon == 0 and spamon == 1)
kendunlop 3:79133dcea836 734 {
kendunlop 3:79133dcea836 735 incrementer = (incrementer * 16);
kendunlop 9:7c27efe30a77 736 pc.printf("\n\rMultiplied Incrementer by 16. It's now now %d.\r\n", incrementer);
kendunlop 3:79133dcea836 737 }
kendunlop 9:7c27efe30a77 738 if (c == 'i' and spamon == 1)
kendunlop 1:19d183cf2689 739 {
kendunlop 1:19d183cf2689 740 incrementer = incrementer + 16;
kendunlop 1:19d183cf2689 741 std::stringstream sstream;
kendunlop 1:19d183cf2689 742 sstream << std::hex << incrementer;
kendunlop 1:19d183cf2689 743 string stringsofar = sstream.str();
kendunlop 1:19d183cf2689 744 //pc.printf("Incrementer is now %d.\r\n", incrementer);
kendunlop 1:19d183cf2689 745 //pc.printf("StringStream says '%s'.\r\n", stringsofar);
kendunlop 1:19d183cf2689 746 int length = stringsofar.length();
kendunlop 1:19d183cf2689 747 //pc.printf("Length is %d/16.\r\n", length);
kendunlop 1:19d183cf2689 748 for (int i = 0; i < (16-length); i++)
kendunlop 1:19d183cf2689 749 stringsofar = "0" + stringsofar;
kendunlop 1:19d183cf2689 750 //pc.printf("stringsofar says '%s'.\r\n", stringsofar);
kendunlop 9:7c27efe30a77 751 //messageOutText = "305 8 " + stringsofar;
kendunlop 1:19d183cf2689 752 //pc.printf("Will try to send '%s'.\r\n", messageOutText);
kendunlop 1:19d183cf2689 753 }
kendunlop 1:19d183cf2689 754 messageOut1.format = CANStandard;
kendunlop 8:6f096b45ca15 755 if (c == 'z' and aspamisgoing == 0)
kendunlop 1:19d183cf2689 756 {
kendunlop 7:a9150dc1e481 757 resettest();
kendunlop 8:6f096b45ca15 758 pc.printf("Starting spam sequence for ID %s. Press 'x' to end. Press 'o' to increase incrementer.\r\n", spampreamble);
kendunlop 1:19d183cf2689 759 spamon = 1;
kendunlop 8:6f096b45ca15 760 aspamisgoing = 1;
kendunlop 8:6f096b45ca15 761 checklaterbytes = 1;
kendunlop 1:19d183cf2689 762 incrementer = 0;
kendunlop 2:11339018dda6 763 spamcount = 0;
kendunlop 3:79133dcea836 764 spamstarttime = clock();
kendunlop 9:7c27efe30a77 765 //pc.printf("Spam start time is %d.\r\n", spamstarttime);
kendunlop 1:19d183cf2689 766 }
kendunlop 1:19d183cf2689 767 //Pressing 'x' switches off spam mode
kendunlop 1:19d183cf2689 768 if (c == 'x' and spamon == 1)
kendunlop 1:19d183cf2689 769 {
kendunlop 8:6f096b45ca15 770 spamon = 2;
kendunlop 8:6f096b45ca15 771 aspamisgoing = 0;
kendunlop 3:79133dcea836 772 spamendtime = clock();
kendunlop 9:7c27efe30a77 773 getspamsecondstotal();//Process the spam information
kendunlop 9:7c27efe30a77 774 //pc.printf("Total spams ever are %d.\r\n", totalspamsever);
kendunlop 7:a9150dc1e481 775 endtest();
kendunlop 7:a9150dc1e481 776 //pc.printf("-------------------------\r\n");
kendunlop 3:79133dcea836 777 }
kendunlop 8:6f096b45ca15 778 if (c == 'v' and aspamisgoing == 0)
kendunlop 3:79133dcea836 779 {
kendunlop 7:a9150dc1e481 780 resettest();
kendunlop 6:2882710e4f1e 781 idspamon = 1; //Set the 'idspamon' integer to 1 so ID spam happens.
kendunlop 8:6f096b45ca15 782 aspamisgoing = 1;
kendunlop 8:6f096b45ca15 783 checklaterbytes = 0;
kendunlop 6:2882710e4f1e 784 idlistincrementer = 0;
kendunlop 9:7c27efe30a77 785 pc.printf("Beginning check of all possible CAN IDs (000 - 7FF).\r\n");
kendunlop 9:7c27efe30a77 786 spamstarttime = clock();
kendunlop 9:7c27efe30a77 787 //pc.printf("Spam started at clock time %d.\r\n", spamstarttime);
kendunlop 8:6f096b45ca15 788 if (checklaterbytes == 0)
kendunlop 8:6f096b45ca15 789 {
kendunlop 8:6f096b45ca15 790 pc.printf("Will only check CAN IDs, not later bytes.\r\n");
kendunlop 8:6f096b45ca15 791 }
kendunlop 1:19d183cf2689 792 }
kendunlop 1:19d183cf2689 793 if (messageOutText != "")
kendunlop 1:19d183cf2689 794 {
kendunlop 6:2882710e4f1e 795 getCANfrommessageOutText();
kendunlop 7:a9150dc1e481 796 sendCAN();
kendunlop 7:a9150dc1e481 797 //CanBus.write(messageOut1);
kendunlop 7:a9150dc1e481 798 //CanBus2.write(messageOut1);
kendunlop 6:2882710e4f1e 799 if (spamon == 0 and idspamon == 0)
kendunlop 3:79133dcea836 800 {
kendunlop 7:a9150dc1e481 801 //printMessageOut();
kendunlop 3:79133dcea836 802 }
kendunlop 3:79133dcea836 803 messageOutText = "";
kendunlop 1:19d183cf2689 804 }
kendunlop 5:bf4c6278ca8b 805 }
kendunlop 4:e8e9bc25b1ca 806 }
kendunlop 5:bf4c6278ca8b 807
kendunlop 4:e8e9bc25b1ca 808
kendunlop 7:a9150dc1e481 809 //If spam mode is on, spam an incrementing CAN message
kendunlop 8:6f096b45ca15 810 if (spamon == 1 and expectationresolved == 1)
kendunlop 5:bf4c6278ca8b 811 {
kendunlop 5:bf4c6278ca8b 812 spamcount ++;
kendunlop 5:bf4c6278ca8b 813 totalspamsever ++;
kendunlop 5:bf4c6278ca8b 814 incrementer ++;
kendunlop 5:bf4c6278ca8b 815 std::stringstream sstream;
kendunlop 5:bf4c6278ca8b 816 sstream << std::hex << incrementer;
kendunlop 5:bf4c6278ca8b 817 string stringsofar = sstream.str();
kendunlop 5:bf4c6278ca8b 818 int length = stringsofar.length();
kendunlop 5:bf4c6278ca8b 819 for (int i = 0; i < (16-length); i++)
kendunlop 5:bf4c6278ca8b 820 stringsofar = "0" + stringsofar;
kendunlop 6:2882710e4f1e 821 messageOutText = spampreamble + " 8 " + stringsofar;
kendunlop 6:2882710e4f1e 822 getCANfrommessageOutText();
kendunlop 7:a9150dc1e481 823 sendCAN();
kendunlop 5:bf4c6278ca8b 824 }
kendunlop 8:6f096b45ca15 825 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 826 {
kendunlop 9:7c27efe30a77 827 spamendtime = clock();
kendunlop 9:7c27efe30a77 828 getspamsecondstotal();//Process the spam information
kendunlop 8:6f096b45ca15 829 idspamon = 0;
kendunlop 8:6f096b45ca15 830 aspamisgoing = 0;
kendunlop 8:6f096b45ca15 831 addnewlinetonextmessage = 1;
kendunlop 9:7c27efe30a77 832 //addnewlineifneeded();
kendunlop 9:7c27efe30a77 833 //pc.printf("Ending ID spam sequence. %d IDs checked total.\r\n", idscheckedcount);
kendunlop 8:6f096b45ca15 834 idlistincrementer = 0;
kendunlop 8:6f096b45ca15 835 endtest();
kendunlop 8:6f096b45ca15 836 }
kendunlop 8:6f096b45ca15 837 if (idspamon == 1 and expectationresolved == 1)
kendunlop 6:2882710e4f1e 838 {
kendunlop 9:7c27efe30a77 839 spamcount++;
kendunlop 6:2882710e4f1e 840 std::stringstream sstream;
kendunlop 6:2882710e4f1e 841 sstream << std::hex << idlistincrementer;
kendunlop 6:2882710e4f1e 842 string idstring = "";
kendunlop 6:2882710e4f1e 843 idstring = sstream.str();
kendunlop 6:2882710e4f1e 844 //pc.printf("idstring says '%s'\r\n", idstring);
kendunlop 6:2882710e4f1e 845 if (idstring.length() < 3)
kendunlop 6:2882710e4f1e 846 {
kendunlop 6:2882710e4f1e 847 for (int i = 0; i < (4 - idstring.length()); i++)
kendunlop 6:2882710e4f1e 848 {
kendunlop 6:2882710e4f1e 849 idstring = "0" + idstring;
kendunlop 6:2882710e4f1e 850 }
kendunlop 6:2882710e4f1e 851 }
kendunlop 6:2882710e4f1e 852 //pc.printf("idstring now says '%s'\r\n", idstring);
kendunlop 6:2882710e4f1e 853 //pc.printf("%s\r\n", idstring);
kendunlop 7:a9150dc1e481 854 idstring = idstring + " 8 01 02 03 04 05 06 07 08";
kendunlop 6:2882710e4f1e 855 //pc.printf("...and now idstring now says '%s'\r\n", idstring);
kendunlop 6:2882710e4f1e 856 messageOutText = idstring;
kendunlop 6:2882710e4f1e 857 getCANfrommessageOutText();
kendunlop 7:a9150dc1e481 858 sendCAN();
kendunlop 7:a9150dc1e481 859 //CanBus.write(messageOut1);
kendunlop 7:a9150dc1e481 860 //CanBus2.write(messageOut1);
kendunlop 6:2882710e4f1e 861 //pc.printf("ID list incrementer is now %d\r\n", idlistincrementer);
kendunlop 8:6f096b45ca15 862
kendunlop 6:2882710e4f1e 863 wait(0.01);//ID spam at 100Hz
kendunlop 6:2882710e4f1e 864 }
kendunlop 1:19d183cf2689 865 //Check for CAN messages coming in
kendunlop 7:a9150dc1e481 866 if (CanBus2.read(messageIn))
kendunlop 7:a9150dc1e481 867 {
kendunlop 9:7c27efe30a77 868 if (noreplystreak > 0)
kendunlop 8:6f096b45ca15 869 {
kendunlop 9:7c27efe30a77 870 noreplystreak = 0;
kendunlop 9:7c27efe30a77 871 noreplystreakstartid = 0;
kendunlop 9:7c27efe30a77 872 pc.printf("\r\n\n");
kendunlop 9:7c27efe30a77 873 }
kendunlop 9:7c27efe30a77 874 if (goodnoreplystreak > 0)
kendunlop 9:7c27efe30a77 875 {
kendunlop 9:7c27efe30a77 876 goodnoreplystreak = 0;
kendunlop 9:7c27efe30a77 877 goodnoreplystreakstartid = 0;
kendunlop 9:7c27efe30a77 878 pc.printf("\r\n\n");
kendunlop 8:6f096b45ca15 879 }
kendunlop 9:7c27efe30a77 880 CANCount++;
kendunlop 9:7c27efe30a77 881 reportsentCAN();
kendunlop 9:7c27efe30a77 882 if (1 == 1)//Prepare to print out the message coming in
kendunlop 7:a9150dc1e481 883 {
kendunlop 9:7c27efe30a77 884 //Data shouldn't come in at all if 'shouldntcomein' is 1!
kendunlop 9:7c27efe30a77 885 if (shouldntcomein == 1)
kendunlop 9:7c27efe30a77 886 {
kendunlop 9:7c27efe30a77 887 if (unexpectedCANstreak == 0)//If it's the start of a new unexpected-CAN streak, mark the starting ID.
kendunlop 9:7c27efe30a77 888 {
kendunlop 9:7c27efe30a77 889 unexpectedCANstreakstartid = idlistincrementer;
kendunlop 9:7c27efe30a77 890 }
kendunlop 9:7c27efe30a77 891 if (aspamisgoing == 0)
kendunlop 9:7c27efe30a77 892 {
kendunlop 9:7c27efe30a77 893 unexpectedCANstreakstartid = idlistincrementer;
kendunlop 9:7c27efe30a77 894 }
kendunlop 9:7c27efe30a77 895 if (unexpectedCANstreakstartid != idlistincrementer)
kendunlop 9:7c27efe30a77 896 {
kendunlop 9:7c27efe30a77 897 pc.printf("\033[0;35mUnexpected data came in from IDs %03X to %03X!\033[0m", unexpectedCANstreakstartid, idlistincrementer);
kendunlop 9:7c27efe30a77 898 }
kendunlop 9:7c27efe30a77 899 if (unexpectedCANstreakstartid == idlistincrementer)
kendunlop 9:7c27efe30a77 900 {
kendunlop 9:7c27efe30a77 901 pc.printf("\033[0;35mUnexpected data came in for %03X!\033[0m", idlistincrementer);
kendunlop 9:7c27efe30a77 902 }
kendunlop 9:7c27efe30a77 903 failsthistest++;
kendunlop 9:7c27efe30a77 904 unexpectedCANfails++;
kendunlop 9:7c27efe30a77 905 unexpectedCANstreak++;
kendunlop 9:7c27efe30a77 906 }
kendunlop 9:7c27efe30a77 907 //Establish earlier whether it's a match in the 10 places
kendunlop 7:a9150dc1e481 908 int itsamatch = 1;
kendunlop 7:a9150dc1e481 909 if (expected1 != messageIn.id)
kendunlop 9:7c27efe30a77 910 {itsamatch = 0;}
kendunlop 9:7c27efe30a77 911 if (expected2 != messageIn.len and checklaterbytes >= 1)
kendunlop 9:7c27efe30a77 912 {itsamatch = 0;}
kendunlop 9:7c27efe30a77 913 if (expected3 != messageIn.data[0] and checklaterbytes >= 1)
kendunlop 9:7c27efe30a77 914 {itsamatch = 0;}
kendunlop 9:7c27efe30a77 915 if (expected4 != messageIn.data[1] and checklaterbytes >= 1)
kendunlop 9:7c27efe30a77 916 {itsamatch = 0;}
kendunlop 9:7c27efe30a77 917 if (expected5 != messageIn.data[2] and checklaterbytes >= 1)
kendunlop 9:7c27efe30a77 918 {itsamatch = 0;}
kendunlop 9:7c27efe30a77 919 if (expected6 != messageIn.data[3] and checklaterbytes >= 1)
kendunlop 9:7c27efe30a77 920 {itsamatch = 0;}
kendunlop 9:7c27efe30a77 921 if (expected7 != messageIn.data[4] and checklaterbytes >= 1)
kendunlop 9:7c27efe30a77 922 {itsamatch = 0;}
kendunlop 9:7c27efe30a77 923 if (expected8 != messageIn.data[5] and checklaterbytes >= 1)
kendunlop 9:7c27efe30a77 924 {itsamatch = 0;}
kendunlop 9:7c27efe30a77 925 if (expected9 != messageIn.data[6] and checklaterbytes >= 1)
kendunlop 9:7c27efe30a77 926 {itsamatch = 0;}
kendunlop 9:7c27efe30a77 927 if (expected10 != messageIn.data[7] and checklaterbytes >= 1)
kendunlop 9:7c27efe30a77 928 {itsamatch = 0;}
kendunlop 9:7c27efe30a77 929 if (itsamatch == 0 and shouldntcomein == 0)
kendunlop 7:a9150dc1e481 930 {
kendunlop 9:7c27efe30a77 931 pc.printf("\033[0;31mIncoming data does not match expectation!\033[0m");
kendunlop 9:7c27efe30a77 932 }
kendunlop 9:7c27efe30a77 933 // Once it's known whether the data matches expectation, set the match streak
kendunlop 9:7c27efe30a77 934 if (itsamatch == 1 and shouldntcomein == 0)
kendunlop 9:7c27efe30a77 935 {
kendunlop 9:7c27efe30a77 936 if (CANmatchstreak == 0)
kendunlop 9:7c27efe30a77 937 {
kendunlop 9:7c27efe30a77 938 CANmatchstreakstartID = idlistincrementer;
kendunlop 9:7c27efe30a77 939 }
kendunlop 9:7c27efe30a77 940 CANmatchstreak++;
kendunlop 9:7c27efe30a77 941 }
kendunlop 9:7c27efe30a77 942 if (itsamatch == 0 and CANmatchstreak > 0) //If it's NOT a match, break any CANmatchstreak
kendunlop 9:7c27efe30a77 943 {
kendunlop 9:7c27efe30a77 944 CANmatchstreak = 0;
kendunlop 9:7c27efe30a77 945 CANmatchstreakstartID = 0;
kendunlop 9:7c27efe30a77 946 }
kendunlop 9:7c27efe30a77 947 // Print needed message
kendunlop 9:7c27efe30a77 948 int messageinisneeded = 0;
kendunlop 9:7c27efe30a77 949 if (aspamisgoing == 0)
kendunlop 9:7c27efe30a77 950 {messageinisneeded = 1;}
kendunlop 9:7c27efe30a77 951 if (itsamatch == 0 and shouldntcomein == 0)
kendunlop 9:7c27efe30a77 952 {messageinisneeded = 1;}
kendunlop 9:7c27efe30a77 953 if (messageinisneeded == 0) //If a message in is NOT needed, simply report what the CAN is doing on the same line.
kendunlop 9:7c27efe30a77 954 {
kendunlop 9:7c27efe30a77 955 if (itsamatch == 1 and CANmatchstreakstartID == idlistincrementer and CANmatchstreak > 0)
kendunlop 9:7c27efe30a77 956 {
kendunlop 9:7c27efe30a77 957 pc.printf("\033[0;32mCAN matches expected values on ID %03X.\033[0m", idlistincrementer);
kendunlop 9:7c27efe30a77 958 }
kendunlop 9:7c27efe30a77 959 if (itsamatch == 1 and CANmatchstreakstartID != idlistincrementer and CANmatchstreak > 0)
kendunlop 9:7c27efe30a77 960 {
kendunlop 9:7c27efe30a77 961 pc.printf("\033[0;32mCAN matches expected values from IDs %03X to %03X.\033[0m", CANmatchstreakstartID, idlistincrementer);
kendunlop 9:7c27efe30a77 962 }
kendunlop 9:7c27efe30a77 963 }
kendunlop 9:7c27efe30a77 964 if (messageinisneeded == 1) //Only if message in is needed do you need to look at printing each part of the message
kendunlop 9:7c27efe30a77 965 {
kendunlop 9:7c27efe30a77 966 pc.printf("\r\nMessage received was: ");
kendunlop 9:7c27efe30a77 967 if (expected1 != messageIn.id)
kendunlop 8:6f096b45ca15 968 {
kendunlop 8:6f096b45ca15 969 //pc.printf("\r\nID of %d does NOT match %d.\r\n", messageIn.id, expected1);
kendunlop 9:7c27efe30a77 970 pc.printf("\033[1;31m%03X\033[0m ", messageIn.id);//Show the ID in red if it doesn't match.
kendunlop 9:7c27efe30a77 971 }
kendunlop 9:7c27efe30a77 972 if (expected1 == messageIn.id)
kendunlop 9:7c27efe30a77 973 {
kendunlop 9:7c27efe30a77 974 pc.printf("\033[1;32m%03X\033[0m ", messageIn.id);//Show the ID in green if it matches.
kendunlop 9:7c27efe30a77 975 }
kendunlop 9:7c27efe30a77 976 if (expected2 != messageIn.len and checklaterbytes >= 1)
kendunlop 9:7c27efe30a77 977 {
kendunlop 9:7c27efe30a77 978 //pc.printf("Length of %d does NOT match %d.\r\n", messageIn.len, expected2);
kendunlop 9:7c27efe30a77 979 pc.printf("\033[1;31m%01X\033[0m ", messageIn.len);
kendunlop 9:7c27efe30a77 980 }
kendunlop 9:7c27efe30a77 981 else
kendunlop 9:7c27efe30a77 982 {
kendunlop 9:7c27efe30a77 983 pc.printf("%01X\033[0m ", messageIn.len);
kendunlop 9:7c27efe30a77 984 }
kendunlop 9:7c27efe30a77 985 if (expected3 != messageIn.data[0] and checklaterbytes >= 1)
kendunlop 9:7c27efe30a77 986 {
kendunlop 9:7c27efe30a77 987 //pc.printf("Data 0 of %d does NOT match %d.\r\n", messageIn.data[0], expected3);
kendunlop 9:7c27efe30a77 988 pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[0]);
kendunlop 9:7c27efe30a77 989 }
kendunlop 9:7c27efe30a77 990 else
kendunlop 9:7c27efe30a77 991 {
kendunlop 9:7c27efe30a77 992 pc.printf("%02X\033[0m ", messageIn.data[0]);
kendunlop 9:7c27efe30a77 993 }
kendunlop 9:7c27efe30a77 994 if (expected4 != messageIn.data[1] and checklaterbytes >= 1)
kendunlop 9:7c27efe30a77 995 {
kendunlop 9:7c27efe30a77 996 //pc.printf("Data 1 of %d does NOT match %d.\r\n", messageIn.data[1], expected4);
kendunlop 9:7c27efe30a77 997 pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[1]);
kendunlop 9:7c27efe30a77 998 }
kendunlop 9:7c27efe30a77 999 else
kendunlop 9:7c27efe30a77 1000 {
kendunlop 9:7c27efe30a77 1001 pc.printf("%02X\033[0m ", messageIn.data[1]);
kendunlop 9:7c27efe30a77 1002 }
kendunlop 9:7c27efe30a77 1003 if (expected5 != messageIn.data[2] and checklaterbytes >= 1)
kendunlop 9:7c27efe30a77 1004 {
kendunlop 9:7c27efe30a77 1005 //pc.printf("Data 2 of %d does NOT match %d.\r\n", messageIn.data[2], expected5);
kendunlop 9:7c27efe30a77 1006 pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[2]);
kendunlop 9:7c27efe30a77 1007 }
kendunlop 9:7c27efe30a77 1008 else
kendunlop 9:7c27efe30a77 1009 {
kendunlop 9:7c27efe30a77 1010 pc.printf("%02X\033[0m ", messageIn.data[2]);
kendunlop 8:6f096b45ca15 1011 }
kendunlop 9:7c27efe30a77 1012 if (expected6 != messageIn.data[3] and checklaterbytes >= 1)
kendunlop 9:7c27efe30a77 1013 {
kendunlop 9:7c27efe30a77 1014 //pc.printf("Data 3 of %d does NOT match %d.\r\n", messageIn.data[3], expected6);
kendunlop 9:7c27efe30a77 1015 pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[3]);
kendunlop 9:7c27efe30a77 1016 }
kendunlop 9:7c27efe30a77 1017
kendunlop 9:7c27efe30a77 1018 else
kendunlop 9:7c27efe30a77 1019 {
kendunlop 9:7c27efe30a77 1020 pc.printf("%02X\033[0m ", messageIn.data[3]);
kendunlop 9:7c27efe30a77 1021 }
kendunlop 9:7c27efe30a77 1022 if (expected7 != messageIn.data[4] and checklaterbytes >= 1)
kendunlop 9:7c27efe30a77 1023 {
kendunlop 9:7c27efe30a77 1024 //pc.printf("Data 4 of %d does NOT match %d.\r\n", messageIn.data[4], expected7);
kendunlop 9:7c27efe30a77 1025 pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[4]);
kendunlop 9:7c27efe30a77 1026 }
kendunlop 9:7c27efe30a77 1027 else
kendunlop 9:7c27efe30a77 1028 {
kendunlop 9:7c27efe30a77 1029 pc.printf("%02X\033[0m ", messageIn.data[4]);
kendunlop 9:7c27efe30a77 1030 }
kendunlop 9:7c27efe30a77 1031 if (expected8 != messageIn.data[5] and checklaterbytes >= 1)
kendunlop 9:7c27efe30a77 1032 {
kendunlop 9:7c27efe30a77 1033 //pc.printf("Data 5 of %d does NOT match %d.\r\n", messageIn.data[5], expected8);
kendunlop 9:7c27efe30a77 1034 pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[5]);
kendunlop 9:7c27efe30a77 1035 }
kendunlop 9:7c27efe30a77 1036 else
kendunlop 9:7c27efe30a77 1037 {
kendunlop 9:7c27efe30a77 1038 pc.printf("%02X\033[0m ", messageIn.data[5]);
kendunlop 9:7c27efe30a77 1039 }
kendunlop 9:7c27efe30a77 1040 if (expected9 != messageIn.data[6] and checklaterbytes >= 1)
kendunlop 9:7c27efe30a77 1041 {
kendunlop 9:7c27efe30a77 1042 //pc.printf("Data 6 of %d does NOT match %d.\r\n", messageIn.data[6], expected9);
kendunlop 9:7c27efe30a77 1043 pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[6]);
kendunlop 9:7c27efe30a77 1044 }
kendunlop 9:7c27efe30a77 1045 else
kendunlop 9:7c27efe30a77 1046 {
kendunlop 9:7c27efe30a77 1047 pc.printf("%02X\033[0m ", messageIn.data[6]);
kendunlop 9:7c27efe30a77 1048 }
kendunlop 9:7c27efe30a77 1049 if (expected10 != messageIn.data[7] and checklaterbytes >= 1)
kendunlop 9:7c27efe30a77 1050 {
kendunlop 9:7c27efe30a77 1051 //pc.printf("Data 7 of %d does NOT match %d.\r\n", messageIn.data[7], expected10);
kendunlop 9:7c27efe30a77 1052 pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[7]);
kendunlop 9:7c27efe30a77 1053 }
kendunlop 9:7c27efe30a77 1054 else
kendunlop 9:7c27efe30a77 1055 {
kendunlop 9:7c27efe30a77 1056 pc.printf("%02X\033[0m ", messageIn.data[7]);
kendunlop 9:7c27efe30a77 1057 }
kendunlop 9:7c27efe30a77 1058 if (aspamisgoing == 0)
kendunlop 9:7c27efe30a77 1059 {
kendunlop 9:7c27efe30a77 1060 pc.printf("\r\n");//Add a return and a new line after the 'Sent out' message is finished if it's not spam mode.
kendunlop 9:7c27efe30a77 1061 }
kendunlop 8:6f096b45ca15 1062 }
kendunlop 7:a9150dc1e481 1063 if (itsamatch == 0)
kendunlop 7:a9150dc1e481 1064 {
kendunlop 8:6f096b45ca15 1065 mismatchfailsthistest++;
kendunlop 9:7c27efe30a77 1066 if (aspamisgoing == 1)
kendunlop 9:7c27efe30a77 1067 {
kendunlop 9:7c27efe30a77 1068 pc.printf("\r\n"); //Having a new line here separated 'message received was:...' from 'Does NOT match...' too much.
kendunlop 9:7c27efe30a77 1069 }
kendunlop 9:7c27efe30a77 1070 pc.printf("Does NOT match : %03X %01X %02X %02X %02X %02X %02X %02X %02X %02X\r\n\n", expected1, expected2, expected3, expected4, expected5, expected6, expected7, expected8, expected9, expected10);
kendunlop 9:7c27efe30a77 1071 if (aspamisgoing == 0)
kendunlop 9:7c27efe30a77 1072 {
kendunlop 9:7c27efe30a77 1073 //pc.printf("\r\n");
kendunlop 9:7c27efe30a77 1074 }
kendunlop 7:a9150dc1e481 1075 failsthistest++; //Add 1 to failsthistest to mark the fail
kendunlop 8:6f096b45ca15 1076 expectationwasfulfilled = 0;
kendunlop 8:6f096b45ca15 1077 expectationresolved = 1;
kendunlop 8:6f096b45ca15 1078 }
kendunlop 8:6f096b45ca15 1079 if (itsamatch == 1)
kendunlop 8:6f096b45ca15 1080 {
kendunlop 8:6f096b45ca15 1081 expectationwasfulfilled = 1;
kendunlop 8:6f096b45ca15 1082 expectationresolved = 1;
kendunlop 9:7c27efe30a77 1083 matchesthistest++;
kendunlop 9:7c27efe30a77 1084 //If it's a match, write out a 'Matches expectation...' message
kendunlop 9:7c27efe30a77 1085 if (CANpassthrough == 0 and aspamisgoing == 0)
kendunlop 9:7c27efe30a77 1086 {
kendunlop 9:7c27efe30a77 1087 pc.printf("Matches expectation : %03X %01X %02X %02X %02X %02X %02X %02X %02X %02X\r\n\n", expected1, expected2, expected3, expected4, expected5, expected6, expected7, expected8, expected9, expected10);
kendunlop 9:7c27efe30a77 1088 }
kendunlop 7:a9150dc1e481 1089 }
kendunlop 7:a9150dc1e481 1090 }
kendunlop 9:7c27efe30a77 1091 if (idspamon == 1)
kendunlop 9:7c27efe30a77 1092 {
kendunlop 9:7c27efe30a77 1093 idscheckedcount++;
kendunlop 9:7c27efe30a77 1094 idlistincrementer++; //Only add 1 to the idlist incrementer at the end so the first ID it sends is '000'.
kendunlop 9:7c27efe30a77 1095 }
kendunlop 1:19d183cf2689 1096 }
kendunlop 8:6f096b45ca15 1097 //if (expected1 != -1) //If CANBus does not read a message in
kendunlop 8:6f096b45ca15 1098 //{
kendunlop 8:6f096b45ca15 1099 //
kendunlop 8:6f096b45ca15 1100 //}
kendunlop 8:6f096b45ca15 1101 }
kendunlop 5:bf4c6278ca8b 1102 }