|
|
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
|
|
|
//
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
// You may obtain a copy of the License at
|
|
|
//
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
//
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
// See the License for the specific language governing permissions and
|
|
|
// limitations under the License.
|
|
|
|
|
|
/*!
|
|
|
* @file main.cxx
|
|
|
* This file acts as a main entry point to the application.
|
|
|
*
|
|
|
* This file was generated by the tool fastddsgen.
|
|
|
*/
|
|
|
|
|
|
// ===== 新增:测试辅助(与 MsgHandler.cxx 中的同名函数等价,main.cxx 也需要) =====
|
|
|
#include <cstdlib>
|
|
|
static int dbg_int(const char* k, int def) {
|
|
|
const char* v = getenv(k);
|
|
|
return v ? atoi(v) : def;
|
|
|
}
|
|
|
|
|
|
#include <csignal>
|
|
|
#include <cstring>
|
|
|
#include <functional>
|
|
|
#include <iostream>
|
|
|
#include <stdexcept>
|
|
|
#include <thread>
|
|
|
|
|
|
#include <cstdio>
|
|
|
#include <cstdlib>
|
|
|
#include <cstdint>
|
|
|
#include <map>
|
|
|
#include <memory>
|
|
|
#include <termios.h>
|
|
|
#include <unistd.h>
|
|
|
#include <vector>
|
|
|
|
|
|
#include <fastdds/dds/log/Log.hpp>
|
|
|
|
|
|
#include "Subscriber.hpp"
|
|
|
#include "Publisher.hpp"
|
|
|
#include "SerialMsgHandler.hpp"
|
|
|
#include "msg.hpp"
|
|
|
#include "DEBUG.hpp"
|
|
|
|
|
|
using eprosima::fastdds::dds::Log;
|
|
|
|
|
|
std::function<void(int)> stop_handler;
|
|
|
void signal_handler(
|
|
|
int signum)
|
|
|
{
|
|
|
stop_handler(signum);
|
|
|
}
|
|
|
|
|
|
std::string parse_signal(
|
|
|
const int& signum)
|
|
|
{
|
|
|
switch (signum)
|
|
|
{
|
|
|
case SIGINT:
|
|
|
return "SIGINT";
|
|
|
case SIGTERM:
|
|
|
return "SIGTERM";
|
|
|
#ifndef _WIN32
|
|
|
case SIGQUIT:
|
|
|
return "SIGQUIT";
|
|
|
case SIGHUP:
|
|
|
return "SIGHUP";
|
|
|
#endif // _WIN32
|
|
|
default:
|
|
|
return "UNKNOWN SIGNAL";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
std::queue<WeighingSystem::PrintReq> MsgData::PrintReq_queue_;
|
|
|
std::mutex MsgData::queue_cv_mtx_;
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
{
|
|
|
auto ret = EXIT_SUCCESS;
|
|
|
std::shared_ptr<SubscriberApp> sub;
|
|
|
std::shared_ptr<PublisherApp> pub;
|
|
|
std::shared_ptr<MsgHandler> dev;
|
|
|
|
|
|
int domain_id = 0;
|
|
|
const char* interface = "serial";
|
|
|
const char* port = "ttyS1";
|
|
|
const char* baudrate = "9600";
|
|
|
const char* device = "NP80A";
|
|
|
|
|
|
for (int i = 1; i < argc; i++)
|
|
|
{
|
|
|
if (strcmp(argv[i], "--domain") == 0 && i + 1 < argc)
|
|
|
{
|
|
|
domain_id = atoi(argv[++i]);
|
|
|
}
|
|
|
else if (strcmp(argv[i], "--interface") == 0 && i + 1 < argc)
|
|
|
{
|
|
|
interface = argv[++i];
|
|
|
}
|
|
|
else if (strcmp(argv[i], "--port") == 0 && i + 1 < argc)
|
|
|
{
|
|
|
port = argv[++i];
|
|
|
}
|
|
|
else if (strcmp(argv[i], "--baudrate") == 0 && i + 1 < argc)
|
|
|
{
|
|
|
baudrate = argv[++i];
|
|
|
}
|
|
|
else if (strcmp(argv[i], "--device") == 0 && i + 1 < argc)
|
|
|
{
|
|
|
device = argv[++i];
|
|
|
}
|
|
|
else if (strcmp(argv[i], "--version") == 0)
|
|
|
{
|
|
|
std::cout << "Vesrion: " << PROGRAM_VERSION << "\n";
|
|
|
return EXIT_SUCCESS;
|
|
|
}
|
|
|
else if (strcmp(argv[i], "--help") == 0)
|
|
|
{
|
|
|
std::cout << "Usage: [options]\n"
|
|
|
<< "Options:\n"
|
|
|
<< " --domain Set domain ID\n"
|
|
|
<< " --interface Set interface (e.g., serial)\n"
|
|
|
<< " --baudrate Set baudrate (e.g., 9600)\n"
|
|
|
<< " --port Set port name (e.g., ttyS1)\n"
|
|
|
<< " --device Set device name (e.g., NP80A)\n"
|
|
|
<< " --version Show software Version\n"
|
|
|
<< " --help Show this help message\n";
|
|
|
return EXIT_SUCCESS;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
std::cerr << "Unknown option: " << argv[i] << "\n";
|
|
|
std::cout << "Usage: [options]\n"
|
|
|
<< "Options:\n"
|
|
|
<< " --domain Set domain ID\n"
|
|
|
<< " --interface Set interface (e.g., serial)\n"
|
|
|
<< " --baudrate Set baudrate (e.g., 9600)\n"
|
|
|
<< " --port Set port name (e.g., ttyS1)\n"
|
|
|
<< " --device Set device name (e.g., NP80A)\n"
|
|
|
<< " --version Show software Version\n"
|
|
|
<< " --help Show this help message\n";
|
|
|
return EXIT_FAILURE;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
sub = std::make_shared<SubscriberApp>(domain_id);
|
|
|
pub = std::make_shared<PublisherApp>(domain_id);
|
|
|
dev = std::make_shared<SerialMsgHandler>();
|
|
|
|
|
|
dev->OpenPort(port, baudrate);
|
|
|
dev->SetDevice(device);
|
|
|
|
|
|
// ===== 新增:串口回环自检(必须先停服务并将 ttyS2 的 TX-RX 短接) =====
|
|
|
if (getenv("PW_LOOPBACK"))
|
|
|
{
|
|
|
// 复用主流程的 port/baudrate/device
|
|
|
auto lt = std::make_shared<SerialMsgHandler>();
|
|
|
lt->OpenPort(port, baudrate);
|
|
|
lt->SetDevice(device);
|
|
|
|
|
|
std::vector<uint8_t> tx;
|
|
|
for (int i = 0; i < 256; i++) tx.push_back((uint8_t)(i ^ 0xA5));
|
|
|
tx.push_back(0xFF); tx.push_back(0x00); tx.push_back(0x55);
|
|
|
|
|
|
int iterations = dbg_int("PW_LOOPBACK_N", 200);
|
|
|
int mismatch = 0;
|
|
|
for (int it = 0; it < iterations; it++)
|
|
|
{
|
|
|
tcflush(lt->fd, TCIOFLUSH);
|
|
|
ssize_t w = write(lt->fd, tx.data(), tx.size());
|
|
|
if (w != (ssize_t)tx.size()) { mismatch++; continue; }
|
|
|
std::vector<uint8_t> rx;
|
|
|
for (int tries = 0; tries < 60 && (int)rx.size() < (int)tx.size(); tries++) {
|
|
|
uint8_t buf[64];
|
|
|
ssize_t n = read(lt->fd, buf, sizeof(buf));
|
|
|
if (n > 0) rx.insert(rx.end(), buf, buf + n);
|
|
|
else usleep(1000);
|
|
|
}
|
|
|
if (rx != tx) {
|
|
|
mismatch++;
|
|
|
if (mismatch <= 3) {
|
|
|
size_t lim = std::min(rx.size(), tx.size());
|
|
|
for (size_t k = 0; k < lim; k++) {
|
|
|
if (rx[k] != tx[k]) {
|
|
|
std::cerr << "[回环] 第" << it << "次不一致 offset=" << k
|
|
|
<< " 发出=0x" << std::hex << (int)tx[k]
|
|
|
<< " 收到=0x" << (int)rx[k] << std::dec << std::endl;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
lt->ClosePort();
|
|
|
std::cerr << "[回环] " << iterations << " 次测试, 不一致 " << mismatch << " 次" << std::endl;
|
|
|
std::cerr << (mismatch == 0
|
|
|
? "[回环] >> 物理层完好,问题在打印机侧(固件/缓冲上限)"
|
|
|
: "[回环] >> 物理层丢数据,检查接线/干扰/地线") << std::endl;
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
// ===== 新增:本地自测模式(不创建 DDS,直接打印 N 张完整单子) =====
|
|
|
if (getenv("PW_SELFTEST"))
|
|
|
{
|
|
|
const int times = dbg_int("PW_SELFTEST_N", 3);
|
|
|
|
|
|
auto st = std::make_shared<SerialMsgHandler>();
|
|
|
st->OpenPort(port, baudrate);
|
|
|
st->SetDevice(device);
|
|
|
|
|
|
std::cerr << "[Print] ==== 本地自测模式: 打印 " << times
|
|
|
<< " 张 绥阳铁合金 完整单子 ====" << std::endl;
|
|
|
|
|
|
for (int i = 0; i < times; i++)
|
|
|
{
|
|
|
char coal[64], datetimes[64];
|
|
|
snprintf(coal, sizeof(coal), "Q20261008-%03d", i + 1);
|
|
|
snprintf(datetimes, sizeof(datetimes), "2026-10-08 %02d:%02d", 9 + i, i * 10);
|
|
|
|
|
|
std::map<std::string, std::string> test_msg = {
|
|
|
{"Title", "贵州渝黔锰业有限公司提货单"}, // EncodeMsg 门控要的
|
|
|
{"CoalNumber", coal},
|
|
|
{"GoodsType", "锰矿石"},
|
|
|
{"OrderNumber", "DD20261008-001"},
|
|
|
{"CarNumber", "贵A12345"},
|
|
|
{"Supplier", "绥阳铁合金厂"},
|
|
|
{"Customer", "贵州渝黔锰业有限公司"},
|
|
|
{"Transporter", "绥阳运输公司"},
|
|
|
{"GrossWeight", "38.50"},
|
|
|
{"GrossTime", datetimes},
|
|
|
{"TareWeight", "12.30"},
|
|
|
{"TareTime", datetimes},
|
|
|
{"SuttleWeight", "26.20"},
|
|
|
};
|
|
|
|
|
|
std::cerr << "[Print] ---- 第 " << i + 1 << "/" << times
|
|
|
<< " 张 (" << coal << ") ----" << std::endl;
|
|
|
st->HandleDdsMsg(test_msg); // 走真实路径,含 sleep(2)+sleep(4)=6s
|
|
|
if (i + 1 < times) sleep(0); // 等上一张(含 320x320 章)完整走纸
|
|
|
}
|
|
|
|
|
|
st->ClosePort();
|
|
|
std::cerr << "[Print] ==== 自测结束 ====" << std::endl;
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
std::thread pub_thread(&PublisherApp::run, pub, dev);
|
|
|
|
|
|
DEBUG("Program is running. Please press Ctrl+C to stop at any time.");
|
|
|
|
|
|
stop_handler = [&](int signum)
|
|
|
{
|
|
|
std::cout << "\n" << parse_signal(signum) << " received, stopping " << argv[1]
|
|
|
<< " execution." << std::endl;
|
|
|
pub->stop();
|
|
|
};
|
|
|
|
|
|
signal(SIGINT, signal_handler);
|
|
|
signal(SIGTERM, signal_handler);
|
|
|
#ifndef _WIN32
|
|
|
signal(SIGQUIT, signal_handler);
|
|
|
signal(SIGHUP, signal_handler);
|
|
|
#endif // _WIN32
|
|
|
|
|
|
pub_thread.join();
|
|
|
|
|
|
Log::Reset();
|
|
|
return ret;
|
|
|
}
|