Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Ken_CAN_test.cpp@13:e3a4abc461f4, 2022-07-11 (annotated)
- Committer:
- kendunlop
- Date:
- Mon Jul 11 16:28:07 2022 +0000
- Revision:
- 13:e3a4abc461f4
- Parent:
- 12:e950feba910d
Finished a nicer, more readable way of defining CAN expectations in the 'CANpairslist[x]' table. IDfrom and IDmessageto are strings that can be written to easily make a set of CAN expectations.
Who changed what in which revision?
User | Revision | Line number | New 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 | 10:3e15baff40e7 | 40 | int messagesinpersecond = 0; |
kendunlop | 9:7c27efe30a77 | 41 | |
kendunlop | 3:79133dcea836 | 42 | int vlistincrementer = 0; |
kendunlop | 6:2882710e4f1e | 43 | int idlistincrementer = 0; |
kendunlop | 6:2882710e4f1e | 44 | string spampreamble = "333"; |
kendunlop | 7:a9150dc1e481 | 45 | int expected1 = 0; |
kendunlop | 7:a9150dc1e481 | 46 | int expected2 = 0; |
kendunlop | 7:a9150dc1e481 | 47 | unsigned char expected3 = 0; |
kendunlop | 7:a9150dc1e481 | 48 | unsigned char expected4 = 0; |
kendunlop | 7:a9150dc1e481 | 49 | unsigned char expected5 = 0; |
kendunlop | 7:a9150dc1e481 | 50 | unsigned char expected6 = 0; |
kendunlop | 7:a9150dc1e481 | 51 | unsigned char expected7 = 0; |
kendunlop | 7:a9150dc1e481 | 52 | unsigned char expected8 = 0; |
kendunlop | 7:a9150dc1e481 | 53 | unsigned char expected9 = 0; |
kendunlop | 7:a9150dc1e481 | 54 | unsigned char expected10 = 0; |
kendunlop | 8:6f096b45ca15 | 55 | int expectationwasfulfilled = -1; |
kendunlop | 8:6f096b45ca15 | 56 | int expectationresolved = 1; |
kendunlop | 8:6f096b45ca15 | 57 | int expectationtimer = 0; |
kendunlop | 8:6f096b45ca15 | 58 | int shouldntcomein = 0; |
kendunlop | 9:7c27efe30a77 | 59 | int goodnoreplies = 0; |
kendunlop | 9:7c27efe30a77 | 60 | int goodnoreplystreak = 0; |
kendunlop | 9:7c27efe30a77 | 61 | int goodnoreplystreakstartid = 0; |
kendunlop | 9:7c27efe30a77 | 62 | int CANmatchstreak = 0; |
kendunlop | 9:7c27efe30a77 | 63 | int CANmatchstreakstartID = 0; |
kendunlop | 9:7c27efe30a77 | 64 | int unexpectedCANfails = 0; |
kendunlop | 9:7c27efe30a77 | 65 | int unexpectedCANstreak = 0; |
kendunlop | 9:7c27efe30a77 | 66 | int unexpectedCANstreakstartid = 0; |
kendunlop | 7:a9150dc1e481 | 67 | int failsthistest = 0; |
kendunlop | 11:abd3df435a2b | 68 | int failsstreak = 0; |
kendunlop | 11:abd3df435a2b | 69 | int failsstreakstart = 0; |
kendunlop | 13:e3a4abc461f4 | 70 | int reportfailsindividually = 1; |
kendunlop | 8:6f096b45ca15 | 71 | int noreplyfailsthistest = 0; |
kendunlop | 9:7c27efe30a77 | 72 | int noreplystreak = 0; |
kendunlop | 9:7c27efe30a77 | 73 | int noreplystreakstartid = 0; |
kendunlop | 9:7c27efe30a77 | 74 | |
kendunlop | 8:6f096b45ca15 | 75 | int mismatchfailsthistest = 0; |
kendunlop | 9:7c27efe30a77 | 76 | int matchesthistest = 0; |
kendunlop | 7:a9150dc1e481 | 77 | int addnewlinetonextmessage = 0; |
kendunlop | 11:abd3df435a2b | 78 | int checklaterbytes = 1; |
kendunlop | 8:6f096b45ca15 | 79 | int aspamisgoing = 0; |
kendunlop | 8:6f096b45ca15 | 80 | int CANpassthrough = 1; |
kendunlop | 10:3e15baff40e7 | 81 | int speedspamison = 0; |
kendunlop | 10:3e15baff40e7 | 82 | int messagesincount = 0; |
kendunlop | 11:abd3df435a2b | 83 | int messageinisneeded = 0; |
kendunlop | 11:abd3df435a2b | 84 | |
kendunlop | 11:abd3df435a2b | 85 | int idwasfound = 0; |
kendunlop | 11:abd3df435a2b | 86 | int columnidwasfound = 0; |
kendunlop | 8:6f096b45ca15 | 87 | |
kendunlop | 12:e950feba910d | 88 | char c = NULL; |
kendunlop | 12:e950feba910d | 89 | |
kendunlop | 9:7c27efe30a77 | 90 | int mbedCANbusspeed = 500 * 1000; |
kendunlop | 9:7c27efe30a77 | 91 | int CANGatewayCANbusspeed = 500 * 1000; |
kendunlop | 10:3e15baff40e7 | 92 | int listen = 1; |
kendunlop | 11:abd3df435a2b | 93 | int testwasaborted = 0; |
kendunlop | 11:abd3df435a2b | 94 | |
kendunlop | 11:abd3df435a2b | 95 | int customentry = 0; |
kendunlop | 11:abd3df435a2b | 96 | int normalkeypresses = 1; |
kendunlop | 12:e950feba910d | 97 | int tablesmode = 0; |
kendunlop | 11:abd3df435a2b | 98 | string customentrystring = ""; |
kendunlop | 11:abd3df435a2b | 99 | int hextotal = 0; |
kendunlop | 9:7c27efe30a77 | 100 | |
kendunlop | 12:e950feba910d | 101 | int numberkeypressed = -1; |
kendunlop | 12:e950feba910d | 102 | |
kendunlop | 8:6f096b45ca15 | 103 | //Array of IDs to translate can be overwritten with the 'copyarray'. |
kendunlop | 8:6f096b45ca15 | 104 | //It's arranged in columns of IDs that are translated to the ID in the next column. |
kendunlop | 8:6f096b45ca15 | 105 | //NOTE: Array must be a large enough size to contain all ID translations. |
kendunlop | 11:abd3df435a2b | 106 | int idsarray[2][8][10] = {}; |
kendunlop | 12:e950feba910d | 107 | //int copyarray[2][8][10] = {}; |
kendunlop | 8:6f096b45ca15 | 108 | int arrayx = 2; |
kendunlop | 8:6f096b45ca15 | 109 | int arrayy = 8; |
kendunlop | 11:abd3df435a2b | 110 | int arrayz = 10; |
kendunlop | 12:e950feba910d | 111 | int x = 0; |
kendunlop | 12:e950feba910d | 112 | int y = 0; |
kendunlop | 12:e950feba910d | 113 | int z = 0; |
kendunlop | 12:e950feba910d | 114 | int totalarraysize = 0; |
kendunlop | 8:6f096b45ca15 | 115 | |
kendunlop | 9:7c27efe30a77 | 116 | //A function to easily get the total of seconds a spam has taken |
kendunlop | 9:7c27efe30a77 | 117 | int getspamsecondstotal(void) |
kendunlop | 9:7c27efe30a77 | 118 | { |
kendunlop | 9:7c27efe30a77 | 119 | //pc.printf("\r\nHere I'll get the spam seconds total!\r\n"); |
kendunlop | 9:7c27efe30a77 | 120 | //pc.printf("Spam start/end time was %d / %d.\r\n", spamstarttime, spamendtime); |
kendunlop | 9:7c27efe30a77 | 121 | int spamtickstotal = (spamendtime - spamstarttime); |
kendunlop | 9:7c27efe30a77 | 122 | //pc.printf("Spam ticks total is %d.\r\n", spamtickstotal); |
kendunlop | 9:7c27efe30a77 | 123 | //pc.printf("CLOCKS_PER_SEC are %d.\r\n", CLOCKS_PER_SEC); |
kendunlop | 9:7c27efe30a77 | 124 | //pc.printf("Spamcount this test is %d.\r\n", spamcount); |
kendunlop | 9:7c27efe30a77 | 125 | spamsecondstotal = ((spamendtime - spamstarttime) / CLOCKS_PER_SEC); |
kendunlop | 9:7c27efe30a77 | 126 | //pc.printf("Spam seconds total is %d.\r\n", spamsecondstotal); |
kendunlop | 9:7c27efe30a77 | 127 | spamspersecond = (10000 * spamcount); |
kendunlop | 10:3e15baff40e7 | 128 | messagesinpersecond = (10000 * messagesincount); |
kendunlop | 9:7c27efe30a77 | 129 | //pc.printf("Spams biggened x 10,000 is %d.\r\n", spamspersecond); |
kendunlop | 9:7c27efe30a77 | 130 | spamspersecond = (spamspersecond / spamtickstotal); |
kendunlop | 10:3e15baff40e7 | 131 | messagesinpersecond = (messagesinpersecond / spamtickstotal); |
kendunlop | 9:7c27efe30a77 | 132 | //pc.printf("Spams per tick, still biggened, is %d.\r\n", spamspersecond); |
kendunlop | 9:7c27efe30a77 | 133 | spamspersecond = (spamspersecond * CLOCKS_PER_SEC); |
kendunlop | 10:3e15baff40e7 | 134 | messagesinpersecond = (messagesinpersecond * CLOCKS_PER_SEC); |
kendunlop | 9:7c27efe30a77 | 135 | //pc.printf("Spams per second, still biggened, given %d ticks per second, is %d.\r\n", CLOCKS_PER_SEC, spamspersecond); |
kendunlop | 10:3e15baff40e7 | 136 | spamspersecond = (spamspersecond / 10000); |
kendunlop | 10:3e15baff40e7 | 137 | messagesinpersecond = (messagesinpersecond / 10000); |
kendunlop | 9:7c27efe30a77 | 138 | //pc.printf("Spams per second, un-biggened, is %d.\r\n", spamspersecond); |
kendunlop | 10:3e15baff40e7 | 139 | //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); //Old version of the message |
kendunlop | 10:3e15baff40e7 | 140 | if (speedspamison == 0) |
kendunlop | 10:3e15baff40e7 | 141 | { |
kendunlop | 11:abd3df435a2b | 142 | if (aspamisgoing == 1) |
kendunlop | 11:abd3df435a2b | 143 | { |
kendunlop | 11:abd3df435a2b | 144 | pc.printf("\n"); |
kendunlop | 11:abd3df435a2b | 145 | } |
kendunlop | 10:3e15baff40e7 | 146 | pc.printf("\rSent %d messages over %d seconds. Messages per second is %d.\r\n", spamcount, spamsecondstotal, spamspersecond); |
kendunlop | 10:3e15baff40e7 | 147 | } |
kendunlop | 9:7c27efe30a77 | 148 | return(spamsecondstotal, spamspersecond); |
kendunlop | 9:7c27efe30a77 | 149 | } |
kendunlop | 9:7c27efe30a77 | 150 | |
kendunlop | 12:e950feba910d | 151 | //A function to get table cells |
kendunlop | 12:e950feba910d | 152 | void dealwithtable(int copyarray) |
kendunlop | 12:e950feba910d | 153 | { |
kendunlop | 12:e950feba910d | 154 | for (int arrayprogress = 0; arrayprogress < totalarraysize; arrayprogress++) |
kendunlop | 12:e950feba910d | 155 | { |
kendunlop | 12:e950feba910d | 156 | x++; |
kendunlop | 12:e950feba910d | 157 | if (x >= arrayx) |
kendunlop | 12:e950feba910d | 158 | { |
kendunlop | 12:e950feba910d | 159 | x = 0; |
kendunlop | 12:e950feba910d | 160 | y++; |
kendunlop | 12:e950feba910d | 161 | } |
kendunlop | 12:e950feba910d | 162 | if (y >= arrayy) |
kendunlop | 12:e950feba910d | 163 | { |
kendunlop | 12:e950feba910d | 164 | y = 0; |
kendunlop | 12:e950feba910d | 165 | z++; |
kendunlop | 12:e950feba910d | 166 | } |
kendunlop | 12:e950feba910d | 167 | //int cellcontents = copyarray[x][y][z]; |
kendunlop | 12:e950feba910d | 168 | //pc.printf("Array progress is %d. Examining %d,%d,%d. It says '%d'.\r\n", arrayprogress, x, y, z, cellcontents); |
kendunlop | 12:e950feba910d | 169 | } |
kendunlop | 12:e950feba910d | 170 | } |
kendunlop | 12:e950feba910d | 171 | |
kendunlop | 13:e3a4abc461f4 | 172 | //Structure for CAN conversion pair, an ID and ID-plus-message, expressed in strings |
kendunlop | 13:e3a4abc461f4 | 173 | struct CANpair { |
kendunlop | 13:e3a4abc461f4 | 174 | string IDfrom; |
kendunlop | 13:e3a4abc461f4 | 175 | string IDmessageto; |
kendunlop | 13:e3a4abc461f4 | 176 | }; |
kendunlop | 13:e3a4abc461f4 | 177 | |
kendunlop | 13:e3a4abc461f4 | 178 | struct CANpair CANpairslist[32]; //This defines a list of CANpair string pairs, each with 'IDfrom' and 'IDmessageto'. |
kendunlop | 13:e3a4abc461f4 | 179 | |
kendunlop | 12:e950feba910d | 180 | //Structure for CAN conversion table |
kendunlop | 12:e950feba910d | 181 | struct CANconversion { |
kendunlop | 12:e950feba910d | 182 | int IDfrom; |
kendunlop | 12:e950feba910d | 183 | int IDto; |
kendunlop | 12:e950feba910d | 184 | int length; |
kendunlop | 12:e950feba910d | 185 | int Data [8]; |
kendunlop | 12:e950feba910d | 186 | }; |
kendunlop | 12:e950feba910d | 187 | |
kendunlop | 12:e950feba910d | 188 | struct CANconversion CANconversionlist[32]; //This defines a list of CANconversion structures, each with its own IDfrom, IDto, length, etc. (E.g.: 'CANconversionlist[0].IDfrom') |
kendunlop | 12:e950feba910d | 189 | int CANconversionlistheight = 32; |
kendunlop | 12:e950feba910d | 190 | |
kendunlop | 13:e3a4abc461f4 | 191 | //A function to reset the CAN pairs list so it can be re-written into |
kendunlop | 13:e3a4abc461f4 | 192 | void resetCANpairslist() |
kendunlop | 12:e950feba910d | 193 | { |
kendunlop | 13:e3a4abc461f4 | 194 | CANconversionlistheight = 0; |
kendunlop | 13:e3a4abc461f4 | 195 | for (int i = 0; i < 32; i++) |
kendunlop | 12:e950feba910d | 196 | { |
kendunlop | 13:e3a4abc461f4 | 197 | CANpairslist[i].IDfrom = ""; |
kendunlop | 13:e3a4abc461f4 | 198 | CANpairslist[i].IDmessageto = ""; |
kendunlop | 13:e3a4abc461f4 | 199 | //pc.printf("Emptying CANpairslist row %d.\r\n", i); |
kendunlop | 12:e950feba910d | 200 | } |
kendunlop | 12:e950feba910d | 201 | } |
kendunlop | 12:e950feba910d | 202 | |
kendunlop | 2:11339018dda6 | 203 | //A function to deal with each CAN message part (e.g.: 301, 8, 01, 02...) |
kendunlop | 1:19d183cf2689 | 204 | void dealwithpart(void) |
kendunlop | 1:19d183cf2689 | 205 | { |
kendunlop | 11:abd3df435a2b | 206 | hextotal = 0; |
kendunlop | 1:19d183cf2689 | 207 | partincrement = partincrement + 1; |
kendunlop | 1:19d183cf2689 | 208 | //pc.printf("Dealing with part %d. (%s)\r\n", partincrement, part); |
kendunlop | 1:19d183cf2689 | 209 | //int partincrementb = 0; |
kendunlop | 1:19d183cf2689 | 210 | int textlength = part.length(); |
kendunlop | 1:19d183cf2689 | 211 | //int characterinc = 0; |
kendunlop | 1:19d183cf2689 | 212 | //pc.printf("That's %d characters long.\r\n", textlength); |
kendunlop | 1:19d183cf2689 | 213 | for (int i = 0; i < part.size(); i++) |
kendunlop | 1:19d183cf2689 | 214 | { |
kendunlop | 1:19d183cf2689 | 215 | //pc.printf("Examining character %d/%d.\r\n", (i+1), textlength); |
kendunlop | 1:19d183cf2689 | 216 | char individualcharacter = part.at(i); |
kendunlop | 1:19d183cf2689 | 217 | //pc.printf("That's '%c'.\r\n", individualcharacter); |
kendunlop | 1:19d183cf2689 | 218 | int numberized = 0; |
kendunlop | 1:19d183cf2689 | 219 | if(isdigit(individualcharacter)) |
kendunlop | 1:19d183cf2689 | 220 | { |
kendunlop | 1:19d183cf2689 | 221 | //pc.printf("That character is a digit.\r\n"); |
kendunlop | 1:19d183cf2689 | 222 | numberized = individualcharacter - '0'; |
kendunlop | 1:19d183cf2689 | 223 | //pc.printf("Numberized that's '%d'.\r\n", numberized); |
kendunlop | 1:19d183cf2689 | 224 | } |
kendunlop | 1:19d183cf2689 | 225 | else |
kendunlop | 1:19d183cf2689 | 226 | { |
kendunlop | 1:19d183cf2689 | 227 | //pc.printf("That character is NOT a digit.\r\n"); |
kendunlop | 1:19d183cf2689 | 228 | int asciivalue = toupper(individualcharacter); |
kendunlop | 1:19d183cf2689 | 229 | //pc.printf("Ascii value is %d.\r\n", asciivalue); |
kendunlop | 1:19d183cf2689 | 230 | numberized = asciivalue - 55; |
kendunlop | 1:19d183cf2689 | 231 | //pc.printf("Hex value is %d.\r\n", numberized); |
kendunlop | 1:19d183cf2689 | 232 | } |
kendunlop | 1:19d183cf2689 | 233 | //pc.printf("Eventual numberization is %d.\r\n", numberized); |
kendunlop | 1:19d183cf2689 | 234 | //pc.printf("Hex total is now %d.\r\n", hextotal); |
kendunlop | 1:19d183cf2689 | 235 | int powertoraise = part.size() - (i+1); |
kendunlop | 1:19d183cf2689 | 236 | //pc.printf("Must multiply by 16 to the power of %d.\r\n", powertoraise); |
kendunlop | 1:19d183cf2689 | 237 | int amounttoadd = numberized; |
kendunlop | 1:19d183cf2689 | 238 | //pc.printf("powertoraise is '%d'.\r\n", powertoraise); |
kendunlop | 1:19d183cf2689 | 239 | if (powertoraise == 1) |
kendunlop | 1:19d183cf2689 | 240 | { |
kendunlop | 1:19d183cf2689 | 241 | amounttoadd = numberized * 16; |
kendunlop | 1:19d183cf2689 | 242 | //pc.printf("Multiplying by 16.\r\n"); |
kendunlop | 1:19d183cf2689 | 243 | //pc.printf("powertoraise is '%d'.\r\n", powertoraise); |
kendunlop | 1:19d183cf2689 | 244 | } |
kendunlop | 1:19d183cf2689 | 245 | if (powertoraise == 2) |
kendunlop | 1:19d183cf2689 | 246 | { |
kendunlop | 1:19d183cf2689 | 247 | amounttoadd = numberized * 256; |
kendunlop | 1:19d183cf2689 | 248 | //pc.printf("Multiplying by 256.\r\n"); |
kendunlop | 1:19d183cf2689 | 249 | } |
kendunlop | 1:19d183cf2689 | 250 | //pc.printf("Amount to add is therefore %d.\r\n", amounttoadd); |
kendunlop | 1:19d183cf2689 | 251 | hextotal = hextotal + amounttoadd; |
kendunlop | 1:19d183cf2689 | 252 | //pc.printf("hextotal so far for this part is therefore %d.\r\n", hextotal); |
kendunlop | 1:19d183cf2689 | 253 | } |
kendunlop | 1:19d183cf2689 | 254 | //pc.printf("hextotal for whole part is therefore %d.\r\n", hextotal); |
kendunlop | 1:19d183cf2689 | 255 | //pc.printf("Need to convert that into true hex.\r\n"); |
kendunlop | 1:19d183cf2689 | 256 | std::stringstream sstream; |
kendunlop | 1:19d183cf2689 | 257 | sstream << std::hex << hextotal; |
kendunlop | 1:19d183cf2689 | 258 | //pc.printf("StringSteam says '%s'.\r\n", sstream.str()); |
kendunlop | 1:19d183cf2689 | 259 | if (partincrement == 1) |
kendunlop | 7:a9150dc1e481 | 260 | { |
kendunlop | 11:abd3df435a2b | 261 | if (hextotal > 0xFFF) |
kendunlop | 11:abd3df435a2b | 262 | { |
kendunlop | 11:abd3df435a2b | 263 | hextotal = 0xFFF; |
kendunlop | 11:abd3df435a2b | 264 | } |
kendunlop | 7:a9150dc1e481 | 265 | messageOut1.id = hextotal; |
kendunlop | 7:a9150dc1e481 | 266 | expected1 = hextotal; |
kendunlop | 11:abd3df435a2b | 267 | if (customentry == 1) |
kendunlop | 11:abd3df435a2b | 268 | { |
kendunlop | 11:abd3df435a2b | 269 | idlistincrementer = hextotal; |
kendunlop | 11:abd3df435a2b | 270 | } |
kendunlop | 8:6f096b45ca15 | 271 | //For IDs, check if the ID is represented in the idsarray. |
kendunlop | 8:6f096b45ca15 | 272 | shouldntcomein = 0; |
kendunlop | 9:7c27efe30a77 | 273 | if (mbedCANbusspeed != CANGatewayCANbusspeed) |
kendunlop | 9:7c27efe30a77 | 274 | { |
kendunlop | 9:7c27efe30a77 | 275 | shouldntcomein = 1; |
kendunlop | 9:7c27efe30a77 | 276 | } |
kendunlop | 11:abd3df435a2b | 277 | idwasfound = 0; |
kendunlop | 8:6f096b45ca15 | 278 | if (CANpassthrough == 0) |
kendunlop | 8:6f096b45ca15 | 279 | { |
kendunlop | 12:e950feba910d | 280 | for(int i = 0; i <=CANconversionlistheight-1; i++) |
kendunlop | 8:6f096b45ca15 | 281 | { |
kendunlop | 12:e950feba910d | 282 | if (CANconversionlist[i].IDfrom == expected1) |
kendunlop | 8:6f096b45ca15 | 283 | { |
kendunlop | 11:abd3df435a2b | 284 | //pc.printf("ID was found in the array at %d.\r\n", i); |
kendunlop | 8:6f096b45ca15 | 285 | idwasfound = 1; |
kendunlop | 11:abd3df435a2b | 286 | //pc.printf("IDwasfound is now '%d'.\r\n", idwasfound); |
kendunlop | 11:abd3df435a2b | 287 | columnidwasfound = i; |
kendunlop | 8:6f096b45ca15 | 288 | int originalexpectedvalue = expected1; |
kendunlop | 9:7c27efe30a77 | 289 | //Get string of hex code for original expected value (ID code found in array) |
kendunlop | 9:7c27efe30a77 | 290 | std::stringstream sstream; |
kendunlop | 9:7c27efe30a77 | 291 | sstream << std::hex << originalexpectedvalue; |
kendunlop | 9:7c27efe30a77 | 292 | string tempstring = sstream.str(); |
kendunlop | 9:7c27efe30a77 | 293 | //Get string of hex code for value to adjust to |
kendunlop | 9:7c27efe30a77 | 294 | std::stringstream sstream2; |
kendunlop | 9:7c27efe30a77 | 295 | sstream2 << std::hex << expected1; |
kendunlop | 9:7c27efe30a77 | 296 | string tempstring2 = sstream2.str(); |
kendunlop | 9:7c27efe30a77 | 297 | //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 | 298 | } |
kendunlop | 8:6f096b45ca15 | 299 | } |
kendunlop | 8:6f096b45ca15 | 300 | if (idwasfound == 0 and CANpassthrough == 0) |
kendunlop | 8:6f096b45ca15 | 301 | { |
kendunlop | 8:6f096b45ca15 | 302 | //pc.printf("CANpassthrough is %d.\r\n", CANpassthrough); |
kendunlop | 8:6f096b45ca15 | 303 | shouldntcomein = 1; |
kendunlop | 11:abd3df435a2b | 304 | //pc.printf("ID of %d was never found, expecting it NOT to come in.\r\n", expected1); |
kendunlop | 8:6f096b45ca15 | 305 | } |
kendunlop | 9:7c27efe30a77 | 306 | if (shouldntcomein == 0 and unexpectedCANstreak > 0) //If data is expected to come in, this ends any unexpected-CAN streak. |
kendunlop | 9:7c27efe30a77 | 307 | { |
kendunlop | 9:7c27efe30a77 | 308 | 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 | 309 | { |
kendunlop | 11:abd3df435a2b | 310 | //pc.printf("\r\n(FrombreakingunexpectedCANstreak 2)\n"); |
kendunlop | 9:7c27efe30a77 | 311 | pc.printf("\r\n\n"); |
kendunlop | 9:7c27efe30a77 | 312 | } |
kendunlop | 9:7c27efe30a77 | 313 | unexpectedCANstreak = 0; |
kendunlop | 9:7c27efe30a77 | 314 | unexpectedCANstreakstartid = 0; |
kendunlop | 9:7c27efe30a77 | 315 | } |
kendunlop | 8:6f096b45ca15 | 316 | } |
kendunlop | 7:a9150dc1e481 | 317 | } |
kendunlop | 1:19d183cf2689 | 318 | if (partincrement == 2) |
kendunlop | 7:a9150dc1e481 | 319 | { |
kendunlop | 7:a9150dc1e481 | 320 | messageOut1.len = hextotal; |
kendunlop | 7:a9150dc1e481 | 321 | expected2 = hextotal; |
kendunlop | 7:a9150dc1e481 | 322 | } |
kendunlop | 1:19d183cf2689 | 323 | if (partincrement >= 3) |
kendunlop | 7:a9150dc1e481 | 324 | { |
kendunlop | 7:a9150dc1e481 | 325 | messageOut1.data[partincrement-3] = hextotal; |
kendunlop | 7:a9150dc1e481 | 326 | } |
kendunlop | 7:a9150dc1e481 | 327 | if (partincrement == 3) |
kendunlop | 7:a9150dc1e481 | 328 | { |
kendunlop | 7:a9150dc1e481 | 329 | expected3 = hextotal; |
kendunlop | 7:a9150dc1e481 | 330 | } |
kendunlop | 7:a9150dc1e481 | 331 | if (partincrement == 4) |
kendunlop | 7:a9150dc1e481 | 332 | { |
kendunlop | 7:a9150dc1e481 | 333 | expected4 = hextotal; |
kendunlop | 7:a9150dc1e481 | 334 | } |
kendunlop | 7:a9150dc1e481 | 335 | if (partincrement == 5) |
kendunlop | 7:a9150dc1e481 | 336 | { |
kendunlop | 7:a9150dc1e481 | 337 | expected5 = hextotal; |
kendunlop | 7:a9150dc1e481 | 338 | } |
kendunlop | 7:a9150dc1e481 | 339 | if (partincrement == 6) |
kendunlop | 7:a9150dc1e481 | 340 | { |
kendunlop | 7:a9150dc1e481 | 341 | expected6 = hextotal; |
kendunlop | 7:a9150dc1e481 | 342 | } |
kendunlop | 7:a9150dc1e481 | 343 | if (partincrement == 7) |
kendunlop | 7:a9150dc1e481 | 344 | { |
kendunlop | 7:a9150dc1e481 | 345 | expected7 = hextotal; |
kendunlop | 7:a9150dc1e481 | 346 | } |
kendunlop | 7:a9150dc1e481 | 347 | if (partincrement == 8) |
kendunlop | 7:a9150dc1e481 | 348 | { |
kendunlop | 7:a9150dc1e481 | 349 | expected8 = hextotal; |
kendunlop | 7:a9150dc1e481 | 350 | } |
kendunlop | 7:a9150dc1e481 | 351 | if (partincrement == 9) |
kendunlop | 7:a9150dc1e481 | 352 | { |
kendunlop | 7:a9150dc1e481 | 353 | expected9 = hextotal; |
kendunlop | 7:a9150dc1e481 | 354 | } |
kendunlop | 7:a9150dc1e481 | 355 | if (partincrement == 10) |
kendunlop | 7:a9150dc1e481 | 356 | { |
kendunlop | 7:a9150dc1e481 | 357 | expected10 = hextotal; |
kendunlop | 7:a9150dc1e481 | 358 | } |
kendunlop | 11:abd3df435a2b | 359 | //pc.printf("IDwasfound says '%d'.\r\n", idwasfound); |
kendunlop | 11:abd3df435a2b | 360 | if (idwasfound == 1 and partincrement == 10)//If the ID was found in the table earlier, use it as the basis for the expected values instead. |
kendunlop | 11:abd3df435a2b | 361 | { |
kendunlop | 11:abd3df435a2b | 362 | //pc.printf("ID was found in table earlier, setting expectations\r\n"); |
kendunlop | 11:abd3df435a2b | 363 | int i = columnidwasfound; |
kendunlop | 12:e950feba910d | 364 | expected1 = CANconversionlist[i].IDto; |
kendunlop | 12:e950feba910d | 365 | expected2 = CANconversionlist[i].length; |
kendunlop | 12:e950feba910d | 366 | expected3 = CANconversionlist[i].Data[0]; |
kendunlop | 12:e950feba910d | 367 | expected4 = CANconversionlist[i].Data[1]; |
kendunlop | 12:e950feba910d | 368 | expected5 = CANconversionlist[i].Data[2]; |
kendunlop | 12:e950feba910d | 369 | expected6 = CANconversionlist[i].Data[3]; |
kendunlop | 12:e950feba910d | 370 | expected7 = CANconversionlist[i].Data[4]; |
kendunlop | 12:e950feba910d | 371 | expected8 = CANconversionlist[i].Data[5]; |
kendunlop | 12:e950feba910d | 372 | expected9 = CANconversionlist[i].Data[6]; |
kendunlop | 12:e950feba910d | 373 | expected10 = CANconversionlist[i].Data[7]; |
kendunlop | 11:abd3df435a2b | 374 | } |
kendunlop | 1:19d183cf2689 | 375 | //pc.printf("Part %d complete.\r\n", partincrement); |
kendunlop | 1:19d183cf2689 | 376 | //pc.printf("--------------------------------------\r\n"); |
kendunlop | 1:19d183cf2689 | 377 | } |
kendunlop | 1:19d183cf2689 | 378 | |
kendunlop | 13:e3a4abc461f4 | 379 | //A function to go through each row in the string CAN pairs list and turn them into data for the CAN conversion list. |
kendunlop | 13:e3a4abc461f4 | 380 | void gothroughCANpairslist(void) |
kendunlop | 13:e3a4abc461f4 | 381 | { |
kendunlop | 13:e3a4abc461f4 | 382 | for (int i = 0; i < 30; i++) |
kendunlop | 13:e3a4abc461f4 | 383 | { |
kendunlop | 13:e3a4abc461f4 | 384 | string tempstringA = CANpairslist[i].IDfrom; |
kendunlop | 13:e3a4abc461f4 | 385 | string tempstringB = CANpairslist[i].IDmessageto; |
kendunlop | 13:e3a4abc461f4 | 386 | //pc.printf("Strings found are '%s' and '%s'.\r\n", tempstringA, tempstringB); |
kendunlop | 13:e3a4abc461f4 | 387 | //pc.printf("Will try to get an ID from out of '%s'.\r\n", tempstringA); |
kendunlop | 13:e3a4abc461f4 | 388 | if (tempstringA != "" and tempstringB != "")//Only get a number from the part if tempstring A and B have something in them. |
kendunlop | 13:e3a4abc461f4 | 389 | { |
kendunlop | 13:e3a4abc461f4 | 390 | CANconversionlistheight = i+1; |
kendunlop | 13:e3a4abc461f4 | 391 | partincrement = 0; |
kendunlop | 13:e3a4abc461f4 | 392 | part = tempstringA; |
kendunlop | 13:e3a4abc461f4 | 393 | dealwithpart(); |
kendunlop | 13:e3a4abc461f4 | 394 | //pc.printf("Hextotal obtained is %d.\r\n", hextotal); |
kendunlop | 13:e3a4abc461f4 | 395 | //pc.printf("partincrement is now %d.\r\n", partincrement); |
kendunlop | 13:e3a4abc461f4 | 396 | CANconversionlist[i].IDfrom = hextotal; |
kendunlop | 13:e3a4abc461f4 | 397 | partincrement = 0; |
kendunlop | 13:e3a4abc461f4 | 398 | part = tempstringB.substr(0,3); |
kendunlop | 13:e3a4abc461f4 | 399 | dealwithpart(); |
kendunlop | 13:e3a4abc461f4 | 400 | //pc.printf("Hextotal obtained is %d.\r\n", hextotal); |
kendunlop | 13:e3a4abc461f4 | 401 | CANconversionlist[i].IDto = hextotal; |
kendunlop | 13:e3a4abc461f4 | 402 | part = tempstringB.substr(4,1); |
kendunlop | 13:e3a4abc461f4 | 403 | dealwithpart(); |
kendunlop | 13:e3a4abc461f4 | 404 | //pc.printf("Hextotal obtained is %d.\r\n", hextotal); |
kendunlop | 13:e3a4abc461f4 | 405 | CANconversionlist[i].length = hextotal; |
kendunlop | 13:e3a4abc461f4 | 406 | part = tempstringB.substr(6,2); |
kendunlop | 13:e3a4abc461f4 | 407 | dealwithpart(); |
kendunlop | 13:e3a4abc461f4 | 408 | //pc.printf("Hextotal obtained is %d.\r\n", hextotal); |
kendunlop | 13:e3a4abc461f4 | 409 | CANconversionlist[i].Data [0] = hextotal; |
kendunlop | 13:e3a4abc461f4 | 410 | part = tempstringB.substr(9,2); |
kendunlop | 13:e3a4abc461f4 | 411 | dealwithpart(); |
kendunlop | 13:e3a4abc461f4 | 412 | //pc.printf("Hextotal obtained is %d.\r\n", hextotal); |
kendunlop | 13:e3a4abc461f4 | 413 | CANconversionlist[i].Data [1] = hextotal; |
kendunlop | 13:e3a4abc461f4 | 414 | part = tempstringB.substr(12,2); |
kendunlop | 13:e3a4abc461f4 | 415 | dealwithpart(); |
kendunlop | 13:e3a4abc461f4 | 416 | //pc.printf("Hextotal obtained is %d.\r\n", hextotal); |
kendunlop | 13:e3a4abc461f4 | 417 | CANconversionlist[i].Data [2] = hextotal; |
kendunlop | 13:e3a4abc461f4 | 418 | part = tempstringB.substr(15,2); |
kendunlop | 13:e3a4abc461f4 | 419 | dealwithpart(); |
kendunlop | 13:e3a4abc461f4 | 420 | //pc.printf("Hextotal obtained is %d.\r\n", hextotal); |
kendunlop | 13:e3a4abc461f4 | 421 | CANconversionlist[i].Data [3] = hextotal; |
kendunlop | 13:e3a4abc461f4 | 422 | part = tempstringB.substr(18,2); |
kendunlop | 13:e3a4abc461f4 | 423 | dealwithpart(); |
kendunlop | 13:e3a4abc461f4 | 424 | //pc.printf("Hextotal obtained is %d.\r\n", hextotal); |
kendunlop | 13:e3a4abc461f4 | 425 | CANconversionlist[i].Data [4] = hextotal; |
kendunlop | 13:e3a4abc461f4 | 426 | part = tempstringB.substr(21,2); |
kendunlop | 13:e3a4abc461f4 | 427 | dealwithpart(); |
kendunlop | 13:e3a4abc461f4 | 428 | //pc.printf("Hextotal obtained is %d.\r\n", hextotal); |
kendunlop | 13:e3a4abc461f4 | 429 | CANconversionlist[i].Data [5] = hextotal; |
kendunlop | 13:e3a4abc461f4 | 430 | part = tempstringB.substr(24,2); |
kendunlop | 13:e3a4abc461f4 | 431 | dealwithpart(); |
kendunlop | 13:e3a4abc461f4 | 432 | //pc.printf("Hextotal obtained is %d.\r\n", hextotal); |
kendunlop | 13:e3a4abc461f4 | 433 | CANconversionlist[i].Data [6] = hextotal; |
kendunlop | 13:e3a4abc461f4 | 434 | part = tempstringB.substr(27,2); |
kendunlop | 13:e3a4abc461f4 | 435 | dealwithpart(); |
kendunlop | 13:e3a4abc461f4 | 436 | //pc.printf("Hextotal obtained is %d.\r\n", hextotal); |
kendunlop | 13:e3a4abc461f4 | 437 | CANconversionlist[i].Data [7] = hextotal; |
kendunlop | 13:e3a4abc461f4 | 438 | } |
kendunlop | 13:e3a4abc461f4 | 439 | } |
kendunlop | 13:e3a4abc461f4 | 440 | } |
kendunlop | 13:e3a4abc461f4 | 441 | |
kendunlop | 13:e3a4abc461f4 | 442 | //A function to handle key presses in tables mode |
kendunlop | 13:e3a4abc461f4 | 443 | void tablesmodekeypress(void) |
kendunlop | 13:e3a4abc461f4 | 444 | { |
kendunlop | 13:e3a4abc461f4 | 445 | //pc.printf("Key press detected in tables mode!\r\n"); |
kendunlop | 13:e3a4abc461f4 | 446 | if (c >= 48 and c <= 57) |
kendunlop | 13:e3a4abc461f4 | 447 | { |
kendunlop | 13:e3a4abc461f4 | 448 | numberkeypressed = c - 48; |
kendunlop | 13:e3a4abc461f4 | 449 | pc.printf("That's a number key press.('%d')\r\n", numberkeypressed); |
kendunlop | 13:e3a4abc461f4 | 450 | resetCANpairslist(); |
kendunlop | 13:e3a4abc461f4 | 451 | if (numberkeypressed == 1) |
kendunlop | 13:e3a4abc461f4 | 452 | { |
kendunlop | 13:e3a4abc461f4 | 453 | CANpairslist[0].IDfrom = "301"; |
kendunlop | 13:e3a4abc461f4 | 454 | CANpairslist[0].IDmessageto = "301 8 08 00 00 00 00 00 00 00"; |
kendunlop | 13:e3a4abc461f4 | 455 | CANpairslist[1].IDfrom = "302"; |
kendunlop | 13:e3a4abc461f4 | 456 | CANpairslist[1].IDmessageto = "302 8 06 07 00 00 00 00 00 00"; |
kendunlop | 13:e3a4abc461f4 | 457 | CANpairslist[2].IDfrom = "303"; |
kendunlop | 13:e3a4abc461f4 | 458 | CANpairslist[2].IDmessageto = "303 8 00 00 00 00 00 00 00 00"; |
kendunlop | 13:e3a4abc461f4 | 459 | CANpairslist[3].IDfrom = "304"; |
kendunlop | 13:e3a4abc461f4 | 460 | CANpairslist[3].IDmessageto = "304 8 08 00 00 00 00 00 00 00"; |
kendunlop | 13:e3a4abc461f4 | 461 | CANpairslist[4].IDfrom = "305"; |
kendunlop | 13:e3a4abc461f4 | 462 | CANpairslist[4].IDmessageto = "305 8 08 00 00 00 00 00 00 00"; |
kendunlop | 13:e3a4abc461f4 | 463 | CANpairslist[5].IDfrom = "306"; |
kendunlop | 13:e3a4abc461f4 | 464 | CANpairslist[5].IDmessageto = "306 8 02 03 00 00 00 00 00 00"; |
kendunlop | 13:e3a4abc461f4 | 465 | CANpairslist[6].IDfrom = "307"; |
kendunlop | 13:e3a4abc461f4 | 466 | CANpairslist[6].IDmessageto = "307 8 06 07 08 00 00 00 00 00"; |
kendunlop | 13:e3a4abc461f4 | 467 | CANpairslist[7].IDfrom = "309"; |
kendunlop | 13:e3a4abc461f4 | 468 | CANpairslist[7].IDmessageto = "308 8 06 07 08 00 00 00 00 00"; |
kendunlop | 13:e3a4abc461f4 | 469 | } |
kendunlop | 13:e3a4abc461f4 | 470 | if (numberkeypressed == 2) |
kendunlop | 13:e3a4abc461f4 | 471 | { |
kendunlop | 13:e3a4abc461f4 | 472 | CANpairslist[0].IDfrom = "301"; |
kendunlop | 13:e3a4abc461f4 | 473 | CANpairslist[0].IDmessageto = "401 8 08 00 00 00 04 05 06 01"; |
kendunlop | 13:e3a4abc461f4 | 474 | CANpairslist[1].IDfrom = "302"; |
kendunlop | 13:e3a4abc461f4 | 475 | CANpairslist[1].IDmessageto = "402 8 04 05 06 07 06 07 08 00"; |
kendunlop | 13:e3a4abc461f4 | 476 | CANpairslist[2].IDfrom = "303"; |
kendunlop | 13:e3a4abc461f4 | 477 | CANpairslist[2].IDmessageto = "403 8 03 04 05 05 06 80 00 00"; |
kendunlop | 13:e3a4abc461f4 | 478 | CANpairslist[3].IDfrom = "304"; |
kendunlop | 13:e3a4abc461f4 | 479 | CANpairslist[3].IDmessageto = "404 8 04 05 06 07 06 07 08 00"; |
kendunlop | 13:e3a4abc461f4 | 480 | CANpairslist[4].IDfrom = "305"; |
kendunlop | 13:e3a4abc461f4 | 481 | CANpairslist[4].IDmessageto = "405 8 04 05 06 07 06 07 08 00"; |
kendunlop | 13:e3a4abc461f4 | 482 | CANpairslist[5].IDfrom = "306"; |
kendunlop | 13:e3a4abc461f4 | 483 | CANpairslist[5].IDmessageto = "406 8 02 03 00 00 00 00 00 00"; |
kendunlop | 13:e3a4abc461f4 | 484 | CANpairslist[6].IDfrom = "308"; |
kendunlop | 13:e3a4abc461f4 | 485 | CANpairslist[6].IDmessageto = "408 8 06 07 08 00 00 00 00 00"; |
kendunlop | 13:e3a4abc461f4 | 486 | CANpairslist[7].IDfrom = "309"; |
kendunlop | 13:e3a4abc461f4 | 487 | CANpairslist[7].IDmessageto = "409 8 06 07 08 00 00 00 00 00"; |
kendunlop | 13:e3a4abc461f4 | 488 | } |
kendunlop | 13:e3a4abc461f4 | 489 | if (numberkeypressed == 3) |
kendunlop | 13:e3a4abc461f4 | 490 | { |
kendunlop | 13:e3a4abc461f4 | 491 | CANpairslist[0].IDfrom = "317"; |
kendunlop | 13:e3a4abc461f4 | 492 | CANpairslist[0].IDmessageto = "40A 8 08 00 00 00 04 05 06 07"; |
kendunlop | 13:e3a4abc461f4 | 493 | CANpairslist[1].IDfrom = "318"; |
kendunlop | 13:e3a4abc461f4 | 494 | CANpairslist[1].IDmessageto = "409 8 08 00 00 00 04 05 06 07"; |
kendunlop | 13:e3a4abc461f4 | 495 | CANpairslist[2].IDfrom = "319"; |
kendunlop | 13:e3a4abc461f4 | 496 | CANpairslist[2].IDmessageto = "408 8 06 07 02 03 04 05 00 00"; |
kendunlop | 13:e3a4abc461f4 | 497 | CANpairslist[3].IDfrom = "31A"; |
kendunlop | 13:e3a4abc461f4 | 498 | CANpairslist[3].IDmessageto = "407 8 08 00 00 00 04 05 02 03"; |
kendunlop | 13:e3a4abc461f4 | 499 | CANpairslist[4].IDfrom = "31B"; |
kendunlop | 13:e3a4abc461f4 | 500 | CANpairslist[4].IDmessageto = "40C 8 04 05 06 07 02 03 08 00"; |
kendunlop | 13:e3a4abc461f4 | 501 | CANpairslist[5].IDfrom = "31C"; |
kendunlop | 13:e3a4abc461f4 | 502 | CANpairslist[5].IDmessageto = "40B 8 06 07 04 05 02 03 00 00"; |
kendunlop | 13:e3a4abc461f4 | 503 | CANpairslist[6].IDfrom = "31D"; |
kendunlop | 13:e3a4abc461f4 | 504 | CANpairslist[6].IDmessageto = "406 8 08 00 00 00 04 05 06 07"; |
kendunlop | 13:e3a4abc461f4 | 505 | CANpairslist[7].IDfrom = "31E"; |
kendunlop | 13:e3a4abc461f4 | 506 | CANpairslist[7].IDmessageto = "405 8 04 05 06 07 00 00 00 00"; |
kendunlop | 13:e3a4abc461f4 | 507 | CANpairslist[8].IDfrom = "324"; |
kendunlop | 13:e3a4abc461f4 | 508 | CANpairslist[8].IDmessageto = "403 8 06 07 08 07 00 00 00 00"; |
kendunlop | 13:e3a4abc461f4 | 509 | CANpairslist[9].IDfrom = "325"; |
kendunlop | 13:e3a4abc461f4 | 510 | CANpairslist[9].IDmessageto = "404 8 04 05 06 07 00 00 00 00"; |
kendunlop | 13:e3a4abc461f4 | 511 | CANpairslist[10].IDfrom = "329"; |
kendunlop | 13:e3a4abc461f4 | 512 | CANpairslist[10].IDmessageto = "402 8 08 00 00 00 04 05 06 07"; |
kendunlop | 13:e3a4abc461f4 | 513 | CANpairslist[11].IDfrom = "32A"; |
kendunlop | 13:e3a4abc461f4 | 514 | CANpairslist[11].IDmessageto = "401 8 04 05 02 03 06 05 00 00"; |
kendunlop | 13:e3a4abc461f4 | 515 | } |
kendunlop | 13:e3a4abc461f4 | 516 | gothroughCANpairslist(); |
kendunlop | 13:e3a4abc461f4 | 517 | pc.printf("CAN conversion list currently has %d rows:\r\n", CANconversionlistheight); |
kendunlop | 13:e3a4abc461f4 | 518 | int i = 0; |
kendunlop | 13:e3a4abc461f4 | 519 | while (i < CANconversionlistheight) |
kendunlop | 13:e3a4abc461f4 | 520 | { |
kendunlop | 13:e3a4abc461f4 | 521 | //pc.printf("Printing row %d: ", i+1); |
kendunlop | 13:e3a4abc461f4 | 522 | pc.printf("ID \033[1;37m%03X\033[0m will convert to: \033[1;30;47m%03X %01X %02X %02X %02X %02X %02X %02X %02X %02X\033[0m", CANconversionlist[i].IDfrom, CANconversionlist[i].IDto, CANconversionlist[i].length, CANconversionlist[i].Data[0], CANconversionlist[i].Data[1], CANconversionlist[i].Data[2], CANconversionlist[i].Data[3], CANconversionlist[i].Data[4], CANconversionlist[i].Data[5], CANconversionlist[i].Data[6], CANconversionlist[i].Data[7]); |
kendunlop | 13:e3a4abc461f4 | 523 | pc.printf("\r\n"); |
kendunlop | 13:e3a4abc461f4 | 524 | i++; |
kendunlop | 13:e3a4abc461f4 | 525 | } |
kendunlop | 13:e3a4abc461f4 | 526 | CANpassthrough = 0; |
kendunlop | 13:e3a4abc461f4 | 527 | pc.printf("CANpassthrough is now set to %d.\r\n", CANpassthrough); |
kendunlop | 13:e3a4abc461f4 | 528 | numberkeypressed = -1; |
kendunlop | 13:e3a4abc461f4 | 529 | } |
kendunlop | 13:e3a4abc461f4 | 530 | if (c == 27) |
kendunlop | 13:e3a4abc461f4 | 531 | { |
kendunlop | 13:e3a4abc461f4 | 532 | pc.printf("Leaving Expectation Table mode.\r\n"); |
kendunlop | 13:e3a4abc461f4 | 533 | normalkeypresses = 1; |
kendunlop | 13:e3a4abc461f4 | 534 | tablesmode = 0; |
kendunlop | 13:e3a4abc461f4 | 535 | } |
kendunlop | 13:e3a4abc461f4 | 536 | } |
kendunlop | 13:e3a4abc461f4 | 537 | |
kendunlop | 13:e3a4abc461f4 | 538 | //A function to report if baud rates differ |
kendunlop | 13:e3a4abc461f4 | 539 | void reportbaudrates(void) |
kendunlop | 13:e3a4abc461f4 | 540 | { |
kendunlop | 13:e3a4abc461f4 | 541 | if (mbedCANbusspeed != CANGatewayCANbusspeed) |
kendunlop | 13:e3a4abc461f4 | 542 | { |
kendunlop | 13:e3a4abc461f4 | 543 | pc.printf("Mbed baud rate (%d) is different from assumed CAN Gateway baud rate (%d), will expect no CAN.\r\n", (mbedCANbusspeed / 1000), (CANGatewayCANbusspeed / 1000)); |
kendunlop | 13:e3a4abc461f4 | 544 | } |
kendunlop | 13:e3a4abc461f4 | 545 | } |
kendunlop | 13:e3a4abc461f4 | 546 | |
kendunlop | 13:e3a4abc461f4 | 547 | //A function to handle when CAN-expectations are not fulfilled |
kendunlop | 13:e3a4abc461f4 | 548 | void expectationwasnotfulfilled(void) |
kendunlop | 13:e3a4abc461f4 | 549 | { |
kendunlop | 13:e3a4abc461f4 | 550 | //pc.printf("Expectation was not fulfilled for that message!\r\n"); |
kendunlop | 13:e3a4abc461f4 | 551 | } |
kendunlop | 13:e3a4abc461f4 | 552 | |
kendunlop | 13:e3a4abc461f4 | 553 | //A function to clear all test-related variables |
kendunlop | 13:e3a4abc461f4 | 554 | void resetCANbuses()//A quick method to reset both CAN buses conveniently |
kendunlop | 13:e3a4abc461f4 | 555 | { |
kendunlop | 13:e3a4abc461f4 | 556 | CanBus.reset(); // Clear any bus errors. Will this stop the 'cached' CAN sticking around? |
kendunlop | 13:e3a4abc461f4 | 557 | CanBus2.reset(); // Clear any bus errors. Will this stop the 'cached' CAN sticking around? |
kendunlop | 13:e3a4abc461f4 | 558 | } |
kendunlop | 13:e3a4abc461f4 | 559 | |
kendunlop | 13:e3a4abc461f4 | 560 | void resettest(void) |
kendunlop | 13:e3a4abc461f4 | 561 | { |
kendunlop | 13:e3a4abc461f4 | 562 | //pc.printf("Resetting test.\r\n"); |
kendunlop | 13:e3a4abc461f4 | 563 | listen = 1; |
kendunlop | 13:e3a4abc461f4 | 564 | failsthistest = 0; |
kendunlop | 13:e3a4abc461f4 | 565 | failsstreak = 0; |
kendunlop | 13:e3a4abc461f4 | 566 | failsstreakstart = 0; |
kendunlop | 13:e3a4abc461f4 | 567 | spamcount = 0; |
kendunlop | 13:e3a4abc461f4 | 568 | noreplyfailsthistest = 0; |
kendunlop | 13:e3a4abc461f4 | 569 | goodnoreplies = 0; |
kendunlop | 13:e3a4abc461f4 | 570 | goodnoreplystreak = 0; |
kendunlop | 13:e3a4abc461f4 | 571 | goodnoreplystreakstartid = 0; |
kendunlop | 13:e3a4abc461f4 | 572 | noreplystreak = 0; |
kendunlop | 13:e3a4abc461f4 | 573 | noreplystreakstartid = 0; |
kendunlop | 13:e3a4abc461f4 | 574 | CANmatchstreak = 0; |
kendunlop | 13:e3a4abc461f4 | 575 | CANmatchstreakstartID = 0; |
kendunlop | 13:e3a4abc461f4 | 576 | mismatchfailsthistest = 0; |
kendunlop | 13:e3a4abc461f4 | 577 | matchesthistest = 0; |
kendunlop | 13:e3a4abc461f4 | 578 | idscheckedcount = 0; |
kendunlop | 13:e3a4abc461f4 | 579 | unexpectedCANfails = 0; |
kendunlop | 13:e3a4abc461f4 | 580 | unexpectedCANstreak = 0; |
kendunlop | 13:e3a4abc461f4 | 581 | unexpectedCANstreakstartid = 0; |
kendunlop | 13:e3a4abc461f4 | 582 | messagesincount = 0; |
kendunlop | 13:e3a4abc461f4 | 583 | testwasaborted = 0; |
kendunlop | 13:e3a4abc461f4 | 584 | } |
kendunlop | 13:e3a4abc461f4 | 585 | |
kendunlop | 13:e3a4abc461f4 | 586 | //A function to add a carriage return in the output if the system has been writing out the CAN messages it sends. |
kendunlop | 13:e3a4abc461f4 | 587 | void addnewlineifneeded(void) |
kendunlop | 13:e3a4abc461f4 | 588 | { |
kendunlop | 13:e3a4abc461f4 | 589 | if (addnewlinetonextmessage == 1) |
kendunlop | 13:e3a4abc461f4 | 590 | { |
kendunlop | 13:e3a4abc461f4 | 591 | pc.printf("\r\n"); |
kendunlop | 13:e3a4abc461f4 | 592 | addnewlinetonextmessage = 0; |
kendunlop | 13:e3a4abc461f4 | 593 | } |
kendunlop | 13:e3a4abc461f4 | 594 | } |
kendunlop | 13:e3a4abc461f4 | 595 | |
kendunlop | 13:e3a4abc461f4 | 596 | void reportsentCAN() //A function to print out the CAN that was sent. Messages can only be positioned correctly once their results are known. |
kendunlop | 13:e3a4abc461f4 | 597 | { |
kendunlop | 13:e3a4abc461f4 | 598 | pc.printf("\033[1;33m\rSent out : %s \033[0m", messageOutTextWithSpaces); |
kendunlop | 13:e3a4abc461f4 | 599 | } |
kendunlop | 13:e3a4abc461f4 | 600 | |
kendunlop | 13:e3a4abc461f4 | 601 | |
kendunlop | 13:e3a4abc461f4 | 602 | //A function to end a test and report on findings. |
kendunlop | 13:e3a4abc461f4 | 603 | void endtest() |
kendunlop | 13:e3a4abc461f4 | 604 | { |
kendunlop | 13:e3a4abc461f4 | 605 | listen = 0; |
kendunlop | 13:e3a4abc461f4 | 606 | expectationresolved = 1; |
kendunlop | 13:e3a4abc461f4 | 607 | if (testwasaborted == 1) |
kendunlop | 13:e3a4abc461f4 | 608 | { |
kendunlop | 13:e3a4abc461f4 | 609 | pc.printf("\033[1;35mTest was aborted, results invalid.\033[0m\r\n"); |
kendunlop | 13:e3a4abc461f4 | 610 | pc.printf("\033[1;35m%d IDs sent out. %d failures found so far.\033[0m\r\n", spamcount, failsthistest); |
kendunlop | 13:e3a4abc461f4 | 611 | if (CANpassthrough == 0) |
kendunlop | 13:e3a4abc461f4 | 612 | { |
kendunlop | 13:e3a4abc461f4 | 613 | pc.printf("\033[1;35m%d expected non-replies found so far. \033[0m", goodnoreplies); |
kendunlop | 13:e3a4abc461f4 | 614 | } |
kendunlop | 13:e3a4abc461f4 | 615 | pc.printf("\033[1;35m%d CAN matches found so far.\033[0m\r\n", matchesthistest); |
kendunlop | 13:e3a4abc461f4 | 616 | } |
kendunlop | 13:e3a4abc461f4 | 617 | if (failsthistest == 0 and testwasaborted == 0) |
kendunlop | 13:e3a4abc461f4 | 618 | { |
kendunlop | 13:e3a4abc461f4 | 619 | pc.printf("\033[1;32mTest passed.\033[0m\r\n"); |
kendunlop | 13:e3a4abc461f4 | 620 | pc.printf("\033[1;32m%d IDs sent out. \033[0m", spamcount); |
kendunlop | 13:e3a4abc461f4 | 621 | pc.printf("\033[1;32m%d failures found.\033[0m\r\n", failsthistest); |
kendunlop | 13:e3a4abc461f4 | 622 | if (CANpassthrough == 0) |
kendunlop | 13:e3a4abc461f4 | 623 | { |
kendunlop | 13:e3a4abc461f4 | 624 | pc.printf("\033[1;32m%d expected non-replies found. \033[0m", goodnoreplies); |
kendunlop | 13:e3a4abc461f4 | 625 | } |
kendunlop | 13:e3a4abc461f4 | 626 | pc.printf("\033[1;32m%d CAN matches found.\033[0m\r\n", matchesthistest); |
kendunlop | 13:e3a4abc461f4 | 627 | } |
kendunlop | 13:e3a4abc461f4 | 628 | if (failsthistest > 0 and testwasaborted == 0) |
kendunlop | 13:e3a4abc461f4 | 629 | { |
kendunlop | 13:e3a4abc461f4 | 630 | pc.printf("\033[1;31mTest failed.\r\n%d failures found.\033[0m\r\n", failsthistest); |
kendunlop | 13:e3a4abc461f4 | 631 | pc.printf("\033[1;31m%d mismatch failures, %d 'no reply' failures, %d unexpected CAN messages.\033[0m\r\n", mismatchfailsthistest, noreplyfailsthistest, unexpectedCANfails); |
kendunlop | 13:e3a4abc461f4 | 632 | if (goodnoreplies > 0) |
kendunlop | 13:e3a4abc461f4 | 633 | { |
kendunlop | 13:e3a4abc461f4 | 634 | pc.printf("\033[1;31m%d expected non-replies occurred.\033[0m\r\n", goodnoreplies); |
kendunlop | 13:e3a4abc461f4 | 635 | } |
kendunlop | 13:e3a4abc461f4 | 636 | } |
kendunlop | 13:e3a4abc461f4 | 637 | pc.printf("-------------------------------------\r\n"); |
kendunlop | 13:e3a4abc461f4 | 638 | failsthistest = 0; |
kendunlop | 13:e3a4abc461f4 | 639 | normalkeypresses = 1; |
kendunlop | 13:e3a4abc461f4 | 640 | } |
kendunlop | 13:e3a4abc461f4 | 641 | |
kendunlop | 13:e3a4abc461f4 | 642 | //'End ID spam test' function so you can end it by reaching 2048 IDs or pressing 'Esc' while it's running |
kendunlop | 13:e3a4abc461f4 | 643 | void endidspamtest() |
kendunlop | 13:e3a4abc461f4 | 644 | { |
kendunlop | 13:e3a4abc461f4 | 645 | spamendtime = clock(); |
kendunlop | 13:e3a4abc461f4 | 646 | getspamsecondstotal();//Process the spam information |
kendunlop | 13:e3a4abc461f4 | 647 | idspamon = 0; |
kendunlop | 13:e3a4abc461f4 | 648 | aspamisgoing = 0; |
kendunlop | 13:e3a4abc461f4 | 649 | normalkeypresses = 0; |
kendunlop | 13:e3a4abc461f4 | 650 | addnewlinetonextmessage = 1; |
kendunlop | 13:e3a4abc461f4 | 651 | //addnewlineifneeded(); |
kendunlop | 13:e3a4abc461f4 | 652 | //pc.printf("Ending ID spam sequence. %d IDs checked total.\r\n", idscheckedcount); |
kendunlop | 13:e3a4abc461f4 | 653 | idlistincrementer = 0; |
kendunlop | 13:e3a4abc461f4 | 654 | endtest(); |
kendunlop | 13:e3a4abc461f4 | 655 | } |
kendunlop | 13:e3a4abc461f4 | 656 | |
kendunlop | 13:e3a4abc461f4 | 657 | void endspeedtest()//A different 'end' routine for speed tests. |
kendunlop | 13:e3a4abc461f4 | 658 | { |
kendunlop | 13:e3a4abc461f4 | 659 | //if (messagesincount == spamcount - 1)//If the messages in are only one less than messages out, adjust since it's probably just the test cancelling before getting that last message. |
kendunlop | 13:e3a4abc461f4 | 660 | //{ |
kendunlop | 13:e3a4abc461f4 | 661 | //pc.printf("messagesincount is only one less than spamcount, setting it.\r\n"); |
kendunlop | 13:e3a4abc461f4 | 662 | //messagesincount = spamcount; |
kendunlop | 13:e3a4abc461f4 | 663 | //} |
kendunlop | 13:e3a4abc461f4 | 664 | listen = 0; |
kendunlop | 13:e3a4abc461f4 | 665 | pc.printf("Ending CAN speed test.\r\n"); |
kendunlop | 13:e3a4abc461f4 | 666 | pc.printf("%d messages sent. ", spamcount); |
kendunlop | 13:e3a4abc461f4 | 667 | pc.printf("%d messages detected coming back in over %d seconds.\r\n", messagesincount, spamsecondstotal); |
kendunlop | 13:e3a4abc461f4 | 668 | int speedtestpercentage = ((messagesincount * 100)/spamcount); |
kendunlop | 13:e3a4abc461f4 | 669 | int speedtestpasslevel = 90; |
kendunlop | 13:e3a4abc461f4 | 670 | if (speedtestpercentage >= speedtestpasslevel) |
kendunlop | 13:e3a4abc461f4 | 671 | { |
kendunlop | 13:e3a4abc461f4 | 672 | //pc.printf("\033[1;32mTest pass: %d%% of messages detected back.\033[0m\r\n\n", speedtestpercentage); |
kendunlop | 13:e3a4abc461f4 | 673 | } |
kendunlop | 13:e3a4abc461f4 | 674 | else |
kendunlop | 13:e3a4abc461f4 | 675 | { |
kendunlop | 13:e3a4abc461f4 | 676 | //pc.printf("\033[1;31mTest fail: %d%% of messages detected back.\033[0m\r\n\n", speedtestpercentage); |
kendunlop | 13:e3a4abc461f4 | 677 | } |
kendunlop | 13:e3a4abc461f4 | 678 | speedtestpasslevel = (CANGatewayCANbusspeed / 1000) * 8; //The HD2 can do 80 channels at 100Hz, so 8,000 messages per second are required at top baud rate (1000kbit/s) |
kendunlop | 13:e3a4abc461f4 | 679 | if (CANGatewayCANbusspeed == mbedCANbusspeed) |
kendunlop | 13:e3a4abc461f4 | 680 | { |
kendunlop | 13:e3a4abc461f4 | 681 | if (messagesinpersecond >= speedtestpasslevel) |
kendunlop | 13:e3a4abc461f4 | 682 | { |
kendunlop | 13:e3a4abc461f4 | 683 | pc.printf("\033[1;32mTest pass: %d messages in per second. (%d required for pass at Gateway baud rate %d.)\033[0m\r\n", messagesinpersecond, speedtestpasslevel, CANGatewayCANbusspeed / 1000); |
kendunlop | 13:e3a4abc461f4 | 684 | } |
kendunlop | 13:e3a4abc461f4 | 685 | if (messagesinpersecond < speedtestpasslevel) |
kendunlop | 13:e3a4abc461f4 | 686 | { |
kendunlop | 13:e3a4abc461f4 | 687 | pc.printf("\033[1;31mTest fail: %d messages in per second. (%d required for pass at Gateway baud rate %d.)\033[0m\r\n", messagesinpersecond, speedtestpasslevel, CANGatewayCANbusspeed / 1000); |
kendunlop | 13:e3a4abc461f4 | 688 | } |
kendunlop | 13:e3a4abc461f4 | 689 | } |
kendunlop | 13:e3a4abc461f4 | 690 | if (CANGatewayCANbusspeed != mbedCANbusspeed) |
kendunlop | 13:e3a4abc461f4 | 691 | { |
kendunlop | 13:e3a4abc461f4 | 692 | pc.printf("\033[1;35mTest invalid: Mbed CAN bus baud rate (%d) does not match presumed CAN Gateway baud rate (%d).\033[0m\r\n", mbedCANbusspeed / 1000, CANGatewayCANbusspeed / 1000); |
kendunlop | 13:e3a4abc461f4 | 693 | } |
kendunlop | 13:e3a4abc461f4 | 694 | pc.printf("\n");//Add an extra new line so the whole baud rate test is separate from the rest of the text. |
kendunlop | 13:e3a4abc461f4 | 695 | } |
kendunlop | 13:e3a4abc461f4 | 696 | |
kendunlop | 13:e3a4abc461f4 | 697 | //A generic 'send CAN' function to send CAN with a standard message |
kendunlop | 13:e3a4abc461f4 | 698 | void sendCAN(void) |
kendunlop | 13:e3a4abc461f4 | 699 | { |
kendunlop | 13:e3a4abc461f4 | 700 | CanBus.write(messageOut1); |
kendunlop | 13:e3a4abc461f4 | 701 | //addnewlineifneeded(); |
kendunlop | 13:e3a4abc461f4 | 702 | if (aspamisgoing == 0) |
kendunlop | 13:e3a4abc461f4 | 703 | { |
kendunlop | 13:e3a4abc461f4 | 704 | //pc.printf("\r\n"); |
kendunlop | 13:e3a4abc461f4 | 705 | } |
kendunlop | 13:e3a4abc461f4 | 706 | expectationresolved = 0; |
kendunlop | 13:e3a4abc461f4 | 707 | //pc.printf("Expectationresolved now is %d.", expectationresolved); |
kendunlop | 13:e3a4abc461f4 | 708 | expectationtimer = 0; |
kendunlop | 13:e3a4abc461f4 | 709 | //addnewlinetonextmessage = 1; |
kendunlop | 13:e3a4abc461f4 | 710 | } |
kendunlop | 13:e3a4abc461f4 | 711 | |
kendunlop | 3:79133dcea836 | 712 | //A function to get a coherent CAN message from one, uninterrupted string |
kendunlop | 6:2882710e4f1e | 713 | void getCANfrommessageOutText(void) |
kendunlop | 1:19d183cf2689 | 714 | { |
kendunlop | 11:abd3df435a2b | 715 | //pc.printf("messageOutText before space-removal is '%s'\r\n", messageOutText); |
kendunlop | 7:a9150dc1e481 | 716 | messageOutTextWithSpaces = messageOutText; |
kendunlop | 1:19d183cf2689 | 717 | remove(messageOutText.begin(), messageOutText.end(), ' '); //Remove the spaces from the text to send out so it can be parsed. |
kendunlop | 1:19d183cf2689 | 718 | //pc.printf("After removing spaces, messageOutText is '%s'\r\n", messageOutText); |
kendunlop | 1:19d183cf2689 | 719 | 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 | 720 | //pc.printf("String to parse is '%s'.\r\n", startofstring); |
kendunlop | 8:6f096b45ca15 | 721 | expectationresolved = 0; |
kendunlop | 8:6f096b45ca15 | 722 | if (expectationwasfulfilled == 0) |
kendunlop | 8:6f096b45ca15 | 723 | { |
kendunlop | 8:6f096b45ca15 | 724 | expectationwasnotfulfilled(); |
kendunlop | 8:6f096b45ca15 | 725 | } |
kendunlop | 8:6f096b45ca15 | 726 | if (expectationwasfulfilled == -1) |
kendunlop | 8:6f096b45ca15 | 727 | { |
kendunlop | 8:6f096b45ca15 | 728 | expectationwasfulfilled = 0; |
kendunlop | 8:6f096b45ca15 | 729 | } |
kendunlop | 8:6f096b45ca15 | 730 | expectationwasfulfilled = 0; |
kendunlop | 1:19d183cf2689 | 731 | partincrement = 0; |
kendunlop | 11:abd3df435a2b | 732 | if (mbedCANbusspeed != CANGatewayCANbusspeed) // If the two bus speeds don't match, note that CAN should not be expected to ocme in and report the issue. |
kendunlop | 11:abd3df435a2b | 733 | { |
kendunlop | 11:abd3df435a2b | 734 | shouldntcomein = 1; |
kendunlop | 11:abd3df435a2b | 735 | if (aspamisgoing == 0) |
kendunlop | 11:abd3df435a2b | 736 | { |
kendunlop | 11:abd3df435a2b | 737 | reportbaudrates(); |
kendunlop | 11:abd3df435a2b | 738 | } |
kendunlop | 11:abd3df435a2b | 739 | } |
kendunlop | 1:19d183cf2689 | 740 | part = startofstring.substr(0,3); |
kendunlop | 1:19d183cf2689 | 741 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 742 | part = startofstring.substr(3,1); |
kendunlop | 1:19d183cf2689 | 743 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 744 | part = startofstring.substr(4,2); |
kendunlop | 1:19d183cf2689 | 745 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 746 | part = startofstring.substr(6,2); |
kendunlop | 1:19d183cf2689 | 747 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 748 | part = startofstring.substr(8,2); |
kendunlop | 1:19d183cf2689 | 749 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 750 | part = startofstring.substr(10,2); |
kendunlop | 1:19d183cf2689 | 751 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 752 | part = startofstring.substr(12,2); |
kendunlop | 1:19d183cf2689 | 753 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 754 | part = startofstring.substr(14,2); |
kendunlop | 1:19d183cf2689 | 755 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 756 | part = startofstring.substr(16,2); |
kendunlop | 1:19d183cf2689 | 757 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 758 | part = startofstring.substr(18,2); |
kendunlop | 1:19d183cf2689 | 759 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 760 | } |
kendunlop | 1:19d183cf2689 | 761 | |
kendunlop | 11:abd3df435a2b | 762 | void displayhelp(void) |
kendunlop | 11:abd3df435a2b | 763 | { |
kendunlop | 11:abd3df435a2b | 764 | pc.printf("\033[1;32m---------------HELP---------------\r\n"); |
kendunlop | 11:abd3df435a2b | 765 | pc.printf("Ken_CAN_Test is a program designed by Ken Dunlop to test CAN on the CAN Gateway.\r\n"); |
kendunlop | 11:abd3df435a2b | 766 | pc.printf("The program is designed to send CAN, then test the response.\r\n"); |
kendunlop | 11:abd3df435a2b | 767 | pc.printf("Press 'z' to begin sending a series of incrementing messages. Press 'Esc' to finish the test.\r\n"); |
kendunlop | 11:abd3df435a2b | 768 | pc.printf("Press 'v' to send a message on all IDs (000 - 7FF). Press 'Esc' to abort the test.\r\n"); |
kendunlop | 11:abd3df435a2b | 769 | pc.printf("Use 'q', 'w', 'e', and 'r' to change the Mbed's CAN bus speed.\r\n"); |
kendunlop | 11:abd3df435a2b | 770 | pc.printf("Use 't', 'y', 'u', and 'i' to change the presumed CAN bus speed of the CAN Gateway.\r\n"); |
kendunlop | 11:abd3df435a2b | 771 | pc.printf("If the two CAN bus speeds are different, the program will expect to get no CAN messages back.\r\n"); |
kendunlop | 11:abd3df435a2b | 772 | pc.printf("Press 'a' to begin a baud rate test. This sends CAN messages quickly and measures how many come back.\r\n"); |
kendunlop | 11:abd3df435a2b | 773 | pc.printf("Press 'k' to enter Custom Entry mode. This allows you to enter an CAN ID manually and then send a CAN message.\r\n"); |
kendunlop | 11:abd3df435a2b | 774 | pc.printf("Press 'z' to begin sending incrementing CAN messages. The data in the message will increment by 1 each time.\r\n"); |
kendunlop | 11:abd3df435a2b | 775 | pc.printf("Press 'o' while sending incrementing messages to multiply the incrementation by 16. This shifts the number up one half-byte.\r\n"); |
kendunlop | 11:abd3df435a2b | 776 | pc.printf("Press 'k' to enter Custom Entry mode. This allows you to enter an CAN ID manually and then send a CAN message.\r\n"); |
kendunlop | 11:abd3df435a2b | 777 | pc.printf("Press 'm' to set up an expectation table. This changes what CAN the system expects back when sending certain CAN IDs.\r\n"); |
kendunlop | 11:abd3df435a2b | 778 | pc.printf("Press 'n' to cancel the expectation table and re-enter CAN passthrough mode. This will simply expect the same CAN back as was sent.\r\n"); |
kendunlop | 11:abd3df435a2b | 779 | pc.printf("Press the number keys from '1' to '0' to send various pre-defined CAN messages out.\r\n"); |
kendunlop | 11:abd3df435a2b | 780 | pc.printf("Press '?' to bring up this help screen!\r\n"); |
kendunlop | 11:abd3df435a2b | 781 | pc.printf("---------------------------------\033[0m\r\n"); |
kendunlop | 11:abd3df435a2b | 782 | } |
kendunlop | 1:19d183cf2689 | 783 | |
kendunlop | 3:79133dcea836 | 784 | void printMessageOut (void) |
kendunlop | 3:79133dcea836 | 785 | { |
kendunlop | 1:19d183cf2689 | 786 | //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 | 787 | 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 | 788 | } |
kendunlop | 1:19d183cf2689 | 789 | |
kendunlop | 3:79133dcea836 | 790 | void printMessageIn (void) |
kendunlop | 3:79133dcea836 | 791 | { |
kendunlop | 8:6f096b45ca15 | 792 | //This function will print out whatever the CAN bus is receiving. |
kendunlop | 8:6f096b45ca15 | 793 | //addnewlineifneeded(); |
kendunlop | 10:3e15baff40e7 | 794 | 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 | 795 | } |
kendunlop | 0:7a500ebaa7a6 | 796 | |
kendunlop | 0:7a500ebaa7a6 | 797 | //The 'main' function will run as soon as the program starts. |
kendunlop | 0:7a500ebaa7a6 | 798 | int main() |
kendunlop | 1:19d183cf2689 | 799 | { |
kendunlop | 1:19d183cf2689 | 800 | pc.baud(115200); // serial port at 115200 |
kendunlop | 1:19d183cf2689 | 801 | CanBus.frequency(500 * 1000); // CAN bus at 500k |
kendunlop | 3:79133dcea836 | 802 | CanBus2.frequency(500 * 1000); // CAN bus at 500k |
kendunlop | 1:19d183cf2689 | 803 | CanBus.reset(); // clear any bus errors |
kendunlop | 3:79133dcea836 | 804 | CanBus2.reset(); // clear any bus errors |
kendunlop | 1:19d183cf2689 | 805 | //NOTE: Print messages must be below this line to work. |
kendunlop | 8:6f096b45ca15 | 806 | pc.printf("\033[0m------------------------------------------\r\n"); |
kendunlop | 1:19d183cf2689 | 807 | pc.printf("Welcome to Ken CAN test.\r\n"); |
kendunlop | 1:19d183cf2689 | 808 | pc.printf("Setting CAN bus to 500k.\r\n"); |
kendunlop | 6:2882710e4f1e | 809 | pc.printf("Setting serial port to baud rate 115200.\r\n"); |
kendunlop | 8:6f096b45ca15 | 810 | pc.printf("Using pins 9 and 10 for CANBus 1 (out) and 30 and 29 for CANBus 2 (in).\r\n"); |
kendunlop | 1:19d183cf2689 | 811 | pc.printf("Version %d.%d\r\n",kMajorVersion,kMinorVersion); |
kendunlop | 1:19d183cf2689 | 812 | pc.printf("Build date %s %s\r\n",__DATE__,__TIME__); |
kendunlop | 11:abd3df435a2b | 813 | pc.printf("\033[1;32mFor help, press '?'.\033[0m\r\n"); |
kendunlop | 1:19d183cf2689 | 814 | pc.printf("------------------------------------------\r\n"); |
kendunlop | 12:e950feba910d | 815 | //char c; |
kendunlop | 1:19d183cf2689 | 816 | |
kendunlop | 1:19d183cf2689 | 817 | //Check for button presses |
kendunlop | 1:19d183cf2689 | 818 | while (1) |
kendunlop | 0:7a500ebaa7a6 | 819 | { |
kendunlop | 8:6f096b45ca15 | 820 | if (expectationtimer < 100 and expectationresolved == 0) |
kendunlop | 8:6f096b45ca15 | 821 | { |
kendunlop | 8:6f096b45ca15 | 822 | if (expectationresolved == 0) |
kendunlop | 8:6f096b45ca15 | 823 | { |
kendunlop | 9:7c27efe30a77 | 824 | expectationtimer++; |
kendunlop | 9:7c27efe30a77 | 825 | if (expectationtimer >= 2)//If expectation timer gets too high, the CAN reply is deemed not to have arrived. |
kendunlop | 8:6f096b45ca15 | 826 | { |
kendunlop | 9:7c27efe30a77 | 827 | if (CANmatchstreak > 0) //CAN not arriving breaks any CAN match streak |
kendunlop | 9:7c27efe30a77 | 828 | { |
kendunlop | 9:7c27efe30a77 | 829 | CANmatchstreak = 0; |
kendunlop | 9:7c27efe30a77 | 830 | CANmatchstreakstartID = 0; |
kendunlop | 9:7c27efe30a77 | 831 | if (aspamisgoing == 1) |
kendunlop | 9:7c27efe30a77 | 832 | { |
kendunlop | 11:abd3df435a2b | 833 | pc.printf("\r\n\nFrombreakingCANmatchstreak");//Add a return into the terminal to show the old streak on a line by itself. |
kendunlop | 9:7c27efe30a77 | 834 | } |
kendunlop | 9:7c27efe30a77 | 835 | } |
kendunlop | 9:7c27efe30a77 | 836 | if (goodnoreplystreak > 0 and shouldntcomein == 0) //A missing expected reply breaks any combos for expected no-replies |
kendunlop | 9:7c27efe30a77 | 837 | { |
kendunlop | 9:7c27efe30a77 | 838 | if (aspamisgoing == 1) |
kendunlop | 9:7c27efe30a77 | 839 | { |
kendunlop | 9:7c27efe30a77 | 840 | //pc.printf("\r\n(From breaking goodnoreplystreak)\n\r"); |
kendunlop | 9:7c27efe30a77 | 841 | pc.printf("\r\n\n"); |
kendunlop | 9:7c27efe30a77 | 842 | } |
kendunlop | 9:7c27efe30a77 | 843 | goodnoreplystreak = 0; |
kendunlop | 9:7c27efe30a77 | 844 | goodnoreplystreakstartid = 0; |
kendunlop | 9:7c27efe30a77 | 845 | } |
kendunlop | 11:abd3df435a2b | 846 | // If data did not come in when it was expected... |
kendunlop | 8:6f096b45ca15 | 847 | if (shouldntcomein == 0) |
kendunlop | 8:6f096b45ca15 | 848 | { |
kendunlop | 8:6f096b45ca15 | 849 | expectationwasfulfilled = 0; |
kendunlop | 8:6f096b45ca15 | 850 | failsthistest++; |
kendunlop | 8:6f096b45ca15 | 851 | noreplyfailsthistest++; |
kendunlop | 8:6f096b45ca15 | 852 | if (aspamisgoing == 0) |
kendunlop | 8:6f096b45ca15 | 853 | { |
kendunlop | 9:7c27efe30a77 | 854 | noreplystreakstartid = idlistincrementer; |
kendunlop | 9:7c27efe30a77 | 855 | } |
kendunlop | 9:7c27efe30a77 | 856 | if (goodnoreplystreak > 0) |
kendunlop | 9:7c27efe30a77 | 857 | { |
kendunlop | 9:7c27efe30a77 | 858 | goodnoreplystreak = 0; |
kendunlop | 9:7c27efe30a77 | 859 | if (aspamisgoing == 1) |
kendunlop | 9:7c27efe30a77 | 860 | { |
kendunlop | 11:abd3df435a2b | 861 | pc.printf("(\r\n\n");//Add an extra line if breaking an 'as expected' no reply streak. |
kendunlop | 9:7c27efe30a77 | 862 | } |
kendunlop | 8:6f096b45ca15 | 863 | } |
kendunlop | 11:abd3df435a2b | 864 | if (failsstreak > 0) |
kendunlop | 11:abd3df435a2b | 865 | { |
kendunlop | 11:abd3df435a2b | 866 | if (aspamisgoing == 1) |
kendunlop | 11:abd3df435a2b | 867 | { |
kendunlop | 11:abd3df435a2b | 868 | pc.printf("\r\n\n");//Add an extra line if breaking a failure 'does not match expectation' streak |
kendunlop | 11:abd3df435a2b | 869 | } |
kendunlop | 11:abd3df435a2b | 870 | failsstreak = 0; |
kendunlop | 11:abd3df435a2b | 871 | failsstreakstart = 0; |
kendunlop | 11:abd3df435a2b | 872 | } |
kendunlop | 9:7c27efe30a77 | 873 | if (aspamisgoing == 1) |
kendunlop | 9:7c27efe30a77 | 874 | { |
kendunlop | 9:7c27efe30a77 | 875 | if (noreplystreak == 0) |
kendunlop | 9:7c27efe30a77 | 876 | { |
kendunlop | 9:7c27efe30a77 | 877 | noreplystreakstartid = idlistincrementer; |
kendunlop | 9:7c27efe30a77 | 878 | } |
kendunlop | 9:7c27efe30a77 | 879 | noreplystreak++; |
kendunlop | 9:7c27efe30a77 | 880 | } |
kendunlop | 9:7c27efe30a77 | 881 | 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 | 882 | if (noreplystreakstartid != idlistincrementer) |
kendunlop | 9:7c27efe30a77 | 883 | { |
kendunlop | 9:7c27efe30a77 | 884 | pc.printf("\033[38;5;220mNo reply message detected for IDs %03X to %03X.\033[0m\r", noreplystreakstartid, idlistincrementer); |
kendunlop | 9:7c27efe30a77 | 885 | } |
kendunlop | 9:7c27efe30a77 | 886 | if (noreplystreakstartid == idlistincrementer) |
kendunlop | 9:7c27efe30a77 | 887 | { |
kendunlop | 9:7c27efe30a77 | 888 | pc.printf("\033[38;5;220mNo reply message detected for ID %03X.\033[0m\r", idlistincrementer); |
kendunlop | 9:7c27efe30a77 | 889 | } |
kendunlop | 9:7c27efe30a77 | 890 | if (aspamisgoing == 0) |
kendunlop | 9:7c27efe30a77 | 891 | { |
kendunlop | 9:7c27efe30a77 | 892 | pc.printf("\r\n\n"); //If it's not spamming, you can safely add a new line after the fail message. |
kendunlop | 9:7c27efe30a77 | 893 | } |
kendunlop | 8:6f096b45ca15 | 894 | } |
kendunlop | 8:6f096b45ca15 | 895 | if (shouldntcomein == 1) |
kendunlop | 8:6f096b45ca15 | 896 | { |
kendunlop | 8:6f096b45ca15 | 897 | expectationwasfulfilled = 1; |
kendunlop | 9:7c27efe30a77 | 898 | //Getting an expected non-reply breaks any unexpected reply streaks |
kendunlop | 9:7c27efe30a77 | 899 | if (unexpectedCANstreak > 0) |
kendunlop | 9:7c27efe30a77 | 900 | { |
kendunlop | 9:7c27efe30a77 | 901 | if (aspamisgoing == 1) //Add a new line if it's in spam mode here to report new expected-non-message stuff. |
kendunlop | 9:7c27efe30a77 | 902 | { |
kendunlop | 11:abd3df435a2b | 903 | //pc.printf("\r\n\n(FrombreakingunexpectedCANstreak)"); |
kendunlop | 9:7c27efe30a77 | 904 | pc.printf("\r\n\n"); |
kendunlop | 9:7c27efe30a77 | 905 | } |
kendunlop | 9:7c27efe30a77 | 906 | unexpectedCANstreak = 0; |
kendunlop | 9:7c27efe30a77 | 907 | unexpectedCANstreakstartid = 0; |
kendunlop | 9:7c27efe30a77 | 908 | } |
kendunlop | 9:7c27efe30a77 | 909 | //Getting an expected non-reply also breaks any unexpected non-reply streaks |
kendunlop | 9:7c27efe30a77 | 910 | if (noreplystreak > 0) |
kendunlop | 9:7c27efe30a77 | 911 | { |
kendunlop | 9:7c27efe30a77 | 912 | if (aspamisgoing == 1) //Add a new line if it's in spam mode here to report new expected-non-message stuff. |
kendunlop | 9:7c27efe30a77 | 913 | { |
kendunlop | 9:7c27efe30a77 | 914 | //pc.printf("\r\n(From breaking noreplystreak)\n"); |
kendunlop | 9:7c27efe30a77 | 915 | pc.printf("\r\n\n"); |
kendunlop | 9:7c27efe30a77 | 916 | } |
kendunlop | 9:7c27efe30a77 | 917 | noreplystreak = 0; |
kendunlop | 9:7c27efe30a77 | 918 | noreplystreakstartid = 0; |
kendunlop | 9:7c27efe30a77 | 919 | } |
kendunlop | 11:abd3df435a2b | 920 | // Also, getting an expected non-reply breaks any CAN-fail streaks |
kendunlop | 11:abd3df435a2b | 921 | if (failsstreak > 0 ) |
kendunlop | 11:abd3df435a2b | 922 | { |
kendunlop | 11:abd3df435a2b | 923 | if (aspamisgoing == 1) |
kendunlop | 11:abd3df435a2b | 924 | { |
kendunlop | 11:abd3df435a2b | 925 | //pc.printf("\r\n(From breaking CAN-mismatch streak)\n"); |
kendunlop | 11:abd3df435a2b | 926 | pc.printf("\r\n\n"); |
kendunlop | 11:abd3df435a2b | 927 | } |
kendunlop | 11:abd3df435a2b | 928 | failsstreak = 0; |
kendunlop | 11:abd3df435a2b | 929 | failsstreakstart = 0; |
kendunlop | 11:abd3df435a2b | 930 | } |
kendunlop | 9:7c27efe30a77 | 931 | 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 | 932 | { |
kendunlop | 9:7c27efe30a77 | 933 | goodnoreplystreakstartid = idlistincrementer; |
kendunlop | 9:7c27efe30a77 | 934 | if (CANmatchstreak > 0 and aspamisgoing == 1)//Also add an extra return for the breaking of the old CANmatchstreak if needed |
kendunlop | 9:7c27efe30a77 | 935 | { |
kendunlop | 9:7c27efe30a77 | 936 | CANmatchstreak = 0; |
kendunlop | 9:7c27efe30a77 | 937 | pc.printf("(From breaking of CANmatchstreak)\r\n"); |
kendunlop | 9:7c27efe30a77 | 938 | } |
kendunlop | 9:7c27efe30a77 | 939 | } |
kendunlop | 9:7c27efe30a77 | 940 | 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 | 941 | if (aspamisgoing == 0) |
kendunlop | 9:7c27efe30a77 | 942 | { |
kendunlop | 9:7c27efe30a77 | 943 | goodnoreplystreakstartid = idlistincrementer; |
kendunlop | 9:7c27efe30a77 | 944 | } |
kendunlop | 9:7c27efe30a77 | 945 | if (goodnoreplystreakstartid != idlistincrementer) |
kendunlop | 9:7c27efe30a77 | 946 | { |
kendunlop | 9:7c27efe30a77 | 947 | pc.printf("\033[0;32mNo reply message detected from %03X to %03X, as expected.\033[0m\r", goodnoreplystreakstartid, idlistincrementer); |
kendunlop | 9:7c27efe30a77 | 948 | } |
kendunlop | 9:7c27efe30a77 | 949 | if (goodnoreplystreakstartid == idlistincrementer) |
kendunlop | 9:7c27efe30a77 | 950 | { |
kendunlop | 9:7c27efe30a77 | 951 | pc.printf("\033[0;32mNo reply message detected for ID %03X, as expected.\033[0m\r", goodnoreplystreakstartid); |
kendunlop | 9:7c27efe30a77 | 952 | } |
kendunlop | 9:7c27efe30a77 | 953 | if (aspamisgoing == 0) |
kendunlop | 9:7c27efe30a77 | 954 | { |
kendunlop | 11:abd3df435a2b | 955 | pc.printf("\r\n\n"); //If it's not spamming, you can safely add a new line after the fail message. |
kendunlop | 9:7c27efe30a77 | 956 | } |
kendunlop | 9:7c27efe30a77 | 957 | goodnoreplies++; |
kendunlop | 9:7c27efe30a77 | 958 | goodnoreplystreak++; |
kendunlop | 8:6f096b45ca15 | 959 | } |
kendunlop | 8:6f096b45ca15 | 960 | expectationresolved = 1; |
kendunlop | 8:6f096b45ca15 | 961 | //pc.printf("Expectation timer reached %d!\r\n", expectationtimer); |
kendunlop | 8:6f096b45ca15 | 962 | expectationtimer = 0; |
kendunlop | 8:6f096b45ca15 | 963 | if (idspamon == 1) |
kendunlop | 8:6f096b45ca15 | 964 | { |
kendunlop | 8:6f096b45ca15 | 965 | idscheckedcount++; |
kendunlop | 9:7c27efe30a77 | 966 | idlistincrementer++; //Only add 1 to the idlist incrementer at the end so the first ID it sends is '000'. |
kendunlop | 8:6f096b45ca15 | 967 | } |
kendunlop | 11:abd3df435a2b | 968 | if (customentry == 1) |
kendunlop | 11:abd3df435a2b | 969 | { |
kendunlop | 11:abd3df435a2b | 970 | pc.printf("Enter CAN ID to send: "); |
kendunlop | 11:abd3df435a2b | 971 | } |
kendunlop | 8:6f096b45ca15 | 972 | } |
kendunlop | 8:6f096b45ca15 | 973 | wait(0.01); |
kendunlop | 8:6f096b45ca15 | 974 | } |
kendunlop | 8:6f096b45ca15 | 975 | } |
kendunlop | 1:19d183cf2689 | 976 | if (pc.readable()) |
kendunlop | 1:19d183cf2689 | 977 | { |
kendunlop | 9:7c27efe30a77 | 978 | c = tolower(pc.getc()); |
kendunlop | 3:79133dcea836 | 979 | if (c != NULL) |
kendunlop | 1:19d183cf2689 | 980 | { |
kendunlop | 3:79133dcea836 | 981 | //When the a key is pressed, define a CAN message and send it. |
kendunlop | 1:19d183cf2689 | 982 | //pc.printf("A key was pressed! (%c)\r\n", c); |
kendunlop | 1:19d183cf2689 | 983 | messageOutText = ""; |
kendunlop | 8:6f096b45ca15 | 984 | buttonPressMessageOutText = ""; |
kendunlop | 11:abd3df435a2b | 985 | if (c == '1' and normalkeypresses == 1) |
kendunlop | 7:a9150dc1e481 | 986 | { |
kendunlop | 11:abd3df435a2b | 987 | buttonPressMessageOutText = "301 8 01 02 03 04 05 06 07 08"; |
kendunlop | 9:7c27efe30a77 | 988 | idlistincrementer = 0x301; |
kendunlop | 7:a9150dc1e481 | 989 | } |
kendunlop | 11:abd3df435a2b | 990 | if (c == '2' and normalkeypresses == 1) |
kendunlop | 7:a9150dc1e481 | 991 | { |
kendunlop | 11:abd3df435a2b | 992 | buttonPressMessageOutText = "302 8 01 02 03 04 05 06 07 08"; |
kendunlop | 9:7c27efe30a77 | 993 | idlistincrementer = 0x302; |
kendunlop | 7:a9150dc1e481 | 994 | } |
kendunlop | 11:abd3df435a2b | 995 | if (c == '3' and normalkeypresses == 1) |
kendunlop | 7:a9150dc1e481 | 996 | { |
kendunlop | 11:abd3df435a2b | 997 | buttonPressMessageOutText = "303 8 01 02 03 04 05 06 07 08"; |
kendunlop | 9:7c27efe30a77 | 998 | idlistincrementer = 0x303; |
kendunlop | 7:a9150dc1e481 | 999 | } |
kendunlop | 11:abd3df435a2b | 1000 | if (c == '4' and normalkeypresses == 1) |
kendunlop | 7:a9150dc1e481 | 1001 | { |
kendunlop | 11:abd3df435a2b | 1002 | buttonPressMessageOutText = "304 8 01 02 03 04 05 06 07 08"; |
kendunlop | 9:7c27efe30a77 | 1003 | idlistincrementer = 0x304; |
kendunlop | 7:a9150dc1e481 | 1004 | } |
kendunlop | 11:abd3df435a2b | 1005 | if (c == '5' and normalkeypresses == 1) |
kendunlop | 7:a9150dc1e481 | 1006 | { |
kendunlop | 12:e950feba910d | 1007 | buttonPressMessageOutText = "305 8 01 02 03 04 05 06 07 08"; |
kendunlop | 13:e3a4abc461f4 | 1008 | idlistincrementer = 0x305; |
kendunlop | 7:a9150dc1e481 | 1009 | } |
kendunlop | 11:abd3df435a2b | 1010 | if (c == '6' and normalkeypresses == 1) |
kendunlop | 7:a9150dc1e481 | 1011 | { |
kendunlop | 11:abd3df435a2b | 1012 | buttonPressMessageOutText = "306 8 01 02 03 04 05 06 07 08"; |
kendunlop | 9:7c27efe30a77 | 1013 | idlistincrementer = 0x306; |
kendunlop | 7:a9150dc1e481 | 1014 | } |
kendunlop | 11:abd3df435a2b | 1015 | if (c == '7' and normalkeypresses == 1) |
kendunlop | 7:a9150dc1e481 | 1016 | { |
kendunlop | 11:abd3df435a2b | 1017 | buttonPressMessageOutText = "307 8 01 02 03 04 05 06 07 08"; |
kendunlop | 9:7c27efe30a77 | 1018 | idlistincrementer = 0x307; |
kendunlop | 7:a9150dc1e481 | 1019 | } |
kendunlop | 11:abd3df435a2b | 1020 | if (c == '8' and normalkeypresses == 1) |
kendunlop | 7:a9150dc1e481 | 1021 | { |
kendunlop | 11:abd3df435a2b | 1022 | buttonPressMessageOutText = "308 8 01 02 03 04 05 06 07 08"; |
kendunlop | 9:7c27efe30a77 | 1023 | idlistincrementer = 0x308; |
kendunlop | 7:a9150dc1e481 | 1024 | } |
kendunlop | 11:abd3df435a2b | 1025 | if (c == '9' and normalkeypresses == 1) |
kendunlop | 7:a9150dc1e481 | 1026 | { |
kendunlop | 11:abd3df435a2b | 1027 | buttonPressMessageOutText = "309 8 01 02 03 04 05 06 07 08"; |
kendunlop | 9:7c27efe30a77 | 1028 | idlistincrementer = 0x309; |
kendunlop | 7:a9150dc1e481 | 1029 | } |
kendunlop | 11:abd3df435a2b | 1030 | if (c == '0' and normalkeypresses == 1) |
kendunlop | 7:a9150dc1e481 | 1031 | { |
kendunlop | 9:7c27efe30a77 | 1032 | buttonPressMessageOutText = "7FF 8 FF FF FF FF FF FF FF FF";//NOTE: CAN IDs only go to 7FF, not FFF. |
kendunlop | 10:3e15baff40e7 | 1033 | idlistincrementer = 0x7FF; |
kendunlop | 10:3e15baff40e7 | 1034 | } |
kendunlop | 11:abd3df435a2b | 1035 | if (c == 'a' and normalkeypresses == 1) |
kendunlop | 10:3e15baff40e7 | 1036 | { |
kendunlop | 11:abd3df435a2b | 1037 | listen = 1; |
kendunlop | 11:abd3df435a2b | 1038 | pc.printf("Starting baud rate speed test. Mbed at %d and CAN Gateway presumed %d. (Press 'Esc' to end.)\r\n", mbedCANbusspeed / 1000, CANGatewayCANbusspeed / 1000); |
kendunlop | 10:3e15baff40e7 | 1039 | pc.printf(""); |
kendunlop | 10:3e15baff40e7 | 1040 | resettest(); |
kendunlop | 10:3e15baff40e7 | 1041 | incrementer = 0x333; |
kendunlop | 10:3e15baff40e7 | 1042 | spamstarttime = clock(); |
kendunlop | 10:3e15baff40e7 | 1043 | speedspamison = 1; |
kendunlop | 10:3e15baff40e7 | 1044 | aspamisgoing = 1; |
kendunlop | 11:abd3df435a2b | 1045 | normalkeypresses = 0; |
kendunlop | 10:3e15baff40e7 | 1046 | messageOut1.format = CANStandard; //Quickly define a convenient messageOut for the speed spam to use. |
kendunlop | 10:3e15baff40e7 | 1047 | messageOut1.id = 0x301; |
kendunlop | 10:3e15baff40e7 | 1048 | messageOut1.len = 8; |
kendunlop | 10:3e15baff40e7 | 1049 | messageOut1.data[0] = 0x06; |
kendunlop | 10:3e15baff40e7 | 1050 | messageOut1.data[1] = 0x3f; |
kendunlop | 10:3e15baff40e7 | 1051 | messageOut1.data[2] = 0xb2; |
kendunlop | 10:3e15baff40e7 | 1052 | messageOut1.data[3] = 0x29; |
kendunlop | 10:3e15baff40e7 | 1053 | messageOut1.data[4] = 0x19; |
kendunlop | 10:3e15baff40e7 | 1054 | messageOut1.data[5] = 0x97; |
kendunlop | 10:3e15baff40e7 | 1055 | messageOut1.data[6] = 0x67; |
kendunlop | 10:3e15baff40e7 | 1056 | messageOut1.data[7] = 0x37; |
kendunlop | 11:abd3df435a2b | 1057 | pc.printf("Will send out '%03X %01X %02X %02X %02X %02X %02X %02X %02X %02X' as fast as possible.\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 | 10:3e15baff40e7 | 1058 | } |
kendunlop | 11:abd3df435a2b | 1059 | if (c == 27 and speedspamison == 1)//Cancel speed spam mode |
kendunlop | 10:3e15baff40e7 | 1060 | { |
kendunlop | 10:3e15baff40e7 | 1061 | //pc.printf("\r\nEnding baud rate speed test mode from upper key press method.\r\n"); |
kendunlop | 10:3e15baff40e7 | 1062 | aspamisgoing = 0; |
kendunlop | 11:abd3df435a2b | 1063 | normalkeypresses = 1; |
kendunlop | 10:3e15baff40e7 | 1064 | spamendtime = clock(); |
kendunlop | 10:3e15baff40e7 | 1065 | //endtest(); |
kendunlop | 10:3e15baff40e7 | 1066 | getspamsecondstotal(); |
kendunlop | 10:3e15baff40e7 | 1067 | endspeedtest(); |
kendunlop | 10:3e15baff40e7 | 1068 | speedspamison = 0; |
kendunlop | 8:6f096b45ca15 | 1069 | } |
kendunlop | 8:6f096b45ca15 | 1070 | if (buttonPressMessageOutText != "")//Centralized 'button press MessageOut' routine that applies to all button presses. |
kendunlop | 8:6f096b45ca15 | 1071 | { |
kendunlop | 8:6f096b45ca15 | 1072 | if (aspamisgoing == 0) |
kendunlop | 8:6f096b45ca15 | 1073 | { |
kendunlop | 9:7c27efe30a77 | 1074 | //addnewlineifneeded(); |
kendunlop | 11:abd3df435a2b | 1075 | //resetCANbuses(); |
kendunlop | 11:abd3df435a2b | 1076 | listen = 1; |
kendunlop | 8:6f096b45ca15 | 1077 | messageOutText = buttonPressMessageOutText; |
kendunlop | 8:6f096b45ca15 | 1078 | addnewlinetonextmessage = 1; |
kendunlop | 8:6f096b45ca15 | 1079 | checklaterbytes = 1;//Check all bytes for button-press messages. |
kendunlop | 8:6f096b45ca15 | 1080 | } |
kendunlop | 8:6f096b45ca15 | 1081 | buttonPressMessageOutText = ""; |
kendunlop | 7:a9150dc1e481 | 1082 | } |
kendunlop | 11:abd3df435a2b | 1083 | if (c == '/' and normalkeypresses == 1) |
kendunlop | 11:abd3df435a2b | 1084 | { |
kendunlop | 11:abd3df435a2b | 1085 | displayhelp(); |
kendunlop | 11:abd3df435a2b | 1086 | } |
kendunlop | 11:abd3df435a2b | 1087 | if (c == '?' and normalkeypresses == 1) |
kendunlop | 11:abd3df435a2b | 1088 | { |
kendunlop | 11:abd3df435a2b | 1089 | displayhelp(); |
kendunlop | 11:abd3df435a2b | 1090 | } |
kendunlop | 11:abd3df435a2b | 1091 | if (c == 'q' and normalkeypresses == 1) |
kendunlop | 3:79133dcea836 | 1092 | { |
kendunlop | 9:7c27efe30a77 | 1093 | pc.printf("Changing Mbed CAN bus speed to 125.\r\n"); |
kendunlop | 3:79133dcea836 | 1094 | CanBus.frequency(125 * 1000); // CAN bus at 125k |
kendunlop | 5:bf4c6278ca8b | 1095 | CanBus2.frequency(125 * 1000); // CAN bus at 125k |
kendunlop | 9:7c27efe30a77 | 1096 | mbedCANbusspeed = (125 * 1000); |
kendunlop | 3:79133dcea836 | 1097 | } |
kendunlop | 11:abd3df435a2b | 1098 | if (c == 'w' and normalkeypresses == 1) |
kendunlop | 3:79133dcea836 | 1099 | { |
kendunlop | 9:7c27efe30a77 | 1100 | pc.printf("Changing Mbed CAN bus speed to 250.\r\n"); |
kendunlop | 3:79133dcea836 | 1101 | CanBus.frequency(250 * 1000); // CAN bus at 250k |
kendunlop | 5:bf4c6278ca8b | 1102 | CanBus2.frequency(250 * 1000); // CAN bus at 250k |
kendunlop | 9:7c27efe30a77 | 1103 | mbedCANbusspeed = (250 * 1000); |
kendunlop | 3:79133dcea836 | 1104 | } |
kendunlop | 11:abd3df435a2b | 1105 | if (c == 'e' and normalkeypresses == 1) |
kendunlop | 3:79133dcea836 | 1106 | { |
kendunlop | 9:7c27efe30a77 | 1107 | pc.printf("Changing Mbed CAN bus speed to 500.\r\n"); |
kendunlop | 3:79133dcea836 | 1108 | CanBus.frequency(500 * 1000); // CAN bus at 500k |
kendunlop | 3:79133dcea836 | 1109 | CanBus2.frequency(500 * 1000);// CAN bus at 500k |
kendunlop | 9:7c27efe30a77 | 1110 | mbedCANbusspeed = (500 * 1000); |
kendunlop | 3:79133dcea836 | 1111 | } |
kendunlop | 11:abd3df435a2b | 1112 | if (c == 'r' and normalkeypresses == 1) |
kendunlop | 3:79133dcea836 | 1113 | { |
kendunlop | 9:7c27efe30a77 | 1114 | pc.printf("Changing Mbed CAN bus speed to 1000.\r\n"); |
kendunlop | 3:79133dcea836 | 1115 | CanBus.frequency(1000 * 1000); // CAN bus at 1000k |
kendunlop | 3:79133dcea836 | 1116 | CanBus2.frequency(1000 * 1000); // CAN bus at 1000k |
kendunlop | 9:7c27efe30a77 | 1117 | mbedCANbusspeed = (1000 * 1000); |
kendunlop | 3:79133dcea836 | 1118 | } |
kendunlop | 11:abd3df435a2b | 1119 | if (c == 't' and normalkeypresses == 1) |
kendunlop | 9:7c27efe30a77 | 1120 | { |
kendunlop | 9:7c27efe30a77 | 1121 | CANGatewayCANbusspeed = (125 * 1000); |
kendunlop | 9:7c27efe30a77 | 1122 | pc.printf("CAN Gateway is now presumed to be at CAN bus speed %d.\r\n", CANGatewayCANbusspeed / 1000); |
kendunlop | 9:7c27efe30a77 | 1123 | } |
kendunlop | 11:abd3df435a2b | 1124 | if (c == 'y' and normalkeypresses == 1) |
kendunlop | 9:7c27efe30a77 | 1125 | { |
kendunlop | 9:7c27efe30a77 | 1126 | CANGatewayCANbusspeed = (250 * 1000); |
kendunlop | 9:7c27efe30a77 | 1127 | pc.printf("CAN Gateway is now presumed to be at CAN bus speed %d.\r\n", CANGatewayCANbusspeed / 1000); |
kendunlop | 9:7c27efe30a77 | 1128 | } |
kendunlop | 11:abd3df435a2b | 1129 | if (c == 'u' and normalkeypresses == 1) |
kendunlop | 8:6f096b45ca15 | 1130 | { |
kendunlop | 9:7c27efe30a77 | 1131 | CANGatewayCANbusspeed = (500 * 1000); |
kendunlop | 9:7c27efe30a77 | 1132 | pc.printf("CAN Gateway is now presumed to be at CAN bus speed %d.\r\n", CANGatewayCANbusspeed / 1000); |
kendunlop | 9:7c27efe30a77 | 1133 | } |
kendunlop | 11:abd3df435a2b | 1134 | if (c == 'i' and normalkeypresses == 1) |
kendunlop | 9:7c27efe30a77 | 1135 | { |
kendunlop | 9:7c27efe30a77 | 1136 | CANGatewayCANbusspeed = (1000 * 1000); |
kendunlop | 9:7c27efe30a77 | 1137 | pc.printf("CAN Gateway is now presumed to be at CAN bus speed %d.\r\n", CANGatewayCANbusspeed / 1000); |
kendunlop | 9:7c27efe30a77 | 1138 | } |
kendunlop | 11:abd3df435a2b | 1139 | if (c == 'k' and normalkeypresses == 1) |
kendunlop | 11:abd3df435a2b | 1140 | { |
kendunlop | 11:abd3df435a2b | 1141 | pc.printf("Will enter custom CAN entry mode. (Press 'Esc' to end.)\r\nEnter CAN ID to send: "); |
kendunlop | 11:abd3df435a2b | 1142 | customentry = 1; |
kendunlop | 11:abd3df435a2b | 1143 | normalkeypresses = 0; |
kendunlop | 11:abd3df435a2b | 1144 | c = NULL; |
kendunlop | 11:abd3df435a2b | 1145 | customentrystring = ""; |
kendunlop | 11:abd3df435a2b | 1146 | } |
kendunlop | 11:abd3df435a2b | 1147 | if (c == 27 and customentry == 1)//Pressing Esc ends Custom Entry mode |
kendunlop | 11:abd3df435a2b | 1148 | { |
kendunlop | 11:abd3df435a2b | 1149 | customentry = 0; |
kendunlop | 11:abd3df435a2b | 1150 | normalkeypresses = 1; |
kendunlop | 11:abd3df435a2b | 1151 | pc.printf("\r\nEnding custom entry mode.\r\n"); |
kendunlop | 11:abd3df435a2b | 1152 | c = NULL; |
kendunlop | 11:abd3df435a2b | 1153 | } |
kendunlop | 11:abd3df435a2b | 1154 | if (c == 13 and customentry == 1 and customentrystring != "")//Pressing enter in Custom Entry mode tries to send a CAN message |
kendunlop | 11:abd3df435a2b | 1155 | { |
kendunlop | 11:abd3df435a2b | 1156 | pc.printf("\r\n"); |
kendunlop | 11:abd3df435a2b | 1157 | //pc.printf("Will try to confirm CES ('%s').\r\n", customentrystring); |
kendunlop | 11:abd3df435a2b | 1158 | if (customentrystring.length() < 3) |
kendunlop | 11:abd3df435a2b | 1159 | { |
kendunlop | 11:abd3df435a2b | 1160 | customentrystring = "0" + customentrystring; |
kendunlop | 11:abd3df435a2b | 1161 | } |
kendunlop | 11:abd3df435a2b | 1162 | if (customentrystring.length() < 3) |
kendunlop | 11:abd3df435a2b | 1163 | { |
kendunlop | 11:abd3df435a2b | 1164 | customentrystring = "0" + customentrystring; |
kendunlop | 11:abd3df435a2b | 1165 | } |
kendunlop | 11:abd3df435a2b | 1166 | checklaterbytes = 1; |
kendunlop | 11:abd3df435a2b | 1167 | partincrement = 0; |
kendunlop | 11:abd3df435a2b | 1168 | part = customentrystring; |
kendunlop | 11:abd3df435a2b | 1169 | dealwithpart(); |
kendunlop | 11:abd3df435a2b | 1170 | //pc.printf("Hextotal now says '%d'.\r\n", hextotal); |
kendunlop | 11:abd3df435a2b | 1171 | if (hextotal > 0x7FF)//If the measured total is more than 0x7FF (the max ID), reduce it. |
kendunlop | 11:abd3df435a2b | 1172 | { |
kendunlop | 11:abd3df435a2b | 1173 | hextotal = 0x7FF; |
kendunlop | 11:abd3df435a2b | 1174 | customentrystring = "7FF"; |
kendunlop | 11:abd3df435a2b | 1175 | } |
kendunlop | 11:abd3df435a2b | 1176 | if (hextotal < 0) // If the measured total is less than zero, make it zero instead. |
kendunlop | 11:abd3df435a2b | 1177 | { |
kendunlop | 11:abd3df435a2b | 1178 | hextotal = 0x000; |
kendunlop | 11:abd3df435a2b | 1179 | customentrystring = "000"; |
kendunlop | 11:abd3df435a2b | 1180 | } |
kendunlop | 11:abd3df435a2b | 1181 | messageOutText = customentrystring + " 8 01 02 03 04 05 06 07 08"; |
kendunlop | 11:abd3df435a2b | 1182 | customentrystring = ""; |
kendunlop | 11:abd3df435a2b | 1183 | if (expectationresolved == 1 and messageOutText == "") |
kendunlop | 11:abd3df435a2b | 1184 | { |
kendunlop | 11:abd3df435a2b | 1185 | pc.printf("Enter CAN ID to send: "); |
kendunlop | 11:abd3df435a2b | 1186 | } |
kendunlop | 11:abd3df435a2b | 1187 | } |
kendunlop | 11:abd3df435a2b | 1188 | if (c != NULL and customentry == 1 and expectationresolved == 1 and messageOutText == "") |
kendunlop | 11:abd3df435a2b | 1189 | { |
kendunlop | 11:abd3df435a2b | 1190 | //pc.printf("Custom entry detected! (%c)\r\n", c); |
kendunlop | 11:abd3df435a2b | 1191 | if (c != '\r' and c != 8 and customentrystring.length() < 3) //Normal key presses add to the custom string. |
kendunlop | 11:abd3df435a2b | 1192 | { |
kendunlop | 11:abd3df435a2b | 1193 | customentrystring.push_back(toupper(c)); |
kendunlop | 11:abd3df435a2b | 1194 | //pc.printf("Custom entry string now says '%s'.", customentrystring); |
kendunlop | 11:abd3df435a2b | 1195 | } |
kendunlop | 11:abd3df435a2b | 1196 | if (c == 8 and customentrystring.length() > 0) //Backspace (character 8) causes the text to go back one character. |
kendunlop | 11:abd3df435a2b | 1197 | { |
kendunlop | 11:abd3df435a2b | 1198 | pc.printf("\b "); |
kendunlop | 11:abd3df435a2b | 1199 | customentrystring.erase(customentrystring.size()-1,1); |
kendunlop | 11:abd3df435a2b | 1200 | } |
kendunlop | 11:abd3df435a2b | 1201 | pc.printf("\rEnter CAN ID to send: %s", customentrystring); |
kendunlop | 11:abd3df435a2b | 1202 | } |
kendunlop | 11:abd3df435a2b | 1203 | if (c == 'n' and CANpassthrough == 0 and normalkeypresses == 1) |
kendunlop | 9:7c27efe30a77 | 1204 | { |
kendunlop | 9:7c27efe30a77 | 1205 | pc.printf("Starting CAN passthrough mode. Will expect to recieve the same CAN that is sent.\r\n"); |
kendunlop | 9:7c27efe30a77 | 1206 | CANpassthrough = 1; |
kendunlop | 9:7c27efe30a77 | 1207 | } |
kendunlop | 12:e950feba910d | 1208 | if (c == 'm' and normalkeypresses == 1) //New menu to select multiple expectation tables |
kendunlop | 9:7c27efe30a77 | 1209 | { |
kendunlop | 12:e950feba910d | 1210 | normalkeypresses = 0; //Go out of 'normal key presses' mode. |
kendunlop | 12:e950feba910d | 1211 | pc.printf("Now entering Expectation Table mode. Press the number keys to activate different expectation tables.\r\n"); |
kendunlop | 12:e950feba910d | 1212 | tablesmode = 1; |
kendunlop | 8:6f096b45ca15 | 1213 | } |
kendunlop | 12:e950feba910d | 1214 | if (c != NULL and tablesmode == 1) |
kendunlop | 12:e950feba910d | 1215 | { |
kendunlop | 12:e950feba910d | 1216 | tablesmodekeypress(); |
kendunlop | 12:e950feba910d | 1217 | } |
kendunlop | 8:6f096b45ca15 | 1218 | if (c == 'o' and idspamon == 0 and spamon == 1) |
kendunlop | 3:79133dcea836 | 1219 | { |
kendunlop | 3:79133dcea836 | 1220 | incrementer = (incrementer * 16); |
kendunlop | 10:3e15baff40e7 | 1221 | pc.printf("\n\rMultiplied Incrementer by 16. It's now %d.\r\n", incrementer); |
kendunlop | 3:79133dcea836 | 1222 | } |
kendunlop | 9:7c27efe30a77 | 1223 | if (c == 'i' and spamon == 1) |
kendunlop | 1:19d183cf2689 | 1224 | { |
kendunlop | 1:19d183cf2689 | 1225 | incrementer = incrementer + 16; |
kendunlop | 1:19d183cf2689 | 1226 | std::stringstream sstream; |
kendunlop | 1:19d183cf2689 | 1227 | sstream << std::hex << incrementer; |
kendunlop | 1:19d183cf2689 | 1228 | string stringsofar = sstream.str(); |
kendunlop | 1:19d183cf2689 | 1229 | //pc.printf("Incrementer is now %d.\r\n", incrementer); |
kendunlop | 1:19d183cf2689 | 1230 | //pc.printf("StringStream says '%s'.\r\n", stringsofar); |
kendunlop | 1:19d183cf2689 | 1231 | int length = stringsofar.length(); |
kendunlop | 1:19d183cf2689 | 1232 | //pc.printf("Length is %d/16.\r\n", length); |
kendunlop | 1:19d183cf2689 | 1233 | for (int i = 0; i < (16-length); i++) |
kendunlop | 1:19d183cf2689 | 1234 | stringsofar = "0" + stringsofar; |
kendunlop | 1:19d183cf2689 | 1235 | //pc.printf("stringsofar says '%s'.\r\n", stringsofar); |
kendunlop | 9:7c27efe30a77 | 1236 | //messageOutText = "305 8 " + stringsofar; |
kendunlop | 1:19d183cf2689 | 1237 | //pc.printf("Will try to send '%s'.\r\n", messageOutText); |
kendunlop | 1:19d183cf2689 | 1238 | } |
kendunlop | 1:19d183cf2689 | 1239 | messageOut1.format = CANStandard; |
kendunlop | 11:abd3df435a2b | 1240 | if (c == 'z' and normalkeypresses == 1) |
kendunlop | 1:19d183cf2689 | 1241 | { |
kendunlop | 7:a9150dc1e481 | 1242 | resettest(); |
kendunlop | 11:abd3df435a2b | 1243 | pc.printf("Starting spam sequence for ID %s. Press 'Esc' to end. Press 'o' to increase incrementer.\r\n", spampreamble); |
kendunlop | 10:3e15baff40e7 | 1244 | idlistincrementer = 0x333; |
kendunlop | 10:3e15baff40e7 | 1245 | reportbaudrates(); |
kendunlop | 1:19d183cf2689 | 1246 | spamon = 1; |
kendunlop | 8:6f096b45ca15 | 1247 | aspamisgoing = 1; |
kendunlop | 11:abd3df435a2b | 1248 | normalkeypresses = 0; |
kendunlop | 8:6f096b45ca15 | 1249 | checklaterbytes = 1; |
kendunlop | 1:19d183cf2689 | 1250 | incrementer = 0; |
kendunlop | 2:11339018dda6 | 1251 | spamcount = 0; |
kendunlop | 3:79133dcea836 | 1252 | spamstarttime = clock(); |
kendunlop | 9:7c27efe30a77 | 1253 | //pc.printf("Spam start time is %d.\r\n", spamstarttime); |
kendunlop | 1:19d183cf2689 | 1254 | } |
kendunlop | 11:abd3df435a2b | 1255 | if (c == 27 and spamon == 1)//Cancel normal '333' incrementing spam mode |
kendunlop | 1:19d183cf2689 | 1256 | { |
kendunlop | 10:3e15baff40e7 | 1257 | pc.printf("\r\n"); |
kendunlop | 8:6f096b45ca15 | 1258 | spamon = 2; |
kendunlop | 8:6f096b45ca15 | 1259 | aspamisgoing = 0; |
kendunlop | 11:abd3df435a2b | 1260 | normalkeypresses = 1; |
kendunlop | 3:79133dcea836 | 1261 | spamendtime = clock(); |
kendunlop | 9:7c27efe30a77 | 1262 | getspamsecondstotal();//Process the spam information |
kendunlop | 9:7c27efe30a77 | 1263 | //pc.printf("Total spams ever are %d.\r\n", totalspamsever); |
kendunlop | 7:a9150dc1e481 | 1264 | endtest(); |
kendunlop | 7:a9150dc1e481 | 1265 | //pc.printf("-------------------------\r\n"); |
kendunlop | 3:79133dcea836 | 1266 | } |
kendunlop | 11:abd3df435a2b | 1267 | if (c == 27 and idspamon == 1)//Cancel CAN ID spam mode |
kendunlop | 11:abd3df435a2b | 1268 | { |
kendunlop | 11:abd3df435a2b | 1269 | //pc.printf("Cancelling ID test mode.\r\n"); |
kendunlop | 11:abd3df435a2b | 1270 | testwasaborted = 1; |
kendunlop | 11:abd3df435a2b | 1271 | endidspamtest(); |
kendunlop | 11:abd3df435a2b | 1272 | normalkeypresses = 1; |
kendunlop | 11:abd3df435a2b | 1273 | } |
kendunlop | 11:abd3df435a2b | 1274 | if (c == 'v' and normalkeypresses == 1) |
kendunlop | 3:79133dcea836 | 1275 | { |
kendunlop | 7:a9150dc1e481 | 1276 | resettest(); |
kendunlop | 6:2882710e4f1e | 1277 | idspamon = 1; //Set the 'idspamon' integer to 1 so ID spam happens. |
kendunlop | 8:6f096b45ca15 | 1278 | aspamisgoing = 1; |
kendunlop | 11:abd3df435a2b | 1279 | normalkeypresses = 0; |
kendunlop | 11:abd3df435a2b | 1280 | checklaterbytes = 1; |
kendunlop | 6:2882710e4f1e | 1281 | idlistincrementer = 0; |
kendunlop | 11:abd3df435a2b | 1282 | pc.printf("Beginning check of all possible CAN IDs (000 - 7FF). Press 'Esc' to abort.\r\n"); |
kendunlop | 10:3e15baff40e7 | 1283 | reportbaudrates(); |
kendunlop | 9:7c27efe30a77 | 1284 | spamstarttime = clock(); |
kendunlop | 9:7c27efe30a77 | 1285 | //pc.printf("Spam started at clock time %d.\r\n", spamstarttime); |
kendunlop | 8:6f096b45ca15 | 1286 | if (checklaterbytes == 0) |
kendunlop | 8:6f096b45ca15 | 1287 | { |
kendunlop | 8:6f096b45ca15 | 1288 | pc.printf("Will only check CAN IDs, not later bytes.\r\n"); |
kendunlop | 8:6f096b45ca15 | 1289 | } |
kendunlop | 11:abd3df435a2b | 1290 | if (checklaterbytes == 1) |
kendunlop | 11:abd3df435a2b | 1291 | { |
kendunlop | 11:abd3df435a2b | 1292 | pc.printf("Will check all bytes in each CAN message.\r\n"); |
kendunlop | 11:abd3df435a2b | 1293 | } |
kendunlop | 1:19d183cf2689 | 1294 | } |
kendunlop | 10:3e15baff40e7 | 1295 | if (messageOutText != "" and speedspamison == 0) |
kendunlop | 1:19d183cf2689 | 1296 | { |
kendunlop | 6:2882710e4f1e | 1297 | getCANfrommessageOutText(); |
kendunlop | 7:a9150dc1e481 | 1298 | sendCAN(); |
kendunlop | 7:a9150dc1e481 | 1299 | //CanBus.write(messageOut1); |
kendunlop | 7:a9150dc1e481 | 1300 | //CanBus2.write(messageOut1); |
kendunlop | 6:2882710e4f1e | 1301 | if (spamon == 0 and idspamon == 0) |
kendunlop | 3:79133dcea836 | 1302 | { |
kendunlop | 7:a9150dc1e481 | 1303 | //printMessageOut(); |
kendunlop | 3:79133dcea836 | 1304 | } |
kendunlop | 3:79133dcea836 | 1305 | messageOutText = ""; |
kendunlop | 1:19d183cf2689 | 1306 | } |
kendunlop | 5:bf4c6278ca8b | 1307 | } |
kendunlop | 4:e8e9bc25b1ca | 1308 | } |
kendunlop | 5:bf4c6278ca8b | 1309 | |
kendunlop | 4:e8e9bc25b1ca | 1310 | |
kendunlop | 7:a9150dc1e481 | 1311 | //If spam mode is on, spam an incrementing CAN message |
kendunlop | 8:6f096b45ca15 | 1312 | if (spamon == 1 and expectationresolved == 1) |
kendunlop | 5:bf4c6278ca8b | 1313 | { |
kendunlop | 5:bf4c6278ca8b | 1314 | spamcount ++; |
kendunlop | 5:bf4c6278ca8b | 1315 | totalspamsever ++; |
kendunlop | 5:bf4c6278ca8b | 1316 | incrementer ++; |
kendunlop | 5:bf4c6278ca8b | 1317 | std::stringstream sstream; |
kendunlop | 5:bf4c6278ca8b | 1318 | sstream << std::hex << incrementer; |
kendunlop | 5:bf4c6278ca8b | 1319 | string stringsofar = sstream.str(); |
kendunlop | 5:bf4c6278ca8b | 1320 | int length = stringsofar.length(); |
kendunlop | 5:bf4c6278ca8b | 1321 | for (int i = 0; i < (16-length); i++) |
kendunlop | 5:bf4c6278ca8b | 1322 | stringsofar = "0" + stringsofar; |
kendunlop | 6:2882710e4f1e | 1323 | messageOutText = spampreamble + " 8 " + stringsofar; |
kendunlop | 6:2882710e4f1e | 1324 | getCANfrommessageOutText(); |
kendunlop | 7:a9150dc1e481 | 1325 | sendCAN(); |
kendunlop | 5:bf4c6278ca8b | 1326 | } |
kendunlop | 8:6f096b45ca15 | 1327 | 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 | 1328 | { |
kendunlop | 11:abd3df435a2b | 1329 | endidspamtest(); |
kendunlop | 8:6f096b45ca15 | 1330 | } |
kendunlop | 8:6f096b45ca15 | 1331 | if (idspamon == 1 and expectationresolved == 1) |
kendunlop | 6:2882710e4f1e | 1332 | { |
kendunlop | 9:7c27efe30a77 | 1333 | spamcount++; |
kendunlop | 6:2882710e4f1e | 1334 | std::stringstream sstream; |
kendunlop | 6:2882710e4f1e | 1335 | sstream << std::hex << idlistincrementer; |
kendunlop | 6:2882710e4f1e | 1336 | string idstring = ""; |
kendunlop | 6:2882710e4f1e | 1337 | idstring = sstream.str(); |
kendunlop | 6:2882710e4f1e | 1338 | //pc.printf("idstring says '%s'\r\n", idstring); |
kendunlop | 6:2882710e4f1e | 1339 | if (idstring.length() < 3) |
kendunlop | 6:2882710e4f1e | 1340 | { |
kendunlop | 6:2882710e4f1e | 1341 | for (int i = 0; i < (4 - idstring.length()); i++) |
kendunlop | 6:2882710e4f1e | 1342 | { |
kendunlop | 6:2882710e4f1e | 1343 | idstring = "0" + idstring; |
kendunlop | 6:2882710e4f1e | 1344 | } |
kendunlop | 6:2882710e4f1e | 1345 | } |
kendunlop | 6:2882710e4f1e | 1346 | //pc.printf("idstring now says '%s'\r\n", idstring); |
kendunlop | 6:2882710e4f1e | 1347 | //pc.printf("%s\r\n", idstring); |
kendunlop | 7:a9150dc1e481 | 1348 | idstring = idstring + " 8 01 02 03 04 05 06 07 08"; |
kendunlop | 6:2882710e4f1e | 1349 | //pc.printf("...and now idstring now says '%s'\r\n", idstring); |
kendunlop | 6:2882710e4f1e | 1350 | messageOutText = idstring; |
kendunlop | 6:2882710e4f1e | 1351 | getCANfrommessageOutText(); |
kendunlop | 7:a9150dc1e481 | 1352 | sendCAN(); |
kendunlop | 7:a9150dc1e481 | 1353 | //CanBus.write(messageOut1); |
kendunlop | 7:a9150dc1e481 | 1354 | //CanBus2.write(messageOut1); |
kendunlop | 6:2882710e4f1e | 1355 | //pc.printf("ID list incrementer is now %d\r\n", idlistincrementer); |
kendunlop | 6:2882710e4f1e | 1356 | wait(0.01);//ID spam at 100Hz |
kendunlop | 6:2882710e4f1e | 1357 | } |
kendunlop | 11:abd3df435a2b | 1358 | |
kendunlop | 11:abd3df435a2b | 1359 | //Optional: mention any un-listened-to CAN messages. Having this stopped it from reading messages in when listen was on. |
kendunlop | 11:abd3df435a2b | 1360 | //if (CanBus2.read(messageIn) and listen == 0) |
kendunlop | 10:3e15baff40e7 | 1361 | // { |
kendunlop | 11:abd3df435a2b | 1362 | // pc.printf("(Un-listened-to CAN message came in.)\r\n"); |
kendunlop | 10:3e15baff40e7 | 1363 | // } |
kendunlop | 11:abd3df435a2b | 1364 | |
kendunlop | 11:abd3df435a2b | 1365 | //Check for CAN messages coming in |
kendunlop | 11:abd3df435a2b | 1366 | if (CanBus2.read(messageIn) and listen == 1) |
kendunlop | 7:a9150dc1e481 | 1367 | { |
kendunlop | 10:3e15baff40e7 | 1368 | if (speedspamison == 1) |
kendunlop | 9:7c27efe30a77 | 1369 | { |
kendunlop | 10:3e15baff40e7 | 1370 | messagesincount++; |
kendunlop | 10:3e15baff40e7 | 1371 | //pc.printf("\033[0;36m Speed spam message 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 | 8:6f096b45ca15 | 1372 | } |
kendunlop | 10:3e15baff40e7 | 1373 | if (speedspamison == 0) |
kendunlop | 7:a9150dc1e481 | 1374 | { |
kendunlop | 10:3e15baff40e7 | 1375 | //pc.printf("\033[0;36mNormal message 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 | 10:3e15baff40e7 | 1376 | if (noreplystreak > 0) |
kendunlop | 9:7c27efe30a77 | 1377 | { |
kendunlop | 10:3e15baff40e7 | 1378 | noreplystreak = 0; |
kendunlop | 10:3e15baff40e7 | 1379 | noreplystreakstartid = 0; |
kendunlop | 10:3e15baff40e7 | 1380 | pc.printf("\r\n\n"); |
kendunlop | 10:3e15baff40e7 | 1381 | } |
kendunlop | 10:3e15baff40e7 | 1382 | if (goodnoreplystreak > 0) |
kendunlop | 10:3e15baff40e7 | 1383 | { |
kendunlop | 10:3e15baff40e7 | 1384 | goodnoreplystreak = 0; |
kendunlop | 10:3e15baff40e7 | 1385 | goodnoreplystreakstartid = 0; |
kendunlop | 11:abd3df435a2b | 1386 | if (aspamisgoing == 1) |
kendunlop | 11:abd3df435a2b | 1387 | { |
kendunlop | 11:abd3df435a2b | 1388 | pc.printf("\r\n\n"); |
kendunlop | 11:abd3df435a2b | 1389 | } |
kendunlop | 9:7c27efe30a77 | 1390 | } |
kendunlop | 10:3e15baff40e7 | 1391 | CANCount++; |
kendunlop | 10:3e15baff40e7 | 1392 | if (1 == 1)//Prepare to print out the message coming in |
kendunlop | 7:a9150dc1e481 | 1393 | { |
kendunlop | 10:3e15baff40e7 | 1394 | //Data shouldn't come in at all if 'shouldntcomein' is 1! |
kendunlop | 11:abd3df435a2b | 1395 | //pc.printf("Wah! Unexpected CAN came in!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\r\n"); |
kendunlop | 10:3e15baff40e7 | 1396 | if (shouldntcomein == 1) |
kendunlop | 9:7c27efe30a77 | 1397 | { |
kendunlop | 11:abd3df435a2b | 1398 | if (failsstreak > 0) |
kendunlop | 11:abd3df435a2b | 1399 | { |
kendunlop | 11:abd3df435a2b | 1400 | failsstreak = 0; |
kendunlop | 11:abd3df435a2b | 1401 | failsstreakstart = 0; |
kendunlop | 11:abd3df435a2b | 1402 | if (aspamisgoing == 1) |
kendunlop | 11:abd3df435a2b | 1403 | { |
kendunlop | 11:abd3df435a2b | 1404 | pc.printf("\n\n\r"); |
kendunlop | 11:abd3df435a2b | 1405 | } |
kendunlop | 11:abd3df435a2b | 1406 | } |
kendunlop | 10:3e15baff40e7 | 1407 | if (unexpectedCANstreak == 0)//If it's the start of a new unexpected-CAN streak, mark the starting ID. |
kendunlop | 10:3e15baff40e7 | 1408 | { |
kendunlop | 10:3e15baff40e7 | 1409 | unexpectedCANstreakstartid = idlistincrementer; |
kendunlop | 10:3e15baff40e7 | 1410 | } |
kendunlop | 10:3e15baff40e7 | 1411 | if (aspamisgoing == 0) |
kendunlop | 10:3e15baff40e7 | 1412 | { |
kendunlop | 10:3e15baff40e7 | 1413 | unexpectedCANstreakstartid = idlistincrementer; |
kendunlop | 10:3e15baff40e7 | 1414 | } |
kendunlop | 11:abd3df435a2b | 1415 | reportsentCAN(); |
kendunlop | 10:3e15baff40e7 | 1416 | if (unexpectedCANstreakstartid != idlistincrementer) |
kendunlop | 10:3e15baff40e7 | 1417 | { |
kendunlop | 10:3e15baff40e7 | 1418 | pc.printf("\033[0;35mUnexpected data came in for IDs %03X to %03X!\033[0m", unexpectedCANstreakstartid, idlistincrementer); |
kendunlop | 10:3e15baff40e7 | 1419 | } |
kendunlop | 10:3e15baff40e7 | 1420 | if (unexpectedCANstreakstartid == idlistincrementer) |
kendunlop | 10:3e15baff40e7 | 1421 | { |
kendunlop | 10:3e15baff40e7 | 1422 | pc.printf("\033[0;35mUnexpected data came in for %03X!\033[0m", idlistincrementer); |
kendunlop | 10:3e15baff40e7 | 1423 | } |
kendunlop | 10:3e15baff40e7 | 1424 | failsthistest++; |
kendunlop | 10:3e15baff40e7 | 1425 | unexpectedCANfails++; |
kendunlop | 10:3e15baff40e7 | 1426 | unexpectedCANstreak++; |
kendunlop | 10:3e15baff40e7 | 1427 | } |
kendunlop | 10:3e15baff40e7 | 1428 | //Establish earlier whether it's a match in the 10 places |
kendunlop | 10:3e15baff40e7 | 1429 | int itsamatch = 1; |
kendunlop | 10:3e15baff40e7 | 1430 | if (expected1 != messageIn.id) |
kendunlop | 10:3e15baff40e7 | 1431 | {itsamatch = 0;} |
kendunlop | 10:3e15baff40e7 | 1432 | if (expected2 != messageIn.len and checklaterbytes >= 1) |
kendunlop | 10:3e15baff40e7 | 1433 | {itsamatch = 0;} |
kendunlop | 10:3e15baff40e7 | 1434 | if (expected3 != messageIn.data[0] and checklaterbytes >= 1) |
kendunlop | 10:3e15baff40e7 | 1435 | {itsamatch = 0;} |
kendunlop | 10:3e15baff40e7 | 1436 | if (expected4 != messageIn.data[1] and checklaterbytes >= 1) |
kendunlop | 10:3e15baff40e7 | 1437 | {itsamatch = 0;} |
kendunlop | 10:3e15baff40e7 | 1438 | if (expected5 != messageIn.data[2] and checklaterbytes >= 1) |
kendunlop | 10:3e15baff40e7 | 1439 | {itsamatch = 0;} |
kendunlop | 10:3e15baff40e7 | 1440 | if (expected6 != messageIn.data[3] and checklaterbytes >= 1) |
kendunlop | 10:3e15baff40e7 | 1441 | {itsamatch = 0;} |
kendunlop | 10:3e15baff40e7 | 1442 | if (expected7 != messageIn.data[4] and checklaterbytes >= 1) |
kendunlop | 10:3e15baff40e7 | 1443 | {itsamatch = 0;} |
kendunlop | 10:3e15baff40e7 | 1444 | if (expected8 != messageIn.data[5] and checklaterbytes >= 1) |
kendunlop | 10:3e15baff40e7 | 1445 | {itsamatch = 0;} |
kendunlop | 10:3e15baff40e7 | 1446 | if (expected9 != messageIn.data[6] and checklaterbytes >= 1) |
kendunlop | 10:3e15baff40e7 | 1447 | {itsamatch = 0;} |
kendunlop | 10:3e15baff40e7 | 1448 | if (expected10 != messageIn.data[7] and checklaterbytes >= 1) |
kendunlop | 10:3e15baff40e7 | 1449 | {itsamatch = 0;} |
kendunlop | 11:abd3df435a2b | 1450 | |
kendunlop | 10:3e15baff40e7 | 1451 | if (itsamatch == 0 and shouldntcomein == 0) |
kendunlop | 10:3e15baff40e7 | 1452 | { |
kendunlop | 12:e950feba910d | 1453 | if (goodnoreplystreak > 0) |
kendunlop | 12:e950feba910d | 1454 | { |
kendunlop | 12:e950feba910d | 1455 | goodnoreplystreak = 0; |
kendunlop | 12:e950feba910d | 1456 | goodnoreplystreakstartid = 0; |
kendunlop | 12:e950feba910d | 1457 | if (aspamisgoing == 1) |
kendunlop | 12:e950feba910d | 1458 | { |
kendunlop | 12:e950feba910d | 1459 | pc.printf("\r\n\n"); |
kendunlop | 12:e950feba910d | 1460 | } |
kendunlop | 12:e950feba910d | 1461 | } |
kendunlop | 12:e950feba910d | 1462 | if (CANmatchstreak > 0) |
kendunlop | 12:e950feba910d | 1463 | { |
kendunlop | 12:e950feba910d | 1464 | CANmatchstreak = 0; |
kendunlop | 12:e950feba910d | 1465 | CANmatchstreakstartID = 0; |
kendunlop | 12:e950feba910d | 1466 | if (aspamisgoing == 1) |
kendunlop | 12:e950feba910d | 1467 | { |
kendunlop | 12:e950feba910d | 1468 | pc.printf("\r\n\n"); |
kendunlop | 12:e950feba910d | 1469 | } |
kendunlop | 12:e950feba910d | 1470 | } |
kendunlop | 11:abd3df435a2b | 1471 | if (failsstreak == 0) |
kendunlop | 11:abd3df435a2b | 1472 | { |
kendunlop | 11:abd3df435a2b | 1473 | failsstreakstart = idlistincrementer; |
kendunlop | 11:abd3df435a2b | 1474 | } |
kendunlop | 11:abd3df435a2b | 1475 | failsstreak++; |
kendunlop | 11:abd3df435a2b | 1476 | if (aspamisgoing == 0) |
kendunlop | 11:abd3df435a2b | 1477 | { |
kendunlop | 11:abd3df435a2b | 1478 | failsstreakstart = idlistincrementer; |
kendunlop | 11:abd3df435a2b | 1479 | } |
kendunlop | 11:abd3df435a2b | 1480 | reportsentCAN(); //Report sent CAN before any 'does not match...' messages |
kendunlop | 11:abd3df435a2b | 1481 | if (failsstreakstart == idlistincrementer) |
kendunlop | 11:abd3df435a2b | 1482 | { |
kendunlop | 11:abd3df435a2b | 1483 | pc.printf("\033[0;31mData for ID %03X does not match expectation!\033[0m", idlistincrementer); |
kendunlop | 11:abd3df435a2b | 1484 | } |
kendunlop | 11:abd3df435a2b | 1485 | if (failsstreakstart != idlistincrementer) |
kendunlop | 11:abd3df435a2b | 1486 | { |
kendunlop | 11:abd3df435a2b | 1487 | pc.printf("\033[0;31mData for IDs %03X to %03X does not match expectation!\033[0m", failsstreakstart, idlistincrementer); |
kendunlop | 11:abd3df435a2b | 1488 | } |
kendunlop | 11:abd3df435a2b | 1489 | } |
kendunlop | 11:abd3df435a2b | 1490 | //Decide if a message in will need to be printed ('Message received was:...') |
kendunlop | 11:abd3df435a2b | 1491 | messageinisneeded = 0; |
kendunlop | 11:abd3df435a2b | 1492 | if (aspamisgoing == 0) |
kendunlop | 11:abd3df435a2b | 1493 | {messageinisneeded = 1;} |
kendunlop | 13:e3a4abc461f4 | 1494 | if (itsamatch == 0 and shouldntcomein == 0 and reportfailsindividually == 1) |
kendunlop | 11:abd3df435a2b | 1495 | {messageinisneeded = 1;} |
kendunlop | 11:abd3df435a2b | 1496 | // If a message in is destined to be printed, reset the failsstreak if needed |
kendunlop | 11:abd3df435a2b | 1497 | if (messageinisneeded == 1 and failsstreak > 0) |
kendunlop | 11:abd3df435a2b | 1498 | { |
kendunlop | 11:abd3df435a2b | 1499 | failsstreak = 0; |
kendunlop | 11:abd3df435a2b | 1500 | failsstreakstart = 0; |
kendunlop | 10:3e15baff40e7 | 1501 | } |
kendunlop | 10:3e15baff40e7 | 1502 | // Once it's known whether the data matches expectation, set the match streak |
kendunlop | 10:3e15baff40e7 | 1503 | if (itsamatch == 1 and shouldntcomein == 0) |
kendunlop | 10:3e15baff40e7 | 1504 | { |
kendunlop | 11:abd3df435a2b | 1505 | if (failsstreak > 0) |
kendunlop | 11:abd3df435a2b | 1506 | { |
kendunlop | 11:abd3df435a2b | 1507 | failsstreak = 0; |
kendunlop | 11:abd3df435a2b | 1508 | failsstreakstart = 0; |
kendunlop | 11:abd3df435a2b | 1509 | if (aspamisgoing == 1) |
kendunlop | 11:abd3df435a2b | 1510 | { |
kendunlop | 11:abd3df435a2b | 1511 | pc.printf("\r\n\n"); |
kendunlop | 11:abd3df435a2b | 1512 | } |
kendunlop | 11:abd3df435a2b | 1513 | } |
kendunlop | 10:3e15baff40e7 | 1514 | if (CANmatchstreak == 0) |
kendunlop | 10:3e15baff40e7 | 1515 | { |
kendunlop | 10:3e15baff40e7 | 1516 | CANmatchstreakstartID = idlistincrementer; |
kendunlop | 10:3e15baff40e7 | 1517 | } |
kendunlop | 10:3e15baff40e7 | 1518 | CANmatchstreak++; |
kendunlop | 10:3e15baff40e7 | 1519 | } |
kendunlop | 10:3e15baff40e7 | 1520 | if (itsamatch == 0 and CANmatchstreak > 0) //If it's NOT a match, break any CANmatchstreak |
kendunlop | 10:3e15baff40e7 | 1521 | { |
kendunlop | 10:3e15baff40e7 | 1522 | CANmatchstreak = 0; |
kendunlop | 10:3e15baff40e7 | 1523 | CANmatchstreakstartID = 0; |
kendunlop | 10:3e15baff40e7 | 1524 | } |
kendunlop | 10:3e15baff40e7 | 1525 | // Print needed message |
kendunlop | 11:abd3df435a2b | 1526 | reportsentCAN(); |
kendunlop | 10:3e15baff40e7 | 1527 | if (messageinisneeded == 0) //If a message in is NOT needed, simply report what the CAN is doing on the same line. |
kendunlop | 10:3e15baff40e7 | 1528 | { |
kendunlop | 10:3e15baff40e7 | 1529 | if (itsamatch == 1 and CANmatchstreakstartID == idlistincrementer and CANmatchstreak > 0) |
kendunlop | 10:3e15baff40e7 | 1530 | { |
kendunlop | 10:3e15baff40e7 | 1531 | pc.printf("\033[0;32mCAN matches expected values on ID %03X.\033[0m", idlistincrementer); |
kendunlop | 10:3e15baff40e7 | 1532 | } |
kendunlop | 10:3e15baff40e7 | 1533 | if (itsamatch == 1 and CANmatchstreakstartID != idlistincrementer and CANmatchstreak > 0) |
kendunlop | 10:3e15baff40e7 | 1534 | { |
kendunlop | 10:3e15baff40e7 | 1535 | pc.printf("\033[0;32mCAN matches expected values from IDs %03X to %03X.\033[0m", CANmatchstreakstartID, idlistincrementer); |
kendunlop | 10:3e15baff40e7 | 1536 | } |
kendunlop | 9:7c27efe30a77 | 1537 | } |
kendunlop | 10:3e15baff40e7 | 1538 | if (messageinisneeded == 1) //Only if message in is needed do you need to look at printing each part of the message |
kendunlop | 9:7c27efe30a77 | 1539 | { |
kendunlop | 10:3e15baff40e7 | 1540 | pc.printf("\r\nMessage received was: "); |
kendunlop | 10:3e15baff40e7 | 1541 | if (expected1 != messageIn.id) |
kendunlop | 10:3e15baff40e7 | 1542 | { |
kendunlop | 10:3e15baff40e7 | 1543 | //pc.printf("\r\nID of %d does NOT match %d.\r\n", messageIn.id, expected1); |
kendunlop | 10:3e15baff40e7 | 1544 | pc.printf("\033[1;31m%03X\033[0m ", messageIn.id);//Show the ID in red if it doesn't match. |
kendunlop | 10:3e15baff40e7 | 1545 | } |
kendunlop | 10:3e15baff40e7 | 1546 | if (expected1 == messageIn.id) |
kendunlop | 10:3e15baff40e7 | 1547 | { |
kendunlop | 10:3e15baff40e7 | 1548 | pc.printf("\033[1;32m%03X\033[0m ", messageIn.id);//Show the ID in green if it matches. |
kendunlop | 10:3e15baff40e7 | 1549 | } |
kendunlop | 10:3e15baff40e7 | 1550 | if (expected2 != messageIn.len and checklaterbytes >= 1) |
kendunlop | 10:3e15baff40e7 | 1551 | { |
kendunlop | 10:3e15baff40e7 | 1552 | //pc.printf("Length of %d does NOT match %d.\r\n", messageIn.len, expected2); |
kendunlop | 10:3e15baff40e7 | 1553 | pc.printf("\033[1;31m%01X\033[0m ", messageIn.len); |
kendunlop | 10:3e15baff40e7 | 1554 | } |
kendunlop | 10:3e15baff40e7 | 1555 | else |
kendunlop | 10:3e15baff40e7 | 1556 | { |
kendunlop | 11:abd3df435a2b | 1557 | pc.printf("\033[1;32m%01X\033[0m ", messageIn.len); |
kendunlop | 10:3e15baff40e7 | 1558 | } |
kendunlop | 10:3e15baff40e7 | 1559 | if (expected3 != messageIn.data[0] and checklaterbytes >= 1) |
kendunlop | 10:3e15baff40e7 | 1560 | { |
kendunlop | 10:3e15baff40e7 | 1561 | //pc.printf("Data 0 of %d does NOT match %d.\r\n", messageIn.data[0], expected3); |
kendunlop | 10:3e15baff40e7 | 1562 | pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[0]); |
kendunlop | 10:3e15baff40e7 | 1563 | } |
kendunlop | 10:3e15baff40e7 | 1564 | else |
kendunlop | 10:3e15baff40e7 | 1565 | { |
kendunlop | 11:abd3df435a2b | 1566 | pc.printf("\033[1;32m%02X\033[0m ", messageIn.data[0]); |
kendunlop | 10:3e15baff40e7 | 1567 | } |
kendunlop | 10:3e15baff40e7 | 1568 | if (expected4 != messageIn.data[1] and checklaterbytes >= 1) |
kendunlop | 10:3e15baff40e7 | 1569 | { |
kendunlop | 10:3e15baff40e7 | 1570 | //pc.printf("Data 1 of %d does NOT match %d.\r\n", messageIn.data[1], expected4); |
kendunlop | 10:3e15baff40e7 | 1571 | pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[1]); |
kendunlop | 10:3e15baff40e7 | 1572 | } |
kendunlop | 10:3e15baff40e7 | 1573 | else |
kendunlop | 10:3e15baff40e7 | 1574 | { |
kendunlop | 11:abd3df435a2b | 1575 | pc.printf("\033[1;32m%02X\033[0m ", messageIn.data[1]); |
kendunlop | 10:3e15baff40e7 | 1576 | } |
kendunlop | 10:3e15baff40e7 | 1577 | if (expected5 != messageIn.data[2] and checklaterbytes >= 1) |
kendunlop | 10:3e15baff40e7 | 1578 | { |
kendunlop | 10:3e15baff40e7 | 1579 | //pc.printf("Data 2 of %d does NOT match %d.\r\n", messageIn.data[2], expected5); |
kendunlop | 10:3e15baff40e7 | 1580 | pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[2]); |
kendunlop | 10:3e15baff40e7 | 1581 | } |
kendunlop | 10:3e15baff40e7 | 1582 | else |
kendunlop | 10:3e15baff40e7 | 1583 | { |
kendunlop | 11:abd3df435a2b | 1584 | pc.printf("\033[1;32m%02X\033[0m ", messageIn.data[2]); |
kendunlop | 10:3e15baff40e7 | 1585 | } |
kendunlop | 10:3e15baff40e7 | 1586 | if (expected6 != messageIn.data[3] and checklaterbytes >= 1) |
kendunlop | 10:3e15baff40e7 | 1587 | { |
kendunlop | 10:3e15baff40e7 | 1588 | //pc.printf("Data 3 of %d does NOT match %d.\r\n", messageIn.data[3], expected6); |
kendunlop | 10:3e15baff40e7 | 1589 | pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[3]); |
kendunlop | 10:3e15baff40e7 | 1590 | } |
kendunlop | 10:3e15baff40e7 | 1591 | |
kendunlop | 10:3e15baff40e7 | 1592 | else |
kendunlop | 10:3e15baff40e7 | 1593 | { |
kendunlop | 11:abd3df435a2b | 1594 | pc.printf("\033[1;32m%02X\033[0m ", messageIn.data[3]); |
kendunlop | 10:3e15baff40e7 | 1595 | } |
kendunlop | 10:3e15baff40e7 | 1596 | if (expected7 != messageIn.data[4] and checklaterbytes >= 1) |
kendunlop | 10:3e15baff40e7 | 1597 | { |
kendunlop | 10:3e15baff40e7 | 1598 | //pc.printf("Data 4 of %d does NOT match %d.\r\n", messageIn.data[4], expected7); |
kendunlop | 10:3e15baff40e7 | 1599 | pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[4]); |
kendunlop | 10:3e15baff40e7 | 1600 | } |
kendunlop | 10:3e15baff40e7 | 1601 | else |
kendunlop | 10:3e15baff40e7 | 1602 | { |
kendunlop | 11:abd3df435a2b | 1603 | pc.printf("\033[1;32m%02X\033[0m ", messageIn.data[4]); |
kendunlop | 10:3e15baff40e7 | 1604 | } |
kendunlop | 10:3e15baff40e7 | 1605 | if (expected8 != messageIn.data[5] and checklaterbytes >= 1) |
kendunlop | 10:3e15baff40e7 | 1606 | { |
kendunlop | 10:3e15baff40e7 | 1607 | //pc.printf("Data 5 of %d does NOT match %d.\r\n", messageIn.data[5], expected8); |
kendunlop | 10:3e15baff40e7 | 1608 | pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[5]); |
kendunlop | 10:3e15baff40e7 | 1609 | } |
kendunlop | 10:3e15baff40e7 | 1610 | else |
kendunlop | 10:3e15baff40e7 | 1611 | { |
kendunlop | 11:abd3df435a2b | 1612 | pc.printf("\033[1;32m%02X\033[0m ", messageIn.data[5]); |
kendunlop | 10:3e15baff40e7 | 1613 | } |
kendunlop | 10:3e15baff40e7 | 1614 | if (expected9 != messageIn.data[6] and checklaterbytes >= 1) |
kendunlop | 10:3e15baff40e7 | 1615 | { |
kendunlop | 10:3e15baff40e7 | 1616 | //pc.printf("Data 6 of %d does NOT match %d.\r\n", messageIn.data[6], expected9); |
kendunlop | 10:3e15baff40e7 | 1617 | pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[6]); |
kendunlop | 10:3e15baff40e7 | 1618 | } |
kendunlop | 10:3e15baff40e7 | 1619 | else |
kendunlop | 10:3e15baff40e7 | 1620 | { |
kendunlop | 11:abd3df435a2b | 1621 | pc.printf("\033[1;32m%02X\033[0m ", messageIn.data[6]); |
kendunlop | 10:3e15baff40e7 | 1622 | } |
kendunlop | 10:3e15baff40e7 | 1623 | if (expected10 != messageIn.data[7] and checklaterbytes >= 1) |
kendunlop | 10:3e15baff40e7 | 1624 | { |
kendunlop | 10:3e15baff40e7 | 1625 | //pc.printf("Data 7 of %d does NOT match %d.\r\n", messageIn.data[7], expected10); |
kendunlop | 10:3e15baff40e7 | 1626 | pc.printf("\033[1;31m%02X\033[0m ", messageIn.data[7]); |
kendunlop | 10:3e15baff40e7 | 1627 | } |
kendunlop | 10:3e15baff40e7 | 1628 | else |
kendunlop | 10:3e15baff40e7 | 1629 | { |
kendunlop | 11:abd3df435a2b | 1630 | pc.printf("\033[1;32m%02X\033[0m ", messageIn.data[7]); |
kendunlop | 10:3e15baff40e7 | 1631 | } |
kendunlop | 11:abd3df435a2b | 1632 | if (messageinisneeded == 1) |
kendunlop | 10:3e15baff40e7 | 1633 | { |
kendunlop | 11:abd3df435a2b | 1634 | //pc.printf("\r\n(Aftersentoutmessage)"); |
kendunlop | 10:3e15baff40e7 | 1635 | 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 | 10:3e15baff40e7 | 1636 | } |
kendunlop | 9:7c27efe30a77 | 1637 | } |
kendunlop | 10:3e15baff40e7 | 1638 | if (itsamatch == 0) |
kendunlop | 9:7c27efe30a77 | 1639 | { |
kendunlop | 10:3e15baff40e7 | 1640 | mismatchfailsthistest++; |
kendunlop | 10:3e15baff40e7 | 1641 | if (aspamisgoing == 1) |
kendunlop | 10:3e15baff40e7 | 1642 | { |
kendunlop | 11:abd3df435a2b | 1643 | //pc.printf("\r\n(ToseparateMRWfromDNM)"); //Having a new line here separated 'message received was:...' from 'Does NOT match...' too much. |
kendunlop | 10:3e15baff40e7 | 1644 | } |
kendunlop | 11:abd3df435a2b | 1645 | if (messageinisneeded == 1) |
kendunlop | 10:3e15baff40e7 | 1646 | { |
kendunlop | 11:abd3df435a2b | 1647 | pc.printf("Does NOT match : %03X %01X %02X %02X %02X %02X %02X %02X %02X %02X\r", expected1, expected2, expected3, expected4, expected5, expected6, expected7, expected8, expected9, expected10); |
kendunlop | 11:abd3df435a2b | 1648 | //pc.printf("Check later bytes is %d.\r\n", checklaterbytes); |
kendunlop | 11:abd3df435a2b | 1649 | if (messageinisneeded == 1 and aspamisgoing == 0) |
kendunlop | 11:abd3df435a2b | 1650 | { |
kendunlop | 11:abd3df435a2b | 1651 | pc.printf("\n\n"); |
kendunlop | 11:abd3df435a2b | 1652 | } |
kendunlop | 11:abd3df435a2b | 1653 | if (messageinisneeded == 1 and aspamisgoing == 1) |
kendunlop | 11:abd3df435a2b | 1654 | { |
kendunlop | 11:abd3df435a2b | 1655 | pc.printf("\n\n"); |
kendunlop | 11:abd3df435a2b | 1656 | } |
kendunlop | 10:3e15baff40e7 | 1657 | } |
kendunlop | 10:3e15baff40e7 | 1658 | failsthistest++; //Add 1 to failsthistest to mark the fail |
kendunlop | 10:3e15baff40e7 | 1659 | expectationwasfulfilled = 0; |
kendunlop | 10:3e15baff40e7 | 1660 | expectationresolved = 1; |
kendunlop | 10:3e15baff40e7 | 1661 | } |
kendunlop | 10:3e15baff40e7 | 1662 | if (itsamatch == 1) |
kendunlop | 10:3e15baff40e7 | 1663 | { |
kendunlop | 10:3e15baff40e7 | 1664 | expectationwasfulfilled = 1; |
kendunlop | 10:3e15baff40e7 | 1665 | expectationresolved = 1; |
kendunlop | 10:3e15baff40e7 | 1666 | matchesthistest++; |
kendunlop | 10:3e15baff40e7 | 1667 | //If it's a match, write out a 'Matches expectation...' message |
kendunlop | 11:abd3df435a2b | 1668 | if (messageinisneeded == 1) |
kendunlop | 10:3e15baff40e7 | 1669 | { |
kendunlop | 11:abd3df435a2b | 1670 | pc.printf("Matches expectation : %03X %01X %02X %02X %02X %02X %02X %02X %02X %02X\r", expected1, expected2, expected3, expected4, expected5, expected6, expected7, expected8, expected9, expected10); |
kendunlop | 11:abd3df435a2b | 1671 | if (aspamisgoing == 0) |
kendunlop | 11:abd3df435a2b | 1672 | { |
kendunlop | 11:abd3df435a2b | 1673 | pc.printf("\n\n"); |
kendunlop | 11:abd3df435a2b | 1674 | } |
kendunlop | 10:3e15baff40e7 | 1675 | } |
kendunlop | 9:7c27efe30a77 | 1676 | } |
kendunlop | 9:7c27efe30a77 | 1677 | } |
kendunlop | 10:3e15baff40e7 | 1678 | if (idspamon == 1) |
kendunlop | 9:7c27efe30a77 | 1679 | { |
kendunlop | 10:3e15baff40e7 | 1680 | idscheckedcount++; |
kendunlop | 10:3e15baff40e7 | 1681 | idlistincrementer++; //Only add 1 to the idlist incrementer at the end so the first ID it sends is '000'. |
kendunlop | 7:a9150dc1e481 | 1682 | } |
kendunlop | 11:abd3df435a2b | 1683 | if (customentry == 1) |
kendunlop | 11:abd3df435a2b | 1684 | { |
kendunlop | 11:abd3df435a2b | 1685 | pc.printf("Enter CAN ID to send: "); |
kendunlop | 11:abd3df435a2b | 1686 | } |
kendunlop | 7:a9150dc1e481 | 1687 | } |
kendunlop | 10:3e15baff40e7 | 1688 | //If speed-spam is on, spam a CAN message as fast as possible. |
kendunlop | 1:19d183cf2689 | 1689 | } |
kendunlop | 10:3e15baff40e7 | 1690 | if (speedspamison == 1) |
kendunlop | 10:3e15baff40e7 | 1691 | { |
kendunlop | 10:3e15baff40e7 | 1692 | //sendCAN(); |
kendunlop | 10:3e15baff40e7 | 1693 | CanBus.write(messageOut1); |
kendunlop | 10:3e15baff40e7 | 1694 | spamcount++; |
kendunlop | 10:3e15baff40e7 | 1695 | //pc.printf("\rSent %d '%03X %01X %02X %02X %02X %02X %02X %02X %02X %02X' messages...", spamcount, 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 | 11:abd3df435a2b | 1696 | //Pressing 'Esc' switches off speed spam mode |
kendunlop | 11:abd3df435a2b | 1697 | wait(0.00001); |
kendunlop | 10:3e15baff40e7 | 1698 | } |
kendunlop | 10:3e15baff40e7 | 1699 | } |
kendunlop | 10:3e15baff40e7 | 1700 | } |