1.界面
2.代码
2.1查找串口
string[] ports = SerialPort.GetPortNames();//获取计算机可用串口
if (ports.Length > 0)//有可用串口
{
comboBoxCOMList.Items.AddRange(ports);//添加到下拉列表
comboBoxCOMList.SelectedIndex = 0;//默认选择第一项
}
2.2配置串口参数
serialPort1.BaudRate = 115200;//波特率115200
serialPort1.DataBits = 8;
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;
serialPort1.ReceivedBytesThreshold = 1;
2.3寻卡
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<ISO15693Card> cards = ISO15693CardHandler.InventoryCard(response);
2.4写入和读取
写入:
if (WriteSingleBlock(ISO15693CardHandler.CovertEndian(cards[0].ID), "00", stuffId))
{
this.toolStripStatusLabel2.Text = "写入成功";
}
else
{
this.toolStripStatusLabel2.Text = "写入失败";
读取:
ID = ReadSingleBlock(ISO15693CardHandler.CovertEndian(cards[0].ID), "00");
2.5用户提示
try
{
int res = cmd.ExecuteNonQuery();
// 根据返回值判断是否插入成功
if (res != 0)
{
this.toolStripStatusLabel2.Text = "打卡成功";
id = ID;
}
else
{
this.toolStripStatusLabel2.Text = "打卡失败";
}
sqlConn.Close();
}
catch(Exception ex)
{
this.toolStripStatusLabel1.Text = "打卡失败" + ex.Message;
this.toolStripStatusLabel2.Text = "";
}
}
else
{
this.toolStripStatusLabel1.Text ="请不重复打卡";
this.toolStripStatusLabel2.Text = "";
}
}
else
{
this.toolStripStatusLabel2.Text = "";
this.toolStripStatusLabel1.Text = "";
}