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.
Revision 13:e3a4abc461f4, committed 2022-07-11
- Comitter:
- kendunlop
- Date:
- Mon Jul 11 16:28:07 2022 +0000
- Parent:
- 12:e950feba910d
- Commit message:
- Finished a nicer, more readable way of defining CAN expectations in the 'CANpairslist[x]' table. IDfrom and IDmessageto are strings that can be written to easily make a set of CAN expectations.
Changed in this revision
Ken_CAN_test.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/Ken_CAN_test.cpp Fri Jul 08 13:04:15 2022 +0000 +++ b/Ken_CAN_test.cpp Mon Jul 11 16:28:07 2022 +0000 @@ -67,7 +67,7 @@ int failsthistest = 0; int failsstreak = 0; int failsstreakstart = 0; -int reportfailsindividually = 0; +int reportfailsindividually = 1; int noreplyfailsthistest = 0; int noreplystreak = 0; int noreplystreakstartid = 0; @@ -169,6 +169,14 @@ } } +//Structure for CAN conversion pair, an ID and ID-plus-message, expressed in strings +struct CANpair { + string IDfrom; + string IDmessageto; + }; + +struct CANpair CANpairslist[32]; //This defines a list of CANpair string pairs, each with 'IDfrom' and 'IDmessageto'. + //Structure for CAN conversion table struct CANconversion { int IDfrom; @@ -180,412 +188,18 @@ struct CANconversion CANconversionlist[32]; //This defines a list of CANconversion structures, each with its own IDfrom, IDto, length, etc. (E.g.: 'CANconversionlist[0].IDfrom') int CANconversionlistheight = 32; -//CANconversion CANconversionlist[32] = {}; // Trying to make a list here that contains multiple CANconversion data types. - -//A function to handle key presses in tables mode -void tablesmodekeypress(void) +//A function to reset the CAN pairs list so it can be re-written into +void resetCANpairslist() { - //pc.printf("Key press detected in tables mode!\r\n"); - if (c >= 48 and c <= 57) + CANconversionlistheight = 0; + for (int i = 0; i < 32; i++) { - numberkeypressed = c - 48; - pc.printf("That's a number key press.('%d')\r\n", numberkeypressed); - if (numberkeypressed == 1) - { - CANconversionlist[0].IDfrom = 0x301; - CANconversionlist[0].IDto = 0x401; - CANconversionlist[0].length = 0x8; - CANconversionlist[0].Data[0] = 0x01; - CANconversionlist[0].Data[1] = 0x00; - CANconversionlist[0].Data[2] = 0x00; - CANconversionlist[0].Data[3] = 0x00; - CANconversionlist[0].Data[4] = 0x00; - CANconversionlist[0].Data[5] = 0x00; - CANconversionlist[0].Data[6] = 0x00; - CANconversionlist[0].Data[7] = 0x00; - - CANconversionlist[1].IDfrom = 0x302; - CANconversionlist[1].IDto = 0x402; - CANconversionlist[1].length = 0x8; - CANconversionlist[1].Data[0] = 0x04; - CANconversionlist[1].Data[1] = 0x05; - CANconversionlist[1].Data[2] = 0x06; - CANconversionlist[1].Data[3] = 0x07; - CANconversionlist[1].Data[4] = 0x00; - CANconversionlist[1].Data[5] = 0x00; - CANconversionlist[1].Data[6] = 0x00; - CANconversionlist[1].Data[7] = 0x00; - - CANconversionlist[2].IDfrom = 0x303; - CANconversionlist[2].IDto = 0x403; - CANconversionlist[2].length = 0x8; - CANconversionlist[2].Data[0] = 0x03; - CANconversionlist[2].Data[1] = 0x04; - CANconversionlist[2].Data[2] = 0x05; - CANconversionlist[2].Data[3] = 0x00; - CANconversionlist[2].Data[4] = 0x00; - CANconversionlist[2].Data[5] = 0x00; - CANconversionlist[2].Data[6] = 0x00; - CANconversionlist[2].Data[7] = 0x00; - - CANconversionlist[3].IDfrom = 0x304; - CANconversionlist[3].IDto = 0x404; - CANconversionlist[3].length = 0x8; - CANconversionlist[3].Data[0] = 0x04; - CANconversionlist[3].Data[1] = 0x05; - CANconversionlist[3].Data[2] = 0x06; - CANconversionlist[3].Data[3] = 0x07; - CANconversionlist[3].Data[4] = 0x00; - CANconversionlist[3].Data[5] = 0x00; - CANconversionlist[3].Data[6] = 0x00; - CANconversionlist[3].Data[7] = 0x00; - - CANconversionlist[4].IDfrom = 0x305; - CANconversionlist[4].IDto = 0x405; - CANconversionlist[4].length = 0x8; - CANconversionlist[4].Data[0] = 0x04; - CANconversionlist[4].Data[1] = 0x05; - CANconversionlist[4].Data[2] = 0x06; - CANconversionlist[4].Data[3] = 0x07; - CANconversionlist[4].Data[4] = 0x00; - CANconversionlist[4].Data[5] = 0x00; - CANconversionlist[4].Data[6] = 0x00; - CANconversionlist[4].Data[7] = 0x00; - - CANconversionlist[5].IDfrom = 0x306; - CANconversionlist[5].IDto = 0x406; - CANconversionlist[5].length = 0x8; - CANconversionlist[5].Data[0] = 0x02; - CANconversionlist[5].Data[1] = 0x03; - CANconversionlist[5].Data[2] = 0x00; - CANconversionlist[5].Data[3] = 0x00; - CANconversionlist[5].Data[4] = 0x00; - CANconversionlist[5].Data[5] = 0x00; - CANconversionlist[5].Data[6] = 0x00; - CANconversionlist[5].Data[7] = 0x00; - - CANconversionlist[6].IDfrom = 0x308; - CANconversionlist[6].IDto = 0x407; - CANconversionlist[6].length = 0x8; - CANconversionlist[6].Data[0] = 0x00; - CANconversionlist[6].Data[1] = 0x00; - CANconversionlist[6].Data[2] = 0x00; - CANconversionlist[6].Data[3] = 0x00; - CANconversionlist[6].Data[4] = 0x00; - CANconversionlist[6].Data[5] = 0x00; - CANconversionlist[6].Data[6] = 0x00; - CANconversionlist[6].Data[7] = 0x00; - - CANconversionlist[7].IDfrom = 0x309; - CANconversionlist[7].IDto = 0x408; - CANconversionlist[7].length = 0x8; - CANconversionlist[7].Data[0] = 0x06; - CANconversionlist[7].Data[1] = 0x07; - CANconversionlist[7].Data[2] = 0x08; - CANconversionlist[7].Data[3] = 0x00; - CANconversionlist[7].Data[4] = 0x00; - CANconversionlist[7].Data[5] = 0x00; - CANconversionlist[7].Data[6] = 0x00; - CANconversionlist[7].Data[7] = 0x00; - - CANconversionlistheight = 8; - } - if (numberkeypressed == 2) - { - CANconversionlist[0].IDfrom = 0x301; - CANconversionlist[0].IDto = 0x301; - CANconversionlist[0].length = 0x8; - CANconversionlist[0].Data[0] = 0x08; - CANconversionlist[0].Data[1] = 0x00; - CANconversionlist[0].Data[2] = 0x00; - CANconversionlist[0].Data[3] = 0x00; - CANconversionlist[0].Data[4] = 0x00; - CANconversionlist[0].Data[5] = 0x00; - CANconversionlist[0].Data[6] = 0x00; - CANconversionlist[0].Data[7] = 0x00; - - CANconversionlist[1].IDfrom = 0x302; - CANconversionlist[1].IDto = 0x302; - CANconversionlist[1].length = 0x8; - CANconversionlist[1].Data[0] = 0x06; - CANconversionlist[1].Data[1] = 0x07; - CANconversionlist[1].Data[2] = 0x00; - CANconversionlist[1].Data[3] = 0x00; - CANconversionlist[1].Data[4] = 0x00; - CANconversionlist[1].Data[5] = 0x00; - CANconversionlist[1].Data[6] = 0x00; - CANconversionlist[1].Data[7] = 0x00; - - CANconversionlist[2].IDfrom = 0x303; - CANconversionlist[2].IDto = 0x303; - CANconversionlist[2].length = 0x8; - CANconversionlist[2].Data[0] = 0x00; - CANconversionlist[2].Data[1] = 0x00; - CANconversionlist[2].Data[2] = 0x00; - CANconversionlist[2].Data[3] = 0x00; - CANconversionlist[2].Data[4] = 0x00; - CANconversionlist[2].Data[5] = 0x00; - CANconversionlist[2].Data[6] = 0x00; - CANconversionlist[2].Data[7] = 0x00; - - CANconversionlist[3].IDfrom = 0x304; - CANconversionlist[3].IDto = 0x304; - CANconversionlist[3].length = 0x8; - CANconversionlist[3].Data[0] = 0x08; - CANconversionlist[3].Data[1] = 0x00; - CANconversionlist[3].Data[2] = 0x00; - CANconversionlist[3].Data[3] = 0x00; - CANconversionlist[3].Data[4] = 0x00; - CANconversionlist[3].Data[5] = 0x00; - CANconversionlist[3].Data[6] = 0x00; - CANconversionlist[3].Data[7] = 0x00; - - CANconversionlist[4].IDfrom = 0x305; - CANconversionlist[4].IDto = 0x305; - CANconversionlist[4].length = 0x8; - CANconversionlist[4].Data[0] = 0x08; - CANconversionlist[4].Data[1] = 0x00; - CANconversionlist[4].Data[2] = 0x09; - CANconversionlist[4].Data[3] = 0x09; - CANconversionlist[4].Data[4] = 0x00; - CANconversionlist[4].Data[5] = 0x00; - CANconversionlist[4].Data[6] = 0x00; - CANconversionlist[4].Data[7] = 0x00; - - CANconversionlist[5].IDfrom = 0x306; - CANconversionlist[5].IDto = 0x306; - CANconversionlist[5].length = 0x8; - CANconversionlist[5].Data[0] = 0x02; - CANconversionlist[5].Data[1] = 0x03; - CANconversionlist[5].Data[2] = 0x00; - CANconversionlist[5].Data[3] = 0x00; - CANconversionlist[5].Data[4] = 0x00; - CANconversionlist[5].Data[5] = 0x00; - CANconversionlist[5].Data[6] = 0x00; - CANconversionlist[5].Data[7] = 0x00; - - CANconversionlist[6].IDfrom = 0x308; - CANconversionlist[6].IDto = 0x307; - CANconversionlist[6].length = 0x8; - CANconversionlist[6].Data[0] = 0x06; - CANconversionlist[6].Data[1] = 0x07; - CANconversionlist[6].Data[2] = 0x08; - CANconversionlist[6].Data[3] = 0x00; - CANconversionlist[6].Data[4] = 0x00; - CANconversionlist[6].Data[5] = 0x00; - CANconversionlist[6].Data[6] = 0x00; - CANconversionlist[6].Data[7] = 0x00; - - CANconversionlist[7].IDfrom = 0x309; - CANconversionlist[7].IDto = 0x308; - CANconversionlist[7].length = 0x8; - CANconversionlist[7].Data[0] = 0x06; - CANconversionlist[7].Data[1] = 0x07; - CANconversionlist[7].Data[2] = 0x08; - CANconversionlist[7].Data[3] = 0x00; - CANconversionlist[7].Data[4] = 0x00; - CANconversionlist[7].Data[5] = 0x00; - CANconversionlist[7].Data[6] = 0x00; - CANconversionlist[7].Data[7] = 0x00; - - CANconversionlistheight = 8; - } - pc.printf("CAN conversion list currently has %d rows:\r\n", CANconversionlistheight); - int i = 0; - while (i < CANconversionlistheight) - { - //pc.printf("Printing row %d: ", i+1); - pc.printf("ID \033[1;37m%03X\033[0m will convert to: \033[1;30;47m%03X %01X %02X %02X %02X %02X %02X %02X %02X %02X\033[0m.", CANconversionlist[i].IDfrom, CANconversionlist[i].IDto, CANconversionlist[i].length, CANconversionlist[i].Data[0], CANconversionlist[i].Data[1], CANconversionlist[i].Data[2], CANconversionlist[i].Data[3], CANconversionlist[i].Data[4], CANconversionlist[i].Data[5], CANconversionlist[i].Data[6], CANconversionlist[i].Data[7]); - pc.printf("\r\n"); - i++; - } - CANpassthrough = 0; - pc.printf("CANpassthrough is now set to %d.\r\n", CANpassthrough); - numberkeypressed = -1; - } - if (c == 27) - { - pc.printf("Esc key press found.\r\n"); - normalkeypresses = 1; - tablesmode = 0; + CANpairslist[i].IDfrom = ""; + CANpairslist[i].IDmessageto = ""; + //pc.printf("Emptying CANpairslist row %d.\r\n", i); } } -//A function to report if baud rates differ -void reportbaudrates(void) - { - if (mbedCANbusspeed != CANGatewayCANbusspeed) - { - pc.printf("Mbed baud rate (%d) is different from assumed CAN Gateway baud rate (%d), will expect no CAN.\r\n", (mbedCANbusspeed / 1000), (CANGatewayCANbusspeed / 1000)); - } - } - -//A function to handle when CAN-expectations are not fulfilled -void expectationwasnotfulfilled(void) - { - //pc.printf("Expectation was not fulfilled for that message!\r\n"); - } - -//A function to clear all test-related variables -void resetCANbuses()//A quick method to reset both CAN buses conveniently - { - CanBus.reset(); // Clear any bus errors. Will this stop the 'cached' CAN sticking around? - CanBus2.reset(); // Clear any bus errors. Will this stop the 'cached' CAN sticking around? - } - -void resettest(void) - { - //pc.printf("Resetting test.\r\n"); - listen = 1; - failsthistest = 0; - failsstreak = 0; - failsstreakstart = 0; - spamcount = 0; - noreplyfailsthistest = 0; - goodnoreplies = 0; - goodnoreplystreak = 0; - goodnoreplystreakstartid = 0; - noreplystreak = 0; - noreplystreakstartid = 0; - CANmatchstreak = 0; - CANmatchstreakstartID = 0; - mismatchfailsthistest = 0; - matchesthistest = 0; - idscheckedcount = 0; - unexpectedCANfails = 0; - unexpectedCANstreak = 0; - unexpectedCANstreakstartid = 0; - messagesincount = 0; - testwasaborted = 0; - } - -//A function to add a carriage return in the output if the system has been writing out the CAN messages it sends. -void addnewlineifneeded(void) -{ - if (addnewlinetonextmessage == 1) - { - pc.printf("\r\n"); - addnewlinetonextmessage = 0; - } -} - -void reportsentCAN() //A function to print out the CAN that was sent. Messages can only be positioned correctly once their results are known. - { - pc.printf("\033[1;33m\rSent out : %s \033[0m", messageOutTextWithSpaces); - } - - -//A function to end a test and report on findings. -void endtest() - { - listen = 0; - expectationresolved = 1; - if (testwasaborted == 1) - { - pc.printf("\033[1;35mTest was aborted, results invalid.\033[0m\r\n"); - pc.printf("\033[1;35m%d IDs sent out. %d failures found so far.\033[0m\r\n", spamcount, failsthistest); - if (CANpassthrough == 0) - { - pc.printf("\033[1;35m%d expected non-replies found so far. \033[0m", goodnoreplies); - } - pc.printf("\033[1;35m%d CAN matches found so far.\033[0m\r\n", matchesthistest); - } - if (failsthistest == 0 and testwasaborted == 0) - { - pc.printf("\033[1;32mTest passed.\033[0m\r\n"); - pc.printf("\033[1;32m%d IDs sent out. \033[0m", spamcount); - pc.printf("\033[1;32m%d failures found.\033[0m\r\n", failsthistest); - if (CANpassthrough == 0) - { - pc.printf("\033[1;32m%d expected non-replies found. \033[0m", goodnoreplies); - } - pc.printf("\033[1;32m%d CAN matches found.\033[0m\r\n", matchesthistest); - } - if (failsthistest > 0 and testwasaborted == 0) - { - pc.printf("\033[1;31mTest failed.\r\n%d failures found.\033[0m\r\n", failsthistest); - pc.printf("\033[1;31m%d mismatch failures, %d 'no reply' failures, %d unexpected CAN messages.\033[0m\r\n", mismatchfailsthistest, noreplyfailsthistest, unexpectedCANfails); - if (goodnoreplies > 0) - { - pc.printf("\033[1;31m%d expected non-replies occurred.\033[0m\r\n", goodnoreplies); - } - } - //addnewlineifneeded(); - pc.printf("-------------------------------------\r\n"); - failsthistest = 0; - normalkeypresses = 1; - } - -//'End ID spam test' function so you can end it by reaching 2048 IDs or pressing 'Esc' while it's running -void endidspamtest() - { - spamendtime = clock(); - getspamsecondstotal();//Process the spam information - idspamon = 0; - aspamisgoing = 0; - normalkeypresses = 0; - addnewlinetonextmessage = 1; - //addnewlineifneeded(); - //pc.printf("Ending ID spam sequence. %d IDs checked total.\r\n", idscheckedcount); - idlistincrementer = 0; - endtest(); - } - -void endspeedtest()//A different 'end' routine for speed tests. - { - //if (messagesincount == spamcount - 1)//If the messages in are only one less than messages out, adjust since it's probably just the test cancelling before getting that last message. - //{ - //pc.printf("messagesincount is only one less than spamcount, setting it.\r\n"); - //messagesincount = spamcount; - //} - listen = 0; - pc.printf("Ending CAN speed test.\r\n"); - pc.printf("%d messages sent. ", spamcount); - pc.printf("%d messages detected coming back in over %d seconds.\r\n", messagesincount, spamsecondstotal); - int speedtestpercentage = ((messagesincount * 100)/spamcount); - int speedtestpasslevel = 90; - if (speedtestpercentage >= speedtestpasslevel) - { - //pc.printf("\033[1;32mTest pass: %d%% of messages detected back.\033[0m\r\n\n", speedtestpercentage); - } - else - { - //pc.printf("\033[1;31mTest fail: %d%% of messages detected back.\033[0m\r\n\n", speedtestpercentage); - } - speedtestpasslevel = (CANGatewayCANbusspeed / 1000) * 8; //The HD2 can do 80 channels at 100Hz, so 8,000 messages per second are required at top baud rate (1000kbit/s) - if (CANGatewayCANbusspeed == mbedCANbusspeed) - { - if (messagesinpersecond >= speedtestpasslevel) - { - pc.printf("\033[1;32mTest pass: %d messages in per second. (%d required for pass at Gateway baud rate %d.)\033[0m\r\n", messagesinpersecond, speedtestpasslevel, CANGatewayCANbusspeed / 1000); - } - if (messagesinpersecond < speedtestpasslevel) - { - pc.printf("\033[1;31mTest fail: %d messages in per second. (%d required for pass at Gateway baud rate %d.)\033[0m\r\n", messagesinpersecond, speedtestpasslevel, CANGatewayCANbusspeed / 1000); - } - } - if (CANGatewayCANbusspeed != mbedCANbusspeed) - { - pc.printf("\033[1;35mTest invalid: Mbed CAN bus baud rate (%d) does not match presumed CAN Gateway baud rate (%d).\033[0m\r\n", mbedCANbusspeed / 1000, CANGatewayCANbusspeed / 1000); - } - pc.printf("\n");//Add an extra new line so the whole baud rate test is separate from the rest of the text. - } - -//A generic 'send CAN' function to send CAN with a standard message -void sendCAN(void) -{ - CanBus.write(messageOut1); - //addnewlineifneeded(); - if (aspamisgoing == 0) - { - //pc.printf("\r\n"); - } - expectationresolved = 0; - //pc.printf("Expectationresolved now is %d.", expectationresolved); - expectationtimer = 0; - //addnewlinetonextmessage = 1; -} - //A function to deal with each CAN message part (e.g.: 301, 8, 01, 02...) void dealwithpart(void) { @@ -762,6 +376,339 @@ //pc.printf("--------------------------------------\r\n"); } +//A function to go through each row in the string CAN pairs list and turn them into data for the CAN conversion list. +void gothroughCANpairslist(void) + { + for (int i = 0; i < 30; i++) + { + string tempstringA = CANpairslist[i].IDfrom; + string tempstringB = CANpairslist[i].IDmessageto; + //pc.printf("Strings found are '%s' and '%s'.\r\n", tempstringA, tempstringB); + //pc.printf("Will try to get an ID from out of '%s'.\r\n", tempstringA); + if (tempstringA != "" and tempstringB != "")//Only get a number from the part if tempstring A and B have something in them. + { + CANconversionlistheight = i+1; + partincrement = 0; + part = tempstringA; + dealwithpart(); + //pc.printf("Hextotal obtained is %d.\r\n", hextotal); + //pc.printf("partincrement is now %d.\r\n", partincrement); + CANconversionlist[i].IDfrom = hextotal; + partincrement = 0; + part = tempstringB.substr(0,3); + dealwithpart(); + //pc.printf("Hextotal obtained is %d.\r\n", hextotal); + CANconversionlist[i].IDto = hextotal; + part = tempstringB.substr(4,1); + dealwithpart(); + //pc.printf("Hextotal obtained is %d.\r\n", hextotal); + CANconversionlist[i].length = hextotal; + part = tempstringB.substr(6,2); + dealwithpart(); + //pc.printf("Hextotal obtained is %d.\r\n", hextotal); + CANconversionlist[i].Data [0] = hextotal; + part = tempstringB.substr(9,2); + dealwithpart(); + //pc.printf("Hextotal obtained is %d.\r\n", hextotal); + CANconversionlist[i].Data [1] = hextotal; + part = tempstringB.substr(12,2); + dealwithpart(); + //pc.printf("Hextotal obtained is %d.\r\n", hextotal); + CANconversionlist[i].Data [2] = hextotal; + part = tempstringB.substr(15,2); + dealwithpart(); + //pc.printf("Hextotal obtained is %d.\r\n", hextotal); + CANconversionlist[i].Data [3] = hextotal; + part = tempstringB.substr(18,2); + dealwithpart(); + //pc.printf("Hextotal obtained is %d.\r\n", hextotal); + CANconversionlist[i].Data [4] = hextotal; + part = tempstringB.substr(21,2); + dealwithpart(); + //pc.printf("Hextotal obtained is %d.\r\n", hextotal); + CANconversionlist[i].Data [5] = hextotal; + part = tempstringB.substr(24,2); + dealwithpart(); + //pc.printf("Hextotal obtained is %d.\r\n", hextotal); + CANconversionlist[i].Data [6] = hextotal; + part = tempstringB.substr(27,2); + dealwithpart(); + //pc.printf("Hextotal obtained is %d.\r\n", hextotal); + CANconversionlist[i].Data [7] = hextotal; + } + } + } + +//A function to handle key presses in tables mode +void tablesmodekeypress(void) + { + //pc.printf("Key press detected in tables mode!\r\n"); + if (c >= 48 and c <= 57) + { + numberkeypressed = c - 48; + pc.printf("That's a number key press.('%d')\r\n", numberkeypressed); + resetCANpairslist(); + if (numberkeypressed == 1) + { + CANpairslist[0].IDfrom = "301"; + CANpairslist[0].IDmessageto = "301 8 08 00 00 00 00 00 00 00"; + CANpairslist[1].IDfrom = "302"; + CANpairslist[1].IDmessageto = "302 8 06 07 00 00 00 00 00 00"; + CANpairslist[2].IDfrom = "303"; + CANpairslist[2].IDmessageto = "303 8 00 00 00 00 00 00 00 00"; + CANpairslist[3].IDfrom = "304"; + CANpairslist[3].IDmessageto = "304 8 08 00 00 00 00 00 00 00"; + CANpairslist[4].IDfrom = "305"; + CANpairslist[4].IDmessageto = "305 8 08 00 00 00 00 00 00 00"; + CANpairslist[5].IDfrom = "306"; + CANpairslist[5].IDmessageto = "306 8 02 03 00 00 00 00 00 00"; + CANpairslist[6].IDfrom = "307"; + CANpairslist[6].IDmessageto = "307 8 06 07 08 00 00 00 00 00"; + CANpairslist[7].IDfrom = "309"; + CANpairslist[7].IDmessageto = "308 8 06 07 08 00 00 00 00 00"; + } + if (numberkeypressed == 2) + { + CANpairslist[0].IDfrom = "301"; + CANpairslist[0].IDmessageto = "401 8 08 00 00 00 04 05 06 01"; + CANpairslist[1].IDfrom = "302"; + CANpairslist[1].IDmessageto = "402 8 04 05 06 07 06 07 08 00"; + CANpairslist[2].IDfrom = "303"; + CANpairslist[2].IDmessageto = "403 8 03 04 05 05 06 80 00 00"; + CANpairslist[3].IDfrom = "304"; + CANpairslist[3].IDmessageto = "404 8 04 05 06 07 06 07 08 00"; + CANpairslist[4].IDfrom = "305"; + CANpairslist[4].IDmessageto = "405 8 04 05 06 07 06 07 08 00"; + CANpairslist[5].IDfrom = "306"; + CANpairslist[5].IDmessageto = "406 8 02 03 00 00 00 00 00 00"; + CANpairslist[6].IDfrom = "308"; + CANpairslist[6].IDmessageto = "408 8 06 07 08 00 00 00 00 00"; + CANpairslist[7].IDfrom = "309"; + CANpairslist[7].IDmessageto = "409 8 06 07 08 00 00 00 00 00"; + } + if (numberkeypressed == 3) + { + CANpairslist[0].IDfrom = "317"; + CANpairslist[0].IDmessageto = "40A 8 08 00 00 00 04 05 06 07"; + CANpairslist[1].IDfrom = "318"; + CANpairslist[1].IDmessageto = "409 8 08 00 00 00 04 05 06 07"; + CANpairslist[2].IDfrom = "319"; + CANpairslist[2].IDmessageto = "408 8 06 07 02 03 04 05 00 00"; + CANpairslist[3].IDfrom = "31A"; + CANpairslist[3].IDmessageto = "407 8 08 00 00 00 04 05 02 03"; + CANpairslist[4].IDfrom = "31B"; + CANpairslist[4].IDmessageto = "40C 8 04 05 06 07 02 03 08 00"; + CANpairslist[5].IDfrom = "31C"; + CANpairslist[5].IDmessageto = "40B 8 06 07 04 05 02 03 00 00"; + CANpairslist[6].IDfrom = "31D"; + CANpairslist[6].IDmessageto = "406 8 08 00 00 00 04 05 06 07"; + CANpairslist[7].IDfrom = "31E"; + CANpairslist[7].IDmessageto = "405 8 04 05 06 07 00 00 00 00"; + CANpairslist[8].IDfrom = "324"; + CANpairslist[8].IDmessageto = "403 8 06 07 08 07 00 00 00 00"; + CANpairslist[9].IDfrom = "325"; + CANpairslist[9].IDmessageto = "404 8 04 05 06 07 00 00 00 00"; + CANpairslist[10].IDfrom = "329"; + CANpairslist[10].IDmessageto = "402 8 08 00 00 00 04 05 06 07"; + CANpairslist[11].IDfrom = "32A"; + CANpairslist[11].IDmessageto = "401 8 04 05 02 03 06 05 00 00"; + } + gothroughCANpairslist(); + pc.printf("CAN conversion list currently has %d rows:\r\n", CANconversionlistheight); + int i = 0; + while (i < CANconversionlistheight) + { + //pc.printf("Printing row %d: ", i+1); + pc.printf("ID \033[1;37m%03X\033[0m will convert to: \033[1;30;47m%03X %01X %02X %02X %02X %02X %02X %02X %02X %02X\033[0m", CANconversionlist[i].IDfrom, CANconversionlist[i].IDto, CANconversionlist[i].length, CANconversionlist[i].Data[0], CANconversionlist[i].Data[1], CANconversionlist[i].Data[2], CANconversionlist[i].Data[3], CANconversionlist[i].Data[4], CANconversionlist[i].Data[5], CANconversionlist[i].Data[6], CANconversionlist[i].Data[7]); + pc.printf("\r\n"); + i++; + } + CANpassthrough = 0; + pc.printf("CANpassthrough is now set to %d.\r\n", CANpassthrough); + numberkeypressed = -1; + } + if (c == 27) + { + pc.printf("Leaving Expectation Table mode.\r\n"); + normalkeypresses = 1; + tablesmode = 0; + } + } + +//A function to report if baud rates differ +void reportbaudrates(void) + { + if (mbedCANbusspeed != CANGatewayCANbusspeed) + { + pc.printf("Mbed baud rate (%d) is different from assumed CAN Gateway baud rate (%d), will expect no CAN.\r\n", (mbedCANbusspeed / 1000), (CANGatewayCANbusspeed / 1000)); + } + } + +//A function to handle when CAN-expectations are not fulfilled +void expectationwasnotfulfilled(void) + { + //pc.printf("Expectation was not fulfilled for that message!\r\n"); + } + +//A function to clear all test-related variables +void resetCANbuses()//A quick method to reset both CAN buses conveniently + { + CanBus.reset(); // Clear any bus errors. Will this stop the 'cached' CAN sticking around? + CanBus2.reset(); // Clear any bus errors. Will this stop the 'cached' CAN sticking around? + } + +void resettest(void) + { + //pc.printf("Resetting test.\r\n"); + listen = 1; + failsthistest = 0; + failsstreak = 0; + failsstreakstart = 0; + spamcount = 0; + noreplyfailsthistest = 0; + goodnoreplies = 0; + goodnoreplystreak = 0; + goodnoreplystreakstartid = 0; + noreplystreak = 0; + noreplystreakstartid = 0; + CANmatchstreak = 0; + CANmatchstreakstartID = 0; + mismatchfailsthistest = 0; + matchesthistest = 0; + idscheckedcount = 0; + unexpectedCANfails = 0; + unexpectedCANstreak = 0; + unexpectedCANstreakstartid = 0; + messagesincount = 0; + testwasaborted = 0; + } + +//A function to add a carriage return in the output if the system has been writing out the CAN messages it sends. +void addnewlineifneeded(void) +{ + if (addnewlinetonextmessage == 1) + { + pc.printf("\r\n"); + addnewlinetonextmessage = 0; + } +} + +void reportsentCAN() //A function to print out the CAN that was sent. Messages can only be positioned correctly once their results are known. + { + pc.printf("\033[1;33m\rSent out : %s \033[0m", messageOutTextWithSpaces); + } + + +//A function to end a test and report on findings. +void endtest() + { + listen = 0; + expectationresolved = 1; + if (testwasaborted == 1) + { + pc.printf("\033[1;35mTest was aborted, results invalid.\033[0m\r\n"); + pc.printf("\033[1;35m%d IDs sent out. %d failures found so far.\033[0m\r\n", spamcount, failsthistest); + if (CANpassthrough == 0) + { + pc.printf("\033[1;35m%d expected non-replies found so far. \033[0m", goodnoreplies); + } + pc.printf("\033[1;35m%d CAN matches found so far.\033[0m\r\n", matchesthistest); + } + if (failsthistest == 0 and testwasaborted == 0) + { + pc.printf("\033[1;32mTest passed.\033[0m\r\n"); + pc.printf("\033[1;32m%d IDs sent out. \033[0m", spamcount); + pc.printf("\033[1;32m%d failures found.\033[0m\r\n", failsthistest); + if (CANpassthrough == 0) + { + pc.printf("\033[1;32m%d expected non-replies found. \033[0m", goodnoreplies); + } + pc.printf("\033[1;32m%d CAN matches found.\033[0m\r\n", matchesthistest); + } + if (failsthistest > 0 and testwasaborted == 0) + { + pc.printf("\033[1;31mTest failed.\r\n%d failures found.\033[0m\r\n", failsthistest); + pc.printf("\033[1;31m%d mismatch failures, %d 'no reply' failures, %d unexpected CAN messages.\033[0m\r\n", mismatchfailsthistest, noreplyfailsthistest, unexpectedCANfails); + if (goodnoreplies > 0) + { + pc.printf("\033[1;31m%d expected non-replies occurred.\033[0m\r\n", goodnoreplies); + } + } + pc.printf("-------------------------------------\r\n"); + failsthistest = 0; + normalkeypresses = 1; + } + +//'End ID spam test' function so you can end it by reaching 2048 IDs or pressing 'Esc' while it's running +void endidspamtest() + { + spamendtime = clock(); + getspamsecondstotal();//Process the spam information + idspamon = 0; + aspamisgoing = 0; + normalkeypresses = 0; + addnewlinetonextmessage = 1; + //addnewlineifneeded(); + //pc.printf("Ending ID spam sequence. %d IDs checked total.\r\n", idscheckedcount); + idlistincrementer = 0; + endtest(); + } + +void endspeedtest()//A different 'end' routine for speed tests. + { + //if (messagesincount == spamcount - 1)//If the messages in are only one less than messages out, adjust since it's probably just the test cancelling before getting that last message. + //{ + //pc.printf("messagesincount is only one less than spamcount, setting it.\r\n"); + //messagesincount = spamcount; + //} + listen = 0; + pc.printf("Ending CAN speed test.\r\n"); + pc.printf("%d messages sent. ", spamcount); + pc.printf("%d messages detected coming back in over %d seconds.\r\n", messagesincount, spamsecondstotal); + int speedtestpercentage = ((messagesincount * 100)/spamcount); + int speedtestpasslevel = 90; + if (speedtestpercentage >= speedtestpasslevel) + { + //pc.printf("\033[1;32mTest pass: %d%% of messages detected back.\033[0m\r\n\n", speedtestpercentage); + } + else + { + //pc.printf("\033[1;31mTest fail: %d%% of messages detected back.\033[0m\r\n\n", speedtestpercentage); + } + speedtestpasslevel = (CANGatewayCANbusspeed / 1000) * 8; //The HD2 can do 80 channels at 100Hz, so 8,000 messages per second are required at top baud rate (1000kbit/s) + if (CANGatewayCANbusspeed == mbedCANbusspeed) + { + if (messagesinpersecond >= speedtestpasslevel) + { + pc.printf("\033[1;32mTest pass: %d messages in per second. (%d required for pass at Gateway baud rate %d.)\033[0m\r\n", messagesinpersecond, speedtestpasslevel, CANGatewayCANbusspeed / 1000); + } + if (messagesinpersecond < speedtestpasslevel) + { + pc.printf("\033[1;31mTest fail: %d messages in per second. (%d required for pass at Gateway baud rate %d.)\033[0m\r\n", messagesinpersecond, speedtestpasslevel, CANGatewayCANbusspeed / 1000); + } + } + if (CANGatewayCANbusspeed != mbedCANbusspeed) + { + pc.printf("\033[1;35mTest invalid: Mbed CAN bus baud rate (%d) does not match presumed CAN Gateway baud rate (%d).\033[0m\r\n", mbedCANbusspeed / 1000, CANGatewayCANbusspeed / 1000); + } + pc.printf("\n");//Add an extra new line so the whole baud rate test is separate from the rest of the text. + } + +//A generic 'send CAN' function to send CAN with a standard message +void sendCAN(void) +{ + CanBus.write(messageOut1); + //addnewlineifneeded(); + if (aspamisgoing == 0) + { + //pc.printf("\r\n"); + } + expectationresolved = 0; + //pc.printf("Expectationresolved now is %d.", expectationresolved); + expectationtimer = 0; + //addnewlinetonextmessage = 1; +} + //A function to get a coherent CAN message from one, uninterrupted string void getCANfrommessageOutText(void) { @@ -1058,7 +1005,7 @@ if (c == '5' and normalkeypresses == 1) { buttonPressMessageOutText = "305 8 01 02 03 04 05 06 07 08"; - idlistincrementer = 0x15B; + idlistincrementer = 0x305; } if (c == '6' and normalkeypresses == 1) { @@ -1512,15 +1459,6 @@ pc.printf("\r\n\n"); } } - if (failsstreak > 0) - { - failsstreak = 0; - failsstreakstart = 0; - if (aspamisgoing == 1) - { - pc.printf("\r\n\n"); - } - } if (CANmatchstreak > 0) { CANmatchstreak = 0; @@ -1553,9 +1491,7 @@ messageinisneeded = 0; if (aspamisgoing == 0) {messageinisneeded = 1;} - //if (itsamatch == 0 and shouldntcomein == 0) - // {messageinisneeded = 1;} - if (reportfailsindividually == 1) + if (itsamatch == 0 and shouldntcomein == 0 and reportfailsindividually == 1) {messageinisneeded = 1;} // If a message in is destined to be printed, reset the failsstreak if needed if (messageinisneeded == 1 and failsstreak > 0)