1.注册和打卡功能

初始化串口参数

private void InitSerialPort()
{
      serialPort1.BaudRate = 115200;//波特率115200
      serialPort1.DataBits = 8;
      serialPort1.Parity = Parity.None;
      serialPort1.StopBits = StopBits.One;
      serialPort1.ReceivedBytesThreshold = 1; // [TODO] 设置串口参数
}

读取职员编号

 if (serialPort1.BytesToRead > 0)
{
     response = serialPort1.ReadExisting();
     string stuffId = ISO15693CardHandler.GenerateBlockData(response);
     stuffId = stuffId.TrimStart('0');
     this.toolStripStatusLabel1.Text = "读取到职员编号为" + stuffId;
     return stuffId;
 }

寻卡

 string response = "";
 serialPort1.Write(ISO15693Card.COMMAND_WRITE_REG);
Thread.Sleep(MILLISECOND_IN_SLEEP);
if (serialPort1.BytesToRead > 0) response = serialPort1.ReadExisting(); serialPort1.Write(ISO15693Card.COMMAND_SET_AGC);
    Thread.Sleep(MILLISECOND_IN_SLEEP);
if (serialPort1.BytesToRead > 0) response = serialPort1.ReadExisting();   serialPort1.Write(ISO15693Card.COMMAND_SET_RECV_MODE);
    Thread.Sleep(MILLISECOND_IN_SLEEP);
if (serialPort1.BytesToRead > 0) response = serialPort1.ReadExisting();      serialPort1.Write(ISO15693Card.COMMAND_INVEN_CARD);//寻卡
   Thread.Sleep(MILLISECOND_IN_SLEEP);
if (serialPort1.BytesToRead > 0) response = serialPort1.ReadExisting();//获取返回的字符串
     List cards = ISO15693CardHandler.InventoryCard(response);//解析返回的字符串,获取寻到的卡ID

读取卡上第08块的数据,获得职员编号

 if (cards.Count > 0)
{
     string ID;
     this.toolStripStatusLabel1.Text = "已找到";
     ID = ReadSingleBlock(ISO15693CardHandler.CovertEndian(cards[0].ID), "08");
     if (str == ID)
     return;
     str = ID;
                   
                    // [TODO] 向数据库中插入一条打卡记录,插入成功后显示打卡成功
     String connStr = ConfigurationManager.ConnectionStrings["Attendance"].ConnectionString;
     SqlConnection sqlConn = new SqlConnection(connStr);
     try
 {
     this.toolStripStatusLabel1.Text = "";
     sqlConn.Open();
     String sqlStr = "insert into record(employee_id,date, time, machine_id) values(@id,@date,@time,'1')";
     SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
     string date = DateTime.Now.ToShortDateString();
     string time = DateTime.Now.ToLongTimeString();
     cmd.Parameters.Add(new SqlParameter("@id", ID));
     cmd.Parameters.Add(new SqlParameter("@date", date));
     cmd.Parameters.Add(new SqlParameter("@time", time));
     int res = cmd.ExecuteNonQuery();
     if (res != 0)
{
     this.toolStripStatusLabel1.Text = "打卡成功";

}
     else
{
     this.toolStripStatusLabel1.Text = "打卡失败";
}
}
     catch (Exception exp)
{
     MessageBox.Show("访问数据库错误:" + exp.Message);
}
     finally
{
     sqlConn.Close();
}
}
     else
{
     this.toolStripStatusLabel1.Text = "";
}
}

注册新卡,往第08块写入职员编号

 private void bt_Register_Click(object sender, EventArgs e)
{
      if (!serialPort1.IsOpen)
            {
                this.toolStripStatusLabel1.Text = "请先打开串口";
                return;
            }

       this.toolStripStatusLabel1.Text = "未找到有效的卡";

            // '666'填充为'00000666'
       string stuffId = this.tb_EmployeeId.Text.PadLeft(8, '0');
            // '00000666'转变成'66060000'
       stuffId = ISO15693CardHandler.CovertEndian(stuffId);

            // 检查输入数据的错误
            // ISO15693为32位,4字节,8字符
       if (stuffId.Length != 8)
            {
                MessageBox.Show("请输入4字节的16进制数据!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // 检查16进制字符错误
            if (!ISO15693CardHandler.CheckValidHexBytes(stuffId))
            {
                MessageBox.Show("写入数据的16进制格式错误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // [TODO] 寻卡,将RFID卡号读出来
            string response = "";
            serialPort1.Write(ISO15693Card.COMMAND_WRITE_REG);
            Thread.Sleep(MILLISECOND_IN_SLEEP);
            if (serialPort1.BytesToRead > 0) response = serialPort1.ReadExisting();serialPort1.Write(ISO15693Card.COMMAND_SET_AGC);
            Thread.Sleep(MILLISECOND_IN_SLEEP);
            if (serialPort1.BytesToRead > 0) response = serialPort1.ReadExisting();serialPort1.Write(ISO15693Card.COMMAND_SET_RECV_MODE);
            Thread.Sleep(MILLISECOND_IN_SLEEP);
            if (serialPort1.BytesToRead > 0) response = serialPort1.ReadExisting();serialPort1.Write(ISO15693Card.COMMAND_INVEN_CARD);//寻卡
            Thread.Sleep(MILLISECOND_IN_SLEEP);
            if (serialPort1.BytesToRead > 0) response = serialPort1.ReadExisting();//获取返回的字符串List cards = ISO15693CardHandler.InventoryCard(response);//解析返回的字符串,获取寻到的卡ID // [TODO] 向RFID卡第00块写入职员编号数据,无论成功与否,均应该在状态栏显示提示信息
            if(WriteSingleBlock(ISO15693CardHandler.CovertEndian(cards[0].ID), "08",stuffId))
            {
              this.toolStripStatusLabel1.Text = "写入成功";
            }
        }

你可能感兴趣的:(1.注册和打卡功能)