Utilisation des fonctions pack/unpack code/decode de eerimoq/cantools (motohawk example)
Bonjour,
je souhaite utiliser les fonctions pack/unpack code/decode de l'exemple motohawk.c pour encoder et décoder un message 8 bytes HEX:
Source: https://github.com/eerimoq/cantools/
J'ai préparé un exemple qui compile sans erreur avec mingw g++ mais qui ne délivre pas les résultats attendus.
Le message HEX à décoder est:
{0x80, 0x4A, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00}
Les fonctions de motohaw.c que j'utilise sont:
Code:
1 2 3 4 5 6 7 8 9
| motohawk_example_message_enable_encode
motohawk_example_message_average_radius_encode
motohawk_example_message_temperature_encode
motohawk_example_message_pack
motohawk_example_message_unpack
motohawk_example_message_enable_decode
motohawk_example_message_average_radius_decode
motohawk_example_message_temperature_decode |
Je ne suis pas sur de mon utilisation des pointeurs *dst_p et *src_p dans les fonctions pack/unpack définies dans motohawk.c
Le résultat après "unpack" et "decode" devrait être:
Signal: Enable 'Enabled'
Signal: average_radius 0.0 m
Signal: temperature 255.92 degK
Le résultat après "encode" et "pack" devrait être:
8 Byte message: 80 4A 0F 00 00 00 00 00
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| #include <stdio.h>
#include <iostream>
/* Using c-code for motohawk.dbc from CANTOOLS (https://github.com/eerimoq/cantools/) */
#include "motohawk.h"
#include "motohawk.c"
int main(void)
{
// PART_1: Unpack 8 bytes message using unpack functions from motohawk.c
unsigned char src_p_ID_1F0[8] = {0x80, 0x4A, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00};
// | 0x1F0_enable 'Enabled' -
// | 0x1F0_average_radius 0.0 m
// | 0x1F0_temperature 255.92 degK
struct motohawk_example_message_t dst_p_data; // Object to unpack the message into.
size_t size_src_p_ID_1F0 = 8; // Size of dst_p Buffer
// Unpack the source message into dst_p_data
motohawk_example_message_unpack(&dst_p_data, src_p_ID_1F0, size_src_p_ID_1F0);
// Declare variables to store decoded physical values
uint8_t ID_1F0_enable;
uint8_t ID_1F0_avg_radius;
int16_t ID_1F0_temp;
ID_1F0_enable = motohawk_example_message_enable_decode(dst_p_data.enable);
ID_1F0_avg_radius = motohawk_example_message_average_radius_decode(dst_p_data.average_radius);
ID_1F0_temp = motohawk_example_message_temperature_decode(dst_p_data.temperature);
// To print the content of src_p_data
std::cout << "Decoded_Value_enable: " << unsigned(ID_1F0_enable) << std::endl;
std::cout << "Decoded_Value_average_radius: " << unsigned(ID_1F0_avg_radius) << std::endl;
std::cout << "Decoded_Value_temperature: " << float(ID_1F0_temp) << std::endl;
// PART_2: Pack value data into a 8 bytes message using pack functions from motohawk.h
// Declare src_p_data of type motohawk_example_message_t
struct motohawk_example_message_t src_p_data;
// Encode physical values and assign result to src_p_data
src_p_data.enable = motohawk_example_message_enable_encode(1u);
src_p_data.average_radius = motohawk_example_message_average_radius_encode(0.0);
src_p_data.temperature = motohawk_example_message_temperature_encode(255.92);
// To print the content of src_p_data
std::cout << "Encoded_Hex_enable: " << std::hex << static_cast<int>(src_p_data.enable) << std::endl;
std::cout << "Encoded_Hex_average_radius: " << std::hex << static_cast<int>(src_p_data.average_radius) << std::endl;
std::cout << "Encoded_Hex_temperature: " << std::hex << static_cast<int>(src_p_data.temperature) << std::endl;
unsigned char dst_p_ID_1F0 = {0}; // Buffer to pack the message into.
size_t size_dst_p_ID_1F0 = 8; // Size of dst_p Buffer
motohawk_example_message_pack(&dst_p_ID_1F0, &src_p_data, size_dst_p_ID_1F0);
std::cout << "Packed_message: " << std::hex << static_cast<int>(dst_p_ID_1F0) << std::endl;
// To print the 8 bytes message ID_1F0 in HEX format
int loop;
printf("ID_1F0: ");
for(loop=0;loop<8;loop++)
printf("%02X ",dst_p_ID_1F0);
printf("\n");
return 0;
} |
Voici ce que mon code retourne pour le décodage (incorrect):
Code:
1 2 3
| Decoded_Value_enable: 1
Decoded_Value_average_radius: 0
Decoded_Value_temperature: 255 |
Voici ce que mon code retourne pour l'encodage (incorrect):
Code:
1 2 3 4 5
| Encoded_Hex_enable: 1
Encoded_Hex_average_radius: 0
Encoded_Hex_temperature: 24f
Packed_message: 0
ID_1F0: 00 00 00 00 00 00 00 00 |
Pouvez m'aider s'il vous plait pour la compréhension des fonctions pack/unpack de motohawk.c / motohawk.h et la correction de mon exemple?
(ligne 19 et 54 dans mon code)
https://github.com/eerimoq/cantools/...rce/motohawk.c
https://github.com/eerimoq/cantools/...rce/motohawk.h
Merci, Chris