2

如何在不使用任何焦点的情况下在特定文本框中显示条形码。如果我使用条形码阅读器读取数据,数据应显示在特定的文本框中。

4

2 回答 2

0

您可以将 Serial Port 类与 DataReceive 事件一起使用。收到数据时,将该数据填充到文本框中。

SerialPort mySerialPort = new SerialPort("COM1"); //give your barcode serial port 
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort.Open();

private void DataReceivedHandler(object sender,SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();
        txtBoxName.Text = indata;
    }
于 2013-10-31T08:13:53.233 回答
0

我会添加一个 javascript 函数来监听 window.keypress 事件并识别条形码序列,如下所示:

<html>
<body>
<script type="text/javascript">
    document.currentBarcodeSequence = "";
    document.lastKeypress = new Date();
    var monitorBarcodes = function(e){
        //sequenceLimitMs should be set as low as possible to prevent capture of human keyed numbers.
        //200 allow testing without a barcode scanner, you could try a value of 50 with a scanner.
        var sequenceLimitMs = 200;
        var now = new Date();
        var elapsed = now - document.lastKeypress;
        document.lastKeypress = now;
        if(e.charCode >= 48 && e.charCode <= 57){
            //pressed key is a number
            if(elapsed < sequenceLimitMs || document.currentBarcodeSequence === ""){
                //event is part of a barcode sequence
                document.currentBarcodeSequence += (e.charCode - 48);

                if(document.currentBarcodeSequence.length > 1){
                    clearTimeout(document.printBarcodeTimeout);
                    document.printBarcodeTimeout = setTimeout("setBarcode()", sequenceLimitMs+10);
                }
            } else {
                document.currentBarcodeSequence = "" + (e.charCode - 48);
                clearTimeout(document.printBarcodeTimeout);
            }
        } else {
            document.currentBarcodeSequence = "";
            clearTimeout(document.printBarcodeTimeout);
        }
    }
    var setBarcode = function(){
        var barcodeInput = document.getElementById("barcode");
        barcodeInput.value = document.currentBarcodeSequence;
    }

    window.onkeypress = monitorBarcodes;
</script>
<input type="text" id="barcode"></input>
<input type="text"></input>
</body>
</html>

在 Firefox、IE10、Chrome 和 Opera 上测试。

编辑:脚本的工作假设扫描仪只是像键盘一样发送一个键序列。

于 2013-10-31T08:35:56.453 回答