can bus, how to read a request signal (on slave)and how to respond to the master

Hello, i have a small project based on arduino and mcp2515 can bus board. I have already managed to read and view the broadcast from the master (ecu) on lcd.

Now I'd like to be able to send data from Arduino to the master. To do this I cannot simply send the data because the master would not accept them. To accept the data, the master sends a 29bit request indicating the details to reply by sending the data.

for example, I get a message like this:

extended ID: 0x000C8350 Dl: 3 Data :0x07 0x0d 0x08

According to the protocol used by the master I interpret this way

For ID:

offset = 00000000011 = 3
type = 001 = 1
from id = 0000 = 0
to id = 0110 = 6
table = 1010 = 10
table = 0 = 0
spare = 00 = 0

For data:

0x07, 0x0d, 0x08

\ = 000 = 0
rep|y table = 00111 = 7
reply offset = 00001101000 = 104
\ = 0 = 0
reply bit = 1000 = 8

This interpretation therefore contains all the data to be able to replicate. My question is: what is the best method to process these input data and consequently generate the reply message? I need some advice on how to write a function that reads the request, interprets it and prepares the answer with my 8 bytes containing the data.

If there are any examples I can study it would be perfect, or if you can recommend something like "use this type of function" or "read this article". in short, I do not ask that you write me the program but only an address on how I should do it and on what principles I should follow. I'm quite a novice in C ++ but not completely illiterate. :confused:

I thank those who will have the patience to help me, greetings.

I find your post confusing with regards to the data you want to manipulate....

you might already know this but just for clarity; A CAN messages contains 2 parts that you can manipulate.

CAN-Frame-Message-Parameters-Bit-Start-Length_2.png

In you case it seem that your 'master' is using an extended CAN id (29 bits) :0x000C8350

so first, what CAN ID would the 'slave' reply on? could you specify please

Second, the data you want to send from the slave, could you please structure it in the form below so that we may understand better how you need it packaged? (the different colours each represent a signal in the example)

Untitled.png

CAN-Frame-Message-Parameters-Bit-Start-Length_2.png

Untitled.png

You are asking for a very generic application level programing advice. There are many ways to solve your issue at the application layer. You basically receive 9 numbers, one 32/29bit and eight 8bit numbers. Now you need to react to these numbers.

The problem is we do not know how many different combinations these numbers can form. If you can only get 2 IDs you can simply use an if( id == 0x000C8350 ) and then start looking at the data bytes. But if there are many IDs this will fast grow into unreadable code. Then creating a more generic solution that will use data structures for look ups might be more scalable.

Is there a document you can link to that describes the application layer aka the messages you can receive and what values they require? And how many of them do you want/need to process in your application.

Btw, there are no slaves in CAN, everyone is a master. :slight_smile:

first of all thanks for the answers. :slight_smile:
I understand that the post is a bit confusing and generic, but I have so many doubts that I had to start somewhere : Smiley-confuse:

as you have guessed I receive a 29bit ID and 3 bytes of data field.
Interpreting the documentation of the ecu I understood that the bits of the id contain some information and the bits of the data field contain other information, as I described in the first post

My (perhaps incorrect) idea was to unmount IDs and data fields into variables that would then go on to compose the reply message, adding the required data to it.
I'm not sure that the data request message never changes over time as it contains offset and table in which to go to write and maybe it could vary.

here is the link to the documentation:

in section 3 there is a graph of how the incoming message should be interpreted.
in my specific case it is the one that in the datasheet is called REQ_MSG and I have to reply with an MSG_RSP.


I looked at the documentation and the website.

This is a complex thing. You should not start everything from scratch. I would seriously search the msextra.com forums and github for work other people have done before. There are thousands of topics and tens of thousands of posts there. If you cannot find what you need to get started there, you will need some serious dedication and experience to develop a solution by yourself.

Google "megasquirt github" and "megasquirt arduino site:msextra.com"

I searched a lot on google and I didn't find any project already done (no public at least) ...
The only trace I found is a library called mscan, I tried to study how it works but I didn't understand much.
It seems that he does exactly what I need, that is, he reads, interprets and sets the answer. Unfortunately there is no documentation or example scketch so I don't know how to use it. I attach the library, this is the link of the repository of the creator of the library, where however a scketc is not present unfortunately

https://bitbucket.org/lutorm/arduino/src/master/

MsCan.cpp (10.9 KB)

MsCan.h (4.15 KB)

deg87:
I searched a lot on google and I didn't find any project already done (no public at least) ...
The only trace I found is a library called mscan, I tried to study how it works but I didn't understand much.
It seems that he does exactly what I need, that is, he reads, interprets and sets the answer. Unfortunately there is no documentation or example scketch so I don't know how to use it. I attach the library, this is the link of the repository of the creator of the library, where however a scketc is not present unfortunately

Bitbucket

there are examples actually there.... just needed some futher digging! :slight_smile:

https://bitbucket.org/lutorm/arduino/src/6e78c5b5874a5044c2b88fd347db1e607c742bd6/canbus/?at=master

got that like from the guy's blog:
https://blog.familjenjonsson.org/blog/2016/11/06/microsquirting-the-nc30-part-27-idle-air-controller/

but still will need a lot of work I reckon as those codes contain a LOT of other libraries that I donno where ur gonna find them (more digging for you!)

good luck! :wink:

I already found that link, actually I already downloaded the entire repository, but I didn't find any .ini files. There are only .cc or .h files, but arduino ide cannot open. Maybe I'm missing something, can you please explain how to use .cc or .h files? Maybe they are stupid, but I can't find any indication :frowning:

You need to have a look into how libaries are used. Arduino uses C files, in case of most libaries *.cpp and *.h files. The *.ino is just a C file with a different file extension.

https://www.arduino.cc/en/main/libraries

You can also just install a library from the library manager and look at the files.

Hi,

Hello, i have a small project based on arduino and mcp2515 can bus board. I have already managed to read and view the broadcast from the master (ecu) on lcd.

What model Arduino are you using?
What canbus board are you using?
Can you post the code you used to do this?
What library did you use?
What is the application, what sort of data do you want to exchange.

I think you are looking too deep into canbus just to do a simple master - slave comms.

Thanks.. Tom.... :slight_smile:

Hi,
This github library may help, especially the examples that come with the library.

Tom... :slight_smile:

thanks for the answers, in the end I succeeded, I didn't use any additional library (except mcp2515 and wire of course) I just had to squeeze my brain a little.

1 Like