⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 发卡系统.cs

📁 射频卡读写程序,C#版
💻 CS
字号:
namespace 毕业设计
{
	using System;
	using System.Drawing;
	using System.Collections;
	using System.ComponentModel;
	using System.Windows.Forms;
	using System.Data;

	namespace 射频卡
	{
		/// <summary>
		/// 发卡系统 的摘要说明。
		/// </summary>
		public class 发卡系统 : System.Windows.Forms.Form
		{
			private System.Windows.Forms.StatusBar StatusBar;
			private System.Windows.Forms.StatusBarPanel Pan1;
			private System.Windows.Forms.StatusBarPanel Pan2;
			private System.Windows.Forms.StatusBarPanel Pan3;
			private System.Windows.Forms.StatusBarPanel Pan4;
			private System.Windows.Forms.ListBox LogLsit;
			private System.Windows.Forms.Timer timer;
			private System.ComponentModel.IContainer components;
			OprationCard.OprationCrad Machine;
			private int Step=0;

			private bool  newcard=true;

			public 发卡系统()
			{
				//
				// Windows 窗体设计器支持所必需的
				//
				InitializeComponent();

				//
				// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
				//
			}

			/// <summary>
			/// 清理所有正在使用的资源。
			/// </summary>
			protected override void Dispose( bool disposing )
			{
				if( disposing )
				{
					if(components != null)
					{
						components.Dispose();
					}
				}
				base.Dispose( disposing );
			}

			#region Windows 窗体设计器生成的代码
			/// <summary>
			/// 设计器支持所需的方法 - 不要使用代码编辑器修改
			/// 此方法的内容。
			/// </summary>
			private void InitializeComponent()
			{
				this.components = new System.ComponentModel.Container();
				this.LogLsit = new System.Windows.Forms.ListBox();
				this.StatusBar = new System.Windows.Forms.StatusBar();
				this.Pan1 = new System.Windows.Forms.StatusBarPanel();
				this.Pan2 = new System.Windows.Forms.StatusBarPanel();
				this.Pan3 = new System.Windows.Forms.StatusBarPanel();
				this.Pan4 = new System.Windows.Forms.StatusBarPanel();
				this.timer = new System.Windows.Forms.Timer(this.components);
				((System.ComponentModel.ISupportInitialize)(this.Pan1)).BeginInit();
				((System.ComponentModel.ISupportInitialize)(this.Pan2)).BeginInit();
				((System.ComponentModel.ISupportInitialize)(this.Pan3)).BeginInit();
				((System.ComponentModel.ISupportInitialize)(this.Pan4)).BeginInit();
				this.SuspendLayout();
				// 
				// LogLsit
				// 
				this.LogLsit.ItemHeight = 12;
				this.LogLsit.Location = new System.Drawing.Point(0, 0);
				this.LogLsit.Name = "LogLsit";
				this.LogLsit.Size = new System.Drawing.Size(288, 160);
				this.LogLsit.TabIndex = 0;
				// 
				// StatusBar
				// 
				this.StatusBar.Location = new System.Drawing.Point(0, 160);
				this.StatusBar.Name = "StatusBar";
				this.StatusBar.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
																							 this.Pan1,
																							 this.Pan2,
																							 this.Pan3,
																							 this.Pan4});
				this.StatusBar.ShowPanels = true;
				this.StatusBar.Size = new System.Drawing.Size(292, 22);
				this.StatusBar.TabIndex = 1;
				// 
				// Pan1
				// 
				this.Pan1.Text = "状态";
				this.Pan1.Width = 35;
				// 
				// Pan2
				// 
				this.Pan2.Text = "监测卡";
				// 
				// timer
				// 
				this.timer.Tick += new System.EventHandler(this.timer_Tick);
				// 
				// 发卡系统
				// 
				this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
				this.ClientSize = new System.Drawing.Size(292, 182);
				this.Controls.Add(this.StatusBar);
				this.Controls.Add(this.LogLsit);
				this.Name = "发卡系统";
				this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
				this.Text = "发卡系统";
				this.Closing += new System.ComponentModel.CancelEventHandler(this.发卡系统_Closing);
				this.Load += new System.EventHandler(this.发卡系统_Load);
				((System.ComponentModel.ISupportInitialize)(this.Pan1)).EndInit();
				((System.ComponentModel.ISupportInitialize)(this.Pan2)).EndInit();
				((System.ComponentModel.ISupportInitialize)(this.Pan3)).EndInit();
				((System.ComponentModel.ISupportInitialize)(this.Pan4)).EndInit();
				this.ResumeLayout(false);

			}
			#endregion

