Kenneth Dunlop / Mbed 2 deprecated Ken_CAN_test

Dependencies:   mbed

Revision:
7:a9150dc1e481
Parent:
6:2882710e4f1e
Child:
8:6f096b45ca15
diff -r 2882710e4f1e -r a9150dc1e481 Ken_CAN_test.cpp
--- a/Ken_CAN_test.cpp	Fri Jun 17 10:13:51 2022 +0000
+++ b/Ken_CAN_test.cpp	Tue Jun 21 08:44:17 2022 +0000
@@ -8,13 +8,13 @@
 #include <ctype.h> //For 'is digit' condition
 #include <sstream> //This is used to convert an integer to hex
 #include <time.h> //To measure how long a process took? Includes clock_t and CLOCKS_PER_SEC
-#include <list> //To let me make lists of CAN IDs
+//#include <list> //To let me make lists of CAN IDs
 
 RawSerial pc(USBTX, USBRX); // USB UART Terminal, tx, rx. Is needed for 'pc.printf' functions to work.
 
 // NOTE: Original pins for CAN Bus 1 are 9 and 10.
-CAN CanBus(p9, p10); //CANBus to use pins 29 and 30. This defines CanBus so other 'CanBus' lines will work.
-CAN CanBus2(p30, p29); //CANBus2 for recieving to use pins 29 and 30. This defines CanBus so other 'CanBus' lines will work.
+CAN CanBus(p9, p10); //CANBus to use pins 9 and 10. This defines CanBus so other 'CanBus' lines will work.
+CAN CanBus2(p30, p29); //CANBus2 for recieving to use pins 30 and 29 (in that order!).
 CANMessage messageIn;
 
 //Define a CAN message to send
@@ -22,6 +22,7 @@
 
 //Set messageOutText to a default message so it's defined.
 string messageOutText = "";
+string messageOutTextWithSpaces = "";
 string part = "";
 int partincrement = 0;
 __int64 incrementer = 0;
@@ -35,6 +36,61 @@
 int vlistincrementer = 0;
 int idlistincrementer = 0;
 string spampreamble = "333";
+int expected1 = 0;
+int expected2 = 0;
+unsigned char expected3 = 0;
+unsigned char expected4 = 0;
+unsigned char expected5 = 0;
+unsigned char expected6 = 0;
+unsigned char expected7 = 0;
+unsigned char expected8 = 0;
+unsigned char expected9 = 0;
+unsigned char expected10 = 0;
+int failsthistest = 0;
+int addnewlinetonextmessage = 0;
+
+//A function to clear all test-related variables
+void resettest(void)
+    {
+    pc.printf("Resetting test.\r\n");
+    failsthistest = 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;
+        }
+}
+
+    
+//A function to end a test and report on findings.
+void endtest()
+    {
+    if (failsthistest == 0)
+        {
+        pc.printf("\033[1;32mTest complete. %d failures found.\033[0m\r\n", failsthistest);
+        }
+    if (failsthistest > 0)
+        {
+        pc.printf("\033[1;31mTest complete. %d failures found.\033[0m\r\n", failsthistest);
+        }
+    addnewlineifneeded();
+    pc.printf("-------------------------------------\r\n");
+    failsthistest = 0;
+    }
+
+//A generic 'send CAN' function to send CAN with a standard message
+void sendCAN(void)
+{
+    CanBus.write(messageOut1);
+    //addnewlineifneeded();
+    pc.printf("\033[1;33m%s\033[0m\r", messageOutTextWithSpaces);
+    //addnewlinetonextmessage = 1;
+}
 
 //A function to deal with each CAN message part (e.g.: 301, 8, 01, 02...)
 void dealwithpart(void)
@@ -93,11 +149,51 @@
     sstream << std::hex << hextotal;
     //pc.printf("StringSteam says '%s'.\r\n", sstream.str());
     if (partincrement == 1)
-        {messageOut1.id = hextotal;}
+        {
+        messageOut1.id = hextotal;
+        expected1 = hextotal;
+        }
     if (partincrement == 2)
-        {messageOut1.len = hextotal;}
+        {
+        messageOut1.len = hextotal;
+        expected2 = hextotal;
+        }
     if (partincrement >= 3)
-        {messageOut1.data[partincrement-3] = hextotal;}
+        {
+        messageOut1.data[partincrement-3] = hextotal;
+        }
+    if (partincrement == 3)
+        {
+        expected3 = hextotal;
+        }
+    if (partincrement == 4)
+        {
+        expected4 = hextotal;
+        }
+    if (partincrement == 5)
+        {
+        expected5 = hextotal;
+        }
+    if (partincrement == 6)
+        {
+        expected6 = hextotal;
+        }
+    if (partincrement == 7)
+        {
+        expected7 = hextotal;
+        }    
+    if (partincrement == 8)
+        {
+        expected8 = hextotal;
+        }
+    if (partincrement == 9)
+        {
+        expected9 = hextotal;
+        }
+    if (partincrement == 10)
+        {
+        expected10 = hextotal;
+        }
     //pc.printf("Part %d complete.\r\n", partincrement);
     //pc.printf("--------------------------------------\r\n");
 }
