diff --git a/print/MsgHandler.cxx b/print/MsgHandler.cxx index a5a2c70..2dd0199 100644 --- a/print/MsgHandler.cxx +++ b/print/MsgHandler.cxx @@ -4,10 +4,39 @@ #include #include +#include +#include +#include +#include +#include + #include "MsgHandler.hpp" #include "base.h" #include "strcode.h" +// ===== 新增:测试辅助(短写检测 / CRC 指纹 / 节奏可调) ===== +static int dbg_int(const char* k, int def) { + const char* v = getenv(k); + return v ? atoi(v) : def; +} + +static uint32_t crc32_buf(const uint8_t* p, size_t n) { + static uint32_t tbl[256]; + static bool init = false; + if (!init) { + for (uint32_t i = 0; i < 256; i++) { + uint32_t c = i; + for (int kk = 0; kk < 8; kk++) + c = (c & 1) ? (0xEDB88320u ^ (c >> 1)) : (c >> 1); + tbl[i] = c; + } + init = true; + } + uint32_t c = 0xFFFFFFFFu; + for (size_t i = 0; i < n; i++) c = tbl[(c ^ p[i]) & 0xFF] ^ (c >> 8); + return c ^ 0xFFFFFFFFu; +} + MsgHandler::MsgHandler() : fd(-1) {} bool MsgHandler::OpenPort(const std::string& port, const std::string& baudrate) @@ -71,48 +100,72 @@ int MsgHandler::SendMsg(const std::vector& data) return -1; } - // int bytesWritten = write(fd, data.data(), data.size()); - // if (bytesWritten < 0) - // { - // perror("发送数据失败"); - // } - - // return bytesWritten; - - // 大块数据分块发送,防止 57600 波特下打印机 UART FIFO 溢出 - // 阈值选 512:单行/单字段都 < 512 字节,走单次 write; - // 超过阈值(就是 12800 字节的章图栅格)按 256 字节切,段间等 3ms。 - constexpr size_t CHUNK_THRESHOLD = 512; - constexpr size_t CHUNK_SIZE = 256; - constexpr unsigned int INTER_US = 50000; - if (data.size() < CHUNK_THRESHOLD) - { - int bytesWritten = write(fd, data.data(), data.size()); - if (bytesWritten < 0) perror("发送数据失败"); - return bytesWritten; - } - size_t totalWritten = 0; - size_t pos = 0; + // ===== 新增:分块参数运行时可调(不重编译就能扫参数) ===== + const size_t CHUNK_SIZE = (size_t)dbg_int("PW_CHUNK", 256); + const unsigned int INTER_US = (unsigned)dbg_int("PW_DELAY_US", 50000); + const bool USE_TCDRAIN = dbg_int("PW_TCDRAIN", 0) != 0; + + std::cerr << "[Print] SendMsg: 总长=" << data.size() + << " chunk=" << CHUNK_SIZE + << " delay=" << INTER_US << "us" + << " tcdrain=" << USE_TCDRAIN + << " crc32=0x" << std::hex << crc32_buf(data.data(), data.size()) << std::dec + << std::endl; + + size_t totalWritten = 0, pos = 0; + int call = 0, shortWrites = 0; + size_t lostTotal = 0; + while (pos < data.size()) { size_t end = std::min(pos + CHUNK_SIZE, data.size()); - ssize_t n = write(fd, data.data() + pos, end - pos); - if (n < 0) + size_t req = end - pos; + + // ===== 新增:短写循环重试(原来 pos=end 无条件前进,丢字节永远补不回) ===== + size_t done = 0; + while (done < req) { - perror("发送数据失败"); - return -1; + ssize_t n = write(fd, data.data() + pos + done, req - done); + if (n < 0) + { + if (errno == EINTR) continue; // 信号打断,重试 + perror("发送数据失败"); + return -1; + } + if (n == 0) { + std::cerr << "[Print] write 返回 0,串口异常,剩 " + << (req - done) << " 字节未发出" << std::endl; + return -1; + } + if ((size_t)n < req - done) { // ===== 新增:短写告警 ===== + size_t lost = (req - done) - (size_t)n; + shortWrites++; + lostTotal += lost; + std::cerr << "[Print] [短写] 第" << call << "次 请求=" << req + << " 实际=" << n << " 丢=" << lost << std::endl; + } + done += (size_t)n; } - totalWritten += static_cast(n); + totalWritten += done; pos = end; - if (pos < data.size()) usleep(INTER_US); - - + call++; + if (pos < data.size()) + { + if (USE_TCDRAIN) tcdrain(fd); // ===== 新增:精确排空,替代盲等 ===== + else usleep(INTER_US); + } } - return static_cast(totalWritten); + // ===== 新增:发送汇总 ===== + std::cerr << "[Print] SendMsg 汇总: 写出 " << totalWritten << "/" << data.size() + << " 字节, write " << call << " 次, 短写 " << shortWrites + << " 次, 共丢 " << lostTotal << " 字节" << std::endl; + if (totalWritten != data.size()) + std::cerr << "[Print] !! 有 " << (data.size() - totalWritten) + << " 字节没进串口 -> 后续数据错位, 印章必然乱码" << std::endl; - + return static_cast(totalWritten); } int MsgHandler::RecvMsg(std::vector& data, int timeoutMs) @@ -206,9 +259,9 @@ void MsgHandler::HandleDdsMsg(const std::map& msg) EncodeMsg(msg); SendDeviceMsg(m_PrintData); sleep(2); - //SendDeviceMsg(m_PrintData); // 第 2 次 - sleep(4); - //SendDeviceMsg(m_PrintData); // 第 3 次 + SendDeviceMsg(m_PrintData); // 第 2 次 + sleep(2); + SendDeviceMsg(m_PrintData); // 第 3 次 } void MsgHandler::EncodeMsg(const std::map& msg) @@ -990,6 +1043,14 @@ void MsgHandler::Print1_1(const std::map& msg) m_PrintData.insert(m_PrintData.end(), {0x1B, 0x61, 0x02}); // ESC a 2 右对齐 usleep(50 * 1000); + // ===== 新增:只发印章自测模式(跳过文字/表格/走纸) ===== + const bool STAMP_ONLY = dbg_int("PW_STAMP_ONLY", 0) != 0; + if (STAMP_ONLY) { + m_PrintData.clear(); + m_PrintData.insert(m_PrintData.end(), {0x1B, 0x40}); // ESC @ 复位 + std::cerr << "[Print] ==== 只发印章自测模式 ====" << std::endl; + } + // 9. 打印章图片(GS v 0 光栅位图,160x160 → 放大 2 倍 → 320x320) { std::vector stamp_raw; @@ -1007,6 +1068,12 @@ void MsgHandler::Print1_1(const std::map& msg) }; m_PrintData.insert(m_PrintData.end(), hdr, hdr + 8); m_PrintData.insert(m_PrintData.end(), stamp_scaled.begin(), stamp_scaled.end()); + + // ===== 新增:栅格指纹,每次打印都记下,3 张一致则说明生成端稳定 ===== + std::cerr << "[Print] 栅格 " << stamp_scaled.size() << " 字节 (320x320), crc32=0x" + << std::hex << crc32_buf(stamp_scaled.data(), stamp_scaled.size()) << std::dec + << ", m_PrintData=" << m_PrintData.size() << std::endl; + // 不再把栅格塞进 m_PrintData,直接分块写串口,每 256 字节间隔 3ms // SendDeviceMsg(std::vector(hdr, hdr + 8)); // 先单独发 GS v 0 头 // usleep(50 * 1000); // 等打印机解析完头 @@ -1020,9 +1087,11 @@ void MsgHandler::Print1_1(const std::map& msg) } usleep(100 * 1000); - // 10. 正向走纸 7 行 - m_PrintData.insert(m_PrintData.end(), {0x1B, 0x64, 0x7}); // ESC d 17 - usleep(100 * 1000); + // 10. 正向走纸 7 行(自测模式不连续走纸,省纸) + if (!STAMP_ONLY) { + m_PrintData.insert(m_PrintData.end(), {0x1B, 0x64, 0x7}); // ESC d 7 + usleep(100 * 1000); + } // 11. 全切 m_PrintData.insert(m_PrintData.end(), {0x1B, 0x69}); diff --git a/print/MsgHandler.hpp b/print/MsgHandler.hpp index 3232f76..7c50d3e 100644 --- a/print/MsgHandler.hpp +++ b/print/MsgHandler.hpp @@ -1,5 +1,5 @@ -#ifndef _MSGHANDLER_HPP_ -#define _MSGHANDLER_HPP_ +#ifndef _PRINT_MSGHANDLER_HPP_ +#define _PRINT_MSGHANDLER_HPP_ #include #include diff --git a/print/main.cxx b/print/main.cxx index 2073f2d..9560ac1 100644 --- a/print/main.cxx +++ b/print/main.cxx @@ -19,6 +19,13 @@ * This file was generated by the tool fastddsgen. */ +// ===== 新增:测试辅助(与 MsgHandler.cxx 中的同名函数等价,main.cxx 也需要) ===== +#include +static int dbg_int(const char* k, int def) { + const char* v = getenv(k); + return v ? atoi(v) : def; +} + #include #include #include @@ -26,6 +33,15 @@ #include #include +#include +#include +#include +#include +#include +#include +#include +#include + #include #include "Subscriber.hpp" @@ -142,6 +158,100 @@ int main(int argc, char** argv) dev->OpenPort(port, baudrate); dev->SetDevice(device); + // ===== 新增:串口回环自检(必须先停服务并将 ttyS2 的 TX-RX 短接) ===== + if (getenv("PW_LOOPBACK")) + { + // 复用主流程的 port/baudrate/device + auto lt = std::make_shared(); + lt->OpenPort(port, baudrate); + lt->SetDevice(device); + + std::vector 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 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(); + 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 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.");