1.低频卡概述
低频卡通常指的是工作频率在10kHz至1MHz范围内的RFID卡,这类卡片由于其稳定的性能和较低的成本,在门禁系统、公交系统等多个领域得到了广泛应用。在物联网系统中,C#常用于与串口设备通信,包括低频RFID读卡器。下面我们将详细探讨如何使用C#进行串口通信以读取低频卡信息。
2.串口通信基础
在进行串口通信之前,首先需要确定几个基本的通信参数,包括波特率、数据位、停止位和奇偶校验位。波特率决定了串口通信的速度,数据位指定了每秒钟传送的位数,停止位用来标记数据的结束,而奇偶校验位则是为了保证数据准确性的校验机制。
3.C#串口编程
在C#中,可以使用System.IO.Ports.SerialPort
类来进行串口通信。首先需要实例化一个SerialPort对象,并通过其属性设置上述提到的通信参数。例如,设置波特率为9600,数据位为8,停止位为1,奇偶校验为None。
4.读取低频卡信息
读取低频卡信息一般涉及发送特定的命令到RFID读卡器,并接收读卡器返回的响应。在C#中,这通常涉及到以下几个步骤:
-
硬件连接:首先,您需要将低频卡与电脑相连,这通常通过串口、USB或其他通信接口实现。
-
驱动程序安装:确保您的低频卡驱动程序已正确安装,以便电脑能够识别和与低频卡通信。
-
串口配置:在Visual Studio中,您需要配置串口参数,如端口号、波特率、数据位、停止位和奇偶校验位,以匹配低频卡的通信要求。
-
数据发送与接收:编写代码以初始化通信接口,发送命令到低频卡,并接收来自低频卡的响应。您可以使用
System.IO.Ports.SerialPort
类来实现串口通信。 -
数据解析:接收到的数据通常需要根据特定的通信协议进行解析,以提取有用的信息。
5.低频卡读取应用案例
在实际应用中,低频卡读取技术可以用于门禁系统、考勤系统、停车场管理系统等。例如,在门禁系统中,通过读取员工的低频ID卡,系统可以控制人员的进出,提高安全性。在考勤系统中,可以记录员工的上下班时间,便于人事管理。而在停车场管理系统中,可以实现车辆的进出控制和收费。
1.创建项目
-
打开Visual Studio 2019,选择创建新项目。
-
选择C#语言后搜索Windows窗体应用,最后选择Windows窗体应用(.NET Framework)。
-
为项目命名,并完成项目的创建。
2.设置串口属性
-
在设计界面中,添加必要的控件,如文本框、按钮等,用于显示和输入数据。
-
在属性窗口中,设置串口的属性,如端口号、波特率、数据位、停止位和奇偶校验位。
全部代码;
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 串口设置
{
public partial class Form1 : Form
{
SerialPort com = new SerialPort("com1", 9600, Parity.None, 8, StopBits.One);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.SelectedIndex = 2;
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (!com.IsOpen)
{
com.PortName = comboBox1.Text;
com.Open();
button1.Text = "关闭串口";
}
else
{
com.Close();
button1.Text = "打开串口";
}
}
private void groupBox2_Enter(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)//手动发送接收
{
//com.Write("ABCDEFDG");
//com.Write(textBox1.Text);
byte[] ss = { 0xff, 0x55, 0x00, 0x00, 0x01, 0x01, 0x00, 0x50, 0x74 };//创建变量
com.Write(ss, 0, ss.Length);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
int len = com.BytesToRead;
byte[] rr = new byte[len];
com.Read(rr, 0, rr.Length);
for (int i = 0; i < len; i++)
{
textBox2.Text += rr[i].ToString("X").PadLeft(2, '0') + " ";
}
}
private void label3_Click(object sender, EventArgs e)
{
}
//public static byte[] getBytesFromString(string pString)
//{
// string[] str = pString. Split(' ');
// byte[] bytes = new byte[str.Length];
// for (int i= 0; i< str.Length;i++)
// bytes[i] = Convert. ToByte (Convert. ToInt32(str[i], 16));
// return bytes;
// }
//public static string getBytesFromString(byte[] pByte)
//{
// string str = "";
// for (int i = 0; i < pByte.Length; i++)
// str += (pByte[i].ToString("X").PadLeft(2, '0') + " ");
// str = str.TrimEnd(' ');
// return str;
//}
private byte[] covertToByte(String strl)
{
string[] tempStr = strl.Split(' ');
byte[] tempByte = new byte[tempStr.Length];
for (int i = 0; i < tempByte.Length; i++)
{
tempByte[i] = Convert.ToByte(tempStr[i], 16);
}
return tempByte;
}
private string getBytesFromString(byte[] byte1)
{
string tempStr = "";
for (int i = 0; i < byte1.Length; i++)
{
tempStr += (byte1[i].ToString("X").PadLeft(2, '0') + " ");
}
return tempStr;
}
public static byte[] crcl6(byte[] data, int len)
{
byte[] temdata = new byte[2];
int xda, xdapoly;
byte i, j, xdabit;
xda = 0xFFFF;
xdapoly = 0xA001;
for (i = 0; i < data.Length; i++)
{
xda ^= data[i];
for (j = 0; j < 8; j++)
{
xdabit = (byte)(xda & 0x01);
xda >>= 1;
if (xdabit == 1)
xda = xdapoly;
}
}
temdata[0] = (byte)(xda & 0xFF);
temdata[1] = (byte)(xda >> 8);
return temdata;
}
private void chuank_Click(object sender, EventArgs e)
{
}
}
}