一.创建项目
打开visual 2005,文件->新建->项目
二.设计界面,编写代码
首先进行系统界面设计,编写相关逻辑代码处理
在初始化窗体时,加载扫描头触发事件
private SymbolScanWood.SymbolScanWood _objScan = new SymbolScanWood.SymbolScanWood("Text");
private void Zone_Load(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
_objScan.MyAfterRead += new EventHandler(_objScan_OnAfterRead);
Cursor.Current = Cursors.Default;
}
private void _objScan_OnAfterRead(object sender, EventArgs e)
{
tbZone.Text = _objScan.BarcodeText;//进行扫描相关逻辑处理
}
private void Zone_Load(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
_objScan.MyAfterRead += new EventHandler(_objScan_OnAfterRead);
Cursor.Current = Cursors.Default;
}
private void _objScan_OnAfterRead(object sender, EventArgs e)
{
tbZone.Text = _objScan.BarcodeText;//进行扫描相关逻辑处理
}
窗体关闭时,关闭扫描头,释放扫描头资源,同时保存数据
private void Zone_Closing(object sender, CancelEventArgs e)
{
_objScan.TermReader();
}
{
_objScan.TermReader();
}
程序中引用的symbolScanWood.dll代码如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace SymbolScanWood
{
public class SymbolScanWood
{
private string _strBarcode = "";
private byte[] _byteBarcodeData;
private string _strBarcodeType;
private int _nBarcodeLenth = 0;
private bool _bSuccess = false;
private DateTime _dTimeStamp;
private string _strDataType;
private Symbol.Barcode.Reader _MyReader = null;
private Symbol.Barcode.ReaderData _MyReaderData = null;
private System.EventHandler _MyEventHandler = null;
private event System.EventHandler _MyAfterRead = null;
public string BarcodeText
{
get { return _strBarcode; }
}
public byte[] BarcodeData
{
get { return _byteBarcodeData; }
}
public string BarcodeType
{
get { return _strBarcodeType; }
}
public int BarcodeLenth
{
get { return _nBarcodeLenth; }
}
public bool Success
{
get { return _bSuccess; }
}
public DateTime TimeStamp
{
get { return _dTimeStamp; }
}
public string DataType
{
get { return _strDataType; }
}
public SymbolScanWood(string strDataType)
{
if ((strDataType != "Text") && (strDataType != "Binary"))
{
throw new Exception("设置扫描类型错误!");
}
this._MyReader = null;
this._strDataType = strDataType;
this._MyReaderData = null;
this._MyEventHandler = null;
this._bSuccess = false;
this._strBarcodeType = "";
this._nBarcodeLenth = 0;
this._dTimeStamp = DateTime.Now;
this._strBarcode = "";
this._byteBarcodeData = new byte[0];
InitReader();
this.StartRead();
}
~SymbolScanWood()
{
this.StopRead();
this.TermReader();
}
public System.EventHandler MyAfterRead
{
get{return _MyAfterRead;}
set{_MyAfterRead=value;}
}
/// <summary>
/// Initialize the reader.
/// </summary>
public bool InitReader()
{
// If reader is already present then fail initialize
if (_MyReader != null)
{
return false;
}
// Create new reader, first available reader will be used.
_MyReader = new Symbol.Barcode.Reader();
// Create reader data
if(_strDataType.ToLower().Trim() == "Binary")
{
_MyReaderData = new Symbol.Barcode.ReaderData(
Symbol.Barcode.ReaderDataTypes.Binary,
Symbol.Barcode.ReaderDataLengths.MaximumLabel);
}
else
{
_MyReaderData = new Symbol.Barcode.ReaderData(
Symbol.Barcode.ReaderDataTypes.Text,
Symbol.Barcode.ReaderDataLengths.MaximumLabel);
}
this._MyEventHandler = new EventHandler(MyReader_ReadNotify);
// Enable reader, with wait cursor
_MyReader.Actions.Enable();
_MyReader.Parameters.Feedback.Success.BeepTime = 0;
_MyReader.Parameters.Feedback.Success.WaveFile = "\windows\alarm3.wav";
return true;
}
/// <summary>
/// Stop reading and disable/close reader
/// </summary>
public void TermReader()
{
// If we have a reader
if ( this._MyReader != null )
{
// Disable the reader
this._MyReader.Actions.Disable();
// Free it up
this._MyReader.Dispose();
// Indicate we no longer have one
this._MyReader = null;
}
// If we have a reader data
if ( this._MyReaderData != null )
{
// Free it up
this._MyReaderData.Dispose();
// Indicate we no longer have one
this._MyReaderData = null;
}
}
/// <summary>
/// Start a read on the reader
/// </summary>
public void StartRead()
{
// If we have both a reader and a reader data
if ( ( this._MyReader != null ) &&
( this._MyReaderData != null ) )
{
// Submit a read
this._MyReader.ReadNotify += this._MyEventHandler;
this._MyReader.Actions.Read(this._MyReaderData);
}
}
/// <summary>
/// Stop all reads on the reader
/// </summary>
public void StopRead()
{
// If we have a reader
if ( this._MyReader != null )
{
// Flush (Cancel all pending reads)
this._MyReader.ReadNotify -= this._MyEventHandler;
this._MyReader.Actions.Flush();
}
}
/// <summary>
/// Read complete or failure notification
/// </summary>
private void MyReader_ReadNotify(object sender, EventArgs e)
{
Symbol.Barcode.ReaderData TheReaderData = this._MyReader.GetNextReaderData();
// If it is a successful read (as opposed to a failed one)
if ( TheReaderData.Result == Symbol.Results.SUCCESS )
{
_bSuccess = true;
// Handle the data from this read
this.HandleData(TheReaderData);
// Start the next read
this.StartRead();
}
else
{
_bSuccess = false;
}
if(_MyAfterRead != null)
_MyAfterRead(sender, e);
}
/// <summary>
/// Handle data from the reader
/// </summary>
private void HandleData(Symbol.Barcode.ReaderData TheReaderData)
{
_nBarcodeLenth = TheReaderData.Length;
_strBarcodeType = TheReaderData.Type.ToString();
_dTimeStamp = TheReaderData.TimeStamp;
if(_strDataType.Trim() == "Text")
{
_strBarcode = TheReaderData.Text;
}
else
{
_byteBarcodeData = new byte[_nBarcodeLenth];
for (int i = 0; i < _nBarcodeLenth; i++)
{
_byteBarcodeData[i] = TheReaderData.RawData[i];
}
}
}
}
}
using System.Collections.Generic;
using System.Text;
namespace SymbolScanWood
{
public class SymbolScanWood
{
private string _strBarcode = "";
private byte[] _byteBarcodeData;
private string _strBarcodeType;
private int _nBarcodeLenth = 0;
private bool _bSuccess = false;
private DateTime _dTimeStamp;
private string _strDataType;
private Symbol.Barcode.Reader _MyReader = null;
private Symbol.Barcode.ReaderData _MyReaderData = null;
private System.EventHandler _MyEventHandler = null;
private event System.EventHandler _MyAfterRead = null;
public string BarcodeText
{
get { return _strBarcode; }
}
public byte[] BarcodeData
{
get { return _byteBarcodeData; }
}
public string BarcodeType
{
get { return _strBarcodeType; }
}
public int BarcodeLenth
{
get { return _nBarcodeLenth; }
}
public bool Success
{
get { return _bSuccess; }
}
public DateTime TimeStamp
{
get { return _dTimeStamp; }
}
public string DataType
{
get { return _strDataType; }
}
public SymbolScanWood(string strDataType)
{
if ((strDataType != "Text") && (strDataType != "Binary"))
{
throw new Exception("设置扫描类型错误!");
}
this._MyReader = null;
this._strDataType = strDataType;
this._MyReaderData = null;
this._MyEventHandler = null;
this._bSuccess = false;
this._strBarcodeType = "";
this._nBarcodeLenth = 0;
this._dTimeStamp = DateTime.Now;
this._strBarcode = "";
this._byteBarcodeData = new byte[0];
InitReader();
this.StartRead();
}
~SymbolScanWood()
{
this.StopRead();
this.TermReader();
}
public System.EventHandler MyAfterRead
{
get{return _MyAfterRead;}
set{_MyAfterRead=value;}
}
/// <summary>
/// Initialize the reader.
/// </summary>
public bool InitReader()
{
// If reader is already present then fail initialize
if (_MyReader != null)
{
return false;
}
// Create new reader, first available reader will be used.
_MyReader = new Symbol.Barcode.Reader();
// Create reader data
if(_strDataType.ToLower().Trim() == "Binary")
{
_MyReaderData = new Symbol.Barcode.ReaderData(
Symbol.Barcode.ReaderDataTypes.Binary,
Symbol.Barcode.ReaderDataLengths.MaximumLabel);
}
else
{
_MyReaderData = new Symbol.Barcode.ReaderData(
Symbol.Barcode.ReaderDataTypes.Text,
Symbol.Barcode.ReaderDataLengths.MaximumLabel);
}
this._MyEventHandler = new EventHandler(MyReader_ReadNotify);
// Enable reader, with wait cursor
_MyReader.Actions.Enable();
_MyReader.Parameters.Feedback.Success.BeepTime = 0;
_MyReader.Parameters.Feedback.Success.WaveFile = "\windows\alarm3.wav";
return true;
}
/// <summary>
/// Stop reading and disable/close reader
/// </summary>
public void TermReader()
{
// If we have a reader
if ( this._MyReader != null )
{
// Disable the reader
this._MyReader.Actions.Disable();
// Free it up
this._MyReader.Dispose();
// Indicate we no longer have one
this._MyReader = null;
}
// If we have a reader data
if ( this._MyReaderData != null )
{
// Free it up
this._MyReaderData.Dispose();
// Indicate we no longer have one
this._MyReaderData = null;
}
}
/// <summary>
/// Start a read on the reader
/// </summary>
public void StartRead()
{
// If we have both a reader and a reader data
if ( ( this._MyReader != null ) &&
( this._MyReaderData != null ) )
{
// Submit a read
this._MyReader.ReadNotify += this._MyEventHandler;
this._MyReader.Actions.Read(this._MyReaderData);
}
}
/// <summary>
/// Stop all reads on the reader
/// </summary>
public void StopRead()
{
// If we have a reader
if ( this._MyReader != null )
{
// Flush (Cancel all pending reads)
this._MyReader.ReadNotify -= this._MyEventHandler;
this._MyReader.Actions.Flush();
}
}
/// <summary>
/// Read complete or failure notification
/// </summary>
private void MyReader_ReadNotify(object sender, EventArgs e)
{
Symbol.Barcode.ReaderData TheReaderData = this._MyReader.GetNextReaderData();
// If it is a successful read (as opposed to a failed one)
if ( TheReaderData.Result == Symbol.Results.SUCCESS )
{
_bSuccess = true;
// Handle the data from this read
this.HandleData(TheReaderData);
// Start the next read
this.StartRead();
}
else
{
_bSuccess = false;
}
if(_MyAfterRead != null)
_MyAfterRead(sender, e);
}
/// <summary>
/// Handle data from the reader
/// </summary>
private void HandleData(Symbol.Barcode.ReaderData TheReaderData)
{
_nBarcodeLenth = TheReaderData.Length;
_strBarcodeType = TheReaderData.Type.ToString();
_dTimeStamp = TheReaderData.TimeStamp;
if(_strDataType.Trim() == "Text")
{
_strBarcode = TheReaderData.Text;
}
else
{
_byteBarcodeData = new byte[_nBarcodeLenth];
for (int i = 0; i < _nBarcodeLenth; i++)
{
_byteBarcodeData[i] = TheReaderData.RawData[i];
}
}
}
}
}