diff --git a/RFIDTest.slnx b/RFIDTest.slnx new file mode 100644 index 0000000..b25cc6f --- /dev/null +++ b/RFIDTest.slnx @@ -0,0 +1,3 @@ + + + diff --git a/RFIDTest/BaseRFIDDataAnalyse.cs b/RFIDTest/BaseRFIDDataAnalyse.cs new file mode 100644 index 0000000..789dd8d --- /dev/null +++ b/RFIDTest/BaseRFIDDataAnalyse.cs @@ -0,0 +1,104 @@ +using HighWayIot.Rfid.Entity; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HighWayIot.Rfid +{ + /// + /// RFID基础数据分析 + /// + public class BaseRFIDDataAnalyse + { + + /// + /// 基础接收数据解析 + /// + /// + /// + public static BaseReciveDataEntity BaseReceiveAnalyse(byte[] data) + { + BaseReciveDataEntity result = new BaseReciveDataEntity(); + + int index = 0; + index += 2; + + //取Length + result.DataLength = data[index]; + index++; + + //取Code + result.Code = data[index]; + index++; + + //取Status + result.Status = data[index]; + index++; + + result.Data = new byte[result.DataLength]; + + //取Data + Array.Copy(data, 5, result.Data, 0, result.DataLength); + index += result.DataLength; + + //算Xor + result.Xor = data[index]; + int xor = 0; + for (int i = 0; i < 3 + result.DataLength; i++) + { + xor ^= data[i + 2]; + } + if(xor != data[data.Length - 2]) + { + //_logHelper.Error("数据校验和未通过"); + return null; + } + + return result; + } + + /// + /// 发送数据封装 + /// + /// + /// + public static byte[] BaseSendDataAnalyse(BaseSendDataEntity entity) + { + byte[] result = new byte[entity.Data.Length + 6]; + + int index = 2; + + //指令头 + result[0] = 0xAA; + result[1] = 0x55; + + //数据长度 + result[index] = (byte)entity.Data.Length; + index++; + + //指令编号 + result[index] = entity.Code; + index++; + + //数据 + Array.Copy(entity.Data, 0, result, 4, entity.Data.Length); + index += entity.Data.Length; + + //校验和 + int xor = 0; + for (int i = 0; i < 2 + entity.Data.Length; i++) + { + xor ^= result[i + 2]; + } + result[index] = (byte)xor; + index++; + + //帧尾 + result[index] = 0x0D; + + return result; + } + } +} diff --git a/RFIDTest/Program.cs b/RFIDTest/Program.cs new file mode 100644 index 0000000..77076f5 --- /dev/null +++ b/RFIDTest/Program.cs @@ -0,0 +1,17 @@ +namespace RFIDTest +{ + internal static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + // To customize application configuration such as set high DPI settings or default font, + // see https://aka.ms/applicationconfiguration. + ApplicationConfiguration.Initialize(); + Application.Run(new RFIDTest()); + } + } +} \ No newline at end of file diff --git a/RFIDTest/RFIDTest.Designer.cs b/RFIDTest/RFIDTest.Designer.cs new file mode 100644 index 0000000..3e997d2 --- /dev/null +++ b/RFIDTest/RFIDTest.Designer.cs @@ -0,0 +1,358 @@ +namespace RFIDTest +{ + partial class RFIDTest + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + WriteButton = new Button(); + ContentTextbox = new TextBox(); + StartReadButton = new Button(); + ResultTestbox = new TextBox(); + label1 = new Label(); + label2 = new Label(); + label3 = new Label(); + EPCTextbox = new TextBox(); + label4 = new Label(); + IPTextBox = new TextBox(); + PortTextBox = new TextBox(); + label5 = new Label(); + ConnectButton = new Button(); + HeartBeatTimeLabel = new Label(); + DisconnectButton = new Button(); + StopReadButton = new Button(); + PCResultTextbox = new TextBox(); + label8 = new Label(); + ReadStateLabel = new Label(); + label6 = new Label(); + SelectAddressTextbox = new TextBox(); + label9 = new Label(); + SelectLengthTextbox = new TextBox(); + label10 = new Label(); + WriteLengthTextbox = new TextBox(); + label11 = new Label(); + WriteAddressTextbox = new TextBox(); + SuspendLayout(); + // + // WriteButton + // + WriteButton.Location = new Point(106, 175); + WriteButton.Name = "WriteButton"; + WriteButton.Size = new Size(75, 23); + WriteButton.TabIndex = 0; + WriteButton.Text = "发送"; + WriteButton.UseVisualStyleBackColor = true; + WriteButton.Click += WriteButton_Click; + // + // ContentTextbox + // + ContentTextbox.Location = new Point(106, 262); + ContentTextbox.Name = "ContentTextbox"; + ContentTextbox.Size = new Size(621, 23); + ContentTextbox.TabIndex = 1; + // + // StartReadButton + // + StartReadButton.Location = new Point(106, 64); + StartReadButton.Name = "StartReadButton"; + StartReadButton.Size = new Size(75, 23); + StartReadButton.TabIndex = 2; + StartReadButton.Text = "开始盘点"; + StartReadButton.UseVisualStyleBackColor = true; + StartReadButton.Click += StartReadButton_Click; + // + // ResultTestbox + // + ResultTestbox.Location = new Point(106, 93); + ResultTestbox.Name = "ResultTestbox"; + ResultTestbox.Size = new Size(621, 23); + ResultTestbox.TabIndex = 3; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(44, 96); + label1.Name = "label1"; + label1.Size = new Size(56, 17); + label1.TabIndex = 4; + label1.Text = "盘点结果"; + // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(44, 265); + label2.Name = "label2"; + label2.Size = new Size(56, 17); + label2.TabIndex = 5; + label2.Text = "写入内容"; + // + // label3 + // + label3.AutoSize = true; + label3.Location = new Point(44, 236); + label3.Name = "label3"; + label3.Size = new Size(54, 17); + label3.TabIndex = 7; + label3.Text = "写入EPC"; + // + // EPCTextbox + // + EPCTextbox.Location = new Point(106, 233); + EPCTextbox.Name = "EPCTextbox"; + EPCTextbox.Size = new Size(621, 23); + EPCTextbox.TabIndex = 6; + // + // label4 + // + label4.AutoSize = true; + label4.Location = new Point(18, 364); + label4.Name = "label4"; + label4.Size = new Size(80, 17); + label4.TabIndex = 8; + label4.Text = "上次心跳时间"; + // + // IPTextBox + // + IPTextBox.Location = new Point(106, 10); + IPTextBox.Name = "IPTextBox"; + IPTextBox.Size = new Size(154, 23); + IPTextBox.TabIndex = 9; + IPTextBox.Text = "192.168.0.7"; + // + // PortTextBox + // + PortTextBox.Location = new Point(266, 10); + PortTextBox.Name = "PortTextBox"; + PortTextBox.Size = new Size(100, 23); + PortTextBox.TabIndex = 10; + PortTextBox.Text = "20108"; + // + // label5 + // + label5.AutoSize = true; + label5.Location = new Point(54, 13); + label5.Name = "label5"; + label5.Size = new Size(46, 17); + label5.TabIndex = 11; + label5.Text = "IP:Port"; + // + // ConnectButton + // + ConnectButton.Location = new Point(372, 10); + ConnectButton.Name = "ConnectButton"; + ConnectButton.Size = new Size(75, 23); + ConnectButton.TabIndex = 12; + ConnectButton.Text = "连接"; + ConnectButton.UseVisualStyleBackColor = true; + ConnectButton.Click += ConnectButton_Click; + // + // HeartBeatTimeLabel + // + HeartBeatTimeLabel.AutoSize = true; + HeartBeatTimeLabel.Location = new Point(106, 364); + HeartBeatTimeLabel.Name = "HeartBeatTimeLabel"; + HeartBeatTimeLabel.Size = new Size(56, 17); + HeartBeatTimeLabel.TabIndex = 13; + HeartBeatTimeLabel.Text = "00:00:00"; + // + // DisconnectButton + // + DisconnectButton.Location = new Point(453, 10); + DisconnectButton.Name = "DisconnectButton"; + DisconnectButton.Size = new Size(75, 23); + DisconnectButton.TabIndex = 14; + DisconnectButton.Text = "断开连接"; + DisconnectButton.UseVisualStyleBackColor = true; + DisconnectButton.Click += DisconnectButton_Click; + // + // StopReadButton + // + StopReadButton.Location = new Point(187, 64); + StopReadButton.Name = "StopReadButton"; + StopReadButton.Size = new Size(75, 23); + StopReadButton.TabIndex = 17; + StopReadButton.Text = "停止盘点"; + StopReadButton.UseVisualStyleBackColor = true; + StopReadButton.Click += StopReadButton_Click; + // + // PCResultTextbox + // + PCResultTextbox.Location = new Point(106, 122); + PCResultTextbox.Name = "PCResultTextbox"; + PCResultTextbox.Size = new Size(86, 23); + PCResultTextbox.TabIndex = 18; + // + // label8 + // + label8.AutoSize = true; + label8.Location = new Point(53, 125); + label8.Name = "label8"; + label8.Size = new Size(47, 17); + label8.TabIndex = 19; + label8.Text = "PC结果"; + // + // ReadStateLabel + // + ReadStateLabel.AutoSize = true; + ReadStateLabel.Location = new Point(299, 67); + ReadStateLabel.Name = "ReadStateLabel"; + ReadStateLabel.Size = new Size(32, 17); + ReadStateLabel.TabIndex = 20; + ReadStateLabel.Text = "停止"; + // + // label6 + // + label6.AutoSize = true; + label6.Location = new Point(21, 207); + label6.Name = "label6"; + label6.Size = new Size(79, 17); + label6.TabIndex = 22; + label6.Text = "选择地址(bit)"; + // + // SelectAddressTextbox + // + SelectAddressTextbox.Location = new Point(106, 204); + SelectAddressTextbox.Name = "SelectAddressTextbox"; + SelectAddressTextbox.Size = new Size(86, 23); + SelectAddressTextbox.TabIndex = 21; + // + // label9 + // + label9.AutoSize = true; + label9.Location = new Point(200, 207); + label9.Name = "label9"; + label9.Size = new Size(103, 17); + label9.TabIndex = 24; + label9.Text = "选择数据长度(bit)"; + // + // SelectLengthTextbox + // + SelectLengthTextbox.Location = new Point(309, 204); + SelectLengthTextbox.Name = "SelectLengthTextbox"; + SelectLengthTextbox.Size = new Size(86, 23); + SelectLengthTextbox.TabIndex = 23; + // + // label10 + // + label10.AutoSize = true; + label10.Location = new Point(555, 207); + label10.Name = "label10"; + label10.Size = new Size(80, 17); + label10.TabIndex = 28; + label10.Text = "写入数据长度"; + // + // WriteLengthTextbox + // + WriteLengthTextbox.Location = new Point(641, 204); + WriteLengthTextbox.Name = "WriteLengthTextbox"; + WriteLengthTextbox.Size = new Size(86, 23); + WriteLengthTextbox.TabIndex = 27; + // + // label11 + // + label11.AutoSize = true; + label11.Location = new Point(401, 207); + label11.Name = "label11"; + label11.Size = new Size(56, 17); + label11.TabIndex = 26; + label11.Text = "写入地址"; + // + // WriteAddressTextbox + // + WriteAddressTextbox.Location = new Point(463, 204); + WriteAddressTextbox.Name = "WriteAddressTextbox"; + WriteAddressTextbox.Size = new Size(86, 23); + WriteAddressTextbox.TabIndex = 25; + // + // RFIDTest + // + AutoScaleDimensions = new SizeF(7F, 17F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(786, 419); + Controls.Add(label10); + Controls.Add(WriteLengthTextbox); + Controls.Add(label11); + Controls.Add(WriteAddressTextbox); + Controls.Add(label9); + Controls.Add(SelectLengthTextbox); + Controls.Add(label6); + Controls.Add(SelectAddressTextbox); + Controls.Add(ReadStateLabel); + Controls.Add(label8); + Controls.Add(PCResultTextbox); + Controls.Add(StopReadButton); + Controls.Add(DisconnectButton); + Controls.Add(HeartBeatTimeLabel); + Controls.Add(ConnectButton); + Controls.Add(label5); + Controls.Add(PortTextBox); + Controls.Add(IPTextBox); + Controls.Add(label4); + Controls.Add(label3); + Controls.Add(EPCTextbox); + Controls.Add(label2); + Controls.Add(label1); + Controls.Add(ResultTestbox); + Controls.Add(StartReadButton); + Controls.Add(ContentTextbox); + Controls.Add(WriteButton); + Name = "RFIDTest"; + Text = "RFIDTestWInow"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Button WriteButton; + private TextBox ContentTextbox; + private Button StartReadButton; + private TextBox ResultTestbox; + private Label label1; + private Label label2; + private Label label3; + private TextBox EPCTextbox; + private Label label4; + private TextBox IPTextBox; + private TextBox PortTextBox; + private Label label5; + private Button ConnectButton; + private Label HeartBeatTimeLabel; + private Button DisconnectButton; + private Button StopReadButton; + private TextBox PCResultTextbox; + private Label label8; + private Label ReadStateLabel; + private Label label6; + private TextBox SelectAddressTextbox; + private Label label9; + private TextBox SelectLengthTextbox; + private Label label10; + private TextBox WriteLengthTextbox; + private Label label11; + private TextBox WriteAddressTextbox; + } +} diff --git a/RFIDTest/RFIDTest.cs b/RFIDTest/RFIDTest.cs new file mode 100644 index 0000000..a95fc44 --- /dev/null +++ b/RFIDTest/RFIDTest.cs @@ -0,0 +1,305 @@ +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("ʼ̵"); + } + + private void WriteButton_Click(object sender, EventArgs e) + { + if (!int.TryParse(SelectAddressTextbox.Text, out int selectAddress)) + { + MessageBox.Show("ѡַ"); + } + if (!int.TryParse(SelectLengthTextbox.Text, out int selectLength)) + { + MessageBox.Show("ѡݳȴ"); + } + if (!int.TryParse(WriteAddressTextbox.Text, out int writeAddress)) + { + MessageBox.Show("дַ"); + } + if (!int.TryParse(WriteLengthTextbox.Text, out int writeLength)) + { + MessageBox.Show("дַȴ"); + } + 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("ӳɹ"); + _tcpClient.Send(IPTextBox.Text, rfidDataAnalyse.SendBFH(5)); + + } + else + { + MessageBox.Show("ʧ"); + } + } + + /// + /// Խյݽгַ͵Ľ + /// + public void ReciveDataRoute(byte[] bytes, string ip) + { + BaseReciveDataEntity reciveData = BaseRFIDDataAnalyse.BaseReceiveAnalyse(bytes); + + if (reciveData == null) + { + return; + } + switch (reciveData.Code) + { + case 0x12: //̵ֹͣ + MessageBox.Show("̵ֹͣ"); + this.Invoke(new Action(() => + { + ReadStateLabel.Text = "ֹͣ"; + })); + break; + case 0x11: //̵ + if (reciveData.Status == 00) + { + this.Invoke(new Action(() => + { + ReadStateLabel.Text = "̵"; + })); + } + break; + case 0x01: //ǩ + if (reciveData.Status == 40) + { + this.Invoke(new Action(() => ResultTestbox.Text = "֡")); + } + 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: //ź + this.Invoke(new Action(() => + { + HeartBeatTimeLabel.Text = DateTime.Now.ToString("HH:mm:ss"); + })); + break; + case 0x03: + if (reciveData.Status == 00) + { + MessageBox.Show("дɹ"); + } + else if (reciveData.Status == 46) + { + MessageBox.Show("дʧ"); + } + else + { + MessageBox.Show("δ֪"); + } + break; + default: + + return; + } + } + + private void DisconnectButton_Click(object sender, EventArgs e) + { + if (_tcpClient.DisposeClient(IPTextBox.Text)) + { + MessageBox.Show("Ͽ"); + } + else + { + MessageBox.Show("Ͽʧ"); + } + } + + private void StopReadButton_Click(object sender, EventArgs e) + { + _tcpClient.Send(IPTextBox.Text, rfidDataAnalyse.Send12H()); + } + + /// + /// byteתΪʮַʽ00 0A 0B + /// + 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(); + } + + /// + /// ʮַתbyte + /// + public byte[] HexStringToBytes(string hexString) + { + if (string.IsNullOrWhiteSpace(hexString)) + { + MessageBox.Show("Ϊ"); + return new byte[0]; + } + + // Ƴпո + hexString = hexString.Replace(" ", ""); + + // 鳤ǷΪż + if (hexString.Length % 2 != 0) + MessageBox.Show("ʽȷ16ַΪż"); + + 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("ʽȷ"); } + + + return bytes; + } + + /// + /// תΪָλbyte飨 + /// + /// Ŀbyte鳤 + /// Ҫת + /// ָȵbyte + public static byte[] IntToBytes(int length, int num) + { + if (length <= 0) + throw new ArgumentException("ȱ0", nameof(length)); + + byte[] result = new byte[length]; + + // λʼ + for (int i = 0; i < length; i++) + { + // 㵱ǰֽڵλãӸλλ + // length=2, i=0 ʱλ 8 λi=1 ʱλ 0 λ + int shift = (length - 1 - i) * 8; + result[i] = (byte)((num >> shift) & 0xFF); + } + + return result; + } + + /// + /// byteתΪ + /// + public static int BytesToInt(byte[] bytes) + { + if (bytes == null || bytes.Length == 0) + MessageBox.Show("ֽ鲻Ϊ"); + + if (bytes.Length > 4) + MessageBox.Show("ֽ鳤Ȳܳ4int4ֽڣ"); + + int result = 0; + + // Ӹλλϣ + for (int i = 0; i < bytes.Length; i++) + { + result = (result << 8) | bytes[i]; + } + + return result; + } + + /// + /// byteתΪС + /// + public static int BytesToIntLittleEndian(byte[] bytes) + { + if (bytes == null || bytes.Length == 0) + throw new ArgumentException("ֽ鲻Ϊ", nameof(bytes)); + + if (bytes.Length > 4) + throw new ArgumentException("ֽ鳤Ȳܳ4int4ֽڣ", nameof(bytes)); + + int result = 0; + + // ӵλλϣС + for (int i = 0; i < bytes.Length; i++) + { + result |= (bytes[i] << (i * 8)); + } + + return result; + } + } +} diff --git a/RFIDTest/RFIDTest.csproj b/RFIDTest/RFIDTest.csproj new file mode 100644 index 0000000..2790e61 --- /dev/null +++ b/RFIDTest/RFIDTest.csproj @@ -0,0 +1,15 @@ + + + + WinExe + net8.0-windows + enable + true + enable + + + + + + + \ No newline at end of file diff --git a/RFIDTest/RFIDTest.resx b/RFIDTest/RFIDTest.resx new file mode 100644 index 0000000..8b2ff64 --- /dev/null +++ b/RFIDTest/RFIDTest.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/RFIDTest/RfidDataAnalyse.cs b/RFIDTest/RfidDataAnalyse.cs new file mode 100644 index 0000000..66625ad --- /dev/null +++ b/RFIDTest/RfidDataAnalyse.cs @@ -0,0 +1,258 @@ +using HighWayIot.Rfid.Entity; +using RFIDTest.entity; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace HighWayIot.Rfid +{ + public class RfidDataAnalyse + { + /// + /// 02H时间段盘点发送 + /// + /// 盘点时间毫秒数 + /// + public byte[] Send02H(ushort millisecond) + { + byte[] bytes = BitConverter.GetBytes(millisecond); + bytes = bytes.Reverse().ToArray(); + BaseSendDataEntity entity = new BaseSendDataEntity() + { + Code = 0x02, + Data = bytes + }; + + byte[] result = BaseRFIDDataAnalyse.BaseSendDataAnalyse(entity); + + return result; + } + + /// + /// 接受02H盘点数据 (默认EPC 4位) + /// + /// 数据体 + /// + public Receive02HEntity Receive02H(byte[] data) + { + Receive02HEntity entity = new Receive02HEntity(); + + int index = 0; + + if (data.Length != 0) + { + //取读到多少个标签 + entity.TagCount = data[index]; + entity.Data = new List(); + index++; + } + else + { + entity.TagCount = 0; + } + + //取每一个读到的标签 + for (int i = 0; i < entity.TagCount; i++) + { + Single02HReceive EPCData = new Single02HReceive(); + + //取单个标签读取的次数 + EPCData.Count = data[index]; + index++; + + //取信号强度 + EPCData.RSSI = data[index]; + index++; + + //取天线端口 + EPCData.Ant = data[index]; + index++; + + //取EPC区域 + EPCData.PC = new byte[2]; + Array.Copy(data, index, EPCData.PC, 0, 2); + index += 2; + + //取读到标签的EPC + EPCData.EPC = new byte[12]; + try + { + Array.Copy(data, index, EPCData.EPC, 0, 12); + } + catch (Exception ex) + { + return null; + } + index += 12; + + entity.Data.Add(EPCData); + } + + return entity; + } + + /// + /// 发送心跳配置包 + /// + /// + public byte[] SendBFH(byte second) + { + byte[] data = new byte[3]; + data[0] = 0x00; + data[1] = second; + data[2] = 0x01; + + BaseSendDataEntity entity = new BaseSendDataEntity() + { + Code = 0xBF, + Data = data + }; + + byte[] result = BaseRFIDDataAnalyse.BaseSendDataAnalyse(entity); + + return result; + } + + /// + /// 接收心跳包 + /// + /// + public uint ReceiveBFH(byte[] data) + { + uint result = BitConverter.ToUInt32(data, 0); + return result; + } + + /// + /// 发送连续盘点请求 + /// + /// + /// + public byte[] Send11H() + { + byte[] bytes = new byte[2] { 0x00, 0x00 }; + //byte[] bytes = BitConverter.GetBytes(millisecond); + //bytes = bytes.Reverse().ToArray(); + BaseSendDataEntity entity = new BaseSendDataEntity() + { + Code = 0x11, + Data = bytes + }; + byte[] result = BaseRFIDDataAnalyse.BaseSendDataAnalyse(entity); + return result; + } + + /// + /// 发送停止盘点请求 + /// + /// + public byte[] Send12H() + { + byte[] bytes = new byte[2] { 0x00, 0x00 }; + //byte[] bytes = BitConverter.GetBytes(millisecond); + //bytes = bytes.Reverse().ToArray(); + BaseSendDataEntity entity = new BaseSendDataEntity() + { + Code = 0x12, + Data = bytes + }; + byte[] result = BaseRFIDDataAnalyse.BaseSendDataAnalyse(entity); + return result; + } + + /// + /// 接受连续盘点结果 + /// + /// + /// + public ReciveResult11HEntity Receive11H(byte[] data) + { + int index = 0; + ReciveResult11HEntity entity = new ReciveResult11HEntity(); + entity.Count = data[index]; + index++; + + entity.RSSI = data[index]; + index++; + + entity.Ant = data[index]; + index++; + + entity.PC = new byte[2]; + Array.Copy(data, index, entity.PC, 0, 2); + index += 2; + + int restLength = data.Length - index; + + entity.EPC = new byte[restLength]; + Array.Copy(data, index, entity.EPC, 0, restLength); + index += 12; + + return entity; + } + + /// + /// 发送写入请求 + /// + /// + public byte[] Send03H(Base03HENtity rawData) + { + int selectLength = rawData.SelectLength / 8; + int wordCount = rawData.WordCount * 2; + byte[] bytes = new byte[2 + 4 + 1 + 4 + 1 + selectLength + 1 + 4 + 1 + wordCount]; + + int index = 0; + + Array.Copy(rawData.TimeOut, 0, bytes, index, 2); + index += 2; + + Array.Copy(rawData.AccessPassword, 0, bytes, index, 4); + index += 4; + + bytes[index] = rawData.SelectBank; + index++; + + Array.Copy(rawData.SelectAddress, 0, bytes, index, 4); + index += 4; + + bytes[index] = rawData.SelectLength; + index++; + + Array.Copy(rawData.SelectData, 0, bytes, index, selectLength); + index += selectLength; + + bytes[index] = rawData.WriteBank; + index++; + + Array.Copy(rawData.WriteAddress, 0, bytes, index, 4); + index += 4; + + bytes[index] = rawData.WordCount; + index++; + + Array.Copy(rawData.WriteData, 0, bytes, index, wordCount); + index += wordCount; + + BaseSendDataEntity entity = new BaseSendDataEntity() + { + Code = 0x03, + Data = bytes + }; + byte[] result = BaseRFIDDataAnalyse.BaseSendDataAnalyse(entity); + return result; + } + + /// + /// 发送功率设置包 + /// + /// + /// + public byte[] Send42H(int power) + { + return new byte[0]; + } + } +} diff --git a/RFIDTest/TouchSocketTcpClient.cs b/RFIDTest/TouchSocketTcpClient.cs new file mode 100644 index 0000000..65f4689 --- /dev/null +++ b/RFIDTest/TouchSocketTcpClient.cs @@ -0,0 +1,150 @@ +using System; +using System.Collections.Generic; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; +using TouchSocket.Core; +using TouchSocket.Sockets; +using TcpClient = TouchSocket.Sockets.TcpClient; + +namespace HighWayIot.TouchSocket +{ + public class TouchSocketTcpClient + { + + /// + /// 懒加载 + /// + private static readonly Lazy lazy = new Lazy(() => new TouchSocketTcpClient()); + + public static TouchSocketTcpClient Instance => lazy.Value; + + /// + /// 所有的客户端 + /// + public Dictionary Clients = new Dictionary(); + + public int ClientsCount = 0; + + public Action GetMessageAction; + + public bool CreateTcpClient(string ip, string port) + { + TcpClient tcpClient = new TcpClient(); + + tcpClient.Connecting = (client, e) => + { + return EasyTask.CompletedTask; + };//有客户端正在连接 + tcpClient.Connected = (client, e) => + { + return EasyTask.CompletedTask; + };//有客户端成功连接 + tcpClient.Closing = (client, e) => + { + return EasyTask.CompletedTask; + };//有客户端正在断开连接,只有当主动断开时才有效。 + tcpClient.Closed = (client, e) => + { + return EasyTask.CompletedTask; + };//有客户端断开连接 + tcpClient.Received = (client, e) => + { + var mes = e.Memory.Span.ToString(Encoding.UTF8); + GetMessageAction.Invoke(e.Memory.Span.ToArray(), client.IP); + tcpClient.Logger.Info($"客户端接收到信息:{mes}"); + return EasyTask.CompletedTask; + //GetMessageAction.Invoke(e.ByteBlock.Span.ToArray(), client.IP); + //return EasyTask.CompletedTask; + }; //接收信号 + + tcpClient.SetupAsync(new TouchSocketConfig() + .SetRemoteIPHost($"{ip}:{port}") + .ConfigureContainer(a => + { + a.AddConsoleLogger();//添加一个日志注入 + }) + .ConfigurePlugins(a => + { + a.UseReconnection(options => + { + options.PollingInterval = TimeSpan.FromSeconds(1); + }); + }) + ); + + Result result = Result.Default; //不断尝试重连 + do + { + result = tcpClient.TryConnectAsync().GetAwaiter().GetResult(); + Task.Delay(1000).Wait(); + } + while (!result.IsSuccess); + + if (Clients.ContainsKey(ip)) + { + Clients.Remove(ip); + Clients.Add(ip, tcpClient); + } + else + { + Clients.Add(ip, tcpClient); + } + + return true; + } + + /// + /// 信息发送 + /// + /// Bytes + /// + public bool Send(string ip, byte[] message) + { + try + { + if (Clients.ContainsKey(ip)) + { + Clients[ip].SendAsync(message); + return true; + } + else + { + return false; + } + } + catch (Exception e) + { + return false; + } + } + + /// + /// 客户端释放 + /// + /// + /// + public bool DisposeClient(string ip) + { + try + { + if (Clients.ContainsKey(ip)) + { + Clients[ip].CloseAsync(); + Clients[ip].Dispose(); + Clients.Remove(ip); + } + else + { + return false; + } + return true; + } + catch (Exception e) + { + return false; + } + } + + } +} diff --git a/RFIDTest/entity/BFHEntity.cs b/RFIDTest/entity/BFHEntity.cs new file mode 100644 index 0000000..d2ff58b --- /dev/null +++ b/RFIDTest/entity/BFHEntity.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HighWayIot.Rfid.Entity +{ + /// + /// 心跳设置包 + /// + public class BFHSendEntity + { + /// + /// 返回模式(00严格按时间 01空闲返回) + /// + public byte Mode { get; set; } + + /// + /// 返回时间(单位秒) + /// + public byte Time { get; set; } + + /// + /// 保留(一直等于01就行) + /// + public byte Rev { get; set; } + } +} diff --git a/RFIDTest/entity/Base03HEntity.cs b/RFIDTest/entity/Base03HEntity.cs new file mode 100644 index 0000000..f43da75 --- /dev/null +++ b/RFIDTest/entity/Base03HEntity.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RFIDTest.entity +{ + public class Base03HENtity + { + /// + /// 超时时间 + /// + public byte[] TimeOut { get; set; } + + /// + /// 访问密码 + /// + public byte[] AccessPassword { get; set; } + + /// + /// 选择区域,01 EPC, 02 TID, 03 User + /// + public byte SelectBank { get; set; } + + /// + /// 选择地址 + /// + public byte[] SelectAddress { get; set; } + + /// + /// 选择匹配数据长度 + /// + public byte SelectLength { get; set; } + + /// + /// 选择匹配数据 + /// + public byte[] SelectData { get; set; } + + /// + /// 写入标签区域,00 Reserved, 01 EPC, 02 TID, 03 User + /// + public byte WriteBank { get; set; } + + /// + /// 写入地址(word) + /// + public byte[] WriteAddress { get; set; } + + /// + /// 写入数据量(word) + /// + public byte WordCount { get; set; } + + /// + /// 写入数据 + /// + public byte[] WriteData { get; set; } + } +} diff --git a/RFIDTest/entity/Base11HEntity.cs b/RFIDTest/entity/Base11HEntity.cs new file mode 100644 index 0000000..80481a7 --- /dev/null +++ b/RFIDTest/entity/Base11HEntity.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RFIDTest.entity +{ + public class Send11HEntity + { + public byte[] TimeOut { get; set; } + } + + public class ReciveResult11HEntity + { + public byte Count { get; set; } + + /// + /// 信号强度 + /// + public byte RSSI { get; set; } + + /// + /// 天线端口 + /// + public byte Ant { get; set; } + + /// + /// EPC区域 10h-20h + /// + public byte[] PC { get; set; } + + /// + /// 读到的EPC内容 + /// + public byte[] EPC { get; set; } + } +} diff --git a/RFIDTest/entity/BaseReciveDataEntity.cs b/RFIDTest/entity/BaseReciveDataEntity.cs new file mode 100644 index 0000000..d4469d2 --- /dev/null +++ b/RFIDTest/entity/BaseReciveDataEntity.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HighWayIot.Rfid.Entity +{ + /// + /// 接收数据基础类 + /// + public class BaseReciveDataEntity + { + /// + /// 数据长度2 + /// + public byte DataLength { get; set; } + + /// + /// 指令编号 + /// + public byte Code { get; set; } + + /// + /// 状态码 + /// + public byte Status { get; set; } + + /// + /// 数据 + /// + public byte[] Data { get; set; } + + /// + /// 校验位 + /// + public byte Xor { get; set; } + } +} diff --git a/RFIDTest/entity/BaseSendDataEntity.cs b/RFIDTest/entity/BaseSendDataEntity.cs new file mode 100644 index 0000000..ebde34c --- /dev/null +++ b/RFIDTest/entity/BaseSendDataEntity.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HighWayIot.Rfid.Entity +{ + /// + /// 发送数据基础类 + /// + public class BaseSendDataEntity + { + /// + /// 指令编号 + /// + public byte Code { get; set; } + + /// + /// 数据 + /// + public byte[] Data { get; set; } + } +} diff --git a/RFIDTest/entity/Receive02HEntity.cs b/RFIDTest/entity/Receive02HEntity.cs new file mode 100644 index 0000000..d6f556b --- /dev/null +++ b/RFIDTest/entity/Receive02HEntity.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using System.Threading.Tasks; + +namespace HighWayIot.Rfid.Entity +{ + public class Receive02HEntity + { + /// + /// 标签个数 + /// + public byte TagCount { get; set; } + + /// + /// 时间段内读到的数据集合 + /// + public List Data { get; set; } + + } + + public class Single02HReceive + { + /// + /// 读取次数 + /// + public byte Count { get; set; } + + /// + /// 信号强度 + /// + public byte RSSI { get; set; } + + /// + /// 天线端口 + /// + public byte Ant { get; set; } + + /// + /// EPC区域 + /// + public byte[] PC { get; set; } + + /// + /// EPC内容 + /// + public byte[] EPC { get; set; } + } +}