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@6:2882710e4f1e, 2022-06-17 (annotated)
- Committer:
- kendunlop
- Date:
- Fri Jun 17 10:13:51 2022 +0000
- Revision:
- 6:2882710e4f1e
- Parent:
- 5:bf4c6278ca8b
- Child:
- 7:a9150dc1e481
Pressing 'V' now spams all possible IDs (000 to FFF).
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 | 3:79133dcea836 | 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 | 3:79133dcea836 | 16 | CAN CanBus(p9, p10); //CANBus to use pins 29 and 30. This defines CanBus so other 'CanBus' lines will work. |
kendunlop | 3:79133dcea836 | 17 | CAN CanBus2(p30, p29); //CANBus2 for recieving to use pins 29 and 30. This defines CanBus so other 'CanBus' lines will work. |
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 | 1:19d183cf2689 | 25 | string part = ""; |
kendunlop | 1:19d183cf2689 | 26 | int partincrement = 0; |
kendunlop | 3:79133dcea836 | 27 | __int64 incrementer = 0; |
kendunlop | 1:19d183cf2689 | 28 | int CANCount = 0; |
kendunlop | 4:e8e9bc25b1ca | 29 | int spamon = 0; |
kendunlop | 6:2882710e4f1e | 30 | int idspamon = 0; |
kendunlop | 1:19d183cf2689 | 31 | int spamcount = 0; |
kendunlop | 3:79133dcea836 | 32 | int totalspamsever = 0; |
kendunlop | 2:11339018dda6 | 33 | int spamstarttime = 0; |
kendunlop | 2:11339018dda6 | 34 | int spamendtime = 0; |
kendunlop | 3:79133dcea836 | 35 | int vlistincrementer = 0; |
kendunlop | 6:2882710e4f1e | 36 | int idlistincrementer = 0; |
kendunlop | 6:2882710e4f1e | 37 | string spampreamble = "333"; |
kendunlop | 1:19d183cf2689 | 38 | |
kendunlop | 2:11339018dda6 | 39 | //A function to deal with each CAN message part (e.g.: 301, 8, 01, 02...) |
kendunlop | 1:19d183cf2689 | 40 | void dealwithpart(void) |
kendunlop | 1:19d183cf2689 | 41 | { |
kendunlop | 1:19d183cf2689 | 42 | int hextotal = 0; |
kendunlop | 1:19d183cf2689 | 43 | partincrement = partincrement + 1; |
kendunlop | 1:19d183cf2689 | 44 | //pc.printf("Dealing with part %d. (%s)\r\n", partincrement, part); |
kendunlop | 1:19d183cf2689 | 45 | //int partincrementb = 0; |
kendunlop | 1:19d183cf2689 | 46 | int textlength = part.length(); |
kendunlop | 1:19d183cf2689 | 47 | //int characterinc = 0; |
kendunlop | 1:19d183cf2689 | 48 | //pc.printf("That's %d characters long.\r\n", textlength); |
kendunlop | 1:19d183cf2689 | 49 | for (int i = 0; i < part.size(); i++) |
kendunlop | 1:19d183cf2689 | 50 | { |
kendunlop | 1:19d183cf2689 | 51 | //pc.printf("Examining character %d/%d.\r\n", (i+1), textlength); |
kendunlop | 1:19d183cf2689 | 52 | char individualcharacter = part.at(i); |
kendunlop | 1:19d183cf2689 | 53 | //pc.printf("That's '%c'.\r\n", individualcharacter); |
kendunlop | 1:19d183cf2689 | 54 | int numberized = 0; |
kendunlop | 1:19d183cf2689 | 55 | if(isdigit(individualcharacter)) |
kendunlop | 1:19d183cf2689 | 56 | { |
kendunlop | 1:19d183cf2689 | 57 | //pc.printf("That character is a digit.\r\n"); |
kendunlop | 1:19d183cf2689 | 58 | numberized = individualcharacter - '0'; |
kendunlop | 1:19d183cf2689 | 59 | //pc.printf("Numberized that's '%d'.\r\n", numberized); |
kendunlop | 1:19d183cf2689 | 60 | } |
kendunlop | 1:19d183cf2689 | 61 | else |
kendunlop | 1:19d183cf2689 | 62 | { |
kendunlop | 1:19d183cf2689 | 63 | //pc.printf("That character is NOT a digit.\r\n"); |
kendunlop | 1:19d183cf2689 | 64 | int asciivalue = toupper(individualcharacter); |
kendunlop | 1:19d183cf2689 | 65 | //pc.printf("Ascii value is %d.\r\n", asciivalue); |
kendunlop | 1:19d183cf2689 | 66 | numberized = asciivalue - 55; |
kendunlop | 1:19d183cf2689 | 67 | //pc.printf("Hex value is %d.\r\n", numberized); |
kendunlop | 1:19d183cf2689 | 68 | } |
kendunlop | 1:19d183cf2689 | 69 | //pc.printf("Eventual numberization is %d.\r\n", numberized); |
kendunlop | 1:19d183cf2689 | 70 | //pc.printf("Hex total is now %d.\r\n", hextotal); |
kendunlop | 1:19d183cf2689 | 71 | int powertoraise = part.size() - (i+1); |
kendunlop | 1:19d183cf2689 | 72 | //pc.printf("Must multiply by 16 to the power of %d.\r\n", powertoraise); |
kendunlop | 1:19d183cf2689 | 73 | int amounttoadd = numberized; |
kendunlop | 1:19d183cf2689 | 74 | //pc.printf("powertoraise is '%d'.\r\n", powertoraise); |
kendunlop | 1:19d183cf2689 | 75 | if (powertoraise == 1) |
kendunlop | 1:19d183cf2689 | 76 | { |
kendunlop | 1:19d183cf2689 | 77 | amounttoadd = numberized * 16; |
kendunlop | 1:19d183cf2689 | 78 | //pc.printf("Multiplying by 16.\r\n"); |
kendunlop | 1:19d183cf2689 | 79 | //pc.printf("powertoraise is '%d'.\r\n", powertoraise); |
kendunlop | 1:19d183cf2689 | 80 | } |
kendunlop | 1:19d183cf2689 | 81 | if (powertoraise == 2) |
kendunlop | 1:19d183cf2689 | 82 | { |
kendunlop | 1:19d183cf2689 | 83 | amounttoadd = numberized * 256; |
kendunlop | 1:19d183cf2689 | 84 | //pc.printf("Multiplying by 256.\r\n"); |
kendunlop | 1:19d183cf2689 | 85 | } |
kendunlop | 1:19d183cf2689 | 86 | //pc.printf("Amount to add is therefore %d.\r\n", amounttoadd); |
kendunlop | 1:19d183cf2689 | 87 | hextotal = hextotal + amounttoadd; |
kendunlop | 1:19d183cf2689 | 88 | //pc.printf("hextotal so far for this part is therefore %d.\r\n", hextotal); |
kendunlop | 1:19d183cf2689 | 89 | } |
kendunlop | 1:19d183cf2689 | 90 | //pc.printf("hextotal for whole part is therefore %d.\r\n", hextotal); |
kendunlop | 1:19d183cf2689 | 91 | //pc.printf("Need to convert that into true hex.\r\n"); |
kendunlop | 1:19d183cf2689 | 92 | std::stringstream sstream; |
kendunlop | 1:19d183cf2689 | 93 | sstream << std::hex << hextotal; |
kendunlop | 1:19d183cf2689 | 94 | //pc.printf("StringSteam says '%s'.\r\n", sstream.str()); |
kendunlop | 1:19d183cf2689 | 95 | if (partincrement == 1) |
kendunlop | 1:19d183cf2689 | 96 | {messageOut1.id = hextotal;} |
kendunlop | 1:19d183cf2689 | 97 | if (partincrement == 2) |
kendunlop | 1:19d183cf2689 | 98 | {messageOut1.len = hextotal;} |
kendunlop | 1:19d183cf2689 | 99 | if (partincrement >= 3) |
kendunlop | 1:19d183cf2689 | 100 | {messageOut1.data[partincrement-3] = hextotal;} |
kendunlop | 1:19d183cf2689 | 101 | //pc.printf("Part %d complete.\r\n", partincrement); |
kendunlop | 1:19d183cf2689 | 102 | //pc.printf("--------------------------------------\r\n"); |
kendunlop | 1:19d183cf2689 | 103 | } |
kendunlop | 1:19d183cf2689 | 104 | |
kendunlop | 3:79133dcea836 | 105 | //A function to get a coherent CAN message from one, uninterrupted string |
kendunlop | 6:2882710e4f1e | 106 | void getCANfrommessageOutText(void) |
kendunlop | 1:19d183cf2689 | 107 | { |
kendunlop | 1:19d183cf2689 | 108 | //pc.printf("messageOutText is '%s'\r\n", messageOutText); |
kendunlop | 1:19d183cf2689 | 109 | remove(messageOutText.begin(), messageOutText.end(), ' '); //Remove the spaces from the text to send out so it can be parsed. |
kendunlop | 1:19d183cf2689 | 110 | //pc.printf("After removing spaces, messageOutText is '%s'\r\n", messageOutText); |
kendunlop | 1:19d183cf2689 | 111 | 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 | 112 | //pc.printf("String to parse is '%s'.\r\n", startofstring); |
kendunlop | 1:19d183cf2689 | 113 | partincrement = 0; |
kendunlop | 1:19d183cf2689 | 114 | part = startofstring.substr(0,3); |
kendunlop | 1:19d183cf2689 | 115 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 116 | part = startofstring.substr(3,1); |
kendunlop | 1:19d183cf2689 | 117 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 118 | part = startofstring.substr(4,2); |
kendunlop | 1:19d183cf2689 | 119 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 120 | part = startofstring.substr(6,2); |
kendunlop | 1:19d183cf2689 | 121 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 122 | part = startofstring.substr(8,2); |
kendunlop | 1:19d183cf2689 | 123 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 124 | part = startofstring.substr(10,2); |
kendunlop | 1:19d183cf2689 | 125 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 126 | part = startofstring.substr(12,2); |
kendunlop | 1:19d183cf2689 | 127 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 128 | part = startofstring.substr(14,2); |
kendunlop | 1:19d183cf2689 | 129 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 130 | part = startofstring.substr(16,2); |
kendunlop | 1:19d183cf2689 | 131 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 132 | part = startofstring.substr(18,2); |
kendunlop | 1:19d183cf2689 | 133 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 134 | } |
kendunlop | 1:19d183cf2689 | 135 | |
kendunlop | 1:19d183cf2689 | 136 | void defineCANmessage(void) |
kendunlop | 1:19d183cf2689 | 137 | { |
kendunlop | 1:19d183cf2689 | 138 | //pc.printf("Defining CAN message 1.\r\n"); |
kendunlop | 1:19d183cf2689 | 139 | //messageOut1.format = CANStandard; |
kendunlop | 1:19d183cf2689 | 140 | //messageOut1.id = 0x301; |
kendunlop | 1:19d183cf2689 | 141 | //messageOut1.len = 8; |
kendunlop | 1:19d183cf2689 | 142 | //messageOut1.data[0] = 0x06; |
kendunlop | 1:19d183cf2689 | 143 | //messageOut1.data[1] = 0x3f; |
kendunlop | 1:19d183cf2689 | 144 | //messageOut1.data[2] = 0xb2; |
kendunlop | 1:19d183cf2689 | 145 | //messageOut1.data[3] = 0x29; |
kendunlop | 1:19d183cf2689 | 146 | //messageOut1.data[4] = 0x19; |
kendunlop | 1:19d183cf2689 | 147 | //messageOut1.data[5] = 0x97; |
kendunlop | 1:19d183cf2689 | 148 | //messageOut1.data[6] = 0x67; |
kendunlop | 1:19d183cf2689 | 149 | //messageOut1.data[7] = 0x37; |
kendunlop | 1:19d183cf2689 | 150 | } |
kendunlop | 1:19d183cf2689 | 151 | |
kendunlop | 3:79133dcea836 | 152 | void printMessageOut (void) |
kendunlop | 3:79133dcea836 | 153 | { |
kendunlop | 1:19d183cf2689 | 154 | //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 | 155 | 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 | 156 | } |
kendunlop | 1:19d183cf2689 | 157 | |
kendunlop | 3:79133dcea836 | 158 | void printMessageIn (void) |
kendunlop | 3:79133dcea836 | 159 | { |
kendunlop | 1:19d183cf2689 | 160 | //This function will print out whatever the CAN bus is receiving. |
kendunlop | 1:19d183cf2689 | 161 | 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 | 162 | } |
kendunlop | 0:7a500ebaa7a6 | 163 | |
kendunlop | 0:7a500ebaa7a6 | 164 | //The 'main' function will run as soon as the program starts. |
kendunlop | 0:7a500ebaa7a6 | 165 | int main() |
kendunlop | 1:19d183cf2689 | 166 | { |
kendunlop | 1:19d183cf2689 | 167 | pc.baud(115200); // serial port at 115200 |
kendunlop | 1:19d183cf2689 | 168 | CanBus.frequency(500 * 1000); // CAN bus at 500k |
kendunlop | 3:79133dcea836 | 169 | CanBus2.frequency(500 * 1000); // CAN bus at 500k |
kendunlop | 1:19d183cf2689 | 170 | CanBus.reset(); // clear any bus errors |
kendunlop | 3:79133dcea836 | 171 | CanBus2.reset(); // clear any bus errors |
kendunlop | 1:19d183cf2689 | 172 | //NOTE: Print messages must be below this line to work. |
kendunlop | 1:19d183cf2689 | 173 | pc.printf("------------------------------------------\r\n"); |
kendunlop | 1:19d183cf2689 | 174 | pc.printf("Welcome to Ken CAN test.\r\n"); |
kendunlop | 1:19d183cf2689 | 175 | pc.printf("Setting CAN bus to 500k.\r\n"); |
kendunlop | 6:2882710e4f1e | 176 | pc.printf("Setting serial port to baud rate 115200.\r\n"); |
kendunlop | 6:2882710e4f1e | 177 | pc.printf("Using pins 9 and 10 for CANBus 1 and 30 and 29 for CANBus 2.\r\n"); |
kendunlop | 1:19d183cf2689 | 178 | pc.printf("Version %d.%d\r\n",kMajorVersion,kMinorVersion); |
kendunlop | 1:19d183cf2689 | 179 | pc.printf("Build date %s %s\r\n",__DATE__,__TIME__); |
kendunlop | 1:19d183cf2689 | 180 | pc.printf("------------------------------------------\r\n"); |
kendunlop | 1:19d183cf2689 | 181 | char c; |
kendunlop | 1:19d183cf2689 | 182 | |
kendunlop | 1:19d183cf2689 | 183 | //Check for button presses |
kendunlop | 1:19d183cf2689 | 184 | while (1) |
kendunlop | 0:7a500ebaa7a6 | 185 | { |
kendunlop | 1:19d183cf2689 | 186 | if (pc.readable()) |
kendunlop | 1:19d183cf2689 | 187 | { |
kendunlop | 1:19d183cf2689 | 188 | c = pc.getc(); |
kendunlop | 3:79133dcea836 | 189 | if (c != NULL) |
kendunlop | 1:19d183cf2689 | 190 | { |
kendunlop | 3:79133dcea836 | 191 | //When the a key is pressed, define a CAN message and send it. |
kendunlop | 1:19d183cf2689 | 192 | //pc.printf("A key was pressed! (%c)\r\n", c); |
kendunlop | 1:19d183cf2689 | 193 | messageOutText = ""; |
kendunlop | 1:19d183cf2689 | 194 | if (c == '1') |
kendunlop | 1:19d183cf2689 | 195 | {messageOutText = "301 8 01 01 01 01 01 01 01 01";} |
kendunlop | 1:19d183cf2689 | 196 | if (c == '2') |
kendunlop | 1:19d183cf2689 | 197 | {messageOutText = "302 8 02 02 02 02 02 02 02 02";} |
kendunlop | 1:19d183cf2689 | 198 | if (c == '3') |
kendunlop | 3:79133dcea836 | 199 | {messageOutText = "303 8 00 00 00 00 00 00 00 33";} |
kendunlop | 1:19d183cf2689 | 200 | if (c == '4') |
kendunlop | 1:19d183cf2689 | 201 | {messageOutText = "304 8 04 04 04 04 04 04 04 04";} |
kendunlop | 1:19d183cf2689 | 202 | if (c == '5') |
kendunlop | 1:19d183cf2689 | 203 | {messageOutText = "305 8 05 05 05 05 05 05 05 05";} |
kendunlop | 1:19d183cf2689 | 204 | if (c == '6') |
kendunlop | 1:19d183cf2689 | 205 | {messageOutText = "306 8 06 06 06 06 06 06 06 06";} |
kendunlop | 1:19d183cf2689 | 206 | if (c == '7') |
kendunlop | 1:19d183cf2689 | 207 | {messageOutText = "307 8 07 07 07 07 07 07 07 07";} |
kendunlop | 1:19d183cf2689 | 208 | if (c == '8') |
kendunlop | 1:19d183cf2689 | 209 | {messageOutText = "308 8 08 08 08 08 08 08 08 08";} |
kendunlop | 1:19d183cf2689 | 210 | if (c == '9') |
kendunlop | 1:19d183cf2689 | 211 | {messageOutText = "309 8 09 09 09 09 09 09 09 09";} |
kendunlop | 1:19d183cf2689 | 212 | if (c == 'f') |
kendunlop | 1:19d183cf2689 | 213 | {messageOutText = "FFF 8 FF FF FF FF FF FF FF FF";} |
kendunlop | 1:19d183cf2689 | 214 | if (c == 'q') |
kendunlop | 3:79133dcea836 | 215 | { |
kendunlop | 3:79133dcea836 | 216 | pc.printf("Changing CAN bus speed to 125.\r\n"); |
kendunlop | 3:79133dcea836 | 217 | CanBus.frequency(125 * 1000); // CAN bus at 125k |
kendunlop | 5:bf4c6278ca8b | 218 | CanBus2.frequency(125 * 1000); // CAN bus at 125k |
kendunlop | 3:79133dcea836 | 219 | } |
kendunlop | 1:19d183cf2689 | 220 | if (c == 'w') |
kendunlop | 3:79133dcea836 | 221 | { |
kendunlop | 3:79133dcea836 | 222 | pc.printf("Changing CAN bus speed to 250.\r\n"); |
kendunlop | 3:79133dcea836 | 223 | CanBus.frequency(250 * 1000); // CAN bus at 250k |
kendunlop | 5:bf4c6278ca8b | 224 | CanBus2.frequency(250 * 1000); // CAN bus at 250k |
kendunlop | 3:79133dcea836 | 225 | } |
kendunlop | 1:19d183cf2689 | 226 | if (c == 'e') |
kendunlop | 3:79133dcea836 | 227 | { |
kendunlop | 3:79133dcea836 | 228 | pc.printf("Changing CAN bus speed to 500.\r\n"); |
kendunlop | 3:79133dcea836 | 229 | CanBus.frequency(500 * 1000); // CAN bus at 500k |
kendunlop | 3:79133dcea836 | 230 | CanBus2.frequency(500 * 1000);// CAN bus at 500k |
kendunlop | 3:79133dcea836 | 231 | } |
kendunlop | 1:19d183cf2689 | 232 | if (c == 'r') |
kendunlop | 3:79133dcea836 | 233 | { |
kendunlop | 3:79133dcea836 | 234 | pc.printf("Changing CAN bus speed to 1000.\r\n"); |
kendunlop | 3:79133dcea836 | 235 | CanBus.frequency(1000 * 1000); // CAN bus at 1000k |
kendunlop | 3:79133dcea836 | 236 | CanBus2.frequency(1000 * 1000); // CAN bus at 1000k |
kendunlop | 3:79133dcea836 | 237 | } |
kendunlop | 1:19d183cf2689 | 238 | if (c == 's') |
kendunlop | 1:19d183cf2689 | 239 | { |
kendunlop | 1:19d183cf2689 | 240 | pc.printf("Sending authentic sats message.\r\n"); |
kendunlop | 1:19d183cf2689 | 241 | messageOutText = "301 8 06 3F B2 29 12 97 67 37"; |
kendunlop | 1:19d183cf2689 | 242 | } |
kendunlop | 3:79133dcea836 | 243 | if (c == 'o') |
kendunlop | 3:79133dcea836 | 244 | { |
kendunlop | 3:79133dcea836 | 245 | incrementer = (incrementer * 16); |
kendunlop | 3:79133dcea836 | 246 | pc.printf("Multiplied Incrementer by 16. It's now now %d.\r\n", incrementer); |
kendunlop | 3:79133dcea836 | 247 | } |
kendunlop | 1:19d183cf2689 | 248 | if (c == 'i') |
kendunlop | 1:19d183cf2689 | 249 | { |
kendunlop | 1:19d183cf2689 | 250 | incrementer = incrementer + 16; |
kendunlop | 1:19d183cf2689 | 251 | std::stringstream sstream; |
kendunlop | 1:19d183cf2689 | 252 | sstream << std::hex << incrementer; |
kendunlop | 1:19d183cf2689 | 253 | string stringsofar = sstream.str(); |
kendunlop | 1:19d183cf2689 | 254 | //pc.printf("Incrementer is now %d.\r\n", incrementer); |
kendunlop | 1:19d183cf2689 | 255 | //pc.printf("StringStream says '%s'.\r\n", stringsofar); |
kendunlop | 1:19d183cf2689 | 256 | int length = stringsofar.length(); |
kendunlop | 1:19d183cf2689 | 257 | //pc.printf("Length is %d/16.\r\n", length); |
kendunlop | 1:19d183cf2689 | 258 | for (int i = 0; i < (16-length); i++) |
kendunlop | 1:19d183cf2689 | 259 | stringsofar = "0" + stringsofar; |
kendunlop | 1:19d183cf2689 | 260 | //pc.printf("stringsofar says '%s'.\r\n", stringsofar); |
kendunlop | 1:19d183cf2689 | 261 | messageOutText = "305 8 " + stringsofar; |
kendunlop | 1:19d183cf2689 | 262 | //pc.printf("Will try to send '%s'.\r\n", messageOutText); |
kendunlop | 1:19d183cf2689 | 263 | } |
kendunlop | 1:19d183cf2689 | 264 | messageOut1.format = CANStandard; |
kendunlop | 1:19d183cf2689 | 265 | if (c == 'z' and spamon == 0) |
kendunlop | 1:19d183cf2689 | 266 | { |
kendunlop | 6:2882710e4f1e | 267 | pc.printf("Starting spam sequence for ID %s. Press 'x' to end. \r\n", spampreamble); |
kendunlop | 1:19d183cf2689 | 268 | spamon = 1; |
kendunlop | 1:19d183cf2689 | 269 | incrementer = 0; |
kendunlop | 2:11339018dda6 | 270 | spamcount = 0; |
kendunlop | 3:79133dcea836 | 271 | spamstarttime = clock(); |
kendunlop | 1:19d183cf2689 | 272 | } |
kendunlop | 1:19d183cf2689 | 273 | //Pressing 'x' switches off spam mode |
kendunlop | 1:19d183cf2689 | 274 | if (c == 'x' and spamon == 1) |
kendunlop | 1:19d183cf2689 | 275 | { |
kendunlop | 3:79133dcea836 | 276 | //spamcount = 9999999; |
kendunlop | 1:19d183cf2689 | 277 | spamon = 0; |
kendunlop | 3:79133dcea836 | 278 | int spamendtime = 0; |
kendunlop | 3:79133dcea836 | 279 | spamendtime = clock(); |
kendunlop | 2:11339018dda6 | 280 | int spamtime = 0; |
kendunlop | 2:11339018dda6 | 281 | spamtime = spamendtime - spamstarttime; |
kendunlop | 3:79133dcea836 | 282 | int spamtimeseconds = 0; |
kendunlop | 3:79133dcea836 | 283 | spamtimeseconds = (spamtime / CLOCKS_PER_SEC); |
kendunlop | 3:79133dcea836 | 284 | 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 | 285 | //pc.printf("Clock right now is %d.\r\n", (spamtime)); |
kendunlop | 3:79133dcea836 | 286 | //pc.printf("Ticks per second are %d.\r\n", CLOCKS_PER_SEC); |
kendunlop | 3:79133dcea836 | 287 | //pc.printf("Spam start/end is %d/%d.\r\n", spamstarttime, spamendtime); |
kendunlop | 3:79133dcea836 | 288 | //pc.printf("Spamtime was %d ticks.\r\n", spamtime); |
kendunlop | 2:11339018dda6 | 289 | int spamspersecond = 0; |
kendunlop | 3:79133dcea836 | 290 | spamspersecond = (10000 * spamcount); |
kendunlop | 3:79133dcea836 | 291 | //pc.printf("spamcount times 10,000 is %d.\r\n", spamspersecond); |
kendunlop | 3:79133dcea836 | 292 | spamspersecond = spamspersecond / spamtime; |
kendunlop | 3:79133dcea836 | 293 | //pc.printf("10,000 times spamcount divided by spamtime is %d spams per tick.\r\n", spamspersecond); |
kendunlop | 3:79133dcea836 | 294 | spamspersecond = ((spamspersecond * CLOCKS_PER_SEC))/10000; |
kendunlop | 3:79133dcea836 | 295 | pc.printf("Spams per second are %d.\r\n", spamspersecond); |
kendunlop | 3:79133dcea836 | 296 | pc.printf("Total spams ever are %d.\r\n", totalspamsever); |
kendunlop | 3:79133dcea836 | 297 | pc.printf("-------------------------\r\n"); |
kendunlop | 3:79133dcea836 | 298 | } |
kendunlop | 6:2882710e4f1e | 299 | if (c == 'v' and idspamon == 0) |
kendunlop | 3:79133dcea836 | 300 | { |
kendunlop | 6:2882710e4f1e | 301 | idspamon = 1; //Set the 'idspamon' integer to 1 so ID spam happens. |
kendunlop | 6:2882710e4f1e | 302 | idlistincrementer = 0; |
kendunlop | 6:2882710e4f1e | 303 | pc.printf("Beginning spam of all possible CAN IDs (000 - FFF).\r\n"); |
kendunlop | 1:19d183cf2689 | 304 | } |
kendunlop | 1:19d183cf2689 | 305 | if (messageOutText != "") |
kendunlop | 1:19d183cf2689 | 306 | { |
kendunlop | 6:2882710e4f1e | 307 | getCANfrommessageOutText(); |
kendunlop | 1:19d183cf2689 | 308 | CanBus.write(messageOut1); |
kendunlop | 3:79133dcea836 | 309 | CanBus2.write(messageOut1); |
kendunlop | 6:2882710e4f1e | 310 | if (spamon == 0 and idspamon == 0) |
kendunlop | 3:79133dcea836 | 311 | { |
kendunlop | 3:79133dcea836 | 312 | printMessageOut(); |
kendunlop | 3:79133dcea836 | 313 | } |
kendunlop | 3:79133dcea836 | 314 | messageOutText = ""; |
kendunlop | 1:19d183cf2689 | 315 | } |
kendunlop | 5:bf4c6278ca8b | 316 | //If spam mode is on, spam an incrementing CAN message |
kendunlop | 5:bf4c6278ca8b | 317 | } |
kendunlop | 4:e8e9bc25b1ca | 318 | } |
kendunlop | 5:bf4c6278ca8b | 319 | |
kendunlop | 4:e8e9bc25b1ca | 320 | |
kendunlop | 6:2882710e4f1e | 321 | if (spamon == 1) |
kendunlop | 5:bf4c6278ca8b | 322 | { |
kendunlop | 5:bf4c6278ca8b | 323 | spamcount ++; |
kendunlop | 5:bf4c6278ca8b | 324 | totalspamsever ++; |
kendunlop | 5:bf4c6278ca8b | 325 | incrementer ++; |
kendunlop | 5:bf4c6278ca8b | 326 | std::stringstream sstream; |
kendunlop | 5:bf4c6278ca8b | 327 | sstream << std::hex << incrementer; |
kendunlop | 5:bf4c6278ca8b | 328 | string stringsofar = sstream.str(); |
kendunlop | 5:bf4c6278ca8b | 329 | int length = stringsofar.length(); |
kendunlop | 5:bf4c6278ca8b | 330 | for (int i = 0; i < (16-length); i++) |
kendunlop | 5:bf4c6278ca8b | 331 | stringsofar = "0" + stringsofar; |
kendunlop | 6:2882710e4f1e | 332 | messageOutText = spampreamble + " 8 " + stringsofar; |
kendunlop | 6:2882710e4f1e | 333 | getCANfrommessageOutText(); |
kendunlop | 5:bf4c6278ca8b | 334 | CanBus.write(messageOut1); |
kendunlop | 6:2882710e4f1e | 335 | CanBus2.write(messageOut1); |
kendunlop | 5:bf4c6278ca8b | 336 | wait(0.01); |
kendunlop | 5:bf4c6278ca8b | 337 | } |
kendunlop | 6:2882710e4f1e | 338 | if (idspamon == 1) |
kendunlop | 6:2882710e4f1e | 339 | { |
kendunlop | 6:2882710e4f1e | 340 | std::stringstream sstream; |
kendunlop | 6:2882710e4f1e | 341 | sstream << std::hex << idlistincrementer; |
kendunlop | 6:2882710e4f1e | 342 | string idstring = ""; |
kendunlop | 6:2882710e4f1e | 343 | idstring = sstream.str(); |
kendunlop | 6:2882710e4f1e | 344 | //pc.printf("idstring says '%s'\r\n", idstring); |
kendunlop | 6:2882710e4f1e | 345 | if (idstring.length() < 3) |
kendunlop | 6:2882710e4f1e | 346 | { |
kendunlop | 6:2882710e4f1e | 347 | for (int i = 0; i < (4 - idstring.length()); i++) |
kendunlop | 6:2882710e4f1e | 348 | { |
kendunlop | 6:2882710e4f1e | 349 | idstring = "0" + idstring; |
kendunlop | 6:2882710e4f1e | 350 | } |
kendunlop | 6:2882710e4f1e | 351 | } |
kendunlop | 6:2882710e4f1e | 352 | //pc.printf("idstring now says '%s'\r\n", idstring); |
kendunlop | 6:2882710e4f1e | 353 | //pc.printf("%s\r\n", idstring); |
kendunlop | 6:2882710e4f1e | 354 | idstring = idstring + " 8 00 00 00 00 00 00 00 01"; |
kendunlop | 6:2882710e4f1e | 355 | //pc.printf("...and now idstring now says '%s'\r\n", idstring); |
kendunlop | 6:2882710e4f1e | 356 | messageOutText = idstring; |
kendunlop | 6:2882710e4f1e | 357 | getCANfrommessageOutText(); |
kendunlop | 6:2882710e4f1e | 358 | CanBus.write(messageOut1); |
kendunlop | 6:2882710e4f1e | 359 | CanBus2.write(messageOut1); |
kendunlop | 6:2882710e4f1e | 360 | idlistincrementer++; //Only add 1 to the idlist incrementer at the end so the first ID it sends is '000'. |
kendunlop | 6:2882710e4f1e | 361 | //pc.printf("ID list incrementer is now %d\r\n", idlistincrementer); |
kendunlop | 6:2882710e4f1e | 362 | if (idlistincrementer >= 4096) //If the spam value gets to 4096 (FFF), end the spam sequence. |
kendunlop | 6:2882710e4f1e | 363 | { |
kendunlop | 6:2882710e4f1e | 364 | idspamon = 0; |
kendunlop | 6:2882710e4f1e | 365 | pc.printf("Ending ID spam sequence.\r\n"); |
kendunlop | 6:2882710e4f1e | 366 | idlistincrementer = 0; |
kendunlop | 6:2882710e4f1e | 367 | } |
kendunlop | 6:2882710e4f1e | 368 | wait(0.01);//ID spam at 100Hz |
kendunlop | 6:2882710e4f1e | 369 | } |
kendunlop | 1:19d183cf2689 | 370 | //Check for CAN messages coming in |
kendunlop | 3:79133dcea836 | 371 | if (CanBus.read(messageIn)) |
kendunlop | 1:19d183cf2689 | 372 | { |
kendunlop | 1:19d183cf2689 | 373 | CANCount ++; |
kendunlop | 1:19d183cf2689 | 374 | printMessageIn(); |
kendunlop | 1:19d183cf2689 | 375 | } |
kendunlop | 3:79133dcea836 | 376 | } |
kendunlop | 5:bf4c6278ca8b | 377 | } |