Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
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:
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 20 Feb 2015Can 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 20 Feb 2015Vector 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 20 Feb 2015Hello 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 23 Feb 2015I'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 23 Feb 2015Thanks 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 24 Feb 2015Hello 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 24 Feb 20158 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.