#include #include "Send.h" #include "GlobalDefs.h" using namespace std; Send::Send() { try { running_ = true; std::unordered_map config; configFromIni("./CPCtrlConfig.ini", config); if (config["mode"] == "udp") { isUdp = true; UdpReceiver.reset(new UDPReceiver(stoi(config["recvPort"]))); UdpReceiver->start_receiving(([](const std::vector &data, sockaddr_in addr, socklen_t len) { myDateTime tmp; string log = tmp.toDateTime() + " Recv: " + string(data.begin(), data.end()); cout << log; historyList.push_back(log); })); UdpSender.reset(new UDPSender(UdpReceiver->sockfd_)); UdpSender->set_destination(config["ip"], stoi(config["sendPort"])); cout << "udp link [" + config["recvPort"] + "] sendto \"" + config["ip"] + ":" + config["sendPort"]+"\"" << endl; } else { isUdp = false; if (config["hh"] == "true") { isHH = true; } else { isHH = false; } Serial.reset(new SerialPort(config["device"], stoi(config["baudRate"]))); Serial->start_receiving(([](const std::vector &data) { myDateTime tmp; string log = tmp.toDateTime() + " Recv: " + string(data.begin(), data.end()); cout << log; historyList.push_back(log); })); cout << "serial link [" + config["device"] + "] baudRate:" + config["baudRate"] + " HH:" + config["hh"]<send((uint8_t *) data.data(), data.size()); } else { if (isHH) { char tmp[6] = "\xeb\x90\x07\x00"; uint16_t len = htons(data.size()); memcpy(tmp + 4, &len, 2); string str = string(tmp,tmp+6) + data; return Serial->write(str); } else { return Serial->write(data); } } } size_t Send::sendto(const vector &data) { if (isUdp) { return UdpSender->send((uint8_t *) data.data(), data.size()); } else { if (isHH) { vector tmpStr(data); char tmp[6] = "\xeb\x90\x07\x00"; uint16_t len = htons(data.size()); memcpy(tmp + 4, &len, 2); vector str = vector(tmp,tmp+6); for (auto i: data) { str.push_back(i); } return Serial->write(str); } else { return Serial->write(data); } } } bool Send::isRunning() const { return running_; }