2

我不能在二维码中打印超过 380 个字符。

遵循 C# 中的代码:

    protected ASCIIEncoding m_encoding = new ASCIIEncoding();
string QRdata = @"35150909165024000175590000193130072726117830|20150924062259|50.00||hdMEPiER6rjZKyKA+4+voi1nncxsAGFbYsEEqnh04SbvUEI/haUF4GUBPxT6Q2Uhf9f8QYgxiwxWo3GxRrvj4WnNeTYgAqUAYmOANPItNkOw0CppmZ4R8i1ZOlnftVhksCM0zrl4RiKgoazbN44hUu2nQf0W/JLvFXzXu12JlcSThNtmyJ6m9WBsMc/sf9BE14HDoXMyKRIQYt5TkEjilHH9Ffa0saRyUIp+Fji89/Moq8YCCFC+qC44XGxsvNCeeHUNOc1LgPP0DbU1miwpVnrBlEl87RU8Iy0r8fN/fNhbcStkwfTEvhYvZz42nEKHrmGTpGZYkHuTFCNZPq7aCA==";
        int store_len = QRdata.Length + 3;
                        byte store_pL = (byte)(store_len % 256);
                        byte store_pH = (byte)(store_len / 256);


                         string txt = m_encoding.GetString(new byte[] { 29, 40, 107, store_pL, store_pH, 49, 80, 48 }); //FUNCTION 180
            txt += QRdata;
            txt += m_encoding.GetString(new byte[] { 29, 40, 107, 3, 0, 49, 69, 48 });//FUNCTION 169
            txt += m_encoding.GetString(new byte[] { 29, 40, 107, 3, 0, 49, 67, 5 });//FUNCTION 167
            txt += m_encoding.GetString(new byte[] { 29, 40, 107, 4, 0, 49, 65, 50, 0 });//FUNCTION 165
            txt += m_encoding.GetString(new byte[] { 29, 40, 107, 3, 0, 49, 81, 48 });//FUNCTION 181

尝试打印时出现如下:

打印

4

1 回答 1

5

ASCII 是个问题,因为它是 7 位编码,但store_PL值大于 127(占用 8 位)。以下是正在发生的事情的演示:

ASCIIEncoding m_encoding = new ASCIIEncoding();
string QRdata = @"35150909165024000175590000193130072726117830|20150924062259|50.00||hdMEPiER6rjZKyKA+4+voi1nncxsAGFbYsEEqnh04SbvUEI/haUF4GUBPxT6Q2Uhf9f8QYgxiwxWo3GxRrvj4WnNeTYgAqUAYmOANPItNkOw0CppmZ4R8i1ZOlnftVhksCM0zrl4RiKgoazbN44hUu2nQf0W/JLvFXzXu12JlcSThNtmyJ6m9WBsMc/sf9BE14HDoXMyKRIQYt5TkEjilHH9Ffa0saRyUIp+Fji89/Moq8YCCFC+qC44XGxsvNCeeHUNOc1LgPP0DbU1miwpVnrBlEl87RU8Iy0r8fN/fNhbcStkwfTEvhYvZz42nEKHrmGTpGZYkHuTFCNZPq7aCA==";
int store_len = QRdata.Length + 3; // 414
byte store_pL = (byte)(store_len % 256); // 158
byte store_pH = (byte)(store_len / 256); // 1
byte[] data = new byte[] { 29, 40, 107, store_pL, store_pH, 49, 80, 48 }; //FUNCTION 180
string txt = m_encoding.GetString(data);
byte[] invalidData = m_encoding.GetBytes(txt);

数据的原始值(预期值):

1d 28 6b 9e 01 31 50 30

串口接收到的实际数据(由于1587 位 ASCII 编码失败):

1d 28 6b 3f 01 31 50 30

如您所见,该值158 (0x9e)更改为63 (0x3f),因为未知符号被编码为?

因此,该问题有两种解决方案。一种是使用Encoding m_encoding = Encoding.GetEncoding("iso-8859-1");编码,或任何其他扩展的 ASCII 编码,但它应该在您在代码中使用的字节编码和串口设置之间同步。另一种解决方案是根本不使用字符串,而是使用字节数组。

于 2015-09-26T19:52:47.463 回答