Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#define ASIO_STANDALONE
#include <iostream>
#include <asio.hpp>
//16進数アスキーを数値に変換
char hex2char(char h)
{
if (h >= '0' && h <= '9')
return h - '0';
else if (h >= 'A' && h <= 'F')
return h - 'A' + 10;
else if (h >= 'a' && h <= 'f')
return h - 'a' + 10;
return 0;
}
//0から15までの数値をアスキーに変換
char char2hex(char h)
{
if (h >= 0 && h <= 9)
return h + '0';
else if (h >= 10 && h <= 15)
return h - 10 + 'A';
return 0;
}
int main()
{
try
{
// 送信コマンド
std::string cmd = "500000FFFF03000C001000010400002C0100A80300";
std::cout << "send: " << cmd << std::endl;
// 送信コマンドをバイナリデータへ変換
std::array<unsigned char, 128> send_buf;
int cnt;
for (size_t i = 0; i < cmd.size(); i += 2)
{
cnt = i / 2;
send_buf[cnt] = (hex2char(cmd[i]) << 4) + hex2char(cmd[i + 1]);
;
}
int send_buf_len = cnt + 1;
// UDP送信
std::string const host = "192.168.0.10";
short const port = 1025;
asio::io_context context;
asio::ip::udp::endpoint endpoint(
asio::ip::address::from_string(host), port);
asio::ip::udp::socket socket(context);
socket.open(asio::ip::udp::v4());
socket.send_to(asio::buffer(send_buf, send_buf_len), endpoint);
// UDP受信
std::array<unsigned char, 128> recv_buf;
asio::ip::udp::endpoint sender_endpoint;
size_t recv_buf_len = socket.receive_from(
asio::buffer(recv_buf), sender_endpoint);
// 受信バイナリデータを文字列に変換
std::string result = "";
for (size_t i = 0; i < recv_buf_len; i++)
{
result += char2hex((recv_buf[i] >> 4) & 0xf);
result += char2hex(recv_buf[i] & 0xf);
}
std::cout << "rev : " << result << std::endl;
}
catch (const std::exception &e)
{
std::cerr << "exception: " << e.what() << std::endl;
}
return 0;
}
#define ASIO_STANDALONE
#include <iostream>
#include <asio.hpp>
//16進数アスキーを数値に変換
char hex2char(char h)
{
if (h >= '0' && h <= '9')
return h - '0';
else if (h >= 'A' && h <= 'F')
return h - 'A' + 10;
else if (h >= 'a' && h <= 'f')
return h - 'a' + 10;
return 0;
}
//0から15までの数値をアスキーに変換
char char2hex(char h)
{
if (h >= 0 && h <= 9)
return h + '0';
else if (h >= 10 && h <= 15)
return h - 10 + 'A';
return 0;
}
int main()
{
try
{
// 送信コマンド
std::string cmd = "500000FFFF03000C001000010400002C0100A80300";
std::cout << "send: " << cmd << std::endl;
// 送信コマンドをバイナリデータへ変換
std::array<unsigned char, 128> send_buf;
int cnt;
for (size_t i = 0; i < cmd.size(); i += 2)
{
cnt = i / 2;
send_buf[cnt] = (hex2char(cmd[i]) << 4) + hex2char(cmd[i + 1]);
;
}
int send_buf_len = cnt + 1;
// TCP送信
std::string const host = "192.168.0.10";
short const port = 1026;
asio::io_context context;
asio::ip::tcp::endpoint endpoint(asio::ip::address::from_string(host), port);
asio::ip::tcp::socket socket(context);
asio::connect(socket,&endpoint);
socket.write_some(asio::buffer(send_buf, send_buf_len));
// TCP受信
std::array<unsigned char, 128> recv_buf;
asio::ip::udp::endpoint sender_endpoint;
size_t recv_buf_len = socket.read_some(
asio::buffer(recv_buf,recv_buf.size()));
// 受信バイナリデータを文字列に変換
std::string result = "";
for (size_t i = 0; i < recv_buf_len; i++)
{
result += char2hex((recv_buf[i] >> 4) & 0xf);
result += char2hex(recv_buf[i] & 0xf);
}
std::cout << "rev : " << result << std::endl;
}
catch (const std::exception &e)
{
std::cerr << "exception: " << e.what() << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment