Skip to content

Commit

Permalink
Merge pull request #9 from ckormanyos/single_header_library
Browse files Browse the repository at this point in the history
Implement as single-file header-only library
  • Loading branch information
ckormanyos committed Jun 11, 2023
2 parents b8fcdc5 + 2750807 commit 66e0439
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 173 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ serial_win32api
<img src="https://github.com/ckormanyos/serial_win32api/actions/workflows/serial_win32api.yml/badge.svg" alt="Build Status"></a>
</p>

`serial_win32api` implements a modern, C++14, header-only serial (COM) driver.
This driver is designed for classic Win32-API in MSVC.
ckormanyos/serial_win32api implements a modern, C++14, single-file,
header-only serial (COM) driver.

This serial (COM) driver is designed for classic Win32-API in MSVC.

## Usage

Using `serial_win32api` is straightforward.
Using ckormanyos/serial_win32api is straightforward.
- Include the header `<serial_win32api.h>`.
- Ensure that its path is included in the compiler's include paths.
- Use the constructor, and the `send()` and `recv()` functions.
Expand Down
1 change: 0 additions & 1 deletion serial.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
<ClCompile Include="test.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="serial\serial.h" />
<ClInclude Include="serial\serial_win32api.h" />
<ClInclude Include="util_sleep.h" />
</ItemGroup>
Expand Down
3 changes: 0 additions & 3 deletions serial.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
<ClInclude Include="util_sleep.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="serial\serial.h">
<Filter>Source Files\serial</Filter>
</ClInclude>
<ClInclude Include="serial\serial_win32api.h">
<Filter>Source Files\serial</Filter>
</ClInclude>
Expand Down
165 changes: 0 additions & 165 deletions serial/serial.h

This file was deleted.

154 changes: 153 additions & 1 deletion serial/serial_win32api.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,161 @@
#ifndef SERIAL_WIN32_API_1998_11_23_H
#define SERIAL_WIN32_API_1998_11_23_H

#include <array>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <vector>

#include <windows.h>

#include <serial.h>
struct t_scb
{
explicit t_scb(const std::uint32_t ch,
const std::uint32_t bd = static_cast<std::uint32_t>(UINT32_C(9600)),
const std::uint32_t n_send = static_cast<std::uint32_t>(UINT32_C(0x10000)),
const std::uint32_t n_recv = static_cast<std::uint32_t>(UINT32_C(0x10000)))
: channel (ch),
baud (bd),
send_buf_len(n_send),
recv_buf_len(n_recv) { }

t_scb() = delete;

~t_scb() = default;

t_scb(const t_scb&) = default;
t_scb(t_scb&&) noexcept = default;

auto operator=(const t_scb&) -> t_scb& = default;
auto operator=(t_scb&&) noexcept -> t_scb& = default;

std::uint32_t channel { static_cast<std::uint32_t>(UINT8_C(1)) };
std::uint32_t baud { static_cast<std::uint32_t>(UINT16_C(9600)) };

std::uint32_t send_buf_len { static_cast<std::uint32_t>(UINT32_C(0x10000)) };
std::uint32_t recv_buf_len { static_cast<std::uint32_t>(UINT32_C(0x10000)) };
};

class serial
{
public:
static constexpr int open_Ok = 0x00000000;
static constexpr int open_BadChannelNumber = 0x00000001;
static constexpr int open_ChannelInUse = 0x00000002;
static constexpr int open_ChannelNotAvailable = 0x00000004;
static constexpr int open_NotEnoughMemory = 0x00000008;
static constexpr int open_InvalidParams = 0x00000010;
static constexpr int open_BadBufferSize = 0x00000020;
static constexpr int open_BaudAdjusted = 0x00000100;
static constexpr int open_BitsAdjusted = 0x00000200;
static constexpr int open_StopAdjusted = 0x00000400;
static constexpr int open_ParityAdjusted = 0x00000800;
static constexpr int open_ModeAdjusted = 0x00001000;
static constexpr int open_ReceiveBufferAdjusted = 0x00002000;
static constexpr int open_SendBufferAdjusted = 0x00004000;
static constexpr int open_Error = open_BadChannelNumber
| open_ChannelInUse
| open_ChannelNotAvailable
| open_NotEnoughMemory
| open_InvalidParams;

explicit serial(const std::uint32_t ch,
const std::uint32_t bd = static_cast<std::uint32_t>(UINT16_C(9600)),
const std::uint32_t n_send = static_cast<std::uint32_t>(UINT32_C(0x10000)),
const std::uint32_t n_recv = static_cast<std::uint32_t>(UINT32_C(0x10000)))
: m_scb(ch, bd, n_send, n_recv) { }

serial() = delete;

serial(const serial&) = delete;
serial(serial&&) noexcept = delete;

auto operator=(const serial&) -> serial& = delete;
auto operator=(serial&&) noexcept -> serial& = delete;

virtual ~serial() = default;

virtual auto open(const t_scb& scb, std::uint32_t& result) -> bool = 0;
virtual auto close() -> bool = 0;

virtual auto recv(::std::vector<std::uint8_t>& data) const -> std::uint32_t = 0;

virtual auto send_in_progress() const -> bool = 0;
virtual auto recv_ready() const -> std::uint32_t = 0;

auto send(const ::std::vector<std::uint8_t>& data) -> bool
{
return this->do_send(data);
}

template<typename InputIteratorType>
auto send(InputIteratorType first, InputIteratorType last) -> bool
{
const auto vin = ::std::vector<std::uint8_t>(first, last);

return this->do_send(vin);
}

auto send(const std::uint8_t b) -> bool
{
using array_one_byte_type = ::std::array<std::uint8_t, static_cast<std::size_t>(UINT8_C(1))>;

const auto ar1 = array_one_byte_type { b };

return send(ar1.cbegin(), ar1.cend());
}

auto set_chan(const std::uint32_t ch) -> bool
{
auto result_set_chan_is_ok = bool { };

if((!m_is_error) && (!m_is_open))
{
m_scb.channel = ch;

result_set_chan_is_ok = true;
}
else
{
result_set_chan_is_ok = false;
}

return result_set_chan_is_ok;
}

auto set_baud(const std::uint32_t bd) -> bool
{
auto result_set_baud_is_ok = bool { };

if((!m_is_error) && (!m_is_open))
{
m_scb.baud = bd;

result_set_baud_is_ok = true;
}
else
{
result_set_baud_is_ok = false;
}

return result_set_baud_is_ok;
}

auto valid() const -> bool { return (is_open() && (!is_error())); }

protected:
t_scb m_scb { (std::numeric_limits<std::uint32_t>::max)() };

bool m_is_open { false };
bool m_is_error { false };

bool is_open() const { return m_is_open; }
bool is_error() const { return m_is_error; }

private:
virtual auto do_send(const ::std::vector<std::uint8_t>& data) -> bool = 0;
};

class serial_win32api : public serial
{
Expand Down

0 comments on commit 66e0439

Please sign in to comment.