📄 scanning.cs.svn-base
字号:
using System;
using System.Data;
using System.Windows.Forms;
namespace GuangMa
{
/// <summary>
/// 类别说明:该类别用于MC系列的条码扫描
/// 作 者:刘洪辉
/// 2006年8日31日
/// 版权所有:成都光码
/// </summary>
public class Scanning
{
public delegate void ScannedEventHandler(object sender, string e);
public event ScannedEventHandler Scanned;
private Symbol.Barcode.Reader MyReader = null;
public Symbol.Barcode.ReaderData MyReaderData = null;
private System.EventHandler MyEventHandler = null;
private Form form;
protected void OnScanned(object sender, string e)
{
if (this.Scanned != null) this.Scanned(sender, e);
}
public Scanning(Form _form)
{
this.form = _form;
// If we can initialize the Reader
if ( this.InitReader() )
{
//Start a read on the reader
//this.StartRead();
}
}
/// <summary>
/// Initialize the reader.
/// </summary>
private bool InitReader()
{
// If reader is already present then fail initialize
if ( this.MyReader != null )
{
return false;
}
// Create new reader, first available reader will be used.
this.MyReader = new Symbol.Barcode.Reader();
// Create reader data
this.MyReaderData = new Symbol.Barcode.ReaderData(
Symbol.Barcode.ReaderDataTypes.Text,
Symbol.Barcode.ReaderDataLengths.MaximumLabel);
// Create event handler delegate
this.MyEventHandler = new EventHandler(MyReader_ReadNotify);
// Enable reader, with wait cursor
this.MyReader.Actions.Enable();
//设置扫描成功后的声音长度
this.MyReader.Parameters.Feedback.Success.BeepTime = 400;
//设置扫描成功后的声音文件
this.MyReader.Parameters.Feedback.Success.WaveFile = "\\windows\\decode.wav";
// Attach to activate and deactivate events
//form.Activated +=new EventHandler(ReaderForm_Activated);
//form.Deactivate +=new EventHandler(ReaderForm_Deactivate);
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>
/// 启动扫描
/// </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>
/// 停止扫描
/// </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 )
{
// Handle the data from this read
this.HandleData(TheReaderData);
// Start the next read
//this.StartRead();
}
}
/// <summary>
/// Handle data from the reader
/// </summary>
private void HandleData(Symbol.Barcode.ReaderData TheReaderData)
{
this.OnScanned(this, TheReaderData.Text);
}
/// <summary>
/// Handler for when the form is actived. This would occur when this application becomes the current
/// application
/// </summary>
private void ReaderForm_Activated(object sender, EventArgs e)
{
// If there are no reads pending on MyReader start a new read
if ( !this.MyReaderData.IsPending )
this.StartRead();
}
/// <summary>
/// Handler for when the form is deactivated. This would be called if another application became the current
/// application. This stops the reading of data from the reader.
/// </summary>
private void ReaderForm_Deactivate(object sender, EventArgs e)
{
this.StopRead();
}
private static Symbol.Audio.Controller m_AudioController = Scanning.InitializeAudio();
private static Symbol.Audio.Controller InitializeAudio()
{
Symbol.Audio.Controller audioController = null;
Symbol.Audio.Device device = (Symbol.Audio.Device)Symbol.StandardForms.SelectDevice.Select(
Symbol.Audio.Controller.Title,
Symbol.Audio.Device.AvailableDevices);
if(device == null)
{
MessageBox.Show("No Device Selected", "SelectDevice");
return null;
}
//check the device type
switch (device.AudioType)
{
//if standard device
case Symbol.Audio.AudioType.StandardAudio:
audioController = new Symbol.Audio.StandardAudio(device);
break;
//if simulated device
case Symbol.Audio.AudioType.SimulatedAudio:
audioController = new Symbol.Audio.SimulatedAudio(device);
break;
default :
throw new Symbol.Exceptions.InvalidDataTypeException("Unknown Device Type");
}
return audioController;
}
public static void Beep(int Duration, int Frequency)
{
try
{
if (Scanning.m_AudioController != null)
Scanning.m_AudioController.PlayAudio(Duration,Frequency);//play Default beep
}
catch
{
}
}
// 初始化背景灯
public static void InitialDisplay()
{
Symbol.Display.Controller discontrol = null;
Symbol.Display.Device device = (Symbol.Display.Device)Symbol.StandardForms.SelectDevice.Select(Symbol.Display.Controller.Title,Symbol.Display.Device.AvailableDevices);
if(device == null) return;
//Check the device type
switch (device.DisplayType)
{
// if device is standard device
case Symbol.Display.DisplayType.StandardDisplay:
discontrol = new Symbol.Display.StandardDisplay(device);
break;
//if device is simulated device
case Symbol.Display.DisplayType.SimulatedDisplay:
discontrol = new Symbol.Display.SimulatedDisplay(device);
break;
}
//显示背景灯
discontrol.BacklightState = Symbol.Display.BacklightState.ON;
discontrol.BacklightIntensityLevel = 4;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -