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

50 lines
1.3 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.

/*
* SerialPacketReader.cpp
*
* Created on: 17 дек. 2021 г.
* Author: titov
*/
#include "SerialPacketReader.hh"
#include <algorithm>
#include <climits>
#include <cstddef>
#include "../communication/format/BinaryHelpers.hh"
driver::SerialPacketReader::SerialPacketReader(
peripheral::ISerialPort & serial_port,
communication::IBinaryDataSubscriber & binary_data,
std::pmr::memory_resource * memory_resource,
std::size_t bitsize, bool data_invers ) : serial_port(serial_port), binary_data(binary_data), bitsize(bitsize), data_frame(memory_resource),
data_invers(data_invers) {
data_frame.resize( communication::format::bits::bytes_on_bits(bitsize) );
}
void driver::SerialPacketReader::process() {
switch(phase) {
case response: {
if (serial_port.receive(&data_frame.front())) {
if( data_invers )
std::reverse(data_frame.begin(), data_frame.end());
binary_data.read( &data_frame.front(), data_frame.size() );
if( not serial_port.transmite(nullptr, bitsize) )
phase = request;
}
} break;
case request: {
if( serial_port.transmite(nullptr, bitsize) )
phase = response;
} break;
default: {} break;
}
}