@@ -106,6 +202,7 @@
 void getCANfrommessageOutText(void)
 {
     //pc.printf("messageOutText is '%s'\r\n", messageOutText);
+    messageOutTextWithSpaces = messageOutText;
     remove(messageOutText.begin(), messageOutText.end(), ' '); //Remove the spaces from the text to send out so it can be parsed.
     //pc.printf("After removing spaces, messageOutText is '%s'\r\n", messageOutText);
     string startofstring = messageOutText.substr(0,20); //Take the first 20 characters of the newly-formed string to get a spaceless CAN message.
@@ -157,8 +254,9 @@
 
 void printMessageIn (void)
 {
-    //This function will print out whatever the CAN bus is receiving.
-    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]);
+    //This function will print out whatever the CAN bus is receiving and keep it in messagetocheck.
+    //messagetocheck = ("%03X %01X %02X %02X %02X %02X %02X %02X %02X %02X", messageIn.id,messageIn.len,messageIn.data[0],messageIn.data[1],messageIn.data[2],messageIn.data[3],messageIn.data[4],messageIn.data[5],messageIn.data[6],messageIn.data[7]);
+    //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]);
 }
 
 //The 'main' function will run as soon as the program starts.
@@ -180,6 +278,12 @@
     pc.printf("------------------------------------------\r\n");
     char c;
     
