我正在开发一个嵌入式系统(STM32F4),我试图将一些数据发送到PC端的一个简单的Windows客户端程序。当我使用基于字符的字符串格式时,一切正常,但是当我更改为二进制包以提高性能时,我遇到了Escape字符的问题。
我使用纳米粒子来实现Google协议缓冲区的传输,我观察到,在5%的包中,我在客户程序中收到异常,告诉我我的包被破坏了。
我在WireShark中进行了调试,发现在这个损坏的包中,大小比原来的包小2-4字节。经过进一步检查,我发现损坏的包总是包含二进制值27,而其他包从未包含此值。我搜索它,发现这个值表示转义字符,这可能会导致问题。
我正在使用的Wi模块的技术文档(Gainspan GSM2100)提到命令前面有转义字符,所以我认为我需要去掉包中的这个值。
我无法找到解决我的问题的办法,所以如果有更有经验的人能引导我找到解决这个问题的正确方法,我会很感激。
发布于 2016-05-02 10:05:41
你是怎么发送数据的?您是在使用库还是发送原始字节?根据手册,您的数据命令应该以转义序列开始,但也应该指定数据长度:
// Each escape sequence starts with the ASCII character 27 (0x1B),
// the equivalent to the ESC key. The contents of < > are a byte or byte stream.
// - Cid is connection id (udp, tcp, etc)
// - Data Length is 4 ASCII char represents decimal value
// i.e. 1400 bytes would be '1' '4' '0' '0' (0x31 0x34 0x30 0x30).
// - Data size must match with specified length.
// Ignore all command or esc sequence in between data pay load.
<Esc>Z<Cid><Data Length xxxx 4 ascii char><data>注意有关数据大小的注意事项:“忽略数据负载之间的所有命令或esc序列”。
例如,GSCore.cpp中的写数据函数是这样的:
// Including a trailing 0 that snprintf insists to write
uint8_t header[8];
// Prepare header: <esc> Z <cid> <ascii length>
snprintf((char*)header, sizeof(header), "\x1bZ%x%04d", cid, len);
// First, write the escape sequence up to the cid. After this, the
// module responds with <ESC>O or <ESC>F.
writeRaw(header, 3);
if (!readDataResponse()) {
if (GS_LOG_ERRORS && this->error)
this->error->println("Sending bulk data frame failed");
return false;
}
// Then, write the rest of the escape sequence (-1 to not write the
// trailing 0)
writeRaw(header + 3, sizeof(header) - 1 - 3);+
// And write the actual data
writeRaw(buf, len);这很有可能奏效。或者,一个肮脏的黑客可能是在发送之前“转义字符”,即在发送之前用两个字符(0x270x27)替换每个0x27,但这只是一个猜测,我假设您应该检查手册。
https://stackoverflow.com/questions/36979543
复制相似问题