Kenneth Dunlop / Mbed 2 deprecated Ken_CAN_test

Dependencies:   mbed

Ken_CAN_test.cpp

Committer:
kendunlop
Date:
2022-06-14
Revision:
1:19d183cf2689
Parent:
0:7a500ebaa7a6
Child:
2:11339018dda6

File content as of revision 1:19d183cf2689:

//This program was created by Ken 8th June 2022 to test the CAN output of a CAN Gateway.
#define kMajorVersion 0
#define kMinorVersion 5

#include "mbed.h" //Including mbed libraries. This should be all it needs to use the Mbed and CAN.
#include <string> //This is needed to make text strings work
#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;

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.
CANMessage messageIn;

//Define a CAN message to send
CANMessage messageOut1;

//Set messageOutText to a default message so it's defined.
string messageOutText = "3AF 8 01 AA BB CC DD EE FF 99";
string part = "";
int partincrement = 0;
int incrementer = 0;
int CANCount = 0;
int spamon = 0;
int spamcount = 0;

//A function to deal with each CAN message part (e.g.: 301, 8, 01, 02)
void dealwithpart(void)
{
    int hextotal = 0;
    partincrement = partincrement + 1;
    //pc.printf("Dealing with part %d. (%s)\r\n", partincrement, part);
    //int partincrementb = 0;
    int textlength = part.length();
    //int characterinc = 0;
    //pc.printf("That's %d characters long.\r\n", textlength);
    for (int i = 0; i < part.size(); i++)
    {
        //pc.printf("Examining character %d/%d.\r\n", (i+1), textlength);
        char individualcharacter = part.at(i);
        //pc.printf("That's '%c'.\r\n", individualcharacter);
        int numberized = 0;
        if(isdigit(individualcharacter))
            {
            //pc.printf("That character is a digit.\r\n");
            numberized = individualcharacter - '0';
            //pc.printf("Numberized that's '%d'.\r\n", numberized);
            }
        else
            {
            //pc.printf("That character is NOT a digit.\r\n");
            int asciivalue = toupper(individualcharacter);
            //pc.printf("Ascii value is %d.\r\n", asciivalue);
            numberized = asciivalue - 55;
            //pc.printf("Hex value is %d.\r\n", numberized);
            }
        //pc.printf("Eventual numberization is %d.\r\n", numberized);
        //pc.printf("Hex total is now %d.\r\n", hextotal);
        int powertoraise = part.size() - (i+1);
        //pc.printf("Must multiply by 16 to the power of %d.\r\n", powertoraise);
        int amounttoadd = numberized;
        //pc.printf("powertoraise is '%d'.\r\n", powertoraise);
        if (powertoraise == 1)
            {
            amounttoadd = numberized * 16;
            //pc.printf("Multiplying by 16.\r\n");
            //pc.printf("powertoraise is '%d'.\r\n", powertoraise);
            }
        if (powertoraise == 2)
            {
            amounttoadd = numberized * 256;
            //pc.printf("Multiplying by 256.\r\n");
            }
        //pc.printf("Amount to add is therefore %d.\r\n", amounttoadd);
        hextotal = hextotal + amounttoadd;
        //pc.printf("hextotal so far for this part is therefore %d.\r\n", hextotal);
    }
    //pc.printf("hextotal for whole part is therefore %d.\r\n", hextotal);
    //pc.printf("Need to convert that into true hex.\r\n");
    std::stringstream sstream;
    sstream << std::hex << hextotal;
    //pc.printf("StringSteam says '%s'.\r\n", sstream.str());
    if (partincrement == 1)
        {messageOut1.id = hextotal;}
    if (partincrement == 2)
        {messageOut1.len = hextotal;}
    if (partincrement >= 3)
        {messageOut1.data[partincrement-3] = hextotal;}
    //pc.printf("Part %d complete.\r\n", partincrement);
    //pc.printf("--------------------------------------\r\n");
}

//An attempted function to get a coherent CAN message from one, uninterrupted string
void getCANfromstring(void)
{
    //pc.printf("messageOutText is '%s'\r\n", 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.
    //pc.printf("String to parse is '%s'.\r\n", startofstring);
    partincrement = 0;
    part = startofstring.substr(0,3);
    dealwithpart();
    part = startofstring.substr(3,1);
    dealwithpart();
    part = startofstring.substr(4,2);
    dealwithpart();
    part = startofstring.substr(6,2);
    dealwithpart();
    part = startofstring.substr(8,2);
    dealwithpart();
    part = startofstring.substr(10,2);
    dealwithpart();
    part = startofstring.substr(12,2);
    dealwithpart();
    part = startofstring.substr(14,2);
    dealwithpart();
    part = startofstring.substr(16,2);
    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)
{
    //pc.printf("Defining CAN message 1.\r\n");
    //messageOut1.format = CANStandard;
    //messageOut1.id = 0x301;
    //messageOut1.len = 8;
    //messageOut1.data[0] = 0x06;
    //messageOut1.data[1] = 0x3f;
    //messageOut1.data[2] = 0xb2;
    //messageOut1.data[3] = 0x29;
    //messageOut1.data[4] = 0x19;
    //messageOut1.data[5] = 0x97;
    //messageOut1.data[6] = 0x67;
    //messageOut1.data[7] = 0x37;
}

