|
|
|
|
|
using HighWayIot.Rfid;
|
|
|
|
|
|
using HighWayIot.Rfid.Entity;
|
|
|
|
|
|
using HighWayIot.TouchSocket;
|
|
|
|
|
|
using RFIDTest.entity;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
|
|
|
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
|
|
|
|
|
|
|
|
|
|
|
namespace RFIDTest
|
|
|
|
|
|
{
|
|
|
|
|
|
public partial class RFIDTest : Form
|
|
|
|
|
|
{
|
|
|
|
|
|
TouchSocketTcpClient _tcpClient = TouchSocketTcpClient.Instance;
|
|
|
|
|
|
|
|
|
|
|
|
RfidDataAnalyse rfidDataAnalyse = new RfidDataAnalyse();
|
|
|
|
|
|
|
|
|
|
|
|
public RFIDTest()
|
|
|
|
|
|
{
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
_tcpClient.GetMessageAction += ReciveDataRoute;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void StartReadButton_Click(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
_tcpClient.Send(IPTextBox.Text, rfidDataAnalyse.Send11H());
|
|
|
|
|
|
ResultTestbox.Text = string.Empty;
|
|
|
|
|
|
PCResultTextbox.Text = string.Empty;
|
|
|
|
|
|
//MessageBox.Show("<22><>ʼ<EFBFBD>̵<EFBFBD>");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void WriteButton_Click(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!int.TryParse(SelectAddressTextbox.Text, out int selectAddress))
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBox.Show("ѡ<><D1A1><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!int.TryParse(SelectLengthTextbox.Text, out int selectLength))
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBox.Show("ѡ<><D1A1><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3>ȴ<EFBFBD><C8B4><EFBFBD>");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!int.TryParse(WriteAddressTextbox.Text, out int writeAddress))
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBox.Show("д<><D0B4><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!int.TryParse(WriteLengthTextbox.Text, out int writeLength))
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBox.Show("д<><D0B4><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD>");
|
|
|
|
|
|
}
|
|
|
|
|
|
Base03HENtity entity = new Base03HENtity()
|
|
|
|
|
|
{
|
|
|
|
|
|
TimeOut = IntToBytes(2, 1000),
|
|
|
|
|
|
AccessPassword = IntToBytes(4, 0),
|
|
|
|
|
|
SelectBank = 01,
|
|
|
|
|
|
SelectAddress = IntToBytes(4, selectAddress),
|
|
|
|
|
|
SelectLength = (byte)selectLength,
|
|
|
|
|
|
SelectData = HexStringToBytes(EPCTextbox.Text),
|
|
|
|
|
|
WriteBank = 01,
|
|
|
|
|
|
WriteAddress = IntToBytes(4, writeAddress),
|
|
|
|
|
|
WordCount = (byte)writeLength,
|
|
|
|
|
|
WriteData = HexStringToBytes(ContentTextbox.Text)
|
|
|
|
|
|
};
|
|
|
|
|
|
byte[] data = rfidDataAnalyse.Send03H(entity);
|
|
|
|
|
|
_tcpClient.Send(IPTextBox.Text, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void ConnectButton_Click(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_tcpClient.CreateTcpClient(IPTextBox.Text, PortTextBox.Text))
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBox.Show("<22><><EFBFBD>ӳɹ<D3B3>");
|
|
|
|
|
|
_tcpClient.Send(IPTextBox.Text, rfidDataAnalyse.SendBFH(5));
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBox.Show("<22><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// <20>Խ<EFBFBD><D4BD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><DDBD>г<EFBFBD><D0B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵Ľ<CDB5><C4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void ReciveDataRoute(byte[] bytes, string ip)
|
|
|
|
|
|
{
|
|
|
|
|
|
BaseReciveDataEntity reciveData = BaseRFIDDataAnalyse.BaseReceiveAnalyse(bytes);
|
|
|
|
|
|
|
|
|
|
|
|
if (reciveData == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
switch (reciveData.Code)
|
|
|
|
|
|
{
|
|
|
|
|
|
case 0x12: //ֹͣ<CDA3>̵<EFBFBD>
|
|
|
|
|
|
MessageBox.Show("ֹͣ<CDA3>̵<EFBFBD>");
|
|
|
|
|
|
this.Invoke(new Action(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ReadStateLabel.Text = "ֹͣ";
|
|
|
|
|
|
}));
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 0x11: //<2F>̵<EFBFBD><CCB5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
if (reciveData.Status == 00)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.Invoke(new Action(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ReadStateLabel.Text = "<22>̵<EFBFBD><CCB5><EFBFBD>";
|
|
|
|
|
|
}));
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 0x01: //<2F><><EFBFBD><EFBFBD>ǩ<EFBFBD><C7A9><EFBFBD><EFBFBD>
|
|
|
|
|
|
if (reciveData.Status == 40)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.Invoke(new Action(() => ResultTestbox.Text = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֡"));
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (reciveData.Status == 00)
|
|
|
|
|
|
{
|
|
|
|
|
|
var res = rfidDataAnalyse.Receive11H(reciveData.Data);
|
|
|
|
|
|
string PC = BytesToHexString(res.PC);
|
|
|
|
|
|
string data = BytesToHexString(res.EPC);
|
|
|
|
|
|
this.Invoke(new Action(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ResultTestbox.Text = data;
|
|
|
|
|
|
PCResultTextbox.Text = PC;
|
|
|
|
|
|
ReadStateLabel.Text = "ֹͣ";
|
|
|
|
|
|
|
|
|
|
|
|
EPCTextbox.Text = data;
|
|
|
|
|
|
ContentTextbox.Text = PC + " " + data;
|
|
|
|
|
|
|
|
|
|
|
|
SelectAddressTextbox.Text = "32";
|
|
|
|
|
|
SelectLengthTextbox.Text = (res.EPC.Count() * 8).ToString();
|
|
|
|
|
|
WriteAddressTextbox.Text = "1";
|
|
|
|
|
|
WriteLengthTextbox.Text = ((res.EPC.Count() / 2) + 1).ToString();
|
|
|
|
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 0xBF: //<2F><><EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD>
|
|
|
|
|
|
this.Invoke(new Action(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
HeartBeatTimeLabel.Text = DateTime.Now.ToString("HH:mm:ss");
|
|
|
|
|
|
}));
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 0x03:
|
|
|
|
|
|
if (reciveData.Status == 00)
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBox.Show("д<><D0B4><EFBFBD>ɹ<EFBFBD>");
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (reciveData.Status == 46)
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBox.Show("д<><D0B4>ʧ<EFBFBD><CAA7>");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBox.Show("δ֪<CEB4><D6AA><EFBFBD><EFBFBD>");
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void DisconnectButton_Click(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_tcpClient.DisposeClient(IPTextBox.Text))
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBox.Show("<22>Ͽ<EFBFBD><CFBF><EFBFBD><EFBFBD><EFBFBD>");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBox.Show("<22>Ͽ<EFBFBD><CFBF><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void StopReadButton_Click(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
_tcpClient.Send(IPTextBox.Text, rfidDataAnalyse.Send12H());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// <20><>byte<74><65><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊʮ<CEAA><CAAE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>00 0A 0B<30><42>
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string BytesToHexString(byte[] bytes)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (bytes == null || bytes.Length == 0)
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < bytes.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
sb.Append(bytes[i].ToString("X2"));
|
|
|
|
|
|
if (i < bytes.Length - 1)
|
|
|
|
|
|
sb.Append(" ");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// <20><>ʮ<EFBFBD><CAAE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>byte<74><65><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public byte[] HexStringToBytes(string hexString)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(hexString))
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBox.Show("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>");
|
|
|
|
|
|
return new byte[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// <20>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD>пո<D0BF>
|
|
|
|
|
|
hexString = hexString.Replace(" ", "");
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>鳤<EFBFBD><E9B3A4><EFBFBD>Ƿ<EFBFBD>Ϊż<CEAA><C5BC>
|
|
|
|
|
|
if (hexString.Length % 2 != 0)
|
|
|
|
|
|
MessageBox.Show("<22><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>ȷ16<31><36><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊż<CEAA><C5BC>");
|
|
|
|
|
|
|
|
|
|
|
|
byte[] bytes = new byte[hexString.Length / 2];
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < bytes.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
bytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch { MessageBox.Show("<22><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>ȷ"); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊָ<CEAA><D6B8>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>byte<74><65><EFBFBD>飨<EFBFBD><E9A3A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="length">Ŀ<><C4BF>byte<74><65><EFBFBD>鳤<EFBFBD><E9B3A4></param>
|
|
|
|
|
|
/// <param name="num">Ҫת<D2AA><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|
|
|
|
|
/// <returns>ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD>byte<74><65><EFBFBD><EFBFBD></returns>
|
|
|
|
|
|
public static byte[] IntToBytes(int length, int num)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (length <= 0)
|
|
|
|
|
|
throw new ArgumentException("<22><><EFBFBD>ȱ<EFBFBD><C8B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0", nameof(length));
|
|
|
|
|
|
|
|
|
|
|
|
byte[] result = new byte[length];
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
|
|
|
|
|
|
for (int i = 0; i < length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
// <20><><EFBFBD>㵱ǰ<E3B5B1>ֽڵ<D6BD>λ<EFBFBD>ã<EFBFBD><C3A3>Ӹ<EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> length=2, i=0 ʱ<><CAB1><EFBFBD><EFBFBD>λ 8 λ<><CEBB>i=1 ʱ<><CAB1><EFBFBD><EFBFBD>λ 0 λ
|
|
|
|
|
|
int shift = (length - 1 - i) * 8;
|
|
|
|
|
|
result[i] = (byte)((num >> shift) & 0xFF);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// <20><>byte<74><65><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static int BytesToInt(byte[] bytes)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (bytes == null || bytes.Length == 0)
|
|
|
|
|
|
MessageBox.Show("<22>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD>鲻<EFBFBD><E9B2BB>Ϊ<EFBFBD><CEAA>");
|
|
|
|
|
|
|
|
|
|
|
|
if (bytes.Length > 4)
|
|
|
|
|
|
MessageBox.Show("<22>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD>鳤<EFBFBD>Ȳ<EFBFBD><C8B2>ܳ<EFBFBD><DCB3><EFBFBD>4<EFBFBD><34>int<6E><74><EFBFBD><EFBFBD>4<EFBFBD>ֽڣ<D6BD>");
|
|
|
|
|
|
|
|
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// <20>Ӹ<EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
for (int i = 0; i < bytes.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
result = (result << 8) | bytes[i];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// <20><>byte<74><65><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static int BytesToIntLittleEndian(byte[] bytes)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (bytes == null || bytes.Length == 0)
|
|
|
|
|
|
throw new ArgumentException("<22>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD>鲻<EFBFBD><E9B2BB>Ϊ<EFBFBD><CEAA>", nameof(bytes));
|
|
|
|
|
|
|
|
|
|
|
|
if (bytes.Length > 4)
|
|
|
|
|
|
throw new ArgumentException("<22>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD>鳤<EFBFBD>Ȳ<EFBFBD><C8B2>ܳ<EFBFBD><DCB3><EFBFBD>4<EFBFBD><34>int<6E><74><EFBFBD><EFBFBD>4<EFBFBD>ֽڣ<D6BD>", nameof(bytes));
|
|
|
|
|
|
|
|
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// <20>ӵ<EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD>ϣ<EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
for (int i = 0; i < bytes.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
result |= (bytes[i] << (i * 8));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|