Re: Understanding the sendQuery and write functions in FreematicsONE

Inquiry and support for Freematics products
Post Reply
jonathanK
Posts: 3
Joined: Mon Nov 13, 2017 11:12 pm

Re: Understanding the sendQuery and write functions in FreematicsONE

Post by jonathanK »

Hey All,
I'm working on adapting my freematicsONE+ (Firmware v5) to be able to query non-standard PIDs from the Battery Monitoring Unit (BMU) of a Mitsubishi Oultander PHEV, and want to clarify for myself what the default program is doing at certain moments. To that end I have a few questions about the default code.

First of all, what I know so far about my task is this: the appropriate CAN protocol for this car is defined in FreematicsBase as PROTO_CAN_11B_500K. If I want to access PIDs that belong to the BMU, i have to send my queries to a specific address (0x761), and I have to send them in mode 0x21. If I send, for example,

0x761 0x02 0x21 0x01 - @address 761, message length of 2, 0x21 0x01 i'm basically asking to see the current data from the BMU and, after sending the appropriate flow control message, I should get a

Which is exactly what I want to log. So I figure for the default program, I'll need to change my CAN protocol, my data Mode, the PIDs sent to readPID, the parsing of success and successful messages in receive(), the way that my data is normalized (adding new PIDS means attending to the correct formulas to convert the data) for a start, and once i've done that, other issues will start to present themselves for my attention.

Nevertheless, I already have questions about what's going on in the default program in the sendQuery():


void COBDSPI::sendQuery(byte pid){
char cmd[8];
sprintf_P(cmd, PSTR("%02X%02X\r"), dataMode, pid);
setTarget(TARGET_OBD);
write(cmd);
}


I am composing a message to be transferred over the OBD-2 port to the can bus, and in my example case, right now cmd might look something like 0x21 0x01. First question: how should I understand the m_target variable (m_target is set to TARGET_OBD by the call to setTarget())?

Alright, moving on we drop down to the write() call, where m_target reappears:

char buf[256];
int len = strlen(s);
if (len > sizeof(buf) - 6) len = sizeof(buf) - 6;
memcpy(buf, targets[m_target], 4);
memcpy(buf + 4, s, len);
len += 4;

As I understand it, the first memcpy call puts {'$', 'O', 'B', 'D'} into buf, but what is that? Maybe I'm just unfamiliar enough with OBD-2, but I know in the response reading section we basically filter messages by looking for that sequence, so is that basically the freematicsONE+'s identifier? How am I to interpret it? After we add that, then we append our message (0x21 0x01), add a terminating byte and ship it off. Where can I intervene to add an addressee (0x761)? Or am i better off trying something with the other version of write(byte* data, int len)?

I'd be greatful for any help I can get (be it simply the answers to my questions, or sharing your own experiences/results).

//JonathanK
Post Reply