void printMessageOut (void){
    //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.
    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]);
}

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]);
}

//The 'main' function will run as soon as the program starts.
int main()
{
    pc.baud(115200); // serial port at 115200
    CanBus.frequency(500 * 1000); // CAN bus at 500k
    CanBus.reset(); // clear any bus errors
    //NOTE: Print messages must be below this line to work.
    pc.printf("------------------------------------------\r\n");
    pc.printf("Welcome to Ken CAN test.\r\n");
    pc.printf("Setting CAN bus to 500k.\r\n");
    pc.printf("Setting serial port to 115200.\r\n");
    pc.printf("Using pins 9 and 10.\r\n");
    pc.printf("Version %d.%d\r\n",kMajorVersion,kMinorVersion);
    pc.printf("Build date %s %s\r\n",__DATE__,__TIME__);
    pc.printf("------------------------------------------\r\n");
    char c;
    
    //Check for button presses
    while (1)
    {
        if (pc.readable())
        {
            c = pc.getc();
            if ((c != NULL))
            {
                // When the a key is pressed, define a CAN message and send it.
                //pc.printf("A key was pressed! (%c)\r\n", c);
                //pc.printf("Will try to send CAN...\r\n");
                //defineCANmessage();
                messageOutText = "";
                if (c == '1')
                    {messageOutText = "301 8 01 01 01 01 01 01 01 01";}
                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";}
                if (c == '4')
                    {messageOutText = "304 8 04 04 04 04 04 04 04 04";}
                if (c == '5')
                    {messageOutText = "305 8 05 05 05 05 05 05 05 05";}
                if (c == '6')
                    {messageOutText = "306 8 06 06 06 06 06 06 06 06";}
                if (c == '7')
                    {messageOutText = "307 8 07 07 07 07 07 07 07 07";}
                if (c == '8')
                    {messageOutText = "308 8 08 08 08 08 08 08 08 08";}
                if (c == '9')
                    {messageOutText = "309 8 09 09 09 09 09 09 09 09";}
                if (c == 'f')
                    {messageOutText = "FFF 8 FF FF FF FF FF FF FF FF";}
                if (c == 'q')
                    {pc.printf("Changing CAN bus speed to 125.\r\n");
                    CanBus.frequency(125 * 1000);} // CAN bus at 125k
                if (c == 'w')
                    {pc.printf("Changing CAN bus speed to 250.\r\n");
                    CanBus.frequency(250 * 1000);} // CAN bus at 250k
                if (c == 'e')
                    {pc.printf("Changing CAN bus speed to 500.\r\n");
                    CanBus.frequency(500 * 1000);} // CAN bus at 500k
                if (c == 'r')
                    {pc.printf("Changing CAN bus speed to 1000.\r\n");
                    CanBus.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 == 'i')
                    {
                    incrementer = incrementer + 16;
                    std::stringstream sstream;
                    sstream << std::hex << incrementer;
                    string stringsofar = sstream.str();
                    //pc.printf("Incrementer is now %d.\r\n", incrementer);
                    //pc.printf("StringStream says '%s'.\r\n", stringsofar);
                    int length = stringsofar.length();
                    //pc.printf("Length is %d/16.\r\n", length);
                    for (int i = 0; i < (16-length); i++)
                        stringsofar = "0" + stringsofar;
                    //pc.printf("stringsofar says '%s'.\r\n", stringsofar);
                    messageOutText = "305 8 " + stringsofar;
                    //pc.printf("Will try to send '%s'.\r\n", messageOutText);
                    }
                messageOut1.format = CANStandard;
                if (c == 'z' and spamon == 0)
                    {
                    pc.printf("Starting spam sequence...\r\n");
                    spamon = 1;
                    incrementer = 0;
                    int spamcount = 0;
                    }
                //Pressing 'x' switches off spam mode
                if (c == 'x' and spamon == 1)
                    {
                    spamon = 0;
                    pc.printf("Ending spam mode.\r\n");
                    }
                if (messageOutText != "")
                    {
                    getCANfromstring();
                    CanBus.write(messageOut1);
                    if (spamon == 0)
                        {printMessageOut();}
                    }
            }
        }
        //If spam mode is on, spam an incrementing CAN message
        if (spamon == 1)
            {
            spamcount ++;
            incrementer ++;
            std::stringstream sstream;
            sstream << std::hex << incrementer;
            string stringsofar = sstream.str();
            int length = stringsofar.length();
            for (int i = 0; i < (16-length); i++)
            stringsofar = "0" + stringsofar;
            messageOutText = "305 8 " + stringsofar;
            getCANfromstring();
            CanBus.write(messageOut1);
            wait(0.01);
            }
        //Check for CAN messages coming in
            if (CanBus.read(messageIn))
            {
            CANCount ++;
            printMessageIn();
            }
        //Spam out the message as fast as possible
        //while (1)
        //{
        //  defineCANmessage();
        //  CanBus.write(messageOut1);
        //}
    }
}