MotorControlModuleSDFM_TMS3.../Projects/EFC_Communication/UMLibrary/driver/HipMessage.hh
2024-06-07 11:12:56 +03:00

68 lines
2.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* HipMessage.h
*
* Created on: 4 мар. 2020 г.
* Author: user
*/
#ifndef UMLIBRARY_DRIVER_HIPMESSAGE_HH_
#define UMLIBRARY_DRIVER_HIPMESSAGE_HH_
#include "HipMessageControl.hh"
namespace driver {
struct Header {
union {
struct {
uint16_t address_value :5;
const uint16_t fixed_pattern :3;
uint16_t :8;
uint16_t command_value :6;
const uint16_t fixed1 :1;
uint16_t error_bit :1;
};
uint16_t bytes_2[2];
};
Header(uint16_t address, uint16_t command)
: address_value(address), command_value(command),
fixed_pattern(0x2), fixed1(0x1) {}
Header(char bytes[2]) { bytes_2[0] = bytes[0], bytes_2[1] = bytes[1]; }
};
//...???
class Message {
private:
static const unsigned short first_data_byte_pos = 2;
char * const data = nullptr; //внешний буфер для приема и передачи сообщений
const std::size_t data_size = 0; //размер внешнего буфера
public:
Message( char * data, std::size_t data_size ) : data(data), data_size(data_size) {}
Message & operator=( const Header & header );
operator Header() const { return Header(data); }
HipResult unpack_message( const MessageControl * message );
/// Функция подготавливает буфер для отправки.
/// Возвращает false, если размер буфера недостаточен.
bool pack_message( const MessageControl * message );
static std::size_t getRecMessageLen( const MessageControl * message ) { return message->rec_data_size + 3; }
static std::size_t getSendMessageLen( const MessageControl * message ) { return message->send_data_size + 3; }
static unsigned short calcControlSumm( const char * data, std::size_t data_len );
};
} /* namespace driver */
#endif /* UMLIBRARY_DRIVER_HIPMESSAGE_HH_ */