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@1:19d183cf2689, 2022-06-14 (annotated)
- Committer:
- kendunlop
- Date:
- Tue Jun 14 10:57:11 2022 +0000
- Revision:
- 1:19d183cf2689
- Parent:
- 0:7a500ebaa7a6
- Child:
- 2:11339018dda6
The first commit of Ken CAN test. v58.
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 | 1:19d183cf2689 | 2 | #define kMajorVersion 0 |
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 | 1:19d183cf2689 | 10 | using namespace std; |
kendunlop | 1:19d183cf2689 | 11 | //#include <cmath> |
kendunlop | 1:19d183cf2689 | 12 | //#include <iostream> //May be needed to turn an integer into a hex byte |
kendunlop | 1:19d183cf2689 | 13 | using namespace std; |
kendunlop | 1:19d183cf2689 | 14 | |
kendunlop | 1:19d183cf2689 | 15 | RawSerial pc(USBTX, USBRX); // USB UART Terminal. Is needed for 'pc.printf' functions to work. |
kendunlop | 1:19d183cf2689 | 16 | CAN CanBus(p9, p10); //CANBus to use pins 9 and 10. This defines CanBus so other 'CanBus' lines will work. |
kendunlop | 1:19d183cf2689 | 17 | CAN CanBus2(p29, p30); //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 | 1:19d183cf2689 | 27 | int incrementer = 0; |
kendunlop | 1:19d183cf2689 | 28 | int CANCount = 0; |
kendunlop | 1:19d183cf2689 | 29 | int spamon = 0; |
kendunlop | 1:19d183cf2689 | 30 | int spamcount = 0; |
kendunlop | 1:19d183cf2689 | 31 | |
kendunlop | 1:19d183cf2689 | 32 | //A function to deal with each CAN message part (e.g.: 301, 8, 01, 02) |
kendunlop | 1:19d183cf2689 | 33 | void dealwithpart(void) |
kendunlop | 1:19d183cf2689 | 34 | { |
kendunlop | 1:19d183cf2689 | 35 | int hextotal = 0; |
kendunlop | 1:19d183cf2689 | 36 | partincrement = partincrement + 1; |
kendunlop | 1:19d183cf2689 | 37 | //pc.printf("Dealing with part %d. (%s)\r\n", partincrement, part); |
kendunlop | 1:19d183cf2689 | 38 | //int partincrementb = 0; |
kendunlop | 1:19d183cf2689 | 39 | int textlength = part.length(); |
kendunlop | 1:19d183cf2689 | 40 | //int characterinc = 0; |
kendunlop | 1:19d183cf2689 | 41 | //pc.printf("That's %d characters long.\r\n", textlength); |
kendunlop | 1:19d183cf2689 | 42 | for (int i = 0; i < part.size(); i++) |
kendunlop | 1:19d183cf2689 | 43 | { |
kendunlop | 1:19d183cf2689 | 44 | //pc.printf("Examining character %d/%d.\r\n", (i+1), textlength); |
kendunlop | 1:19d183cf2689 | 45 | char individualcharacter = part.at(i); |
kendunlop | 1:19d183cf2689 | 46 | //pc.printf("That's '%c'.\r\n", individualcharacter); |
kendunlop | 1:19d183cf2689 | 47 | int numberized = 0; |
kendunlop | 1:19d183cf2689 | 48 | if(isdigit(individualcharacter)) |
kendunlop | 1:19d183cf2689 | 49 | { |
kendunlop | 1:19d183cf2689 | 50 | //pc.printf("That character is a digit.\r\n"); |
kendunlop | 1:19d183cf2689 | 51 | numberized = individualcharacter - '0'; |
kendunlop | 1:19d183cf2689 | 52 | //pc.printf("Numberized that's '%d'.\r\n", numberized); |
kendunlop | 1:19d183cf2689 | 53 | } |
kendunlop | 1:19d183cf2689 | 54 | else |
kendunlop | 1:19d183cf2689 | 55 | { |
kendunlop | 1:19d183cf2689 | 56 | //pc.printf("That character is NOT a digit.\r\n"); |
kendunlop | 1:19d183cf2689 | 57 | int asciivalue = toupper(individualcharacter); |
kendunlop | 1:19d183cf2689 | 58 | //pc.printf("Ascii value is %d.\r\n", asciivalue); |
kendunlop | 1:19d183cf2689 | 59 | numberized = asciivalue - 55; |
kendunlop | 1:19d183cf2689 | 60 | //pc.printf("Hex value is %d.\r\n", numberized); |
kendunlop | 1:19d183cf2689 | 61 | } |
kendunlop | 1:19d183cf2689 | 62 | //pc.printf("Eventual numberization is %d.\r\n", numberized); |
kendunlop | 1:19d183cf2689 | 63 | //pc.printf("Hex total is now %d.\r\n", hextotal); |
kendunlop | 1:19d183cf2689 | 64 | int powertoraise = part.size() - (i+1); |
kendunlop | 1:19d183cf2689 | 65 | //pc.printf("Must multiply by 16 to the power of %d.\r\n", powertoraise); |
kendunlop | 1:19d183cf2689 | 66 | int amounttoadd = numberized; |
kendunlop | 1:19d183cf2689 | 67 | //pc.printf("powertoraise is '%d'.\r\n", powertoraise); |
kendunlop | 1:19d183cf2689 | 68 | if (powertoraise == 1) |
kendunlop | 1:19d183cf2689 | 69 | { |
kendunlop | 1:19d183cf2689 | 70 | amounttoadd = numberized * 16; |
kendunlop | 1:19d183cf2689 | 71 | //pc.printf("Multiplying by 16.\r\n"); |
kendunlop | 1:19d183cf2689 | 72 | //pc.printf("powertoraise is '%d'.\r\n", powertoraise); |
kendunlop | 1:19d183cf2689 | 73 | } |
kendunlop | 1:19d183cf2689 | 74 | if (powertoraise == 2) |
kendunlop | 1:19d183cf2689 | 75 | { |
kendunlop | 1:19d183cf2689 | 76 | amounttoadd = numberized * 256; |
kendunlop | 1:19d183cf2689 | 77 | //pc.printf("Multiplying by 256.\r\n"); |
kendunlop | 1:19d183cf2689 | 78 | } |
kendunlop | 1:19d183cf2689 | 79 | //pc.printf("Amount to add is therefore %d.\r\n", amounttoadd); |
kendunlop | 1:19d183cf2689 | 80 | hextotal = hextotal + amounttoadd; |
kendunlop | 1:19d183cf2689 | 81 | //pc.printf("hextotal so far for this part is therefore %d.\r\n", hextotal); |
kendunlop | 1:19d183cf2689 | 82 | } |
kendunlop | 1:19d183cf2689 | 83 | //pc.printf("hextotal for whole part is therefore %d.\r\n", hextotal); |
kendunlop | 1:19d183cf2689 | 84 | //pc.printf("Need to convert that into true hex.\r\n"); |
kendunlop | 1:19d183cf2689 | 85 | std::stringstream sstream; |
kendunlop | 1:19d183cf2689 | 86 | sstream << std::hex << hextotal; |
kendunlop | 1:19d183cf2689 | 87 | //pc.printf("StringSteam says '%s'.\r\n", sstream.str()); |
kendunlop | 1:19d183cf2689 | 88 | if (partincrement == 1) |
kendunlop | 1:19d183cf2689 | 89 | {messageOut1.id = hextotal;} |
kendunlop | 1:19d183cf2689 | 90 | if (partincrement == 2) |
kendunlop | 1:19d183cf2689 | 91 | {messageOut1.len = hextotal;} |
kendunlop | 1:19d183cf2689 | 92 | if (partincrement >= 3) |
kendunlop | 1:19d183cf2689 | 93 | {messageOut1.data[partincrement-3] = hextotal;} |
kendunlop | 1:19d183cf2689 | 94 | //pc.printf("Part %d complete.\r\n", partincrement); |
kendunlop | 1:19d183cf2689 | 95 | //pc.printf("--------------------------------------\r\n"); |
kendunlop | 1:19d183cf2689 | 96 | } |
kendunlop | 1:19d183cf2689 | 97 | |
kendunlop | 1:19d183cf2689 | 98 | //An attempted function to get a coherent CAN message from one, uninterrupted string |
kendunlop | 1:19d183cf2689 | 99 | void getCANfromstring(void) |
kendunlop | 1:19d183cf2689 | 100 | { |
kendunlop | 1:19d183cf2689 | 101 | //pc.printf("messageOutText is '%s'\r\n", messageOutText); |
kendunlop | 1:19d183cf2689 | 102 | remove(messageOutText.begin(), messageOutText.end(), ' '); //Remove the spaces from the text to send out so it can be parsed. |
kendunlop | 1:19d183cf2689 | 103 | //pc.printf("After removing spaces, messageOutText is '%s'\r\n", messageOutText); |
kendunlop | 1:19d183cf2689 | 104 | 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 | 105 | //pc.printf("String to parse is '%s'.\r\n", startofstring); |
kendunlop | 1:19d183cf2689 | 106 | partincrement = 0; |
kendunlop | 1:19d183cf2689 | 107 | part = startofstring.substr(0,3); |
kendunlop | 1:19d183cf2689 | 108 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 109 | part = startofstring.substr(3,1); |
kendunlop | 1:19d183cf2689 | 110 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 111 | part = startofstring.substr(4,2); |
kendunlop | 1:19d183cf2689 | 112 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 113 | part = startofstring.substr(6,2); |
kendunlop | 1:19d183cf2689 | 114 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 115 | part = startofstring.substr(8,2); |
kendunlop | 1:19d183cf2689 | 116 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 117 | part = startofstring.substr(10,2); |
kendunlop | 1:19d183cf2689 | 118 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 119 | part = startofstring.substr(12,2); |
kendunlop | 1:19d183cf2689 | 120 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 121 | part = startofstring.substr(14,2); |
kendunlop | 1:19d183cf2689 | 122 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 123 | part = startofstring.substr(16,2); |
kendunlop | 1:19d183cf2689 | 124 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 125 | part = startofstring.substr(18,2); |
kendunlop | 1:19d183cf2689 | 126 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 127 | //pc.printf("First part is '%s'.\r\n", part1); |
kendunlop | 1:19d183cf2689 | 128 | //pc.printf("Second part is '%s'.\r\n", part2); |
kendunlop | 1:19d183cf2689 | 129 | //pc.printf("Third part is '%s'.\r\n", part3); |
kendunlop | 1:19d183cf2689 | 130 | //pc.printf("Fourth part is '%s'.\r\n", part4); |
kendunlop | 1:19d183cf2689 | 131 | //pc.printf("Fifth part is '%s'.\r\n", part5); |
kendunlop | 1:19d183cf2689 | 132 | //pc.printf("Sixth part is '%s'.\r\n", part6); |
kendunlop | 1:19d183cf2689 | 133 | //pc.printf("Seventh part is '%s'.\r\n", part7); |
kendunlop | 1:19d183cf2689 | 134 | //pc.printf("Eighth part is '%s'.\r\n", part8); |
kendunlop | 1:19d183cf2689 | 135 | //pc.printf("Ninth part is '%s'.\r\n", part9); |
kendunlop | 1:19d183cf2689 | 136 | //pc.printf("Tenth part is '%s'.\r\n", part10); |
kendunlop | 1:19d183cf2689 | 137 | //string mOTwithoutspaces = messageOutText.erase(remove(messageOutText.begin(), messageOutText.end(), " "), messageOutText.end()); |
kendunlop | 1:19d183cf2689 | 138 | } |
kendunlop | 1:19d183cf2689 | 139 | |
kendunlop | 1:19d183cf2689 | 140 | void defineCANmessage(void) |
kendunlop | 1:19d183cf2689 | 141 | { |
kendunlop | 1:19d183cf2689 | 142 | //pc.printf("Defining CAN message 1.\r\n"); |
kendunlop | 1:19d183cf2689 | 143 | //messageOut1.format = CANStandard; |
kendunlop | 1:19d183cf2689 | 144 | //messageOut1.id = 0x301; |
kendunlop | 1:19d183cf2689 | 145 | //messageOut1.len = 8; |
kendunlop | 1:19d183cf2689 | 146 | //messageOut1.data[0] = 0x06; |
kendunlop | 1:19d183cf2689 | 147 | //messageOut1.data[1] = 0x3f; |
kendunlop | 1:19d183cf2689 | 148 | //messageOut1.data[2] = 0xb2; |
kendunlop | 1:19d183cf2689 | 149 | //messageOut1.data[3] = 0x29; |
kendunlop | 1:19d183cf2689 | 150 | //messageOut1.data[4] = 0x19; |
kendunlop | 1:19d183cf2689 | 151 | //messageOut1.data[5] = 0x97; |
kendunlop | 1:19d183cf2689 | 152 | //messageOut1.data[6] = 0x67; |
kendunlop | 1:19d183cf2689 | 153 | //messageOut1.data[7] = 0x37; |
kendunlop | 1:19d183cf2689 | 154 | } |
kendunlop | 1:19d183cf2689 | 155 | |
kendunlop | 1:19d183cf2689 | 156 | void printMessageOut (void){ |
kendunlop | 1:19d183cf2689 | 157 | //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 | 158 | 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 | 159 | } |
kendunlop | 1:19d183cf2689 | 160 | |
kendunlop | 1:19d183cf2689 | 161 | void printMessageIn (void){ |
kendunlop | 1:19d183cf2689 | 162 | //This function will print out whatever the CAN bus is receiving. |
kendunlop | 1:19d183cf2689 | 163 | 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 | 164 | } |
kendunlop | 0:7a500ebaa7a6 | 165 | |
kendunlop | 0:7a500ebaa7a6 | 166 | //The 'main' function will run as soon as the program starts. |
kendunlop | 0:7a500ebaa7a6 | 167 | int main() |
kendunlop | 1:19d183cf2689 | 168 | { |
kendunlop | 1:19d183cf2689 | 169 | pc.baud(115200); // serial port at 115200 |
kendunlop | 1:19d183cf2689 | 170 | CanBus.frequency(500 * 1000); // CAN bus at 500k |
kendunlop | 1:19d183cf2689 | 171 | CanBus.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 | 1:19d183cf2689 | 176 | pc.printf("Setting serial port to 115200.\r\n"); |
kendunlop | 1:19d183cf2689 | 177 | pc.printf("Using pins 9 and 10.\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 | 1:19d183cf2689 | 189 | if ((c != NULL)) |
kendunlop | 1:19d183cf2689 | 190 | { |
kendunlop | 1:19d183cf2689 | 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 | //pc.printf("Will try to send CAN...\r\n"); |
kendunlop | 1:19d183cf2689 | 194 | //defineCANmessage(); |
kendunlop | 1:19d183cf2689 | 195 | messageOutText = ""; |
kendunlop | 1:19d183cf2689 | 196 | if (c == '1') |
kendunlop | 1:19d183cf2689 | 197 | {messageOutText = "301 8 01 01 01 01 01 01 01 01";} |
kendunlop | 1:19d183cf2689 | 198 | if (c == '2') |
kendunlop | 1:19d183cf2689 | 199 | {messageOutText = "302 8 02 02 02 02 02 02 02 02";} |
kendunlop | 1:19d183cf2689 | 200 | if (c == '3') |
kendunlop | 1:19d183cf2689 | 201 | {messageOutText = "303 8 03 03 03 03 03 03 03 03";} |
kendunlop | 1:19d183cf2689 | 202 | if (c == '4') |
kendunlop | 1:19d183cf2689 | 203 | {messageOutText = "304 8 04 04 04 04 04 04 04 04";} |
kendunlop | 1:19d183cf2689 | 204 | if (c == '5') |
kendunlop | 1:19d183cf2689 | 205 | {messageOutText = "305 8 05 05 05 05 05 05 05 05";} |
kendunlop | 1:19d183cf2689 | 206 | if (c == '6') |
kendunlop | 1:19d183cf2689 | 207 | {messageOutText = "306 8 06 06 06 06 06 06 06 06";} |
kendunlop | 1:19d183cf2689 | 208 | if (c == '7') |
kendunlop | 1:19d183cf2689 | 209 | {messageOutText = "307 8 07 07 07 07 07 07 07 07";} |
kendunlop | 1:19d183cf2689 | 210 | if (c == '8') |
kendunlop | 1:19d183cf2689 | 211 | {messageOutText = "308 8 08 08 08 08 08 08 08 08";} |
kendunlop | 1:19d183cf2689 | 212 | if (c == '9') |
kendunlop | 1:19d183cf2689 | 213 | {messageOutText = "309 8 09 09 09 09 09 09 09 09";} |
kendunlop | 1:19d183cf2689 | 214 | if (c == 'f') |
kendunlop | 1:19d183cf2689 | 215 | {messageOutText = "FFF 8 FF FF FF FF FF FF FF FF";} |
kendunlop | 1:19d183cf2689 | 216 | if (c == 'q') |
kendunlop | 1:19d183cf2689 | 217 | {pc.printf("Changing CAN bus speed to 125.\r\n"); |
kendunlop | 1:19d183cf2689 | 218 | CanBus.frequency(125 * 1000);} // CAN bus at 125k |
kendunlop | 1:19d183cf2689 | 219 | if (c == 'w') |
kendunlop | 1:19d183cf2689 | 220 | {pc.printf("Changing CAN bus speed to 250.\r\n"); |
kendunlop | 1:19d183cf2689 | 221 | CanBus.frequency(250 * 1000);} // CAN bus at 250k |
kendunlop | 1:19d183cf2689 | 222 | if (c == 'e') |
kendunlop | 1:19d183cf2689 | 223 | {pc.printf("Changing CAN bus speed to 500.\r\n"); |
kendunlop | 1:19d183cf2689 | 224 | CanBus.frequency(500 * 1000);} // CAN bus at 500k |
kendunlop | 1:19d183cf2689 | 225 | if (c == 'r') |
kendunlop | 1:19d183cf2689 | 226 | {pc.printf("Changing CAN bus speed to 1000.\r\n"); |
kendunlop | 1:19d183cf2689 | 227 | CanBus.frequency(1000 * 1000);} // CAN bus at 1000k |
kendunlop | 1:19d183cf2689 | 228 | if (c == 's') |
kendunlop | 1:19d183cf2689 | 229 | { |
kendunlop | 1:19d183cf2689 | 230 | pc.printf("Sending authentic sats message.\r\n"); |
kendunlop | 1:19d183cf2689 | 231 | messageOutText = "301 8 06 3F B2 29 12 97 67 37"; |
kendunlop | 1:19d183cf2689 | 232 | } |
kendunlop | 1:19d183cf2689 | 233 | if (c == 'i') |
kendunlop | 1:19d183cf2689 | 234 | { |
kendunlop | 1:19d183cf2689 | 235 | incrementer = incrementer + 16; |
kendunlop | 1:19d183cf2689 | 236 | std::stringstream sstream; |
kendunlop | 1:19d183cf2689 | 237 | sstream << std::hex << incrementer; |
kendunlop | 1:19d183cf2689 | 238 | string stringsofar = sstream.str(); |
kendunlop | 1:19d183cf2689 | 239 | //pc.printf("Incrementer is now %d.\r\n", incrementer); |
kendunlop | 1:19d183cf2689 | 240 | //pc.printf("StringStream says '%s'.\r\n", stringsofar); |
kendunlop | 1:19d183cf2689 | 241 | int length = stringsofar.length(); |
kendunlop | 1:19d183cf2689 | 242 | //pc.printf("Length is %d/16.\r\n", length); |
kendunlop | 1:19d183cf2689 | 243 | for (int i = 0; i < (16-length); i++) |
kendunlop | 1:19d183cf2689 | 244 | stringsofar = "0" + stringsofar; |
kendunlop | 1:19d183cf2689 | 245 | //pc.printf("stringsofar says '%s'.\r\n", stringsofar); |
kendunlop | 1:19d183cf2689 | 246 | messageOutText = "305 8 " + stringsofar; |
kendunlop | 1:19d183cf2689 | 247 | //pc.printf("Will try to send '%s'.\r\n", messageOutText); |
kendunlop | 1:19d183cf2689 | 248 | } |
kendunlop | 1:19d183cf2689 | 249 | messageOut1.format = CANStandard; |
kendunlop | 1:19d183cf2689 | 250 | if (c == 'z' and spamon == 0) |
kendunlop | 1:19d183cf2689 | 251 | { |
kendunlop | 1:19d183cf2689 | 252 | pc.printf("Starting spam sequence...\r\n"); |
kendunlop | 1:19d183cf2689 | 253 | spamon = 1; |
kendunlop | 1:19d183cf2689 | 254 | incrementer = 0; |
kendunlop | 1:19d183cf2689 | 255 | int spamcount = 0; |
kendunlop | 1:19d183cf2689 | 256 | } |
kendunlop | 1:19d183cf2689 | 257 | //Pressing 'x' switches off spam mode |
kendunlop | 1:19d183cf2689 | 258 | if (c == 'x' and spamon == 1) |
kendunlop | 1:19d183cf2689 | 259 | { |
kendunlop | 1:19d183cf2689 | 260 | spamon = 0; |
kendunlop | 1:19d183cf2689 | 261 | pc.printf("Ending spam mode.\r\n"); |
kendunlop | 1:19d183cf2689 | 262 | } |
kendunlop | 1:19d183cf2689 | 263 | if (messageOutText != "") |
kendunlop | 1:19d183cf2689 | 264 | { |
kendunlop | 1:19d183cf2689 | 265 | getCANfromstring(); |
kendunlop | 1:19d183cf2689 | 266 | CanBus.write(messageOut1); |
kendunlop | 1:19d183cf2689 | 267 | if (spamon == 0) |
kendunlop | 1:19d183cf2689 | 268 | {printMessageOut();} |
kendunlop | 1:19d183cf2689 | 269 | } |
kendunlop | 1:19d183cf2689 | 270 | } |
kendunlop | 1:19d183cf2689 | 271 | } |
kendunlop | 1:19d183cf2689 | 272 | //If spam mode is on, spam an incrementing CAN message |
kendunlop | 1:19d183cf2689 | 273 | if (spamon == 1) |
kendunlop | 1:19d183cf2689 | 274 | { |
kendunlop | 1:19d183cf2689 | 275 | spamcount ++; |
kendunlop | 1:19d183cf2689 | 276 | incrementer ++; |
kendunlop | 1:19d183cf2689 | 277 | std::stringstream sstream; |
kendunlop | 1:19d183cf2689 | 278 | sstream << std::hex << incrementer; |
kendunlop | 1:19d183cf2689 | 279 | string stringsofar = sstream.str(); |
kendunlop | 1:19d183cf2689 | 280 | int length = stringsofar.length(); |
kendunlop | 1:19d183cf2689 | 281 | for (int i = 0; i < (16-length); i++) |
kendunlop | 1:19d183cf2689 | 282 | stringsofar = "0" + stringsofar; |
kendunlop | 1:19d183cf2689 | 283 | messageOutText = "305 8 " + stringsofar; |
kendunlop | 1:19d183cf2689 | 284 | getCANfromstring(); |
kendunlop | 1:19d183cf2689 | 285 | CanBus.write(messageOut1); |
kendunlop | 1:19d183cf2689 | 286 | wait(0.01); |
kendunlop | 1:19d183cf2689 | 287 | } |
kendunlop | 1:19d183cf2689 | 288 | //Check for CAN messages coming in |
kendunlop | 1:19d183cf2689 | 289 | if (CanBus.read(messageIn)) |
kendunlop | 1:19d183cf2689 | 290 | { |
kendunlop | 1:19d183cf2689 | 291 | CANCount ++; |
kendunlop | 1:19d183cf2689 | 292 | printMessageIn(); |
kendunlop | 1:19d183cf2689 | 293 | } |
kendunlop | 1:19d183cf2689 | 294 | //Spam out the message as fast as possible |
kendunlop | 1:19d183cf2689 | 295 | //while (1) |
kendunlop | 1:19d183cf2689 | 296 | //{ |
kendunlop | 1:19d183cf2689 | 297 | // defineCANmessage(); |
kendunlop | 1:19d183cf2689 | 298 | // CanBus.write(messageOut1); |
kendunlop | 1:19d183cf2689 | 299 | //} |
kendunlop | 1:19d183cf2689 | 300 | } |
kendunlop | 1:19d183cf2689 | 301 | } |