psen_scan_udp_interface.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2020 Pilz GmbH & Co. KG
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU Lesser General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public License
14 // along with this program. If not, see <https://www.gnu.org/licenses/>.
15 
18 #include <boost/chrono/chrono_io.hpp>
19 #include <chrono>
20 #include <thread>
21 
22 using boost::asio::ip::udp;
23 
24 namespace psen_scan
25 {
26 static const uint64_t TIMEOUT_LOOP_SLEEP_DURATION_MS = 5;
27 
28 PSENscanUDPInterface::PSENscanUDPInterface(const std::string& scanner_ip,
29  const uint32_t& host_udp_port,
30  const unsigned short scanner_port_write,
31  const unsigned short scanner_port_read)
32  : socket_write_(io_service_, udp::endpoint(udp::v4(), host_udp_port + 1))
33  , socket_read_(io_service_, udp::endpoint(udp::v4(), host_udp_port))
34  , udp_write_endpoint_(boost::asio::ip::address_v4::from_string(scanner_ip), scanner_port_write)
35  , udp_read_endpoint_(boost::asio::ip::address_v4::from_string(scanner_ip), scanner_port_read)
36 {
37 }
38 
40 {
41  close();
42 }
43 
45 {
46  try
47  {
50  }
51  // LCOV_EXCL_START
52  catch (const boost::system::system_error& ex)
53  {
54  throw ScannerOpenFailed(ex.what());
55  }
56  // LCOV_EXCL_STOP
57 }
58 
60 {
61  try
62  {
63  socket_write_.close();
64  socket_read_.close();
65  }
66  // LCOV_EXCL_START
67  catch (const boost::system::system_error& ex)
68  {
69  throw ScannerCloseFailed(ex.what());
70  }
71  // LCOV_EXCL_STOP
72 }
73 
74 void PSENscanUDPInterface::write(const boost::asio::mutable_buffers_1& buffer)
75 {
76  try
77  {
78  socket_write_.send(buffer);
79  }
80  // LCOV_EXCL_START
81  catch (const boost::system::system_error& ex)
82  {
83  throw ScannerWriteFailed(ex.what());
84  }
85  // LCOV_EXCL_STOP
86 }
87 
89 {
90  return socket_read_.available() > 0u;
91 }
92 
93 std::size_t PSENscanUDPInterface::read(boost::asio::mutable_buffers_1& buffer,
94  const std::chrono::steady_clock::duration timeout)
95 {
96  const auto start_time{ std::chrono::steady_clock::now() };
97  while (!isUdpMsgAvailable())
98  {
99  if ((std::chrono::steady_clock::now() - start_time) > timeout)
100  {
101  throw ScannerReadTimeout("Timeout while waiting for new UDP message");
102  }
103  std::this_thread::sleep_for(std::chrono::milliseconds(TIMEOUT_LOOP_SLEEP_DURATION_MS));
104  }
105 
106  std::size_t bytes_read{ 0 };
107  try
108  {
109  bytes_read = socket_read_.receive(buffer);
110  }
111  // LCOV_EXCL_START
112  catch (const boost::system::system_error& ex)
113  {
114  throw ScannerReadFailed(ex.what());
115  }
116  // LCOV_EXCL_STOP
117 
118  return bytes_read;
119 }
120 
121 } // namespace psen_scan
PSENscanUDPInterface(const std::string &scanner_ip, const uint32_t &host_udp_port, const unsigned short scanner_port_write=PSEN_SCAN_PORT_WRITE, const unsigned short scanner_port_read=PSEN_SCAN_PORT_READ)
std::size_t read(boost::asio::mutable_buffers_1 &buffer, const std::chrono::steady_clock::duration timeout) override
Receive data from the scanner.
boost::asio::ip::udp::socket socket_write_
static const uint64_t TIMEOUT_LOOP_SLEEP_DURATION_MS
boost::asio::ip::udp::endpoint udp_write_endpoint_
boost::asio::ip::udp::endpoint udp_read_endpoint_
void close() override
Closes the connection to the scanner device.
boost::asio::ip::udp::socket socket_read_
void open() override
Opens the connection to the scanner device.
void write(const boost::asio::mutable_buffers_1 &buffer) override
Sends data to the scanner device.


psen_scan
Author(s):
autogenerated on Mon Feb 28 2022 23:16:20