			private void timer_Tick(object sender, System.EventArgs e)
			{
					毕业设计.射频卡.读卡结果 o=new 毕业设计.射频卡.读卡结果();
				o.Title="不能发放的卡";
				if(this.Step==0)
				{
					if(Machine.FindCard()==0)
					{
						if(this.newcard)
						{
							this.newcard=false;
							this.LogLsit.Items.Add(System.DateTime.Now.ToString()+":监测到射频卡!");
							this.LogLsit.Items.Add(System.DateTime.Now.ToString()+":卡序列号--"+Machine.CardNo.ToString());
							this.Step=1;
						}
						else
							this.Step=0;
					}
					else
					{
						this.newcard=true;
						this.Step=0;
					}
				}

				if(this.Step==1)
				{
					if(Machine.LoadKey()==0)
					{
						this.Step=2;
					}
					else
					{
						this.Step=0;
					}
				}

				if(this.Step==2)
				{
					if(Machine.Authentication()==0)
					{	
						this.Text="卡序列号--"+Machine.CardNo.ToString();
						this.Step=3;
					}
					else
					{
						this.Step=0;
						this.Text="发卡系统";
						
						o.lbCardNo.Text="卡序列号:"+Machine.CardNo.ToString();
						o.lbReason.Text="密码验证失败";
						o.ShowDialog();
					}
				}
			
				if(this.Step==3)
				{
					this.timer.Enabled=false;
					byte[] Datas={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
					if(Machine.Read(Datas,8)!=0)
					{
						this.Step=0;
						this.LogLsit.Items.Add(System.DateTime.Now.ToString()+":读卡错误!!请换卡。。");
					}
					else
					{
						this.Step=4;
						string s="";
						for(int i=0;i<Datas.Length;i++)
							s+=Datas[i];	
						int cardno=0;
						for(int i=0;i<Datas.Length;i++)
						{
							int t=System.Convert.ToInt32(Datas[15-i]);
							t-=48;
							if(t>=0)
								cardno=cardno*10+t;
						}
						this.LogLsit.Items.Add(System.DateTime.Now.ToString()+":卡号为:"+cardno.ToString());
						毕业设计.数据处理.数据处理 oo=new 毕业设计.数据处理.数据处理();
						if(oo.Login(cardno.ToString())!=null)
						{
							o.lbCardNo.Text="卡序列号:"+Machine.CardNo.ToString();
							o.lbReason.Text="此卡已经注册!";
							this.timer.Enabled=false;
							o.ShowDialog();
							this.timer.Enabled=true;
						}

			    		else
						{
							 cardno=oo.GetCards();
							if(cardno==-2)
							{
								this.LogLsit.Items.Add("数据库通讯失败!");
								this.Step=0;
							}
		
							cardno++;
							string strCardNo=cardno.ToString();

							 byte[] Datass={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
							for(int i=0;i<strCardNo.Length;i++)
							{
								Datass[15-i]=(byte)strCardNo[i];
							}

							if(PreparyWrite())
							{
								if(Machine.Write(Datass,8)==0)
								{
									PreparyWrite();
									if(Machine.InitValue(0,9)==0)
									{
										
										if(MessageBox.Show("是否为补发卡?","选择发卡类型",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button1)==DialogResult.Yes)
										{
											//补发卡
											this.Hide();
											毕业设计.软件系统.补发卡系统 ooo=new 毕业设计.软件系统.补发卡系统();
											ooo.CardNo=strCardNo;
											ooo.ShowDialog(this);
											this.Show();
										}
										else
										{
											this.Hide();
											毕业设计.软件系统.注册用户 ooo=new 毕业设计.软件系统.注册用户();
											ooo.cardno=strCardNo;
											ooo.ShowDialog(this);
											this.Show();
										}

										
									}
									
									else
										this.LogLsit.Items.Add("注册过程失败!");
								}
								else
								{
									this.LogLsit.Items.Add("初始化卡失败!");
								}
							}
							else
							{
								this.LogLsit.Items.Add("写卡失败!");
							}			
						}
					}

					this.timer.Enabled=true;
				}

				if(this.Step==4)
				{
					if(Machine.FindCard()==0&&Machine.LoadKey()==0&&Machine.Authentication()==0)
					{
						this.Step=4;
					}
					else
					{
						this.Text="发卡系统";
						this.Step=0;
					}
				}
		
			}

			private void 发卡系统_Load(object sender, System.EventArgs e)
			{
				Machine=new OprationCard.OprationCrad();
				Machine.port=1;
				Machine.HZ=115200;
				DataSet o=new DataSet();
				try
				{
					o.ReadXml("Machine.config");
					if(o.Tables[0].Rows[0][0].ToString()=="COM1")
					{
						Machine.port=0;
					}
					if(o.Tables[0].Rows[0][0].ToString()=="COM2")
					{
						Machine.port=1;
					}
					if(o.Tables[0].Rows[0][0].ToString()=="COM3")
					{
						Machine.port=2;
					}
					if(o.Tables[0].Rows[0][0].ToString()=="COM4")
					{
						Machine.port=3;	
					}			
					if(o.Tables[0].Rows[0][0].ToString()=="COM5")
					{
						Machine.port=4;	
					}

					if(o.Tables[0].Rows[0][0].ToString()=="COM6")
					{
						Machine.port=5;	
					}		
					if(o.Tables[0].Rows[0][0].ToString()=="COM7")
					{
						Machine.port=6;	
					}				
					if(o.Tables[0].Rows[0][0].ToString()=="COM8")
					{
						Machine.port=7;	
					}

					Machine.HZ=System.Convert.ToInt32(o.Tables[0].Rows[0][1].ToString());
					Machine.Mode=System.Convert.ToInt32(o.Tables[0].Rows[0][3].ToString());//模式0
					Machine.SecNo=System.Convert.ToInt32(o.Tables[0].Rows[0][2].ToString());//第二扇区

					Machine.HZ=Convert.ToInt32(o.Tables[0].Rows[0][1].ToString());

					毕业设计.射频卡.初始化状态 status=new 毕业设计.射频卡.初始化状态();
					if(Machine.Init_Machine()<=0)
					{
						status.lbStatus.Text="设备初始化失败,请检查电源和端口!";
					}
					else
					{
						status.lbStatus.Text="设备初始化成功!";

						if(Machine.SetTime(GetTime())==0)
							status.lbStatus.Text+="\r\n时间设置成功!";
						if(Machine.LoadKey()!=0)
							status.lbStatus.Text+="\r\n载入密码失败!";
						else
						{
							status.lbStatus.Text+="\r\n装入密码成功!";
							this.timer.Enabled=true;
						}
					}
					status.Show();
				}
				catch
				{
					MessageBox.Show("配置文件丢失,请配置程序!");
					this.Dispose(true);
					毕业设计.射频卡.读卡器配置 oo=new 毕业设计.射频卡.读卡器配置();
					oo.ShowDialog(this.Parent);
				}
			}

			private byte[] GetTime()
			{
				毕业设计.数据处理.数据处理 o=new 毕业设计.数据处理.数据处理();
				return o.GetTime();
			}

			private bool PreparyWrite()
			{
				if(Machine.FindCard()==0)
				{
					if(Machine.LoadKey()==0)
					{
						if(Machine.Authentication()==0)
						{
							return true;
						}
						else
						{
							return false;
						}
					}
					else
					{
						return false;
					}			
				}
				else
				{
					return false;
				}
			}

			private void 发卡系统_Closing(object sender, System.ComponentModel.CancelEventArgs e)
			{
				Machine.Exit_Machine();
				this.timer.Enabled=false;
				this.Dispose(true);
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -