串口设置低频门禁系统

一、设计要求:

1.设计界面要具备开关串口的功能,串口协议默认。

2.要具备读取卡号c(卡信息)的功能。

3可以对新卡进行注册,可以对已经注册的卡进行注销

4对读取到的数据进行判断,并正确显示“开门”或“禁止入内”等信息。

二、操作步骤

1.设置窗体:


 

2.需要用到的硬件:

(1)NewLab实验台

 (2)M3模块

(3)LF天线模块

 

 (4)低频ID卡

 (5)USB转串口驱动 CH340

 

3.导入代码

打开端口和关闭端口:

 private void button1_Click(object sender, EventArgs e)
        {
            if (!com1.IsOpen)                        //判断串口是否打开
            {
                com1.PortName = comboBox1.Text;       //先获取端口号
                com1.BaudRate = 115200;                 /*int.Parse(comboBox2.Text);*/     //获取选项框2的文本信息,并转换成整数;
                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;
                /*com1.DataReceived += new SerialDataReceivedEventHandler(button3_Click);*/
            }

            else if (com1.IsOpen)
            {
                com1.Close();
                button1.Text = "打开端口";
                button1.BackColor = Color.Red;
            }

写卡(把33 66 写到块2):

  private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = " ";
            byte[] buff = new byte[] { 0xff, 0x55, 0x00, 0x00, 0x03, 0x04, 0x05, 0x02, 0x00, 0x00, 0x33, 0x66, 0x9D, 0x98 };
            com1.Write(buff, 0, buff.Length);
            label1.Text = "注册完成!";
        }

读卡和注销模块:

 private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text = " ";
            byte[] buff = new byte[] { 0xff, 0x55, 0x00, 0x00, 0x03, 0x03, 0x01, 0x02, 0x0E, 0x70 };
            com1.Write(buff, 0, buff.Length);
            label1.Text = "读卡成功";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text = " ";
            byte[] buff = new byte[] { 0xff, 0x55, 0x00, 0x00, 0x03, 0x04, 0x05, 0x02, 0x00, 0x00, 0x00, 0x00, 0x47, 0x0C };
            com1.Write(buff, 0, buff.Length);
            label1.Text = "注销成功";
        }

4.奉上全部代码:

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

namespace 低频
{
    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.SelectedItem = 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;                 /*int.Parse(comboBox2.Text);*/     //获取选项框2的文本信息,并转换成整数;
                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;
                /*com1.DataReceived += new SerialDataReceivedEventHandler(button3_Click);*/
            }

            else if (com1.IsOpen)
            {
                com1.Close();
                button1.Text = "打开端口";
                button1.BackColor = Color.Red;
            }
        }

        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);

                if (buffer[9] == 0x33 && buffer[10] == 0x66)
                {
                    button5.Text = "开门";
                    button5.BackColor = Color.Green;
                }
                else
                {
                    button5.Text = "禁止入内";
                    button5.BackColor = Color.Red;
                }

                for (int i = 0; i < len; i++)
                {
                    textBox1.Text += buffer[i].ToString("X").PadLeft(2, '0') + " ";
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = " ";
            byte[] buff = new byte[] { 0xff, 0x55, 0x00, 0x00, 0x03, 0x04, 0x05, 0x02, 0x00, 0x00, 0x33, 0x66, 0x9D, 0x98 };
            com1.Write(buff, 0, buff.Length);
            label1.Text = "注册完成!";
        }

       

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text = " ";
            byte[] buff = new byte[] { 0xff, 0x55, 0x00, 0x00, 0x03, 0x03, 0x01, 0x02, 0x0E, 0x70 };
            com1.Write(buff, 0, buff.Length);
            label1.Text = "读卡成功";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text = " ";
            byte[] buff = new byte[] { 0xff, 0x55, 0x00, 0x00, 0x03, 0x04, 0x05, 0x02, 0x00, 0x00, 0x00, 0x00, 0x47, 0x0C };
            com1.Write(buff, 0, buff.Length);
            label1.Text = "注销成功";
        }
    }
    
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值