【问题标题】:Scanner scanned value incomplete扫描仪扫描值不完整
【发布时间】:2019-03-14 04:35:16
【问题描述】:

我目前正在开发一个基于 C# 的验证系统。

我有一个数据网格视图,允许用户将条形码扫描到其中并使用正则表达式进行验证。

现在的问题:

我有一个带有 RS GS 和 EOT 的条形码值,所以扫描的值在 c# 中看起来会有所不同

RS \u001e 
GS \u001d 
EOT \u0004 

当我尝试使用扫描仪扫描它时,值仅保留 RS,而 GS 和 EOT 消失了,但我尝试将其扫描到记事本 ++ 并使用将其复制粘贴回输入字段,它只能工作。

C#.net 在尝试读取扫描仪扫描值时遇到一些困难?

样本值:

在记事本++中

在c#字符串扫描值:

[)>\u001e99888887777766665555444433333\u001e

在记事本++中的c#字符串中粘贴值:

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

GS 和 EOT 丢失(我扫描它的那一刻我意识到它在输入字段中丢失)

字节[]的扫描值:

[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

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

【问题讨论】:

  • 你能在 C# 中将你的字符串转换为 byte[] 并告诉我们其中的内容吗?
  • 更新你的问题,不要发表评论。
  • @Enigmativity done 添加关于问题的字节[]信息
  • 你能发一个minimal reproducible example吗?你说 C# 没有达到你的预期,但我们没有一行代码可以查看。

标签: c# .net datagridview barcode barcode-scanner


【解决方案1】:

我自己找到了解决办法。

C#.NET 正在阻止控制字符(GS、RS 和 EOT 等)的值。

为了强制它读取值,我所能做的就是读取用户按键以及输入是否等于控制字符。然后我将以编程方式添加控制字符的 ASCII 值:

RS \u001e
GS \u001d
EOT \u0004

代码会是这样的:

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");
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多