I can not print more than 380 characters in a qr code .
Follows the code in 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
When trying to print appears as follows:
ASCII is a problem, since it is 7-bit encoding, but store_PL
value is greater than 127 (takes 8 bits). The following is demonstration of whats going on:
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);
The original value of data (expected one):
1d 28 6b 9e 01 31 50 30
The actual data the serial port receives (due to failure to encode 158
value in 7-bit ASCII):
1d 28 6b 3f 01 31 50 30
As you can see, the value 158 (0x9e)
is changed to 63 (0x3f)
, since unknown symbol was encoded as ?
.
So, there are 2 solutions to the problem. One is to use Encoding m_encoding = Encoding.GetEncoding("iso-8859-1");
encoding, or any other extended ASCII encoding, but it should be synchronized between byte encoding you use in your code and settings of serial port. And another solution is not to use strings at all, but use byte arrays.