代码如下:Program.csusing System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO.Ports;
using System.Configuration;
using WYC.Common.OU;
using System.Data;
using System.Diagnostics;
using System.Threading;
using System.Web;namespace WYC.WinApp
{    internal static class Program
    { 
        /// <summary>
        /// 读卡
        /// </summary>
        /// <returns></returns>
        internal delegate void ReadCardHandler(CardInfo obj);        internal static event ReadCardHandler ReadCard = null;        /// <summary>
        /// 串口通信对象
        /// </summary>
        private static SerialPort c_Com = null;        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);            AppInit();
            
            Application.Run(new frmMain());
            
             if (c_Com.IsOpen)
                c_Com.Close();        }
        //异常
        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            MessageBox.Show(e.Exception.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //throw new Exception("The method or operation is not implemented.");
        }        /// <summary>
        /// 应用初始化
        /// </summary>
        static void AppInit()
        {
            try
            {
                //设置读卡器串口
                c_Com = new SerialPort();
                c_Com.PortName = "com1"; 
                c_Com.BaudRate = int.Parse(ConfigurationManager.AppSettings.Get("baudRate"));                c_Com.DataReceived+=new SerialDataReceivedEventHandler(c_Com_DataReceived);            }
            catch (UnauthorizedAccessException uae)
            {
                MessageBox.Show(uae.Message.ToString(), "警告1", MessageBoxButtons.OK, MessageBoxIcon.Information); 
            }
            catch (ArgumentException ae)
            {
                MessageBox.Show(ae.Message.ToString(), "警告2", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.IO.IOException ioe)
            {
                MessageBox.Show(ioe.Message.ToString(), "警告3", MessageBoxButtons.OK, MessageBoxIcon.Information); 
            }
            catch (InvalidOperationException ioec)
            {
                MessageBox.Show(ioec.Message.ToString(), "警告4", MessageBoxButtons.OK, MessageBoxIcon.Information); 
            }        }         static void c_Com_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {  
            SerialPort com = sender as SerialPort;            if (ReadCard != null)
            {
                CardInfo obj = new CardInfo();
                ReadCard(obj);
            }            com.DiscardInBuffer();
        }
    }}    
     
frmMain.cs
using System;
using System.Data;
using System.Windows.Forms;
using System.IO.Ports;
using System.Web;
using System.Configuration;
using WYC.DataAccess.OU;
using WYC.Common.OU;
using System.Diagnostics;
using System.Threading;
namespace WYC.WinApp
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            
            InitializeComponent();
            
            Program.ReadCard += new Program.ReadCardHandler(OnReadCard);
        }        public string UrlNum = String.Empty;
        public frmLogin fl = new frmLogin();        void OnReadCard(CardInfo obj)
        {
            ReadCardHandler readcard = new ReadCardHandler(SetInfo);
            if (InvokeRequired)
            {
                try
                {
                    this.Invoke(readcard, new object[] { obj });
                }
                catch
                { }
            }
            else
            {
                SetInfo(obj);
            }
        }        delegate void ReadCardHandler(CardInfo obj);        void SetInfo(CardInfo obj)
        {
        }
         void c_Com_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            SerialPort com = (sender as SerialPort);            com.ReadTimeout = 1000;
            com.ReadLine(); 
        }
}