+    //Define the list of expected hex values for use later
+    //expectedlist[0] = 5;
+    //for (int item : expectedlist)
+    //  pc.printf("%d\r\n", item);
+    //int integer = expectedlist[1];
+    
     //Check for button presses
     while (1)
     {
@@ -192,25 +296,65 @@
                 //pc.printf("A key was pressed! (%c)\r\n", c);
                 messageOutText = "";
                 if (c == '1')
-                    {messageOutText = "301 8 01 01 01 01 01 01 01 01";}
+                    {
+                    addnewlineifneeded();
+                    messageOutText = "301 8 01 01 01 01 01 01 01 01";
+                    addnewlinetonextmessage = 1;
+                    }
                 if (c == '2')
-                    {messageOutText = "302 8 02 02 02 02 02 02 02 02";}
+                    {
+                    addnewlineifneeded();
+                    messageOutText = "302 8 02 02 02 02 02 02 02 02";
+                    addnewlinetonextmessage = 1;
+                    }
                 if (c == '3')
-                    {messageOutText = "303 8 00 00 00 00 00 00 00 33";}
+                    {
+                    addnewlineifneeded();
+                    messageOutText = "303 8 00 00 00 00 00 00 00 33";
+                    addnewlinetonextmessage = 1;
+                    }
                 if (c == '4')
-                    {messageOutText = "304 8 04 04 04 04 04 04 04 04";}
+                    {
+                    addnewlineifneeded();
+                    messageOutText = "304 8 04 04 04 04 04 04 04 04";
+                    addnewlinetonextmessage = 1;
+                    }
                 if (c == '5')
-                    {messageOutText = "305 8 05 05 05 05 05 05 05 05";}
+                    {
+                    addnewlineifneeded();
+                    messageOutText = "305 8 05 05 05 05 05 05 05 05";
+                    addnewlinetonextmessage = 1;
+                    }
                 if (c == '6')
-                    {messageOutText = "306 8 06 06 06 06 06 06 06 06";}
+                    {
+                    addnewlineifneeded();
+                    messageOutText = "306 8 06 06 06 06 06 06 06 06";
+                    addnewlinetonextmessage = 1;
+                    }
                 if (c == '7')
-                    {messageOutText = "307 8 07 07 07 07 07 07 07 07";}
+                    {
+                    addnewlineifneeded();
+                    messageOutText = "307 8 07 07 07 07 07 07 07 07";
+                    addnewlinetonextmessage = 1;
+                    }
                 if (c == '8')
-                    {messageOutText = "308 8 08 08 08 08 08 08 08 08";}
+                    {
+                    addnewlineifneeded();
+                    messageOutText = "308 8 08 08 08 08 08 08 08 08";
+                    addnewlinetonextmessage = 1;
+                    }
                 if (c == '9')
-                    {messageOutText = "309 8 09 09 09 09 09 09 09 09";}
+                    {
+                    addnewlineifneeded();
+                    messageOutText = "309 8 09 09 09 09 09 09 09 09";
+                    addnewlinetonextmessage = 1;
+                    }
                 if (c == 'f')
-                    {messageOutText = "FFF 8 FF FF FF FF FF FF FF FF";}
+                    {
+                    addnewlineifneeded();
+                    messageOutText = "7FF 8 FF FF FF FF FF FF FF FF";//NOTE: CAN IDs only go p to 7FF, not FFF.
+                    addnewlinetonextmessage = 1;
+                    }
                 if (c == 'q')
                     {
                     pc.printf("Changing CAN bus speed to 125.\r\n");
@@ -235,11 +379,11 @@
                     CanBus.frequency(1000 * 1000); // CAN bus at 1000k
                     CanBus2.frequency(1000 * 1000); // CAN bus at 1000k
                     }
-                if (c == 's')
-                    {
-                    pc.printf("Sending authentic sats message.\r\n");
-                    messageOutText = "301 8 06 3F B2 29 12 97 67 37";
-                    }
+                //if (c == 's')
+                //  {
+                //  pc.printf("Sending authentic sats message.\r\n");
+                //  messageOutText = "301 8 06 3F B2 29 12 97 67 37";
+                //  }
                 if (c == 'o')
                     {
                     incrementer = (incrementer * 16);
@@ -264,6 +408,7 @@
                 messageOut1.format = CANStandard;
                 if (c == 'z' and spamon == 0)
                     {
+                    resettest();
                     pc.printf("Starting spam sequence for ID %s. Press 'x' to end. \r\n", spampreamble);
                     spamon = 1;
                     incrementer = 0;
@@ -281,6 +426,7 @@
                     spamtime = spamendtime - spamstarttime;
                     int spamtimeseconds = 0;
                     spamtimeseconds = (spamtime / CLOCKS_PER_SEC);
+                    addnewlineifneeded();
                     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);
                     //pc.printf("Clock right now is %d.\r\n", (spamtime));
                     //pc.printf("Ticks per second are %d.\r\n", CLOCKS_PER_SEC);
@@ -294,30 +440,33 @@
                     spamspersecond = ((spamspersecond * CLOCKS_PER_SEC))/10000;
                     pc.printf("Spams per second are %d.\r\n", spamspersecond);
                     pc.printf("Total spams ever are %d.\r\n", totalspamsever);
-                    pc.printf("-------------------------\r\n");
+                    endtest();
+                    //pc.printf("-------------------------\r\n");
                     }
                 if (c == 'v' and idspamon == 0)
                     {
+                    resettest();
                     idspamon = 1; //Set the 'idspamon' integer to 1 so ID spam happens.
                     idlistincrementer = 0;
-                    pc.printf("Beginning spam of all possible CAN IDs (000 - FFF).\r\n");
+                    pc.printf("Beginning spam of all possible CAN IDs (000 - 7FF).\r\n");
                     }
                 if (messageOutText != "")
                     {
                     getCANfrommessageOutText();
-                    CanBus.write(messageOut1);
-                    CanBus2.write(messageOut1);
+                    sendCAN();
+                    //CanBus.write(messageOut1);
+                    //CanBus2.write(messageOut1);
                     if (spamon == 0 and idspamon == 0)
                         {
-                        printMessageOut();
+                        //printMessageOut();
                         }
                     messageOutText = "";
                     }
-                //If spam mode is on, spam an incrementing CAN message
                 }
         }
 
         
+                //If spam mode is on, spam an incrementing CAN message
                 if (spamon == 1)
                     {
                     spamcount ++;
@@ -331,8 +480,9 @@
                     stringsofar = "0" + stringsofar;
                     messageOutText = spampreamble + " 8 " + stringsofar;
                     getCANfrommessageOutText();
-                    CanBus.write(messageOut1);
-                    CanBus2.write(messageOut1);
+                    sendCAN();
+                    //CanBus.write(messageOut1);
+                    //CanBus2.write(messageOut1);
                     wait(0.01);
                     }
                 if (idspamon == 1)
@@ -351,27 +501,100 @@
                         }
                     //pc.printf("idstring now says '%s'\r\n", idstring);
                     //pc.printf("%s\r\n", idstring);
-                    idstring = idstring + " 8 00 00 00 00 00 00 00 01";
+                    idstring = idstring + " 8 01 02 03 04 05 06 07 08";
                     //pc.printf("...and now idstring now says '%s'\r\n", idstring);
                     messageOutText = idstring;
                     getCANfrommessageOutText();
-                    CanBus.write(messageOut1);
-                    CanBus2.write(messageOut1);
+                    sendCAN();
+                    //CanBus.write(messageOut1);
+                    //CanBus2.write(messageOut1);
                     idlistincrementer++; //Only add 1 to the idlist incrementer at the end so the first ID it sends is '000'.
                     //pc.printf("ID list incrementer is now %d\r\n", idlistincrementer);
