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