Kenneth Dunlop / Mbed 2 deprecated Ken_CAN_test

Dependencies:   mbed

Revision:
2:11339018dda6
Parent:
1:19d183cf2689
Child:
3:79133dcea836
--- a/Ken_CAN_test.cpp	Tue Jun 14 10:57:11 2022 +0000
+++ b/Ken_CAN_test.cpp	Wed Jun 15 08:48:18 2022 +0000
@@ -7,14 +7,11 @@
 #include <stdio.h>
 #include <ctype.h> //For 'is digit' condition
 #include <sstream> //This is used to convert an integer to hex
-using namespace std;
-//#include <cmath>
-//#include <iostream> //May be needed to turn an integer into a hex byte
-using namespace std;
+#include <time.h> //To measure how long a process took? Includes clock_t and CLOCKS_PER_SEC
 
 RawSerial pc(USBTX, USBRX); // USB UART Terminal. Is needed for 'pc.printf' functions to work.
 CAN CanBus(p9, p10); //CANBus to use pins 9 and 10. This defines CanBus so other 'CanBus' lines will work.
-CAN CanBus2(p29, p30); //CANBus2 for recieving to use pins 29 and 30. This defines CanBus so other 'CanBus' lines will work.
+//CAN CanBus2(p29, p30); //CANBus2 for recieving to use pins 29 and 30. This defines CanBus so other 'CanBus' lines will work.
 CANMessage messageIn;
 
 //Define a CAN message to send
@@ -28,8 +25,10 @@
 int CANCount = 0;
 int spamon = 0;
 int spamcount = 0;
+int spamstarttime = 0;
+int spamendtime = 0;
 
-//A function to deal with each CAN message part (e.g.: 301, 8, 01, 02)
+//A function to deal with each CAN message part (e.g.: 301, 8, 01, 02...)
 void dealwithpart(void)
 {
     int hextotal = 0;
@@ -124,17 +123,6 @@
     dealwithpart();
     part = startofstring.substr(18,2);
     dealwithpart();
-    //pc.printf("First part is '%s'.\r\n", part1);
-    //pc.printf("Second part is '%s'.\r\n", part2);
-    //pc.printf("Third part is '%s'.\r\n", part3);
-    //pc.printf("Fourth part is '%s'.\r\n", part4);
-    //pc.printf("Fifth part is '%s'.\r\n", part5);
-    //pc.printf("Sixth part is '%s'.\r\n", part6);
-    //pc.printf("Seventh part is '%s'.\r\n", part7);
-    //pc.printf("Eighth part is '%s'.\r\n", part8);
-    //pc.printf("Ninth part is '%s'.\r\n", part9);
-    //pc.printf("Tenth part is '%s'.\r\n", part10);
-    //string mOTwithoutspaces = messageOutText.erase(remove(messageOutText.begin(), messageOutText.end(), " "), messageOutText.end());
 }
 
 void defineCANmessage(void)
@@ -198,7 +186,7 @@
                 if (c == '2')
                     {messageOutText = "302 8 02 02 02 02 02 02 02 02";}
                 if (c == '3')
-                    {messageOutText = "303 8 03 03 03 03 03 03 03 03";}
+                    {messageOutText = "333 8 00 00 00 00 00 00 00 33";}
                 if (c == '4')
                     {messageOutText = "304 8 04 04 04 04 04 04 04 04";}
                 if (c == '5')
@@ -252,13 +240,21 @@
                     pc.printf("Starting spam sequence...\r\n");
                     spamon = 1;
                     incrementer = 0;
-                    int spamcount = 0;
+                    spamcount = 0;
+                    int spamstarttime = clock();
                     }
                 //Pressing 'x' switches off spam mode
                 if (c == 'x' and spamon == 1)
                     {
                     spamon = 0;
-                    pc.printf("Ending spam mode.\r\n");
+                    pc.printf("Ending spam mode. Spammed %d times.\r\n", spamcount);
+                    int spamendtime = clock();
+                    int spamtime = 0;
+                    spamtime = spamendtime - spamstarttime;
+                    pc.printf("Clock right now is %d.\r\n", (spamtime));
+                    int spamspersecond = 0;
+                    spamspersecond = (spamcount / spamtime)/CLOCKS_PER_SEC;
+                    pc.printf("Spams per second are %d.\r\n", (spamspersecond));
                     }
                 if (messageOutText != "")
                     {
@@ -280,7 +276,7 @@
             int length = stringsofar.length();
             for (int i = 0; i < (16-length); i++)
             stringsofar = "0" + stringsofar;
-            messageOutText = "305 8 " + stringsofar;
+            messageOutText = "333 8 " + stringsofar;
             getCANfromstring();
             CanBus.write(messageOut1);
             wait(0.01);