添加项目文件。

master
SoulStar 2 months ago
parent 2db024ff13
commit 9fbe91580e

@ -0,0 +1,3 @@
<Solution>
<Project Path="RFIDTest/RFIDTest.csproj" />
</Solution>

@ -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
{
/// <summary>
/// RFID基础数据分析
/// </summary>
public class BaseRFIDDataAnalyse
{
/// <summary>
/// 基础接收数据解析
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 发送数据封装
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
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;
}
}
}

@ -0,0 +1,17 @@
namespace RFIDTest
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[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());
}
}
}

@ -0,0 +1,358 @@
namespace RFIDTest
{
partial class RFIDTest
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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;
}
}

@ -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("连接失败");
}
}
/// <summary>
/// 对接收到的数据进行初步解析分发到各个报文类型的解析类中
/// </summary>
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());
}
/// <summary>
/// 将byte数组转换为十六进制字符串格式00 0A 0B
/// </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>
/// 将十六进制字符串转换回byte数组
/// </summary>
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;
}
/// <summary>
/// 将整数转换为指定位数的byte数组大端序
/// </summary>
/// <param name="length">目标byte数组长度</param>
/// <param name="num">要转换的整数</param>
/// <returns>指定长度的byte数组</returns>
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;
}
/// <summary>
/// 将byte数组转换为整数大端序
/// </summary>
public static int BytesToInt(byte[] bytes)
{
if (bytes == null || bytes.Length == 0)
MessageBox.Show("字节数组不能为空");
if (bytes.Length > 4)
MessageBox.Show("字节数组长度不能超过4int最大4字节");
int result = 0;
// 从高位到低位组合(大端序)
for (int i = 0; i < bytes.Length; i++)
{
result = (result << 8) | bytes[i];
}
return result;
}
/// <summary>
/// 将byte数组转换为整数小端序
/// </summary>
public static int BytesToIntLittleEndian(byte[] bytes)
{
if (bytes == null || bytes.Length == 0)
throw new ArgumentException("字节数组不能为空", nameof(bytes));
if (bytes.Length > 4)
throw new ArgumentException("字节数组长度不能超过4int最大4字节", nameof(bytes));
int result = 0;
// 从低位到高位组合(小端序)
for (int i = 0; i < bytes.Length; i++)
{
result |= (bytes[i] << (i * 8));
}
return result;
}
}
}

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="TouchSocket" Version="4.2.4" />
</ItemGroup>
</Project>

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

@ -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
{
/// <summary>
/// 02H时间段盘点发送
/// </summary>
/// <param name="millisecond">盘点时间毫秒数</param>
/// <returns></returns>
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;
}
/// <summary>
/// 接受02H盘点数据 (默认EPC 4位)
/// </summary>
/// <param name="data">数据体</param>
/// <returns></returns>
public Receive02HEntity Receive02H(byte[] data)
{
Receive02HEntity entity = new Receive02HEntity();
int index = 0;
if (data.Length != 0)
{
//取读到多少个标签
entity.TagCount = data[index];
entity.Data = new List<Single02HReceive>();
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;
}
/// <summary>
/// 发送心跳配置包
/// </summary>
/// <returns></returns>
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;
}
/// <summary>
/// 接收心跳包
/// </summary>
/// <returns></returns>
public uint ReceiveBFH(byte[] data)
{
uint result = BitConverter.ToUInt32(data, 0);
return result;
}
/// <summary>
/// 发送连续盘点请求
/// </summary>
/// <param name="millisecond"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 发送停止盘点请求
/// </summary>
/// <returns></returns>
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;
}
/// <summary>
/// 接受连续盘点结果
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 发送写入请求
/// </summary>
/// <returns></returns>
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;
}
/// <summary>
/// 发送功率设置包
/// </summary>
/// <param name="power"></param>
/// <returns></returns>
public byte[] Send42H(int power)
{
return new byte[0];
}
}
}

