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