public class Card
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 8)]
public struct PERSONINFOW
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string name;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]
public string sex;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
public string nation;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
public string birthday;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 36)]
public string address;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
public string cardId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string police;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
public string validStart;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
public string validEnd;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]
public string sexCode;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
public string nationCode;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 36)]
public string appendMsg;
}
[DllImport("cardapi3.dll", EntryPoint = "OpenCardReader",
CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
public static extern Int32 OpenCardReader(Int32 lPort, UInt32 ulFlag, UInt32 ulBaudRate);
[DllImport("cardapi3.dll", EntryPoint = "GetPersonMsgW",
CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
public static extern Int32 GetPersonMsgW(ref PERSONINFOW pInfo, string pszImageFile);
[DllImport("cardapi3.dll", EntryPoint = "GetPersonMsgA",
CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
public static extern Int32 GetPersonMsgA(ref PERSONINFOW pInfo, string pszImageFile);
[DllImport("cardapi3.dll", EntryPoint = "CloseCardReader",
CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
public static extern Int32 CloseCardReader();
[DllImport("cardapi3.dll", EntryPoint = "GetErrorTextW",
CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
public static extern void GetErrorTextW(StringBuilder pszBuffer, UInt32 dwBufLen);
[DllImport("cardapi3.dll", EntryPoint = "GetCardReaderStatus",
CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
public static extern long GetCardReaderStatus(long lPort, ulong ulBaudRate);
}
public class CardMonitorService
{
#region Fields
private Card.PERSONINFOW person;
private int port = Convert.ToInt32(ConfigurationManager.AppSettings["port"].ToString());
private UInt32 flag = Convert.ToUInt32(ConfigurationManager.AppSettings["flag"].ToString());
private UInt32 BaudRate = Convert.ToUInt32(ConfigurationManager.AppSettings["BaudRate"].ToString());
#endregion
public void ServiceStart(ref Card.PERSONINFOW person, string imagePath)
{
ThreadInvoke(ref person, imagePath);
}
private void ThreadInvoke(ref Card.PERSONINFOW person, string imagePath)
{
if (!OpenDevice())
return;
int result = Card.GetPersonMsgW(ref person, imagePath);
if (result == 0)
{
CloseDevice();
}
}
private void CloseDevice()
{
Card.CloseCardReader();
}
private bool OpenDevice()
{
int result = Card.OpenCardReader(port, flag, BaudRate);
if (result == 0)
return true;
else
return false;
}
public delegate void Elapsed();
public event Elapsed OnElapsed;
private long _Interval = 1000;
public long Interval
{
get { return _Interval; }
set { _Interval = value; }
}
private bool _IsStart = false;
private Thread _TimerThread;
public void Start()
{
if (_IsStart)
return;
_IsStart = true;
_TimerThread = new Thread(new ThreadStart(DoTimer));
_TimerThread.Start();
}
public void Stop()
{
try
{
if (!_IsStart)
return;
_IsStart = false;
_TimerThread.Abort();
}
catch (Exception e)
{ }
}
private void DoTimer()
{
try
{
int last = Environment.TickCount;
while (_IsStart)
{
if (Environment.TickCount - last > _Interval)
{
if (OnElapsed != null)
OnElapsed();
last = Environment.TickCount;
}
Thread.Sleep(100);
}
}
catch (Exception e)
{ }
}
}
_Timer = new CardMonitorService();
_Timer.Interval = _Interval;
_Timer.Start();
_Timer.OnElapsed += Timer_OnElapsed;
private void Timer_OnElapsed()
{
Invoke(new Action(() =>
{
});