-                    if (idlistincrementer >= 4096) //If the spam value gets to 4096 (FFF), end the spam sequence.
+                    if (idlistincrementer >= 2048) //If the spam value gets to 2048 (7FF), end the spam sequence. RL only go up to 7FF.
                         {
                            idspamon = 0;
+                           addnewlineifneeded();
                            pc.printf("Ending ID spam sequence.\r\n");
                            idlistincrementer = 0;
+                           endtest();
                         }
                     wait(0.01);//ID spam at 100Hz
                     }
         //Check for CAN messages coming in
         if (CanBus.read(messageIn))
             {
+            //CANCount ++;
+            //printMessageIn();
+            }
+        if (CanBus2.read(messageIn))
+            {
             CANCount ++;
             printMessageIn();
+            if (1 == 1)
+                {
+                //string messagetocheck = "";
+                //string messageintext = ("%03X %01X %d %d %d %d %d %d %d %d", messageIn.id, messageIn.len, messageIn.data[0], messageIn.data[1], messageIn.data[2], messageIn.data[3],messageIn.data[4],messageIn.data[5],messageIn.data[6],messageIn.data[7]);
+                //messagetocheck = ("%03X %01X %02X %02X %02X %02X %02X %02X %02X %02X", messageIn.id, messageIn.len, messageIn.data[0], messageIn.data[1], messageIn.data[2], messageIn.data[3],messageIn.data[4],messageIn.data[5],messageIn.data[6],messageIn.data[7]);
+                //pc.printf("Expectation is %d %d %d %d %d %d %d %d %d %d\r\n", expected1, expected2, expected3, expected4, expected5, expected6, expected7, expected8, expected9, expected10);
+                int itsamatch = 1;
+                addnewlineifneeded();
+                if (expected1 != messageIn.id)
+                    {
+                    pc.printf("ID of %d does NOT match %d.\r\n", messageIn.id, expected1);
+                    itsamatch = 0;
+                    }
+                if (expected2 != messageIn.len)
+                    {
+                    pc.printf("Length of %d does NOT match %d.\r\n", messageIn.len, expected2);
+                    itsamatch = 0;
+                    }
+                if (expected3 != messageIn.data[0])
+                    {
+                    pc.printf("Data 0 of %d does NOT match %d.\r\n", messageIn.data[0], expected3);
+                    itsamatch = 0;
+                    }
+                if (expected4 != messageIn.data[1])
+                    {
+                    pc.printf("Data 1 of %d does NOT match %d.\r\n", messageIn.data[1], expected4);
+                    itsamatch = 0;
+                    }
+                if (expected5 != messageIn.data[2])
+                    {
+                    pc.printf("Data 2 of %d does NOT match %d.\r\n", messageIn.data[2], expected5);
+                    itsamatch = 0;
+                    }                
+                if (expected6 != messageIn.data[3])
+                    {
+                    pc.printf("Data 3 of %d does NOT match %d.\r\n", messageIn.data[3], expected6);
+                    itsamatch = 0;
+                    }
+                if (expected7 != messageIn.data[4])
+                    {
+                    pc.printf("Data 4 of %d does NOT match %d.\r\n", messageIn.data[4], expected7);
+                    itsamatch = 0;
+                    }
+                if (expected8 != messageIn.data[5])
+                    {
+                    pc.printf("Data 5 of %d does NOT match %d.\r\n", messageIn.data[5], expected8);
+                    itsamatch = 0;
+                    }
+                if (expected9 != messageIn.data[6])
+                    {
+                    pc.printf("Data 6 of %d does NOT match %d.\r\n", messageIn.data[6], expected9);
+                    itsamatch = 0;
+                    }
+                if (expected10 != messageIn.data[7])
+                    {
+                    pc.printf("Data 7 of %d does NOT match %d.\r\n", messageIn.data[7], expected10);
+                    itsamatch = 0;
+                    }
+                if (itsamatch == 0)
+                    {
+                    pc.printf("Message received was: %03X %01X %02X %02X %02X %02X %02X %02X %02X %02X\r\n",messageIn.id,messageIn.len,messageIn.data[0],messageIn.data[1],messageIn.data[2],messageIn.data[3],messageIn.data[4],messageIn.data[5],messageIn.data[6],messageIn.data[7]);
+                    pc.printf("That does NOT match expectation! (%03X %01X %02X %02X %02X %02X %02X %02X %02X %02X)\r\n", expected1, expected2, expected3, expected4, expected5, expected6, expected7, expected8, expected9, expected10);
+                    failsthistest++; //Add 1 to failsthistest to mark the fail
+                    }
+                }
             }
        }
 }
\ No newline at end of file