Search code examples
c#.netdatagridviewbarcodebarcode-scanner

Scanner scanned value incomplete


I am currently developing a verification system on C#.

I have a datagridview which allow user to scan barcode into it and verify using regex.

Now the problem:

I have a barcode value with RS GS and EOT, so the scanned value will looks different in c#

RS \u001e 
GS \u001d 
EOT \u0004 

When i try to scan it with scanner the value only remain RS while GS and EOT is gone, but i tried to scan it to notepad++ and using copy paste it back to the input field it only works.

C#.net have some difficulty trying to read scanner scanned value?

Sample value:

in notepad ++

enter image description here

in c# string scanned value:

[)>\u001e99888887777766665555444433333\u001e

in c# string pasted value from notepad++:

[)>\u001e99\u001d88888\u001d77777\u001d6666\u001d5555\u001d4444\u001d33333\u001e\u0004

The GS and EOT is missing (the moment i scan it i realise it is missing in the input field)

byte[ ] of scanned value:

[0]: 91
[1]: 41
[2]: 62
[3]: 30
[4]: 57
[5]: 57
[6]: 56
[7]: 56
[8]: 56
[9]: 56
[10]: 56
[11]: 55
[12]: 55
[13]: 55
[14]: 55
[15]: 55
[16]: 54
[17]: 54
[18]: 54
[19]: 54
[20]: 53
[21]: 53
[22]: 53
[23]: 53
[24]: 52
[25]: 52
[26]: 52
[27]: 52
[28]: 51
[29]: 51
[30]: 51
[31]: 51
[32]: 51
[33]: 30

byte[ ] of pasted value from notepad++:

[0]: 91
[1]: 41
[2]: 62
[3]: 30
[4]: 57
[5]: 57
[6]: 29
[7]: 56
[8]: 56
[9]: 56
[10]: 56
[11]: 56
[12]: 29
[13]: 55
[14]: 55
[15]: 55
[16]: 55
[17]: 55
[18]: 29
[19]: 54
[20]: 54
[21]: 54
[22]: 54
[23]: 29
[24]: 53
[25]: 53
[26]: 53
[27]: 53
[28]: 29
[29]: 52
[30]: 52
[31]: 52
[32]: 52
[33]: 29
[34]: 51
[35]: 51
[36]: 51
[37]: 51
[38]: 51
[39]: 30
[40]: 4


Solution

  • I had found myself the solution.

    C#.NET is blocking the value of Control Characters (GS, RS and EOT etc..)

    In order to force it to read the value, all I can do is reading user key press and if the input equals to the control characters. i will then programmically add in the ASCII value of the control characters:

    RS \u001e
    GS \u001d
    EOT \u0004
    

    The code will be like something like this:

    private void txtScanInput_KeyPress(object sender, KeyPressEventArgs e)
    {
        try
        {
            int i = this.txtScanInput.SelectionStart;
            switch ((int)(e.KeyChar))
            {
            case 4: //EOT
                this.txtScanInput.Text = this.txtScanInput.Text.Insert(this.txtScanInput.SelectionStart, "\u0004");
                this.txtScanInput.SelectionStart = i + 6;
                e.Handled = true;
                break;
    
            case 29: //GS
                this.txtScanInput.Text = this.txtScanInput.Text.Insert(this.txtScanInput.SelectionStart, "\u001d");
                this.txtScanInput.SelectionStart = i + 6;
                e.Handled = true;
                break;
    
            case 30: //RS
                this.txtScanInput.Text = this.txtScanInput.Text.Insert(this.txtScanInput.SelectionStart, "\u001e");
                this.txtScanInput.SelectionStart = i + 6;
                e.Handled = true;
                break;
            }
        }
        catch (Exception ex)
        {
            logger.Error(ex);
            MessageBox.Show(MessageConstants.APPLICATION_ERROR + "\n[" + ex.Message + "]\n[" + ex.InnerException + "]\n" + AppUtil.getLatestErrorDAL(),
                MessageConstants.SYSTEM_NAME + " - txtScanInput_KeyPress");
        }
    }