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

📄 readbrushidframe.cs

📁 独立完成考勤管理软件加打卡系统(C#) 是打卡加管理系统软件 
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsApplication
{
	/// <summary>
	/// ReadBrushIDFrame 的摘要说明。
	/// </summary>
	public class ReadBrushIDFrame : System.Windows.Forms.Form
	{
		#region 控件...
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox textBrushID;
		private System.Windows.Forms.TextBox textDate;
		private System.Windows.Forms.Button btnOpen;
		private System.Windows.Forms.Button btnClose;
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.Timer timer1;
		private AxMSCommLib.AxMSComm axMSComm1;
		#endregion
		#region 变量定义...
		public static string strID="";
		#endregion
		#region 构造函数...
		public ReadBrushIDFrame()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

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

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		#endregion
		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ReadBrushIDFrame));
			this.label1 = new System.Windows.Forms.Label();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.label2 = new System.Windows.Forms.Label();
			this.textBrushID = new System.Windows.Forms.TextBox();
			this.textDate = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.btnOpen = new System.Windows.Forms.Button();
			this.btnClose = new System.Windows.Forms.Button();
			this.timer1 = new System.Windows.Forms.Timer(this.components);
			this.axMSComm1 = new AxMSCommLib.AxMSComm();
			((System.ComponentModel.ISupportInitialize)(this.axMSComm1)).BeginInit();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(128)), ((System.Byte)(255)));
			this.label1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.label1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.label1.ForeColor = System.Drawing.Color.White;
			this.label1.Location = new System.Drawing.Point(0, 0);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(344, 32);
			this.label1.TabIndex = 0;
			this.label1.Text = "读卡操作";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// pictureBox1
			// 
			this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
			this.pictureBox1.Location = new System.Drawing.Point(0, 32);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(136, 232);
			this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
			this.pictureBox1.TabIndex = 1;
			this.pictureBox1.TabStop = false;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(160, 48);
			this.label2.Name = "label2";
			this.label2.TabIndex = 2;
			this.label2.Text = "接收卡号:";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// textBrushID
			// 
			this.textBrushID.Location = new System.Drawing.Point(160, 88);
			this.textBrushID.Name = "textBrushID";
			this.textBrushID.Size = new System.Drawing.Size(152, 21);
			this.textBrushID.TabIndex = 3;
			this.textBrushID.Text = "";
			// 
			// textDate
			// 
			this.textDate.Location = new System.Drawing.Point(160, 168);
			this.textDate.Name = "textDate";
			this.textDate.Size = new System.Drawing.Size(152, 21);
			this.textDate.TabIndex = 5;
			this.textDate.Text = "";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(160, 128);
			this.label3.Name = "label3";
			this.label3.TabIndex = 4;
			this.label3.Text = "日期:";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// btnOpen
			// 
			this.btnOpen.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.btnOpen.Location = new System.Drawing.Point(152, 216);
			this.btnOpen.Name = "btnOpen";
			this.btnOpen.Size = new System.Drawing.Size(75, 24);
			this.btnOpen.TabIndex = 6;
			this.btnOpen.Text = "打开端口";
			this.btnOpen.Visible = false;
			this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click);
			this.btnOpen.MouseEnter += new System.EventHandler(this.btnOpen_MouseEnter);
			this.btnOpen.MouseLeave += new System.EventHandler(this.btnOpen_MouseLeave);
			// 
			// btnClose
			// 
			this.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.btnClose.Location = new System.Drawing.Point(248, 216);
			this.btnClose.Name = "btnClose";
			this.btnClose.Size = new System.Drawing.Size(75, 24);
			this.btnClose.TabIndex = 7;
			this.btnClose.Text = "关闭端口";
			this.btnClose.Visible = false;
			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
			this.btnClose.MouseEnter += new System.EventHandler(this.btnOpen_MouseEnter);
			this.btnClose.MouseLeave += new System.EventHandler(this.btnOpen_MouseLeave);
			// 
			// timer1
			// 
			this.timer1.Interval = 1000;
			this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
			// 
			// axMSComm1
			// 
			this.axMSComm1.Enabled = true;
			this.axMSComm1.Location = new System.Drawing.Point(72, 104);
			this.axMSComm1.Name = "axMSComm1";
			this.axMSComm1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMSComm1.OcxState")));
			this.axMSComm1.Size = new System.Drawing.Size(38, 38);
			this.axMSComm1.TabIndex = 8;
			// 
			// ReadBrushIDFrame
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.ClientSize = new System.Drawing.Size(338, 263);
			this.Controls.Add(this.axMSComm1);
			this.Controls.Add(this.btnClose);
			this.Controls.Add(this.btnOpen);
			this.Controls.Add(this.textDate);
			this.Controls.Add(this.textBrushID);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.pictureBox1);
			this.Controls.Add(this.label1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "ReadBrushIDFrame";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "ReadBrushIDFrame";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.ReadBrushIDFrame_Closing);
			this.Load += new System.EventHandler(this.ReadBrushIDFrame_Load);
			((System.ComponentModel.ISupportInitialize)(this.axMSComm1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion
		#region 初始化事件 ...
		private void ReadBrushIDFrame_Load(object sender, System.EventArgs e)
		{
		    this.textDate.Text=System.DateTime.Now.ToLongDateString().Trim();
			this.btnOpen.Enabled=false;
			this.btnClose.Enabled=false;
			this.axMSComm1.OnComm += new System.EventHandler(this.axMSComm1_OnComm);
			this.axMSComm1.PortOpen=true;
			if(this.axMSComm1.InBufferCount>0)
			{
				axMSComm1_OnComm(sender,e);
				this.btnOpen.Enabled=false;
				this.btnClose.Enabled=true;
			}
			else
			{
				//MessageBox.Show(this,"缓冲内没有任何数据啊!!!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
				this.btnClose.Enabled=false;
				this.btnOpen.Enabled=false;
			}
			//this.axMSComm2.PortOpen=false;
		}
		#endregion
		#region 打开端口事件...
		private void btnOpen_Click(object sender, System.EventArgs e)
		{
		   OpenPortOpen();
		}
		#endregion
		#region 关闭端口事件...
		private void btnClose_Click(object sender, System.EventArgs e)
		{
		   ClosePortOpen();
		}
		#endregion
		#region 光标移动变化事件...
		private void btnOpen_MouseLeave(object sender, System.EventArgs e)
		{
		    ReadBrushIDFrame.MouseLeavebutton(sender,e);
		}

		private void btnOpen_MouseEnter(object sender, System.EventArgs e)
		{
		    ReadBrushIDFrame.MouseEnterbutton(sender,e);
		}
		#endregion
		#region 光标移动变化函数...
		public static void MouseLeavebutton(System.Object sender,System.EventArgs e)
		{
			Button button=(Button)sender;
			button.FlatStyle=FlatStyle.Flat;
		}
		public static void MouseEnterbutton(System.Object sender,System.EventArgs e)
		{
			Button button=(Button)sender;
			button.FlatStyle=FlatStyle.Standard;
		}
		#endregion
		#region 打开端口函数...
		private void OpenPortOpen()
		{
			try
			{
				//this.axMSComm2.PortOpen=true;
				MessageBox.Show(this,"串口已经打开啦!!!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
				this.btnOpen.Enabled=false;
				this.btnClose.Enabled=true;
			}
			catch(Exception exe)
			{
				MessageBox.Show(exe.ToString());
			}
		}
		#endregion
		#region 关闭端口函数...
		private void ClosePortOpen()
		{
			try
			{
				//this.axMSComm2.PortOpen=false;
				MessageBox.Show(this,"串口已经关闭啦!!!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
				this.btnClose.Enabled=false;
				this.btnOpen.Enabled=true;
			}
			catch(Exception exe)
			{
				MessageBox.Show(exe.ToString());
			}
		}
		#endregion
		#region 卡号函数..
		private void ReseivceID()
		{
			try
			{
				#region 接收卡号字符串
				string tempString=RBrushID();
				#endregion
				#region  处理非卡机号的字符串...
				if(CheckBrushID(tempString).Equals(true))
				{
					MessageBox.Show(this,"请与新华易网络科技有限公司取得联系啊!!!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
				}
				#endregion
				#region  卡机正确的字符串...
				else
				{
					#region 截取后8个字符串....
                    string str=deal8String(tempString);
                    #endregion
					#region 转换十进制数...
                    string showControlStr= WindowsApplication.HexToTen.HexToTen.Hex2Ten(str);
					#endregion
					#region  显示字符串...
					this.textBrushID.Text=showControlStr;
					#endregion
                    ReadBrushIDFrame.strID=this.textBrushID.Text.ToString().Trim();
				}
				#endregion
			}
			catch(Exception ele)
			{
				MessageBox.Show(ele.ToString(),"提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
		}
		#endregion
		#region MSCOMM控件自动事件....
		private void axMSComm_OnComm(object sender, System.EventArgs e)
		{
			ReseivceID();
		}
		#endregion
		#region 时间处理事件...
		private void timer1_Tick(object sender, System.EventArgs e)
		{
		    
		}
		#endregion
		#region  处理非卡号的函数
		private bool CheckBrushID(string temp)
		{
			bool f=false;
			if(!(temp.StartsWith("39")))
			{
				MessageBox.Show(this,"此卡机不正确啊!!!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
				f=true;
			}
			return f;
		}
		#endregion
		#region  截取后8个字符串....
		private string deal8String(string temp)
		{
			string str="0123456789";
			string showString="";
			if(temp.ToString().Length.Equals(12))
			{
				showString=temp.Substring(4,8).Trim();
			}
			else 
			{
				return str;
			}
			return showString;
		}
		#endregion
        #region MSCOMM控件自动事件....
		private void axMSComm2_OnComm(object sender, System.EventArgs e)
		{
//		   ReseivceID();
		}
		private void axMSComm1_OnComm(object sender, System.EventArgs e)
		{
			ReseivceID();
		}
		#endregion
		#region 关闭程序事件...
		private void ReadBrushIDFrame_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
		   this.axMSComm1.PortOpen=false;
		}
		#endregion
		#region 接收卡号的函数
		private string RBrushID()
		{
			string temp="";
			try
			{
				if(this.axMSComm1.InBufferCount>0)
				{
					try
					{
						byte[] strBuffer=new byte[this.axMSComm1.InBufferCount];
						strBuffer=(byte[])this.axMSComm1.Input;
						#region 处理字符串...
						for(int i=0;i<strBuffer.Length;i++)
						{
							if(strBuffer[i].ToString("X").Length<=1)
							{
								temp+=strBuffer[i].ToString("X2");
							}
							else
							{
								temp+=strBuffer[i].ToString("X");
							}
						}
						#endregion
						this.axMSComm1.InBufferCount=0;
						this.axMSComm1.OutBufferCount=0; 
					}
					catch(System.FormatException ele)
					{
						MessageBox.Show(ele.ToString());
					}
				}
				else
				{
					MessageBox.Show(this,"缓冲区内没有任何数据!!!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
					this.axMSComm1.InBufferCount=0;
					this.axMSComm1.OutBufferCount=0;
					return string.Empty;
				}
			}
			catch(Exception exe)
			{
				MessageBox.Show(exe.ToString());
			}
			return temp;
		}
		#endregion
	}
}

⌨️ 快捷键说明

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