Working with Char arrays

I'm working with a CAN to serial module that I connected to my Arduino and I have a simple sketch that displays the CAN information on the Serial Monitor which looks like this:

What would I need to to if I wanted to do "something" whenever I receive the message:
0x0 0x2 0xD0 0xC4 0x0 0x0 0x0 0x5
From ID: 91

I've never done anything with character arrays before and I really don't know where to start.

Here is the sketch I'm working on:

#include <Serial_CAN_Module.h>
#include <SoftwareSerial.h>

Serial_CAN can;
#define CAN_RATE_125    13
#define can_tx          2           // tx of serial can module connect to D3
#define can_rx          3           // rx of serial can module connect to D2

int Message_Count = 1;
unsigned long id =  0;
unsigned char  dta[8];

void setup()
{
    Serial.begin(9600);
    while(!Serial);
    can.begin(can_tx, can_rx, 9600);      // tx, rx
    if(can.canRate(CAN_RATE_125))
    {Serial.println("ArduiCAN started!"); }
    else
    {Serial.println("Set can rate fail"); }
}

void loop()
{
    if(can.recv(&id, dta))
    {
        Serial.print("[");
        Serial.print(Message_Count);
        Serial.print("]");
        Serial.print(" from: ");
        Serial.print(id, HEX);
        Serial.print("(ID)");        
        Serial.print('\t');
        for(int i=0; i<8; i++)
        {
            Serial.print("0x");
            Serial.print(dta[i], HEX);
            Serial.print("  ");
        }
        Serial.println();
        Message_Count++;
    }
}

https://cplusplus.com/reference/cstring/memcmp/

1 Like

You already have the ID value in the id variable so that is a simple comparison. Note that your code is displaying this value in HEX so you need to compare to 0x91.

If that is a match, all your data is in dta so you can use memcmp() or simply brute force it

if ( dta[0] == 0x00 && dta[1] == 0x02 && dta[2] == 0xD0 ...
1 Like

Thank you, I think I have it working now. The "memcmp" did the trick! :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.