【发布时间】:2018-07-22 21:48:19
【问题描述】:
我必须向 USB 设备发送十六进制命令。
命令是:
echo -en "\x1b\x70\x00\x19\xfa" > /dev/usb/lp0
如果我在终端上写它,它就可以工作。但是,在 C++ 中,十六进制命令存在问题。 \x00 被检测为以 null 结尾的字符串。
我尝试了两种方法:
std::string cmd = "echo '\\x1b\\x70\\x00\\x19\\xfa' > /dev/usb/lp0";
std::system(cmd.c_str());
什么都没有发生。
还有:
std::ofstream device;
device.open("/dev/usb/lp0");
device << "\x1b\x70\x00\x19\xfa";
device.close();
设备什么都不做。
如何解决这个问题并使用\x00 字符串?
【问题讨论】:
-
别忘了你需要在字符串文字中转义反斜杠。