C#低频门禁卡系统操作

一.窗体界面展示

 


 二.这个系统所需要的控件
1、label

2、comboBox

3、button

4、textBox

5、timer

三.所用到的硬件
1.NewLab实验台

 

2.M3模块

 

3.低频卡数张 

 

4.LF 天线模块 

 

 5、USB转串口驱动 CH340

 

四.代码展示

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SerialPort com1 = new SerialPort(); //创建串口对象

        private void Form1_Load(object sender, EventArgs e)
        {
            String[] port = SerialPort.GetPortNames();  //获取端口
            for (int i = 0; i < port.Length; i++) { comboBox1.Items.Add(port[i]); }
            if (port.Length != 0) comboBox1.SelectedIndex = 0;
            if (!com1.IsOpen)
            {
                button1.BackColor = Color.Red;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!com1.IsOpen) //如果串口关闭,则打开
                com1.PortName = comboBox1.Text;
            com1.BaudRate = 115200;
            com1.DataBits = 8;
            com1.StopBits = StopBits.One;
            com1.Parity = Parity.None;
            com1.Open();
            button1.Text = "关闭端口";
            button1.BackColor = Color.Green;
            button2.Enabled = true;
            button4.Enabled = true;
            timer1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)//发送查询卡号指令
        {
            textBox1.Text = " ";
            byte[] buff = new byte[] { 0xff, 0x55, 0x00, 0x00, 0x01, 0x01, 0x00, 0x50, 0x74 };
            com1.Write(buff, 0, buff.Length);
            label1.Text = "ff, 55, 00, 00 , 01, 00, 50, 74";
        }

        private void timer1_Tick(object sender, EventArgs e)//定时器事件,自动接收数据
        {
            if (com1.BytesToRead != 0)
            {
                int len = com1.BytesToRead;
                byte[] buffer = new byte[len];
                com1.Read(buffer, 0, len);
                for (int i = 0; i < len; i++)
                {
                    textBox1.Text += buffer[i].ToString("X").PadLeft(2, '0') + "  ";
                }
                if (buffer[9] == 0x88 && buffer[10] == 0x66)
                {
                    button4.Text = "开门";
                    button4.BackColor = Color.Green;
                }
                else
                {
                    button4.Text = "禁止入内";
                    button4.BackColor = Color.Red;
                }
                for (int i = 0; i < len; i++)
                {
                    textBox1.Text += buffer[i].ToString("X").PadLeft(2, '0') + " ";
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)//写卡 ,把“8866”写入到块1
        {
            byte[] buff = new byte[] { 0xff, 0x55, 0x00, 0x00, 0x03, 0x04, 0x05, 0x01, 0x00, 0x00, 0x88, 0x66, 0x6D, 0xAE };

            com1.Write(buff, 0, buff.Length);

            label1.Text = "注册完成!";
        }

        private void button5_Click(object sender, EventArgs e)//读卡
        {
            textBox1.Text = " ";
            byte[] buff = new byte[] { 0xff, 0x55, 0x00, 0x00, 0x03, 0x03, 0x01, 0x01, 0x0F, 0x30 };
            com1.Write(buff, 0, buff.Length);
            label1.Text = "读卡完成";
        }

        private void button6_Click(object sender, EventArgs e)//注销
        {
            byte[] buff = new byte[] { 0xff, 0x55, 0x00, 0x00, 0x03, 0x04, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00, 0x47, 0x48 };
            com1.Write(buff, 0, buff.Length);
            label1.Text = "注销完成";
        }
    }
}

五.学习心得

我学习了数据存储方面的知识,熟悉了如何使用C#操控数据库、如何进行数据格式转换等技术。这在门禁卡系统中非常重要,能有效地保证数据的安全和正确性。

在门禁卡系统的开发中,我还加深了对C#语言的理解和掌握,包括基础语法、面向对象编程、多线程编程等方面。同时,我也学习到了很多相关的硬件技术和开发工具,如Arduino、串口通信等。

总体来说,开发C#低频门禁卡系统需要进行综合的学习和实践,需要掌握多种技术,包括软件开发、硬件驱动、数据存储等方面。通过不断地学习和实践,我相信我可以在这些方面有更深入的了解和掌握。

刚开始老师讲这个低频卡门禁系统,我都是听得迷迷糊糊,上完课后,我立马去问了同学,还有老师,以及上网查找资料,总算是把它的原理以及代码搞明白了。所以不管是学什么东西,只要有不懂的东西都可以去问问别人,如果不愿意问别人,你也可以自己上网,查找资料,总比自己在那什么都不做的要强。

...全文
62 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

1,365

社区成员

发帖
与我相关
我的任务
社区描述
柳职院电子信息工程学院同学们的学习园地
社区管理员
  • c_university_1974
  • qq_39231145
  • zhuisir
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

各位加入社区的同学,请完善社区信息,把社区昵称改为【班级-姓名】,社区签名改为【班级-学号-姓名】的格式

如【社区昵称】20计应1班  张某某(班级用简称)

     【社区签名】2020级计算机应用技术1班 20201234567 张某某 (班级用全称)

试试用AI创作助手写篇文章吧