Go Down

Topic: How can I set the CAN-ID for a new CAN-BUS shield? (Read 182 times) previous topic - next topic

yiran09

Hi,

I have a  CAN-BUS shield of version 1. I also download the library from GitHub and renew them. Those example project runs successfully. However, I am confused what is the ID of this shield? I found there is a function named as 'sendMsgBuf()' in the class 'mcp_can'. Is it the way to set CAN-ID for this shield? if it is, how to set its parameters, such as 'ext', 'rtrBit'?
Thanks.


sherzaad

Hi,

I have a  CAN-BUS shield of version 1. I also download the library from GitHub and renew them. Those example project runs successfully. However, I am confused what is the ID of this shield? I found there is a function named as 'sendMsgBuf()' in the class 'mcp_can'. Is it the way to set CAN-ID for this shield? if it is, how to set its parameters, such as 'ext', 'rtrBit'?
Thanks.


this is not I2C where a device has a predefined address.

YOU can transmit any CAN ID(s) you want and this is what 'defines' your device is a sense. The shield is  basically only a Transceiver to transmit/receive CAN messages for your arduino to interprete. Just make sure that whatever messages you decide to transmit do not exist already on the CAN bus.

'ext' is for extended CAN ID

'rtr' is 'remote transmit request'

I suggest you read up a bit more on CAN to understand the basics before you start coding

yiran09

It works now. It shows the CAN-ID was set by function of 'sendMsgBuf()'. For example, I set the unit connected the canshield with ID of 0x02, then the code could be written as 'CAN.sendMsgBuf(0x02,0,8,buf)'. Where '8' means 8 bytes will be send out, which are stored in buf. I used a usb-can to monitor any message on the CAN bus. 

Balabin

#3
Apr 26, 2019, 01:30 am Last Edit: Apr 26, 2019, 01:31 am by Balabin
Hello! I can not understand what is happening in the MCP_CAN_lib-master library

"The readMsgBuf (* ID, * DLC, * DATA)
If the ID is AND 0x80000000, the ID is of the Extended type, it would be standard.
If the ID AND 0x40000000 EQUALS 0x40000000, the message is a remote request.
The sendMsgBuf (ID, DLC, DATA) function can send extended or standard IDs.
To mark the ID as extended, OR the ID with 0x80000000.
To send a remote request, OR the ID with 0x40000000. "
Why we use 0x80000000 (B100000000000000000000000000000) and 0x40000000 (B10000000000000000000000000000),
in datashite MCP2515 bits IDE and RTR are in other positions?

Extended ID
|ID10||||||||||ID0|SRR|IDE|EID17||||||||||||||||EID0|RTR|

sherzaad

#4
Apr 26, 2019, 07:44 am Last Edit: Apr 26, 2019, 09:09 am by sherzaad
It works now. It shows the CAN-ID was set by function of 'sendMsgBuf()'. For example, I set the unit connected the canshield with ID of 0x02, then the code could be written as 'CAN.sendMsgBuf(0x02,0,8,buf)'. Where '8' means 8 bytes will be send out, which are stored in buf. I used a usb-can to monitor any message on the CAN bus. 
Not quite right...

...with ID of 0x02, then the code could be written as 'CAN.sendMsgBuf(0x02,0,8,buf)'. Where '8' means 8 bytes will be send out, which are stored in buf.
'CAN.sendMsgBuf(0x02,0,8,buf)' will transmit a CAN FRAME With ID 0x02 and a data length (DLC) of 8 bytes.

ID 0x02 by no means is the 'ID' of your shield. Your device can very well transmit more than one CAN ID

READ UP ON HOW CAN COMMUNICATION WORKS!



sherzaad

Hello! I can not understand what is happening in the MCP_CAN_lib-master library

"The readMsgBuf (* ID, * DLC, * DATA)
If the ID is AND 0x80000000, the ID is of the Extended type, it would be standard.
If the ID AND 0x40000000 EQUALS 0x40000000, the message is a remote request.
The sendMsgBuf (ID, DLC, DATA) function can send extended or standard IDs.
To mark the ID as extended, OR the ID with 0x80000000.
To send a remote request, OR the ID with 0x40000000. "
Why we use 0x80000000 (B100000000000000000000000000000) and 0x40000000 (B10000000000000000000000000000),
in datashite MCP2515 bits IDE and RTR are in other positions?

Extended ID
|ID10||||||||||ID0|SRR|IDE|EID17||||||||||||||||EID0|RTR|
nothing to do with the datasheet. It's just how the library author decided to package the Ext and RTR information into the 'ID' variable

yiran09

Not quite right...

'CAN.sendMsgBuf(0x02,0,8,buf)' will transmit a CAN FRAME With ID 0x02 and a data length (DLC) of 8 bytes.

ID 0x02 by no means is the 'ID' of your shield. Your device can very well transmit more than one CAN ID

READ UP ON HOW CAN COMMUNICATION WORKS!



Now I become confused again on how to communicate between several ECUs. For example, in master-slave system, a master communicates with more than one slave. How can I set a special CAN-ID for each slave? The CAN-ID of each slave was fixed in projector of microchip PIC18F25K80 that I formerly used. In other words, we distributed CAN ID of each salve in programming stage. Please help me, thank you.

sherzaad

Now I become confused again on how to communicate between several ECUs....
for the THIRD time, READ UP on how CAN works. there's loads of material out there!

hopefully an article like this one might help clear your "confusion".

https://www.kvaser.com/about-can/the-can-protocol/the-can-bus/





Go Up