@ -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
{
/// <summary>
/// 懒加载
/// </summary>
private static readonly Lazy<TouchSocketTcpClient> lazy = new Lazy<TouchSocketTcpClient>(() => new TouchSocketTcpClient());
public static TouchSocketTcpClient Instance => lazy.Value;
/// <summary>
/// 所有的客户端
/// </summary>
public Dictionary<string, TcpClient> Clients = new Dictionary<string, TcpClient>();
public int ClientsCount = 0;
public Action<byte[], string> 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<TcpClient>(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;
}
/// <summary>
/// 信息发送
/// </summary>
/// <param name="message">Bytes</param>
/// <returns></returns>
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;
}
}
/// <summary>
/// 客户端释放
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
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;
}
}
}
}

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HighWayIot.Rfid.Entity
{
/// <summary>
/// 心跳设置包
/// </summary>
public class BFHSendEntity
{
/// <summary>
/// 返回模式00严格按时间 01空闲返回
/// </summary>
public byte Mode { get; set; }
/// <summary>
/// 返回时间(单位秒)
/// </summary>
public byte Time { get; set; }
/// <summary>
/// 保留一直等于01就行
/// </summary>
public byte Rev { get; set; }
}
}

@ -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
{
/// <summary>
/// 超时时间
/// </summary>
public byte[] TimeOut { get; set; }
/// <summary>
/// 访问密码
/// </summary>
public byte[] AccessPassword { get; set; }
/// <summary>
/// 选择区域01 EPC, 02 TID, 03 User
/// </summary>
public byte SelectBank { get; set; }
/// <summary>
/// 选择地址
/// </summary>
public byte[] SelectAddress { get; set; }
/// <summary>
/// 选择匹配数据长度
/// </summary>
public byte SelectLength { get; set; }
/// <summary>
/// 选择匹配数据
/// </summary>
public byte[] SelectData { get; set; }
/// <summary>
/// 写入标签区域00 Reserved, 01 EPC, 02 TID, 03 User
/// </summary>
public byte WriteBank { get; set; }
/// <summary>
/// 写入地址word
/// </summary>
public byte[] WriteAddress { get; set; }
/// <summary>
/// 写入数据量(word)
/// </summary>
public byte WordCount { get; set; }
/// <summary>
/// 写入数据
/// </summary>
public byte[] WriteData { get; set; }
}
}

@ -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; }
/// <summary>
/// 信号强度
/// </summary>
public byte RSSI { get; set; }
/// <summary>
/// 天线端口
/// </summary>
public byte Ant { get; set; }
/// <summary>
/// EPC区域 10h-20h
/// </summary>
public byte[] PC { get; set; }
/// <summary>
/// 读到的EPC内容
/// </summary>
public byte[] EPC { get; set; }
}
}

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HighWayIot.Rfid.Entity
{
/// <summary>
/// 接收数据基础类
/// </summary>
public class BaseReciveDataEntity
{
/// <summary>
/// 数据长度2
/// </summary>
public byte DataLength { get; set; }
/// <summary>
/// 指令编号
/// </summary>
public byte Code { get; set; }
/// <summary>
/// 状态码
/// </summary>
public byte Status { get; set; }
/// <summary>
/// 数据
/// </summary>
public byte[] Data { get; set; }
/// <summary>
/// 校验位
/// </summary>
public byte Xor { get; set; }
}
}

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HighWayIot.Rfid.Entity
{
/// <summary>
/// 发送数据基础类
/// </summary>
public class BaseSendDataEntity
{
/// <summary>
/// 指令编号
/// </summary>
public byte Code { get; set; }
/// <summary>
/// 数据
/// </summary>
public byte[] Data { get; set; }
}
}

@ -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
{
/// <summary>
/// 标签个数
/// </summary>
public byte TagCount { get; set; }
/// <summary>
/// 时间段内读到的数据集合
/// </summary>
public List<Single02HReceive> Data { get; set; }
}
public class Single02HReceive
{
/// <summary>
/// 读取次数
/// </summary>
public byte Count { get; set; }
/// <summary>
/// 信号强度
/// </summary>
public byte RSSI { get; set; }
/// <summary>
/// 天线端口
/// </summary>
public byte Ant { get; set; }
/// <summary>
/// EPC区域
/// </summary>
public byte[] PC { get; set; }
/// <summary>
/// EPC内容
/// </summary>
public byte[] EPC { get; set; }
}
}
Loading…
Cancel
Save