9 years, 9 months ago.

How do I build up a CAN message and send it out?

Hello, Can anyone give me an example on how to build up and output a CAN message.

I have tried for a loong time now in several ways but it doesn't seem to work at all.

I'm not a newbee to CAN as I work with this everyday in my job and I use vector canalyzer doing capl scripting almost every day.

What I need help with is some code so that I can get going in programming my own CAN node.

What I need is jsut a simple program that outputs something on either CAN port.

I have attached CAN transivers if anyone wonders and yes I have terminated it.

BR JOhan

Question relating to:

The mbed-enabled LPC4088 QuickStart Board from Embedded Artists is a easy to use ARM Cortex-M4 rapid prototyping board in a standard through hole DIP package (44-pin), targeted at high-performance as …

2 Answers

9 years, 9 months ago.

The basic code to send a packet is:

CAN MyCanBus(tx,rx);

main () {

// create the message
  CANMessage messageOut;

  messageOut.format = CANStandard or  CANExtended;  // standard or extended ID (can be skipped for standard)
  messageOut.id = id to use;
  messageOut.len = length in bytes (1 to 8);
  messageOut.data[n] = data byte n; // repeat for each byte.

// set up the can bus
  MyCanBus.frequency(500000); // set bus speed
  MyCanBus.reset(); // reset the bus controller

// send the message   
  MyCanBus.write(messageOut);

}

Hello Andy, I tried this:


#include "mbed.h"
//#include "CAN.h"


DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut CAN1on(p39);
DigitalOut CAN2on(p38);
CAN can1(p9, p10);  // rd, td Transmitter
//CAN can2(p30, p29); // rd, td Monitor
//Serial pc(USBTX, USBRX); // tx, rx
//int counter = 0;
//char data[8];

//**********The main program*********************
int main() {
//----------------Initialization-----------------------

    CANMessage messageOut;
 
    messageOut.format = CANStandard;// or  CANExtended;  // standard or extended ID (can be skipped for standard)
    messageOut.id = 0x71;
    messageOut.len = 1;//length in bytes (1 to 8);
    messageOut.data[1] = 0x42; // repeat for each byte.
    CAN1on = 0;
    can1.frequency(500000);
    can1.reset();
    
    
//-----------------------------------------------------

//---------------The read out----------------------
    while(1) {
        wait(0.2);
        can1.write(messageOut);
    }
//----------------------------------------------------
}

With no luck...

BR JOhan

posted by Johan Svensson 20 Feb 2015

Can you define no luck please. Nothing gets sent or the wrong thing gets sent?

It should be data[0] not data[1], arrays always count from 0 in c, so you'll get the wrong data value but other than that your code should work.

What is listening for the CAN packet? Is it set to the correct baud rate? Is the CAN driver wired correctly? Can you put a scope on the CAN TX or the actual can bus lines?

posted by Andy A 20 Feb 2015

Vector CANalyzer with a VN1630 CAN card is listening on the CAN bus with the bittrare set to 500kbds. The trancievers is of model SN65HVD1050 which are terminated with 120 ohms. Does the trancievers need to be of a 3.3V type or can I use this variant?

posted by Johan Svensson 20 Feb 2015

Nothing gets sent at all.

posted by Johan Svensson 20 Feb 2015

I think that driver is fine. Is pin 8 pulled low?

posted by Andy A 20 Feb 2015

Pin 8 is pulled low by pin 39 "can1on"

posted by Johan Svensson 20 Feb 2015

OK, at this point I'm out of ideas without putting a scope on it. Sorry.

posted by Andy A 20 Feb 2015

Hello Andy, Tried it out with a scope and nothing comes out on the pins!

Can it be that the mbed CAN library is broken?

Everything compiles just fine but putting a scope directly on the TX pin doesn't give anything except for a steady high level.

BR JOhan

posted by Johan Svensson 23 Feb 2015

I've not used that board but I've not had any problems on the lpc1768 running two can busses with lots of traffic. If it's a library problem it's specific to that board.

posted by Andy A 23 Feb 2015

Thanks anyway Andy,

I found this: http://developer.mbed.org/questions/2939/mbed-Driver-Bug-CAN-for-the-EA-LPC4088-Q/

But when looking at the src code it was already changed.

So there must be an additional bug.

What to do now?

Any suggestions?

BR JOhan

posted by Johan Svensson 24 Feb 2015

Hello again Andy,

This was strange...

When setting it to 1Mbaud the CAN messege comes out on channel 1 with this code:

 
#include "mbed.h"
#include "CAN.h"
 

CAN can1(p9, p10);  // rd, td Transmitter
//Serial pc(USBTX, USBRX); // tx, rx
//int counter = 0;
//char data[8];
 
//**********The main program*********************
int main() {
//----------------Initialization-----------------------
 
    CANMessage messageOut;
 
    messageOut.format = CANStandard;// or  CANExtended;  // standard or extended ID (can be skipped for standard)
    messageOut.id = 0x71;
    messageOut.len = 1;//length in bytes (1 to 8);
    messageOut.data[0] = 0x42; // repeat for each byte.
    CAN1on = 0;
    can1.frequency(1000000);
    can1.reset();
    
//-----------------------------------------------------
 
//---------------The read out----------------------
    while(1) {
        wait(0.2);
        can1.write(messageOut);
        
    }
//----------------------------------------------------
}

But when I use this code:

 
#include "mbed.h"
#include "CAN.h"
 
 
CAN can1(p9, p10);  // rd, td Transmitter_1
CAN can2(p30, p29); // rd, td Transmitter_2
//Serial pc(USBTX, USBRX); // tx, rx
//int counter = 0;
//char data[8];
 
//**********The main program*********************
int main() {
//----------------Initialization-----------------------
 
    CANMessage messageOut;
 
    messageOut.format = CANStandard;// or  CANExtended;  // standard or extended ID (can be skipped for standard)
    messageOut.id = 0x71;
    messageOut.len = 1;//length in bytes (1 to 8);
    messageOut.data[0] = 0x42; // repeat for each byte.
    CAN1on = 0;
    can1.frequency(1000000);
    can1.reset();
    can2.frequency(1000000);
    can2.reset();
    
//-----------------------------------------------------
 
//---------------The read out----------------------
    while(1) {
        wait(0.2);
        can1.write(messageOut);
        can2.write(messageOut);
    }
//----------------------------------------------------
}

Nothing comes out, but Led3 and 4 starts blinking.

Also when running it on any other frequency than 1Mbaud, nothing happens.

BR JOhan

posted by Johan Svensson 24 Feb 2015
8 years, 9 months ago.

Even though i know this is old, i will just reply for future reference in case it helps someone.

Remove the canx.reset() and it should work.