Arduino Forum

Using Arduino => Project Guidance => Topic started by: Mozernz on Oct 13, 2017, 09:20 pm

Title: Sending multiple messages over CAN with sparkfun can bus shield
Post by: Mozernz on Oct 13, 2017, 09:20 pm
Hi all

Complete newbie in terms of Arduino here.
I would like to send different can messages over my sparkfun can shield.
My can shield is for test purposes hooked up to another can sniffer i have.

I'm using the write example from the library an can also seeing the messages i sent on my can sniffer.
But when I'm adding a second message with different ID and data the code loops through it one time and then send again only one message... Please be kind to me I'm new to programming.. ;)

Code is below sends message1 for 10s and then message2 for 1s and then only message1 for the rest of the time. Why does it not loop trough everything again?

 
Code: [Select]
#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>

//********************************Setup Loop*********************************//

void setup() {
  Serial.begin(9600);
  Serial.println("CAN Write - Testing transmission of CAN Bus messages");
  delay(1000);
 
  if(Canbus.init(CANSPEED_500))  //Initialise MCP2515 CAN controller at the specified speed
    Serial.println("CAN Init ok");
  else
    Serial.println("Can't init CAN");
   
  delay(1000);
}

//********************************Main Loop*********************************//



void loop()
{
tCAN message1;

        message1.id = 0x1A6; //formatted in HEX
        message1.header.rtr = 0;
        message1.header.length = 7; //formatted in DEC
        message1.data[0] = 0x02;
message1.data[1] = 0x8b;
message1.data[2] = 0xFF;
message1.data[3] = 0x67; //formatted in HEX
message1.data[4] = 0x00;
message1.data[5] = 0x00;
message1.data[6] = 0x1F;

tCAN message2;
        message2.id = 0x18F; //formatted in HEX
        message2.header.rtr = 0;
        message2.header.length = 8; //formatted in DEC
        message2.data[0] = 0x02;
  message2.data[1] = 0x8b;
  message2.data[2] = 0xFF;
  message2.data[3] = 0x67; //formatted in HEX
  message2.data[4] = 0x01;
  message2.data[5] = 0x8D;
  message2.data[6] = 0x1F;
  message2.data[7] = 0x1F;

mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);
mcp2515_send_message(&message1);

delay(10000);

mcp2515_send_message(&message2);

delay(1000);

}


Thanks all!
Title: Re: Sending multiple messages over CAN with sparkfun can bus shield
Post by: MorganS on Oct 14, 2017, 01:01 am
Quote
Code: [Select]
mcp2515_send_message(&message);
Are you sure that compiles? You haven't defined message anywhere in your code. Perhaps you meant message1?

You also have a 10-second delay. Did you wait 10 seconds?
Title: Re: Sending multiple messages over CAN with sparkfun can bus shield
Post by: Mozernz on Oct 14, 2017, 02:32 pm
Sorry message1 is message in my code. Yes it compiles and i also see the messages in via my can sniffer. The 10s are just there to see which messages is broadcasted. But like i said i goes through the code once and then only sends message (no message2 again) for the rest of the time after the delays... Don't know why
Title: Re: Sending multiple messages over CAN with sparkfun can bus shield
Post by: Mozernz on Oct 15, 2017, 05:50 pm
Anybody else?  :)
Title: Re: Sending multiple messages over CAN with sparkfun can bus shield
Post by: MorganS on Oct 16, 2017, 04:48 am
You say your code is different to the code posted above. We aren't going to spend any more time debugging that code. Please post the actual code you're using. Use the CTRL-T autoformat first.
Title: Re: Sending multiple messages over CAN with sparkfun can bus shield
Post by: Mozernz on Oct 17, 2017, 11:24 am
Hi morgan code is:
Code: [Select]
#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>

//********************************Setup Loop*********************************//

void setup() {
  Serial.begin(9600);
  Serial.println("CAN Write - Testing transmission of CAN Bus messages");
  delay(1000);
 
  if(Canbus.init(CANSPEED_500))  //Initialise MCP2515 CAN controller at the specified speed
    Serial.println("CAN Init ok");
  else
    Serial.println("Can't init CAN");
   
  delay(1000);
}

//********************************Main Loop*********************************//



void loop()
{
tCAN message1;

        message1.id = 0x1A6; //formatted in HEX
        message1.header.rtr = 0;
        message1.header.length = 7; //formatted in DEC
        message1.data[0] = 0x02;
message1.data[1] = 0x8b;
message1.data[2] = 0xFF;
message1.data[3] = 0x67; //formatted in HEX
message1.data[4] = 0x00;
message1.data[5] = 0x00;
message1.data[6] = 0x1F;

tCAN message2;
        message2.id = 0x18F; //formatted in HEX
        message2.header.rtr = 0;
        message2.header.length = 8; //formatted in DEC
        message2.data[0] = 0x02;
  message2.data[1] = 0x8b;
  message2.data[2] = 0xFF;
  message2.data[3] = 0x67; //formatted in HEX
  message2.data[4] = 0x01;
  message2.data[5] = 0x8D;
  message2.data[6] = 0x1F;
  message2.data[7] = 0x1F;

mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);
mcp2515_send_message(&message1);

delay(10000);

mcp2515_send_message(&message2);

delay(1000);

}


Found a solution to software reset and start the loop again this works.
Code: [Select]
void loop()
{
tCAN message;

        message.id = 0x1A6; //formatted in HEX
        message.header.rtr = 0;
        message.header.length = 7; //formatted in DEC
        message.data[0] = 0x02;
message.data[1] = 0x8b;
message.data[2] = 0xFF;
message.data[3] = 0x67; //formatted in HEX
message.data[4] = 0x00;
message.data[5] = 0x00;
message.data[6] = 0x1F;

tCAN message2;
        message2.id = 0x18F; //formatted in HEX
        message2.header.rtr = 0;
        message2.header.length = 8; //formatted in DEC
        message2.data[0] = 0x02;
  message2.data[1] = 0x8b;
  message2.data[2] = 0xFF;
  message2.data[3] = 0x67; //formatted in HEX
  message2.data[4] = 0x01;
  message2.data[5] = 0x8D;
  message2.data[6] = 0x1F;
  message2.data[7] = 0x1F;

mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);

mcp2515_send_message(&message);
Serial.print(message.id);
delay(10);

mcp2515_send_message(&message2);
Serial.print(message2.id);
delay(10);
software_Reset();

}
void software_Reset() // Restarts program from beginning but does not reset the peripherals and registers
{
asm volatile ("  jmp 0"); 




I think my problem has something to do with resetting the mcp2525.

Thanks
Title: Re: Sending multiple messages over CAN with sparkfun can bus shield
Post by: Mozernz on Oct 31, 2017, 06:42 pm
My problem is not solved... I only can send the first 3 messages with this code all other messages will be ignored.. I think it has something to do with the mcp2525? Anybody?
Title: Re: Sending multiple messages over CAN with sparkfun can bus shield
Post by: jtanner on Aug 12, 2018, 03:57 am
I realise this thread is ooolllddddd..... but, I'm sure you have to

mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);


before EVERY message.  So...

mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);
mcp2515_send_message(&message1);
...
mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);
mcp2515_send_message(&message2);
...
mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);
mcp2515_send_message(&message3);

??