文章插图
表示连接扫码器成功,分别扫描车架号和车牌号:
vbar_open success!开始解码:二维码长度:18LFV3A23C083027701二维码长度:10浜琈D0926车架号是正常的,而车牌号果然是乱码 。
找到 Demohidprotocol 源码 (USB接口C-CPP语言SDK20220411\USB接口C&CPP语言SDK20220411\Demohidprotocal\Demohidprotocal\main.cpp):
#include "channel.h"#include <stdio.h>#include <windows.h>#include <string.h>struct vbar_channel *dev;/*背光灯开关控制 state为1时打开补光灯,为0时关闭补光灯*/void lightswitch(int state){ unsigned char buf[1025] = { 0 }; if (state == 1) {buf[0] = 0x55;buf[1] = 0xAA;buf[2] = 0x24;buf[3] = 0x01;buf[4] = 0x00;buf[5] = 0x01;buf[6] = 0xDB;vbar_channel_send(dev, buf, 1024); } else {buf[0] = 0x55;buf[1] = 0xAA;buf[2] = 0x24;buf[3] = 0x01;buf[4] = 0x00;buf[5] = 0x00;buf[6] = 0xDA;vbar_channel_send(dev, buf, 1024); }}/*扫码开关控制 state为1时打开扫码 , 为0时关闭扫码*/void scanswitch(int state){ unsigned char buf[1025] = {0}; if (state == 1) {buf[0] = 0x55;buf[1] = 0xAA;buf[2] = 0x05;buf[3] = 0x01;buf[4] = 0x00;buf[5] = 0x00;buf[6] = 0xfb;vbar_channel_send(dev, buf, 1024); } else {buf[0] = 0x55;buf[1] = 0xAA;buf[2] = 0x05;buf[3] = 0x01;buf[4] = 0x00;buf[5] = 0x01;buf[6] = 0xfa;vbar_channel_send(dev, buf, 1024); }}int main() { dev = vbar_channel_open(1, 1); if (!dev) {printf("open dev fail!\n");return -1; } else {printf("open dev success!\n"); } printf("开始解码:\r\n"); scanswitch(1); //接收扫码 unsigned char bufresult[1024] = {0}; unsigned char bufferrecv_1[1024] = {0}; unsigned char readBuffers[2048] = {0}; while (1) {if (vbar_channel_recv(dev, bufresult, 1024, 200) > 0){if (bufresult[0] == 0x55 && bufresult[1] == 0xAA && bufresult[2] == 0x30){int datalen = bufresult[4] + (bufresult[5] << 8);if (datalen <= 1017){for (int s1 = 0; s1 < datalen; s1++){readBuffers[s1] = bufresult[6 + s1];}}if (1017 < datalen && datalen <= 2041){for (int s1 = 0; s1 < 1018; s1++){readBuffers[s1] = bufresult[6 + s1];}vbar_channel_recv(dev, bufferrecv_1, 1024, 200);for (int s2 = 0; s2 < datalen + 7 - 1025; s2++){readBuffers[s2 + 1018] = bufferrecv_1[s2];}}printf("二维码长度:%d\n", datalen);readBuffers[datalen + 7] = '\0';printf("%.*s\n", datalen, readBuffers);}} }}谜之编码风格,另外这接口设计的也有点凌乱,程序中出现了好多魔数:1017/1018/2041/200/7,看着头大 。所幸读取的数据位于 readBuffers 缓冲中,只要对它做个编码转换就 OK 啦 。
编码转换windows 中文版编码一般是 gb2312,汉字源编码则可能是 utf-8,为了验证这一点,搬出来了 iconv:
$ echo "浜琈D0926" | iconv -f 'utf-8' -t 'cp936'京MD0926看来确实如此,注意这里使用 cp936 而不是 gb2312 作为 iconv 的第二个参数 。如果没有 iconv,也有许多线上的编码转换工具可用:
文章插图
确定了字符集转换方向,直接从网上搜罗来一些现成的实现:
std::wstring utf8_to_unicode(std::string const& utf8){ int need = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, NULL, 0); if (need > 0) { std::wstring unicode; unicode.resize(need); MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, &unicode[0], need); return unicode; } return std::wstring();}std::string unicode_to_gb2312(std::wstring const& unicode){ int need = WideCharToMultiByte(936, 0, unicode.c_str(), -1, NULL, 0, NULL, NULL); if (need > 0) { std::string gb2312; gb2312.resize(need); WideCharToMultiByte(936, 0, unicode.c_str(), -1, &gb2312[0], need, 0, 0); return gb2312; } return std::string();}std::string utf8_to_gb2312(std::string const& utf8){ std::wstring unicode = utf8_to_unicode(utf8); return unicode_to_gb2312(unicode);}
推荐阅读
- 通过openlayers加载dwg格式的CAD图并与互联网地图叠加
- vivo互联网机器学习平台的建设与实践
- Go的网络编程详解
- MES与工业互联网的关联边界在哪里?
- 工业互联网领域的企业,都已经接入了ERP或者MES系统了吗?
- 如何查看腾讯课堂听课记录
- 海南经济特区万物互联通信工程有限公司_工商信用信息_经营范围期限状态_法人_地址_注册资本_怎么样
- 互联网创业平台有哪些 互联网金融创业有哪些方向
- 互联网时代如何找到心仪的工作
- 互联网时代,怎样健康地使用互联网?