Kenneth Dunlop / Mbed 2 deprecated Ken_CAN_test

Dependencies:   mbed

Committer:
kendunlop
Date:
Tue Jun 21 08:44:17 2022 +0000
Revision:
7:a9150dc1e481
Parent:
6:2882710e4f1e
Child:
8:6f096b45ca15
Here's a partially-complete version of Ken_CAN_test. So far you can; - Send various CAN IDs with the number keys; - Press 'Z' to start spamming incrementing CAN messages; - Press 'V' to start spamming incrementing IDs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kendunlop 1:19d183cf2689 1 //This program was created by Ken 8th June 2022 to test the CAN output of a CAN Gateway.
kendunlop 4:e8e9bc25b1ca 2 #define kMajorVersion 0 //Defines a major and minor version to be shown later in the program.
kendunlop 1:19d183cf2689 3 #define kMinorVersion 5
kendunlop 1:19d183cf2689 4
kendunlop 1:19d183cf2689 5 #include "mbed.h" //Including mbed libraries. This should be all it needs to use the Mbed and CAN.
kendunlop 1:19d183cf2689 6 #include <string> //This is needed to make text strings work
kendunlop 1:19d183cf2689 7 #include <stdio.h>
kendunlop 1:19d183cf2689 8 #include <ctype.h> //For 'is digit' condition
kendunlop 1:19d183cf2689 9 #include <sstream> //This is used to convert an integer to hex
kendunlop 2:11339018dda6 10 #include <time.h> //To measure how long a process took? Includes clock_t and CLOCKS_PER_SEC
kendunlop 7:a9150dc1e481 11 //#include <list> //To let me make lists of CAN IDs
kendunlop 1:19d183cf2689 12
kendunlop 3:79133dcea836 13 RawSerial pc(USBTX, USBRX); // USB UART Terminal, tx, rx. Is needed for 'pc.printf' functions to work.
kendunlop 3:79133dcea836 14
kendunlop 3:79133dcea836 15 // NOTE: Original pins for CAN Bus 1 are 9 and 10.
kendunlop 7:a9150dc1e481 16 CAN CanBus(p9, p10); //CANBus to use pins 9 and 10. This defines CanBus so other 'CanBus' lines will work.
kendunlop 7:a9150dc1e481 17 CAN CanBus2(p30, p29); //CANBus2 for recieving to use pins 30 and 29 (in that order!).
kendunlop 1:19d183cf2689 18 CANMessage messageIn;
kendunlop 1:19d183cf2689 19
kendunlop 1:19d183cf2689 20 //Define a CAN message to send
kendunlop 1:19d183cf2689 21 CANMessage messageOut1;
kendunlop 1:19d183cf2689 22
kendunlop 1:19d183cf2689 23 //Set messageOutText to a default message so it's defined.
kendunlop 5:bf4c6278ca8b 24 string messageOutText = "";
kendunlop 7:a9150dc1e481 25 string messageOutTextWithSpaces = "";
kendunlop 1:19d183cf2689 26 string part = "";
kendunlop 1:19d183cf2689 27 int partincrement = 0;
kendunlop 3:79133dcea836 28 __int64 incrementer = 0;
kendunlop 1:19d183cf2689 29 int CANCount = 0;
kendunlop 4:e8e9bc25b1ca 30 int spamon = 0;
kendunlop 6:2882710e4f1e 31 int idspamon = 0;
kendunlop 1:19d183cf2689 32 int spamcount = 0;
kendunlop 3:79133dcea836 33 int totalspamsever = 0;
kendunlop 2:11339018dda6 34 int spamstarttime = 0;
kendunlop 2:11339018dda6 35 int spamendtime = 0;
kendunlop 3:79133dcea836 36 int vlistincrementer = 0;
kendunlop 6:2882710e4f1e 37 int idlistincrementer = 0;
kendunlop 6:2882710e4f1e 38 string spampreamble = "333";
kendunlop 7:a9150dc1e481 39 int expected1 = 0;
kendunlop 7:a9150dc1e481 40 int expected2 = 0;
kendunlop 7:a9150dc1e481 41 unsigned char expected3 = 0;
kendunlop 7:a9150dc1e481 42 unsigned char expected4 = 0;
kendunlop 7:a9150dc1e481 43 unsigned char expected5 = 0;
kendunlop 7:a9150dc1e481 44 unsigned char expected6 = 0;
kendunlop 7:a9150dc1e481 45 unsigned char expected7 = 0;
kendunlop 7:a9150dc1e481 46 unsigned char expected8 = 0;
kendunlop 7:a9150dc1e481 47 unsigned char expected9 = 0;
kendunlop 7:a9150dc1e481 48 unsigned char expected10 = 0;
kendunlop 7:a9150dc1e481 49 int failsthistest = 0;
kendunlop 7:a9150dc1e481 50 int addnewlinetonextmessage = 0;
kendunlop 7:a9150dc1e481 51
kendunlop 7:a9150dc1e481 52 //A function to clear all test-related variables
kendunlop 7:a9150dc1e481 53 void resettest(void)
kendunlop 7:a9150dc1e481 54 {
kendunlop 7:a9150dc1e481 55 pc.printf("Resetting test.\r\n");
kendunlop 7:a9150dc1e481 56 failsthistest = 0;
kendunlop 7:a9150dc1e481 57 }
kendunlop 7:a9150dc1e481 58
kendunlop 7:a9150dc1e481 59 //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 60 void addnewlineifneeded(void)
kendunlop 7:a9150dc1e481 61 {
kendunlop 7:a9150dc1e481 62 if (addnewlinetonextmessage == 1)
kendunlop 7:a9150dc1e481 63 {
kendunlop 7:a9150dc1e481 64 pc.printf("\r\n");
kendunlop 7:a9150dc1e481 65 addnewlinetonextmessage = 0;
kendunlop 7:a9150dc1e481 66 }
kendunlop 7:a9150dc1e481 67 }
kendunlop 7:a9150dc1e481 68
kendunlop 7:a9150dc1e481 69
kendunlop 7:a9150dc1e481 70 //A function to end a test and report on findings.
kendunlop 7:a9150dc1e481 71 void endtest()
kendunlop 7:a9150dc1e481 72 {
kendunlop 7:a9150dc1e481 73 if (failsthistest == 0)
kendunlop 7:a9150dc1e481 74 {
kendunlop 7:a9150dc1e481 75 pc.printf("\033[1;32mTest complete. %d failures found.\033[0m\r\n", failsthistest);
kendunlop 7:a9150dc1e481 76 }
kendunlop 7:a9150dc1e481 77 if (failsthistest > 0)
kendunlop 7:a9150dc1e481 78 {
kendunlop 7:a9150dc1e481 79 pc.printf("\033[1;31mTest complete. %d failures found.\033[0m\r\n", failsthistest);
kendunlop 7:a9150dc1e481 80 }
kendunlop 7:a9150dc1e481 81 addnewlineifneeded();
kendunlop 7:a9150dc1e481 82 pc.printf("-------------------------------------\r\n");
kendunlop 7:a9150dc1e481 83 failsthistest = 0;
kendunlop 7:a9150dc1e481 84 }
kendunlop 7:a9150dc1e481 85
kendunlop 7:a9150dc1e481 86 //A generic 'send CAN' function to send CAN with a standard message
kendunlop 7:a9150dc1e481 87 void sendCAN(void)
kendunlop 7:a9150dc1e481 88 {
kendunlop 7:a9150dc1e481 89 CanBus.write(messageOut1);
kendunlop 7:a9150dc1e481 90 //addnewlineifneeded();
kendunlop 7:a9150dc1e481 91 pc.printf("\033[1;33m%s\033[0m\r", messageOutTextWithSpaces);
kendunlop 7:a9150dc1e481 92 //addnewlinetonextmessage = 1;
kendunlop 7:a9150dc1e481 93 }
kendunlop 1:19d183cf2689 94
kendunlop 2:11339018dda6 95 //A function to deal with each CAN message part (e.g.: 301, 8, 01, 02...)
kendunlop 1:19d183cf2689 96 void dealwithpart(void)
kendunlop 1:19d183cf2689 97 {
kendunlop 1:19d183cf2689 98 int hextotal = 0;
kendunlop 1:19d183cf2689 99 partincrement = partincrement + 1;
kendunlop 1:19d183cf2689 100 //pc.printf("Dealing with part %d. (%s)\r\n", partincrement, part);
kendunlop 1:19d183cf2689 101 //int partincrementb = 0;
kendunlop 1:19d183cf2689 102 int textlength = part.length();
kendunlop 1:19d183cf2689 103 //int characterinc = 0;
kendunlop 1:19d183cf2689 104 //pc.printf("That's %d characters long.\r\n", textlength);
kendunlop 1:19d183cf2689 105 for (int i = 0; i < part.size(); i++)
kendunlop 1:19d183cf2689 106 {
kendunlop 1:19d183cf2689 107 //pc.printf("Examining character %d/%d.\r\n", (i+1), textlength);
kendunlop 1:19d183cf2689 108 char individualcharacter = part.at(i);
kendunlop 1:19d183cf2689 109 //pc.printf("That's '%c'.\r\n", individualcharacter);
kendunlop 1:19d183cf2689 110 int numberized = 0;
kendunlop 1:19d183cf2689 111 if(isdigit(individualcharacter))
kendunlop 1:19d183cf2689 112 {
kendunlop 1:19d183cf2689 113 //pc.printf("That character is a digit.\r\n");
kendunlop 1:19d183cf2689 114 numberized = individualcharacter - '0';
kendunlop 1:19d183cf2689 115 //pc.printf("Numberized that's '%d'.\r\n", numberized);
kendunlop 1:19d183cf2689 116 }
kendunlop 1:19d183cf2689 117 else
kendunlop 1:19d183cf2689 118 {
kendunlop 1:19d183cf2689 119 //pc.printf("That character is NOT a digit.\r\n");
kendunlop 1:19d183cf2689 120 int asciivalue = toupper(individualcharacter);
kendunlop 1:19d183cf2689 121 //pc.printf("Ascii value is %d.\r\n", asciivalue);
kendunlop 1:19d183cf2689 122 numberized = asciivalue - 55;
kendunlop 1:19d183cf2689 123 //pc.printf("Hex value is %d.\r\n", numberized);
kendunlop 1:19d183cf2689 124 }
kendunlop 1:19d183cf2689 125 //pc.printf("Eventual numberization is %d.\r\n", numberized);
kendunlop 1:19d183cf2689 126 //pc.printf("Hex total is now %d.\r\n", hextotal);
kendunlop 1:19d183cf2689 127 int powertoraise = part.size() - (i+1);
kendunlop 1:19d183cf2689 128 //pc.printf("Must multiply by 16 to the power of %d.\r\n", powertoraise);
kendunlop 1:19d183cf2689 129 int amounttoadd = numberized;
kendunlop 1:19d183cf2689 130 //pc.printf("powertoraise is '%d'.\r\n", powertoraise);
kendunlop 1:19d183cf2689 131 if (powertoraise == 1)
kendunlop 1:19d183cf2689 132 {
kendunlop 1:19d183cf2689 133 amounttoadd = numberized * 16;
kendunlop 1:19d183cf2689 134 //pc.printf("Multiplying by 16.\r\n");
kendunlop 1:19d183cf2689 135 //pc.printf("powertoraise is '%d'.\r\n", powertoraise);
kendunlop 1:19d183cf2689 136 }
kendunlop 1:19d183cf2689 137 if (powertoraise == 2)
kendunlop 1:19d183cf2689 138 {
kendunlop 1:19d183cf2689 139 amounttoadd = numberized * 256;
kendunlop 1:19d183cf2689 140 //pc.printf("Multiplying by 256.\r\n");
kendunlop 1:19d183cf2689 141 }
kendunlop 1:19d183cf2689 142 //pc.printf("Amount to add is therefore %d.\r\n", amounttoadd);
kendunlop 1:19d183cf2689 143 hextotal = hextotal + amounttoadd;
kendunlop 1:19d183cf2689 144 //pc.printf("hextotal so far for this part is therefore %d.\r\n", hextotal);
kendunlop 1:19d183cf2689 145 }
kendunlop 1:19d183cf2689 146 //pc.printf("hextotal for whole part is therefore %d.\r\n", hextotal);
kendunlop 1:19d183cf2689 147 //pc.printf("Need to convert that into true hex.\r\n");
kendunlop 1:19d183cf2689 148 std::stringstream sstream;
kendunlop 1:19d183cf2689 149 sstream << std::hex << hextotal;
kendunlop 1:19d183cf2689 150 //pc.printf("StringSteam says '%s'.\r\n", sstream.str());
kendunlop 1:19d183cf2689 151 if (partincrement == 1)
kendunlop 7:a9150dc1e481 152 {
kendunlop 7:a9150dc1e481 153 messageOut1.id = hextotal;
kendunlop 7:a9150dc1e481 154 expected1 = hextotal;
kendunlop 7:a9150dc1e481 155 }
kendunlop 1:19d183cf2689 156 if (partincrement == 2)
kendunlop 7:a9150dc1e481 157 {
kendunlop 7:a9150dc1e481 158 messageOut1.len = hextotal;
kendunlop 7:a9150dc1e481 159 expected2 = hextotal;
kendunlop 7:a9150dc1e481 160 }
kendunlop 1:19d183cf2689 161 if (partincrement >= 3)
kendunlop 7:a9150dc1e481 162 {
kendunlop 7:a9150dc1e481 163 messageOut1.data[partincrement-3] = hextotal;
kendunlop 7:a9150dc1e481 164 }
kendunlop 7:a9150dc1e481 165 if (partincrement == 3)
kendunlop 7:a9150dc1e481 166 {
kendunlop 7:a9150dc1e481 167 expected3 = hextotal;
kendunlop 7:a9150dc1e481 168 }
kendunlop 7:a9150dc1e481 169 if (partincrement == 4)
kendunlop 7:a9150dc1e481 170 {
kendunlop 7:a9150dc1e481 171 expected4 = hextotal;
kendunlop 7:a9150dc1e481 172 }
kendunlop 7:a9150dc1e481 173 if (partincrement == 5)
kendunlop 7:a9150dc1e481 174 {
kendunlop 7:a9150dc1e481 175 expected5 = hextotal;
kendunlop 7:a9150dc1e481 176 }
kendunlop 7:a9150dc1e481 177 if (partincrement == 6)
kendunlop 7:a9150dc1e481 178 {
kendunlop 7:a9150dc1e481 179 expected6 = hextotal;
kendunlop 7:a9150dc1e481 180 }
kendunlop 7:a9150dc1e481 181 if (partincrement == 7)
kendunlop 7:a9150dc1e481 182 {
kendunlop 7:a9150dc1e481 183 expected7 = hextotal;
kendunlop 7:a9150dc1e481 184 }
kendunlop 7:a9150dc1e481 185 if (partincrement == 8)
kendunlop 7:a9150dc1e481 186 {
kendunlop 7:a9150dc1e481 187 expected8 = hextotal;
kendunlop 7:a9150dc1e481 188 }
kendunlop 7:a9150dc1e481 189 if (partincrement == 9)
kendunlop 7:a9150dc1e481 190 {
kendunlop 7:a9150dc1e481 191 expected9 = hextotal;
kendunlop 7:a9150dc1e481 192 }
kendunlop 7:a9150dc1e481 193 if (partincrement == 10)
kendunlop 7:a9150dc1e481 194 {
kendunlop 7:a9150dc1e481 195 expected10 = hextotal;
kendunlop 7:a9150dc1e481 196 }
kendunlop 1:19d183cf2689 197 //pc.printf("Part %d complete.\r\n", partincrement);
kendunlop 1:19d183cf2689 198 //pc.printf("--------------------------------------\r\n");
kendunlop 1:19d183cf2689 199 }
kendunlop 1:19d183cf2689 200
kendunlop 3:79133dcea836 201 //A function to get a coherent CAN message from one, uninterrupted string
kendunlop 6:2882710e4f1e 202 void getCANfrommessageOutText(void)
kendunlop 1:19d183cf2689 203 {
kendunlop 1:19d183cf2689 204 //pc.printf("messageOutText is '%s'\r\n", messageOutText);
kendunlop 7:a9150dc1e481 205 messageOutTextWithSpaces = messageOutText;
kendunlop 1:19d183cf2689 206 remove(messageOutText.begin(), messageOutText.end(), ' '); //Remove the spaces from the text to send out so it can be parsed.
kendunlop 1:19d183cf2689 207 //pc.printf("After removing spaces, messageOutText is '%s'\r\n", messageOutText);
kendunlop 1:19d183cf2689 208 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 209 //pc.printf("String to parse is '%s'.\r\n", startofstring);
kendunlop 1:19d183cf2689 210 partincrement = 0;
kendunlop 1:19d183cf2689 211 part = startofstring.substr(0,3);
kendunlop 1:19d183cf2689 212 dealwithpart();
kendunlop 1:19d183cf2689 213 part = startofstring.substr(3,1);
kendunlop 1:19d183cf2689 214 dealwithpart();
kendunlop 1:19d183cf2689 215 part = startofstring.substr(4,2);
kendunlop 1:19d183cf2689 216 dealwithpart();
kendunlop 1:19d183cf2689 217 part = startofstring.substr(6,2);
kendunlop 1:19d183cf2689 218 dealwithpart();
kendunlop 1:19d183cf2689 219 part = startofstring.substr(8,2);
kendunlop 1:19d183cf2689 220 dealwithpart();
kendunlop 1:19d183cf2689 221 part = startofstring.substr(10,2);
kendunlop 1:19d183cf2689 222 dealwithpart();
kendunlop 1:19d183cf2689 223 part = startofstring.substr(12,2);
kendunlop 1:19d183cf2689 224 dealwithpart();
kendunlop 1:19d183cf2689 225 part = startofstring.substr(14,2);
kendunlop 1:19d183cf2689 226 dealwithpart();
kendunlop 1:19d183cf2689 227 part = startofstring.substr(16,2);
kendunlop 1:19d183cf2689 228 dealwithpart();
kendunlop 1:19d183cf2689 229 part = startofstring.substr(18,2);
kendunlop 1:19d183cf2689 230 dealwithpart();
kendunlop 1:19d183cf2689 231 }
kendunlop 1:19d183cf2689 232
kendunlop 1:19d183cf2689 233 void defineCANmessage(void)
kendunlop 1:19d183cf2689 234 {
kendunlop 1:19d183cf2689 235 //pc.printf("Defining CAN message 1.\r\n");
kendunlop 1:19d183cf2689 236 //messageOut1.format = CANStandard;
kendunlop 1:19d183cf2689 237 //messageOut1.id = 0x301;
kendunlop 1:19d183cf2689 238 //messageOut1.len = 8;
kendunlop 1:19d183cf2689 239 //messageOut1.data[0] = 0x06;
kendunlop 1:19d183cf2689 240 //messageOut1.data[1] = 0x3f;
kendunlop 1:19d183cf2689 241 //messageOut1.data[2] = 0xb2;
kendunlop 1:19d183cf2689 242 //messageOut1.data[3] = 0x29;
kendunlop 1:19d183cf2689 243 //messageOut1.data[4] = 0x19;
kendunlop 1:19d183cf2689 244 //messageOut1.data[5] = 0x97;
kendunlop 1:19d183cf2689 245 //messageOut1.data[6] = 0x67;
kendunlop 1:19d183cf2689 246 //messageOut1.data[7] = 0x37;
kendunlop 1:19d183cf2689 247 }
kendunlop 1:19d183cf2689 248
kendunlop 3:79133dcea836 249 void printMessageOut (void)
kendunlop 3:79133dcea836 250 {
kendunlop 1:19d183cf2689 251 //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 252 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 253 }
kendunlop 1:19d183cf2689 254
kendunlop 3:79133dcea836 255 void printMessageIn (void)
kendunlop 3:79133dcea836 256 {
kendunlop 7:a9150dc1e481 257 //This function will print out whatever the CAN bus is receiving and keep it in messagetocheck.
kendunlop 7:a9150dc1e481 258 //messagetocheck = ("%03X %01X %02X %02X %02X %02X %02X %02X %02X %02X", 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 7:a9150dc1e481 259 //pc.printf("Message IN: %03X %01X %02X %02X %02X %02X %02X %02X %02X %02X\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 260 }
kendunlop 0:7a500ebaa7a6 261
kendunlop 0:7a500ebaa7a6 262 //The 'main' function will run as soon as the program starts.
kendunlop 0:7a500ebaa7a6 263 int main()
kendunlop 1:19d183cf2689 264 {
kendunlop 1:19d183cf2689 265 pc.baud(115200); // serial port at 115200
kendunlop 1:19d183cf2689 266 CanBus.frequency(500 * 1000); // CAN bus at 500k
kendunlop 3:79133dcea836 267 CanBus2.frequency(500 * 1000); // CAN bus at 500k
kendunlop 1:19d183cf2689 268 CanBus.reset(); // clear any bus errors
kendunlop 3:79133dcea836 269 CanBus2.reset(); // clear any bus errors
kendunlop 1:19d183cf2689 270 //NOTE: Print messages must be below this line to work.
kendunlop 1:19d183cf2689 271 pc.printf("------------------------------------------\r\n");
kendunlop 1:19d183cf2689 272 pc.printf("Welcome to Ken CAN test.\r\n");
kendunlop 1:19d183cf2689 273 pc.printf("Setting CAN bus to 500k.\r\n");
kendunlop 6:2882710e4f1e 274 pc.printf("Setting serial port to baud rate 115200.\r\n");
kendunlop 6:2882710e4f1e 275 pc.printf("Using pins 9 and 10 for CANBus 1 and 30 and 29 for CANBus 2.\r\n");
kendunlop 1:19d183cf2689 276 pc.printf("Version %d.%d\r\n",kMajorVersion,kMinorVersion);
kendunlop 1:19d183cf2689 277 pc.printf("Build date %s %s\r\n",__DATE__,__TIME__);
kendunlop 1:19d183cf2689 278 pc.printf("------------------------------------------\r\n");
kendunlop 1:19d183cf2689 279 char c;
kendunlop 1:19d183cf2689 280
kendunlop 7:a9150dc1e481 281 //Define the list of expected hex values for use later
kendunlop 7:a9150dc1e481 282 //expectedlist[0] = 5;
kendunlop 7:a9150dc1e481 283 //for (int item : expectedlist)
kendunlop 7:a9150dc1e481 284 // pc.printf("%d\r\n", item);
kendunlop 7:a9150dc1e481 285 //int integer = expectedlist[1];
kendunlop 7:a9150dc1e481 286
kendunlop 1:19d183cf2689 287 //Check for button presses
kendunlop 1:19d183cf2689 288 while (1)
kendunlop 0:7a500ebaa7a6 289 {
kendunlop 1:19d183cf2689 290 if (pc.readable())
kendunlop 1:19d183cf2689 291 {
kendunlop 1:19d183cf2689 292 c = pc.getc();
kendunlop 3:79133dcea836 293 if (c != NULL)
kendunlop 1:19d183cf2689 294 {
kendunlop 3:79133dcea836 295 //When the a key is pressed, define a CAN message and send it.
kendunlop 1:19d183cf2689 296 //pc.printf("A key was pressed! (%c)\r\n", c);
kendunlop 1:19d183cf2689 297 messageOutText = "";
kendunlop 1:19d183cf2689 298 if (c == '1')
kendunlop 7:a9150dc1e481 299 {
kendunlop 7:a9150dc1e481 300 addnewlineifneeded();
kendunlop 7:a9150dc1e481 301 messageOutText = "301 8 01 01 01 01 01 01 01 01";
kendunlop 7:a9150dc1e481 302 addnewlinetonextmessage = 1;
kendunlop 7:a9150dc1e481 303 }
kendunlop 1:19d183cf2689 304 if (c == '2')
kendunlop 7:a9150dc1e481 305 {
kendunlop 7:a9150dc1e481 306 addnewlineifneeded();
kendunlop 7:a9150dc1e481 307 messageOutText = "302 8 02 02 02 02 02 02 02 02";
kendunlop 7:a9150dc1e481 308 addnewlinetonextmessage = 1;
kendunlop 7:a9150dc1e481 309 }
kendunlop 1:19d183cf2689 310 if (c == '3')
kendunlop 7:a9150dc1e481 311 {
kendunlop 7:a9150dc1e481 312 addnewlineifneeded();
kendunlop 7:a9150dc1e481 313 messageOutText = "303 8 00 00 00 00 00 00 00 33";
kendunlop 7:a9150dc1e481 314 addnewlinetonextmessage = 1;
kendunlop 7:a9150dc1e481 315 }
kendunlop 1:19d183cf2689 316 if (c == '4')
kendunlop 7:a9150dc1e481 317 {
kendunlop 7:a9150dc1e481 318 addnewlineifneeded();
kendunlop 7:a9150dc1e481 319 messageOutText = "304 8 04 04 04 04 04 04 04 04";
kendunlop 7:a9150dc1e481 320 addnewlinetonextmessage = 1;
kendunlop 7:a9150dc1e481 321 }
kendunlop 1:19d183cf2689 322 if (c == '5')
kendunlop 7:a9150dc1e481 323 {
kendunlop 7:a9150dc1e481 324 addnewlineifneeded();
kendunlop 7:a9150dc1e481 325 messageOutText = "305 8 05 05 05 05 05 05 05 05";
kendunlop 7:a9150dc1e481 326 addnewlinetonextmessage = 1;
kendunlop 7:a9150dc1e481 327 }
kendunlop 1:19d183cf2689 328 if (c == '6')
kendunlop 7:a9150dc1e481 329 {
kendunlop 7:a9150dc1e481 330 addnewlineifneeded();
kendunlop 7:a9150dc1e481 331 messageOutText = "306 8 06 06 06 06 06 06 06 06";
kendunlop 7:a9150dc1e481 332 addnewlinetonextmessage = 1;
kendunlop 7:a9150dc1e481 333 }
kendunlop 1:19d183cf2689 334 if (c == '7')
kendunlop 7:a9150dc1e481 335 {
kendunlop 7:a9150dc1e481 336 addnewlineifneeded();
kendunlop 7:a9150dc1e481 337 messageOutText = "307 8 07 07 07 07 07 07 07 07";
kendunlop 7:a9150dc1e481 338 addnewlinetonextmessage = 1;
kendunlop 7:a9150dc1e481 339 }
kendunlop 1:19d183cf2689 340 if (c == '8')
kendunlop 7:a9150dc1e481 341 {
kendunlop 7:a9150dc1e481 342 addnewlineifneeded();
kendunlop 7:a9150dc1e481 343 messageOutText = "308 8 08 08 08 08 08 08 08 08";
kendunlop 7:a9150dc1e481 344 addnewlinetonextmessage = 1;
kendunlop 7:a9150dc1e481 345 }
kendunlop 1:19d183cf2689 346 if (c == '9')
kendunlop 7:a9150dc1e481 347 {
kendunlop 7:a9150dc1e481 348 addnewlineifneeded();
kendunlop 7:a9150dc1e481 349 messageOutText = "309 8 09 09 09 09 09 09 09 09";
kendunlop 7:a9150dc1e481 350 addnewlinetonextmessage = 1;
kendunlop 7:a9150dc1e481 351 }
kendunlop 1:19d183cf2689 352 if (c == 'f')
kendunlop 7:a9150dc1e481 353 {
kendunlop 7:a9150dc1e481 354 addnewlineifneeded();
kendunlop 7:a9150dc1e481 355 messageOutText = "7FF 8 FF FF FF FF FF FF FF FF";//NOTE: CAN IDs only go p to 7FF, not FFF.
kendunlop 7:a9150dc1e481 356 addnewlinetonextmessage = 1;
kendunlop 7:a9150dc1e481 357 }
kendunlop 1:19d183cf2689 358 if (c == 'q')
kendunlop 3:79133dcea836 359 {
kendunlop 3:79133dcea836 360 pc.printf("Changing CAN bus speed to 125.\r\n");
kendunlop 3:79133dcea836 361 CanBus.frequency(125 * 1000); // CAN bus at 125k
kendunlop 5:bf4c6278ca8b 362 CanBus2.frequency(125 * 1000); // CAN bus at 125k
kendunlop 3:79133dcea836 363 }
kendunlop 1:19d183cf2689 364 if (c == 'w')
kendunlop 3:79133dcea836 365 {
kendunlop 3:79133dcea836 366 pc.printf("Changing CAN bus speed to 250.\r\n");
kendunlop 3:79133dcea836 367 CanBus.frequency(250 * 1000); // CAN bus at 250k
kendunlop 5:bf4c6278ca8b 368 CanBus2.frequency(250 * 1000); // CAN bus at 250k
kendunlop 3:79133dcea836 369 }
kendunlop 1:19d183cf2689 370 if (c == 'e')
kendunlop 3:79133dcea836 371 {
kendunlop 3:79133dcea836 372 pc.printf("Changing CAN bus speed to 500.\r\n");
kendunlop 3:79133dcea836 373 CanBus.frequency(500 * 1000); // CAN bus at 500k
kendunlop 3:79133dcea836 374 CanBus2.frequency(500 * 1000);// CAN bus at 500k
kendunlop 3:79133dcea836 375 }
kendunlop 1:19d183cf2689 376 if (c == 'r')
kendunlop 3:79133dcea836 377 {
kendunlop 3:79133dcea836 378 pc.printf("Changing CAN bus speed to 1000.\r\n");
kendunlop 3:79133dcea836 379 CanBus.frequency(1000 * 1000); // CAN bus at 1000k
kendunlop 3:79133dcea836 380 CanBus2.frequency(1000 * 1000); // CAN bus at 1000k
kendunlop 3:79133dcea836 381 }
kendunlop 7:a9150dc1e481 382 //if (c == 's')
kendunlop 7:a9150dc1e481 383 // {
kendunlop 7:a9150dc1e481 384 // pc.printf("Sending authentic sats message.\r\n");
kendunlop 7:a9150dc1e481 385 // messageOutText = "301 8 06 3F B2 29 12 97 67 37";
kendunlop 7:a9150dc1e481 386 // }
kendunlop 3:79133dcea836 387 if (c == 'o')
kendunlop 3:79133dcea836 388 {
kendunlop 3:79133dcea836 389 incrementer = (incrementer * 16);
kendunlop 3:79133dcea836 390 pc.printf("Multiplied Incrementer by 16. It's now now %d.\r\n", incrementer);
kendunlop 3:79133dcea836 391 }
kendunlop 1:19d183cf2689 392 if (c == 'i')
kendunlop 1:19d183cf2689 393 {
kendunlop 1:19d183cf2689 394 incrementer = incrementer + 16;
kendunlop 1:19d183cf2689 395 std::stringstream sstream;
kendunlop 1:19d183cf2689 396 sstream << std::hex << incrementer;
kendunlop 1:19d183cf2689 397 string stringsofar = sstream.str();
kendunlop 1:19d183cf2689 398 //pc.printf("Incrementer is now %d.\r\n", incrementer);
kendunlop 1:19d183cf2689 399 //pc.printf("StringStream says '%s'.\r\n", stringsofar);
kendunlop 1:19d183cf2689 400 int length = stringsofar.length();
kendunlop 1:19d183cf2689 401 //pc.printf("Length is %d/16.\r\n", length);
kendunlop 1:19d183cf2689 402 for (int i = 0; i < (16-length); i++)
kendunlop 1:19d183cf2689 403 stringsofar = "0" + stringsofar;
kendunlop 1:19d183cf2689 404 //pc.printf("stringsofar says '%s'.\r\n", stringsofar);
kendunlop 1:19d183cf2689 405 messageOutText = "305 8 " + stringsofar;
kendunlop 1:19d183cf2689 406 //pc.printf("Will try to send '%s'.\r\n", messageOutText);
kendunlop 1:19d183cf2689 407 }
kendunlop 1:19d183cf2689 408 messageOut1.format = CANStandard;
kendunlop 1:19d183cf2689 409 if (c == 'z' and spamon == 0)
kendunlop 1:19d183cf2689 410 {
kendunlop 7:a9150dc1e481 411 resettest();
kendunlop 6:2882710e4f1e 412 pc.printf("Starting spam sequence for ID %s. Press 'x' to end. \r\n", spampreamble);
kendunlop 1:19d183cf2689 413 spamon = 1;
kendunlop 1:19d183cf2689 414 incrementer = 0;
kendunlop 2:11339018dda6 415 spamcount = 0;
kendunlop 3:79133dcea836 416 spamstarttime = clock();
kendunlop 1:19d183cf2689 417 }
kendunlop 1:19d183cf2689 418 //Pressing 'x' switches off spam mode
kendunlop 1:19d183cf2689 419 if (c == 'x' and spamon == 1)
kendunlop 1:19d183cf2689 420 {
kendunlop 3:79133dcea836 421 //spamcount = 9999999;
kendunlop 1:19d183cf2689 422 spamon = 0;
kendunlop 3:79133dcea836 423 int spamendtime = 0;
kendunlop 3:79133dcea836 424 spamendtime = clock();
kendunlop 2:11339018dda6 425 int spamtime = 0;
kendunlop 2:11339018dda6 426 spamtime = spamendtime - spamstarttime;
kendunlop 3:79133dcea836 427 int spamtimeseconds = 0;
kendunlop 3:79133dcea836 428 spamtimeseconds = (spamtime / CLOCKS_PER_SEC);
kendunlop 7:a9150dc1e481 429 addnewlineifneeded();
kendunlop 3:79133dcea836 430 pc.printf("Ending spam mode. Spammed %d times for %d ticks (%d seconds) at %d ticks per second.\r\n", spamcount, spamtime, spamtimeseconds, CLOCKS_PER_SEC);
kendunlop 3:79133dcea836 431 //pc.printf("Clock right now is %d.\r\n", (spamtime));
kendunlop 3:79133dcea836 432 //pc.printf("Ticks per second are %d.\r\n", CLOCKS_PER_SEC);
kendunlop 3:79133dcea836 433 //pc.printf("Spam start/end is %d/%d.\r\n", spamstarttime, spamendtime);
kendunlop 3:79133dcea836 434 //pc.printf("Spamtime was %d ticks.\r\n", spamtime);
kendunlop 2:11339018dda6 435 int spamspersecond = 0;
kendunlop 3:79133dcea836 436 spamspersecond = (10000 * spamcount);
kendunlop 3:79133dcea836 437 //pc.printf("spamcount times 10,000 is %d.\r\n", spamspersecond);
kendunlop 3:79133dcea836 438 spamspersecond = spamspersecond / spamtime;
kendunlop 3:79133dcea836 439 //pc.printf("10,000 times spamcount divided by spamtime is %d spams per tick.\r\n", spamspersecond);
kendunlop 3:79133dcea836 440 spamspersecond = ((spamspersecond * CLOCKS_PER_SEC))/10000;
kendunlop 3:79133dcea836 441 pc.printf("Spams per second are %d.\r\n", spamspersecond);
kendunlop 3:79133dcea836 442 pc.printf("Total spams ever are %d.\r\n", totalspamsever);
kendunlop 7:a9150dc1e481 443 endtest();
kendunlop 7:a9150dc1e481 444 //pc.printf("-------------------------\r\n");
kendunlop 3:79133dcea836 445 }
kendunlop 6:2882710e4f1e 446 if (c == 'v' and idspamon == 0)
kendunlop 3:79133dcea836 447 {
kendunlop 7:a9150dc1e481 448 resettest();
kendunlop 6:2882710e4f1e 449 idspamon = 1; //Set the 'idspamon' integer to 1 so ID spam happens.
kendunlop 6:2882710e4f1e 450 idlistincrementer = 0;
kendunlop 7:a9150dc1e481 451 pc.printf("Beginning spam of all possible CAN IDs (000 - 7FF).\r\n");
kendunlop 1:19d183cf2689 452 }
kendunlop 1:19d183cf2689 453 if (messageOutText != "")
kendunlop 1:19d183cf2689 454 {
kendunlop 6:2882710e4f1e 455 getCANfrommessageOutText();
kendunlop 7:a9150dc1e481 456 sendCAN();
kendunlop 7:a9150dc1e481 457 //CanBus.write(messageOut1);
kendunlop 7:a9150dc1e481 458 //CanBus2.write(messageOut1);
kendunlop 6:2882710e4f1e 459 if (spamon == 0 and idspamon == 0)
kendunlop 3:79133dcea836 460 {
kendunlop 7:a9150dc1e481 461 //printMessageOut();
kendunlop 3:79133dcea836 462 }
kendunlop 3:79133dcea836 463 messageOutText = "";
kendunlop 1:19d183cf2689 464 }
kendunlop 5:bf4c6278ca8b 465 }
kendunlop 4:e8e9bc25b1ca 466 }
kendunlop 5:bf4c6278ca8b 467
kendunlop 4:e8e9bc25b1ca 468
kendunlop 7:a9150dc1e481 469 //If spam mode is on, spam an incrementing CAN message
kendunlop 6:2882710e4f1e 470 if (spamon == 1)
kendunlop 5:bf4c6278ca8b 471 {
kendunlop 5:bf4c6278ca8b 472 spamcount ++;
kendunlop 5:bf4c6278ca8b 473 totalspamsever ++;
kendunlop 5:bf4c6278ca8b 474 incrementer ++;
kendunlop 5:bf4c6278ca8b 475 std::stringstream sstream;
kendunlop 5:bf4c6278ca8b 476 sstream << std::hex << incrementer;
kendunlop 5:bf4c6278ca8b 477 string stringsofar = sstream.str();
kendunlop 5:bf4c6278ca8b 478 int length = stringsofar.length();
kendunlop 5:bf4c6278ca8b 479 for (int i = 0; i < (16-length); i++)
kendunlop 5:bf4c6278ca8b 480 stringsofar = "0" + stringsofar;
kendunlop 6:2882710e4f1e 481 messageOutText = spampreamble + " 8 " + stringsofar;
kendunlop 6:2882710e4f1e 482 getCANfrommessageOutText();
kendunlop 7:a9150dc1e481 483 sendCAN();
kendunlop 7:a9150dc1e481 484 //CanBus.write(messageOut1);
kendunlop 7:a9150dc1e481 485 //CanBus2.write(messageOut1);
kendunlop 5:bf4c6278ca8b 486 wait(0.01);
kendunlop 5:bf4c6278ca8b 487 }
kendunlop 6:2882710e4f1e 488 if (idspamon == 1)
kendunlop 6:2882710e4f1e 489 {
kendunlop 6:2882710e4f1e 490 std::stringstream sstream;
kendunlop 6:2882710e4f1e 491 sstream << std::hex << idlistincrementer;
kendunlop 6:2882710e4f1e 492 string idstring = "";
kendunlop 6:2882710e4f1e 493 idstring = sstream.str();
kendunlop 6:2882710e4f1e 494 //pc.printf("idstring says '%s'\r\n", idstring);
kendunlop 6:2882710e4f1e 495 if (idstring.length() < 3)
kendunlop 6:2882710e4f1e 496 {
kendunlop 6:2882710e4f1e 497 for (int i = 0; i < (4 - idstring.length()); i++)
kendunlop 6:2882710e4f1e 498 {
kendunlop 6:2882710e4f1e 499 idstring = "0" + idstring;
kendunlop 6:2882710e4f1e 500 }
kendunlop 6:2882710e4f1e 501 }
kendunlop 6:2882710e4f1e 502 //pc.printf("idstring now says '%s'\r\n", idstring);
kendunlop 6:2882710e4f1e 503 //pc.printf("%s\r\n", idstring);
kendunlop 7:a9150dc1e481 504 idstring = idstring + " 8 01 02 03 04 05 06 07 08";
kendunlop 6:2882710e4f1e 505 //pc.printf("...and now idstring now says '%s'\r\n", idstring);
kendunlop 6:2882710e4f1e 506 messageOutText = idstring;
kendunlop 6:2882710e4f1e 507 getCANfrommessageOutText();
kendunlop 7:a9150dc1e481 508 sendCAN();
kendunlop 7:a9150dc1e481 509 //CanBus.write(messageOut1);
kendunlop 7:a9150dc1e481 510 //CanBus2.write(messageOut1);
kendunlop 6:2882710e4f1e 511 idlistincrementer++; //Only add 1 to the idlist incrementer at the end so the first ID it sends is '000'.
kendunlop 6:2882710e4f1e 512 //pc.printf("ID list incrementer is now %d\r\n", idlistincrementer);
kendunlop 7:a9150dc1e481 513 if (idlistincrementer >= 2048) //If the spam value gets to 2048 (7FF), end the spam sequence. RL only go up to 7FF.
kendunlop 6:2882710e4f1e 514 {
kendunlop 6:2882710e4f1e 515 idspamon = 0;
kendunlop 7:a9150dc1e481 516 addnewlineifneeded();
kendunlop 6:2882710e4f1e 517 pc.printf("Ending ID spam sequence.\r\n");
kendunlop 6:2882710e4f1e 518 idlistincrementer = 0;
kendunlop 7:a9150dc1e481 519 endtest();
kendunlop 6:2882710e4f1e 520 }
kendunlop 6:2882710e4f1e 521 wait(0.01);//ID spam at 100Hz
kendunlop 6:2882710e4f1e 522 }
kendunlop 1:19d183cf2689 523 //Check for CAN messages coming in
kendunlop 3:79133dcea836 524 if (CanBus.read(messageIn))
kendunlop 1:19d183cf2689 525 {
kendunlop 7:a9150dc1e481 526 //CANCount ++;
kendunlop 7:a9150dc1e481 527 //printMessageIn();
kendunlop 7:a9150dc1e481 528 }
kendunlop 7:a9150dc1e481 529 if (CanBus2.read(messageIn))
kendunlop 7:a9150dc1e481 530 {
kendunlop 1:19d183cf2689 531 CANCount ++;
kendunlop 1:19d183cf2689 532 printMessageIn();
kendunlop 7:a9150dc1e481 533 if (1 == 1)
kendunlop 7:a9150dc1e481 534 {
kendunlop 7:a9150dc1e481 535 //string messagetocheck = "";
kendunlop 7:a9150dc1e481 536 //string messageintext = ("%03X %01X %d %d %d %d %d %d %d %d", 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 7:a9150dc1e481 537 //messagetocheck = ("%03X %01X %02X %02X %02X %02X %02X %02X %02X %02X", 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 7:a9150dc1e481 538 //pc.printf("Expectation is %d %d %d %d %d %d %d %d %d %d\r\n", expected1, expected2, expected3, expected4, expected5, expected6, expected7, expected8, expected9, expected10);
kendunlop 7:a9150dc1e481 539 int itsamatch = 1;
kendunlop 7:a9150dc1e481 540 addnewlineifneeded();
kendunlop 7:a9150dc1e481 541 if (expected1 != messageIn.id)
kendunlop 7:a9150dc1e481 542 {
kendunlop 7:a9150dc1e481 543 pc.printf("ID of %d does NOT match %d.\r\n", messageIn.id, expected1);
kendunlop 7:a9150dc1e481 544 itsamatch = 0;
kendunlop 7:a9150dc1e481 545 }
kendunlop 7:a9150dc1e481 546 if (expected2 != messageIn.len)
kendunlop 7:a9150dc1e481 547 {
kendunlop 7:a9150dc1e481 548 pc.printf("Length of %d does NOT match %d.\r\n", messageIn.len, expected2);
kendunlop 7:a9150dc1e481 549 itsamatch = 0;
kendunlop 7:a9150dc1e481 550 }
kendunlop 7:a9150dc1e481 551 if (expected3 != messageIn.data[0])
kendunlop 7:a9150dc1e481 552 {
kendunlop 7:a9150dc1e481 553 pc.printf("Data 0 of %d does NOT match %d.\r\n", messageIn.data[0], expected3);
kendunlop 7:a9150dc1e481 554 itsamatch = 0;
kendunlop 7:a9150dc1e481 555 }
kendunlop 7:a9150dc1e481 556 if (expected4 != messageIn.data[1])
kendunlop 7:a9150dc1e481 557 {
kendunlop 7:a9150dc1e481 558 pc.printf("Data 1 of %d does NOT match %d.\r\n", messageIn.data[1], expected4);
kendunlop 7:a9150dc1e481 559 itsamatch = 0;
kendunlop 7:a9150dc1e481 560 }
kendunlop 7:a9150dc1e481 561 if (expected5 != messageIn.data[2])
kendunlop 7:a9150dc1e481 562 {
kendunlop 7:a9150dc1e481 563 pc.printf("Data 2 of %d does NOT match %d.\r\n", messageIn.data[2], expected5);
kendunlop 7:a9150dc1e481 564 itsamatch = 0;
kendunlop 7:a9150dc1e481 565 }
kendunlop 7:a9150dc1e481 566 if (expected6 != messageIn.data[3])
kendunlop 7:a9150dc1e481 567 {
kendunlop 7:a9150dc1e481 568 pc.printf("Data 3 of %d does NOT match %d.\r\n", messageIn.data[3], expected6);
kendunlop 7:a9150dc1e481 569 itsamatch = 0;
kendunlop 7:a9150dc1e481 570 }
kendunlop 7:a9150dc1e481 571 if (expected7 != messageIn.data[4])
kendunlop 7:a9150dc1e481 572 {
kendunlop 7:a9150dc1e481 573 pc.printf("Data 4 of %d does NOT match %d.\r\n", messageIn.data[4], expected7);
kendunlop 7:a9150dc1e481 574 itsamatch = 0;
kendunlop 7:a9150dc1e481 575 }
kendunlop 7:a9150dc1e481 576 if (expected8 != messageIn.data[5])
kendunlop 7:a9150dc1e481 577 {
kendunlop 7:a9150dc1e481 578 pc.printf("Data 5 of %d does NOT match %d.\r\n", messageIn.data[5], expected8);
kendunlop 7:a9150dc1e481 579 itsamatch = 0;
kendunlop 7:a9150dc1e481 580 }
kendunlop 7:a9150dc1e481 581 if (expected9 != messageIn.data[6])
kendunlop 7:a9150dc1e481 582 {
kendunlop 7:a9150dc1e481 583 pc.printf("Data 6 of %d does NOT match %d.\r\n", messageIn.data[6], expected9);
kendunlop 7:a9150dc1e481 584 itsamatch = 0;
kendunlop 7:a9150dc1e481 585 }
kendunlop 7:a9150dc1e481 586 if (expected10 != messageIn.data[7])
kendunlop 7:a9150dc1e481 587 {
kendunlop 7:a9150dc1e481 588 pc.printf("Data 7 of %d does NOT match %d.\r\n", messageIn.data[7], expected10);
kendunlop 7:a9150dc1e481 589 itsamatch = 0;
kendunlop 7:a9150dc1e481 590 }
kendunlop 7:a9150dc1e481 591 if (itsamatch == 0)
kendunlop 7:a9150dc1e481 592 {
kendunlop 7:a9150dc1e481 593 pc.printf("Message received was: %03X %01X %02X %02X %02X %02X %02X %02X %02X %02X\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 7:a9150dc1e481 594 pc.printf("That does NOT match expectation! (%03X %01X %02X %02X %02X %02X %02X %02X %02X %02X)\r\n", expected1, expected2, expected3, expected4, expected5, expected6, expected7, expected8, expected9, expected10);
kendunlop 7:a9150dc1e481 595 failsthistest++; //Add 1 to failsthistest to mark the fail
kendunlop 7:a9150dc1e481 596 }
kendunlop 7:a9150dc1e481 597 }
kendunlop 1:19d183cf2689 598 }
kendunlop 3:79133dcea836 599 }
kendunlop 5:bf4c6278ca8b 600 }