手持机如何连续按住扫描键扫描?

1. 定义线程变量和委托方法(供子线程反调主线程使用)

        private Thread scanThread;
        private delegate void invokeSetScanedData(string data);
        private delegate void invokeSetScanedData2(barCodeInfoBean bean);


2.  扫描键按下去方法:

     private void ClInTaskDetail_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 238)
            {
                //开始线程 连续扫描条码
                if (scanThread == null)
                {
                    scanThread = new Thread(new ThreadStart(ScanBarCode));
                    scanThread.IsBackground = true;
                    scanThread.Start();
                }
            }
        }


      private void ScanBarCode() {
          while (1>0)
           { 
            string strBarcode = string.Empty;
            if (CLR_Barcode_1D.Scan(out strBarcode))
            {
                CommonHelper.PlaySound();
                ScanedBarcodeType barcodeType = CommonHelper.GetScanedBarcodeType(strBarcode);
                switch (barcodeType)
                {
                    case ScanedBarcodeType.MaterialCode:
                        if (this.txtErpNo.InvokeRequired) {
                            Invoke(new invokeSetScanedData(SetMaterialScanedData), new Object[] { strBarcode.Trim() });                        
                        }
                        //this.txtErpNo.Text = strBarcode.Trim();
                        //this.txtErpNo.Focus();
                        break;
                    case ScanedBarcodeType.BoxCode:
                        if (this.txtBoxID.InvokeRequired)
                        {
                            Invoke(new invokeSetScanedData(SetBoxScanedData), new Object[] { strBarcode.Trim() });
                        }




                        //this.txtBoxID.Text = strBarcode.Trim();
                        //this.txtBoxID.Focus();
                        break;
                    case ScanedBarcodeType.BatchCode:
                        if (this.txtBatchID.InvokeRequired)
                        {
                            Invoke(new invokeSetScanedData(SetBatchScanedData), new Object[] { strBarcode.Trim() });
                        }
                        //this.txtBatchID.Text = strBarcode.Trim();
                        //this.txtBatchID.Focus();
                        //this.GetLeftCnt(txtErpNo.Text.Trim(), txtBatchID.Text.Trim()); //20160115 fix 带不出数量问题
                        break;
                    case ScanedBarcodeType.BarcodeBinding:


                        //barCodeInfoBean bean = new BarcodeBindingBLL().GetBarcodeBindingInfo(strBarcode.Trim());
                        //if (!string.IsNullOrEmpty(bean.erpno) && !string.IsNullOrEmpty(bean.batchId))
                        //{
                        //    //this.txtErpNo.Text = bean.erpno;
                        //    //this.txtBatchID.Text = bean.batchId;
                        //    //this.txtBoxID.Focus();
                        //    if (this.txtErpNo.InvokeRequired && this.txtBatchID.InvokeRequired) {
                        //        Invoke(new invokeSetScanedData2(SetBarcodeBindingScanedData), new Object[] { bean });                            
                        //    }
                        //}
                        //else
                        //{
                        //    this.scanThread.Abort();
                        //    this.scanThread = null;  
                        //    MessageBox.Show("您扫描的条码下面没有物料和批次信息,请检查!", "警告");                             
                        //}


                        if (this.txtErpNo.InvokeRequired && this.txtBatchID.InvokeRequired) {
                            Invoke(new invokeSetScanedData(SetBarcodeBindingScanedData2), new Object[] { strBarcode.Trim() });       
                        }






                        break;


                    default:
                        this.scanThread.Abort();
                        this.scanThread = null;  
                        MessageBox.Show("条码长度不符合条件,请检查!", "警告");                       
                        break;


                }
              }


              Thread.Sleep(300);


            }
        
        
        
        }



3. 扫描按键抬起,则中断连续扫描线程

    private void ClInTaskDetail_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 238)
            {
                //扫描黄键抬起,则结束连续扫描
                this.scanThread.Abort();
                this.scanThread = null;              
            }

}



4. 如何在连续扫描线程中调用主线程的方法:

    4.1 先用this.Control.InvokeRequired判断

    4.2 再用Invoke调用委托方法,记住WebSerive也必须写在委托方法里面去。

    

    private void SetMaterialScanedData(string data)
        {
            this.txtErpNo.Text = data;
            this.txtErpNo.Focus();
        }


        private void SetBatchScanedData(string data)
        {
            this.txtBatchID.Text = data;
            this.txtBatchID.Focus();
            this.GetLeftCnt(txtErpNo.Text.Trim(), txtBatchID.Text.Trim()); //20160115 fix 带不出数量问题
        }


        private void SetBoxScanedData(string data)
        {
            this.txtBoxID.Text = data;
            this.txtBoxID.Focus();
        }


        private void SetBarcodeBindingScanedData(barCodeInfoBean bean)
        {
            this.txtErpNo.Text = bean.erpno;
            this.txtBatchID.Text = bean.batchId;
            this.txtBoxID.Focus();
        }




        private void SetBarcodeBindingScanedData2(string data)
        {
            //注意:连续扫描开了新线程,如果该线程中还想调用WebService去获得条码绑定信息,就用把调用WebService的代码也放入Invoke中
            barCodeInfoBean bean = new BarcodeBindingBLL().GetBarcodeBindingInfo(data);
            if (!string.IsNullOrEmpty(bean.erpno) && !string.IsNullOrEmpty(bean.batchId))
            {
                this.txtErpNo.Text = bean.erpno;
                this.txtBatchID.Text = bean.batchId;
                this.txtBoxID.Focus();
            }
        }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android设备上调用C72手持机的红外扫描功能,首先需要确保该设备支持红外功能并已正确连接到C72手持机。 首先,需要在Android项目中添加适当的权限。打开AndroidManifest.xml文件,并添加以下权限: ``` <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> ``` 接下来,需要在Android项目中添加C72手持机SDK。可以从设备制造商的网站上下载SDK压缩包,并将其解压缩到项目的相应目录。 然后,需要在项目的Android.mk文件中配置NDK。添加以下代码: ``` # 必要的NDK设置 APP_PLATFORM := android-21 APP_ABI := armeabi-v7a APP_STL := gnustl_static APP_CPPFLAGS += -std=gnu++11 ``` 在MainActivity的Java类中,使用Java Native Interface(JNI)进行调用。添加以下代码: ```java static { System.loadLibrary("yourLibraryName"); // 替换为您的C72手持机库名称 } public native String openInfrared(); public native String startScan(); public native String stopScan(); public native String closeInfrared(); ``` 在onCreate方法中,实例化C72手持机对象,并调用红外扫描功能的方法: ```java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String result = openInfrared(); // 打开红外功能 Log.d("Infrared", result); result = startScan(); // 开始红外扫描 Log.d("Infrared", result); // 这里可以添加其他的逻辑代码 result = stopScan(); // 停止红外扫描 Log.d("Infrared", result); result = closeInfrared(); // 关闭红外功能 Log.d("Infrared", result); } ``` 这样,您的Android应用程序就可以调用C72手持机的红外扫描功能了。记得捕获和处理可能出现的异常,并对结果进行适当的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值