此類用于honeywell1500/1300條碼槍的串口讀寫,可實(shí)現(xiàn)控制條碼槍掃描,并從條碼槍中取得掃描的數(shù)據(jù)。使用條碼槍的串口模式有以下幾個(gè)好處:
(1)避免因文本框失去焦點(diǎn)而導(dǎo)致錄入失敗。
(2)可與自動(dòng)化設(shè)備配合。
/*----------------------------------------------------------------// 文件名:BarcodeScanner.cs// 文件功能描述:條碼掃描槍類。用于控制條碼槍進(jìn)行掃描。//// 創(chuàng)建標(biāo)識(shí):leo 2012.10.16//// 修改標(biāo)識(shí):// 修改描述://// 修改標(biāo)識(shí):// 修改描述://----------------------------------------------------------------*/using System;using System.Collections.Generic;using System.Text;using System.IO.Ports;using System.Windows.Forms;using System.Threading;using System.IO;namespace QMS.Infrastructure{ public class BarcodeScanner { //****************************************************** 字 段 ************************************************************************** /// <summary> /// 串口對象。 /// </summary> SerialPort comm = new SerialPort(); /// <summary> /// 窗體對象。 /// </summary> Form _frm; //****************************************************** 屬 性 *************************************************************************** /// <summary> /// 返回條碼值。應(yīng)當(dāng)在條碼掃描完畢事件發(fā)生時(shí),再去取條碼值。 /// </summary> public string Barcode { set; get; } //***************************************************** 事 件 *************************************************************************** /// <summary> /// 掃描完畢事件。 /// </summary> public event EventHandler ScanFinished; //****************************************************** 方 法 ******************************************************************* /// <summary> /// 打開設(shè)備。 /// </summary> /// <param name="portName">串口號</param> /// <param name="frm">窗體</param> public void Open( string portName, Form frm ) { try { // 初始化窗體對象 _frm = frm; _frm.FormClosing += new FormClosingEventHandler( _frm_FormClosing ); //初始化SerialPort對象 comm.PortName = portName; comm.BaudRate = 19200; //honeywell的是這個(gè)值 comm.Open(); comm.DataReceived += comm_DataReceived; //添加事件注冊 } catch( Exception e ) { MessageBox.Show( e.Message ); } } /// <summary> /// 條碼槍進(jìn)行掃描。 /// </summary> public void Scan() { // 掃描超時(shí)監(jiān)視 // tmr.Enabled = true; // 觸發(fā)掃描 byte[] b = { 22, 84, 13 }; //十六進(jìn)制數(shù)為16 54 0d,括號中為這三個(gè)數(shù)的十進(jìn)制形式 string s = System.Text.Encoding.ASCII.GetString( b ); if( comm.IsOpen == true ) { comm.Write( s ); } else { MessageBox.Show( "無法觸發(fā)掃描槍掃描!請檢查連線或重啟程序" ); } } /// <summary> /// 關(guān)閉設(shè)備。 /// </summary> public void Close() { if( comm.IsOpen == true ) { comm.Close(); comm.Dispose(); } } //****************************************************** 內(nèi)部函數(shù) ******************************************************************* /// <summary> /// 串口數(shù)據(jù)接收函數(shù)。當(dāng)接收到數(shù)據(jù)時(shí),則引發(fā)ScanFinished事件。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void comm_DataReceived( object sender, SerialDataReceivedEventArgs e ) { // 掃描超時(shí)監(jiān)視。數(shù)據(jù)返回,則關(guān)閉監(jiān)視 // tmr.Enabled = false; // 讀取串口返回值 this.Barcode = comm.ReadLine().Trim(); // 引發(fā)事件 if( this.ScanFinished != null ) { _frm.Invoke( new MethodInvoker( delegate { this.ScanFinished( this, new EventArgs() ); } ) ); } } /// <summary> /// 在窗體關(guān)閉的時(shí)候關(guān)閉串口連接。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void _frm_FormClosing( object sender, FormClosingEventArgs e ) { this.Close(); } }}新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注