0

I am trying to brute force a CANBUS with ID's and values but my loop doesn't seem to change the ID.

I am expecting the following when changing ID's

0x100 FF FF FF FF FF FF FF FF
0x101 00 00 00 00 00 00 00 00

But I get

0x100 FF FF FF FF FF FF FF FF
0x100 00 00 00 00 00 00 00 00

I am not sure where I am going wrong

#include <SPI.h>
#include "mcp_can.h"
// Define the chip select pin for the MCP2515
#define CAN_CS 5
MCP_CAN CAN(CAN_CS); // Initialize MCP2515 with the CS pin
void setup() {
 Serial.begin(115200);
 // Initialize MCP2515
 if (CAN.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK) {
 Serial.println("MCP2515 Initialized Successfully!");
 } else {
 Serial.println("Error Initializing MCP2515...");
 while (1);
 }
 CAN.setMode(MCP_NORMAL);
}
void loop() {
 for (uint16_t id = 0x100; id <= 0x500; id++) { // Increment ID
 for (uint8_t byteValue = 0x00; byteValue <= 0xFF; byteValue++) { // Increment data bytes from 0x00 to 0xFF
 uint8_t data[8] = {byteValue, byteValue, byteValue, byteValue, byteValue, byteValue, byteValue, byteValue}; // Set all bytes to the current value
 // Send CAN message
 byte sendStatus = CAN.sendMsgBuf(id, 0, 8, data);
 // Keep the if condition unchanged
 if (sendStatus == CAN_OK) {
 Serial.print("Sent message with ID: 0x");
 Serial.print(id, HEX);
 Serial.print(" Data: ");
 for (int k = 0; k < 8; k++) {
 Serial.print(data[k], HEX);
 Serial.print(" ");
 }
 Serial.println();
 } else {
 Serial.print("Error sending message with ID: 0x");
 Serial.println(id, HEX);
 }
 delay(10); // Small delay between messages
 }
 // Print a separator after finishing all data bytes for the current ID
 Serial.println("----------------------------------------");
 }
 Serial.println("Finished all IDs from 0x100 to 0x500 with data bytes from 0x00 to 0xFF.");
 while (1); // Stop after completion
}
Rohit Gupta
6122 gold badges5 silver badges18 bronze badges
asked Jan 25 at 1:17
1
  • 1
    that does not look like a printout that is generated by the program ... please provide the full printout Commented Jan 25 at 3:23

1 Answer 1

2

You outer loop "is not incrementing" might mean your inner loop never ends:

for (uint8_t byteValue = 0x00; byteValue <= 0xFF; byteValue++) {

Now what do we have in your for loop code:

  • uint8_t byteValue -> byteValue can store values from 0 to 255 (0xFF)
  • byteValue <= 0xFF -> maximum value is 255 and condition is basically less than 256 -> it's always true.
  • uint8_t overflow is well defined 255+1 == 0 (unlike signed values)

Edit:

I tried to compile this loop and even without verbose compilation in the Arduino IDE it shows:

sketch_jan25a.ino: In function 'void loop()':
sketch_jan25a.ino:8:46: warning: comparison is always true due to limited range of data type [-Wtype-limits]

So don't ignore warnings in your code (there are also some warnings in Arduino core :D)

answered Jan 25 at 10:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.