Commit 0bf0be36 刘韬

完善FR540SP读写

1 个父辈 7e62a3f1
此文件类型无法预览
......@@ -12,7 +12,7 @@
bool GetAutoMode();
void Init();
void Open();
bool ReadData(out string value);
bool ReadData(out string value, short length);
void TriggerMode(bool auto);
bool WriteData(string id);
}
......
......@@ -165,12 +165,12 @@ namespace Asa.RFID
return rtn;
}
public bool ReadData(out string value)
public bool ReadData(out string value, short length=4)
{
_dataMode = false;
bool rtn = false;
value = "";
Read(0x20, 4);
Read(0x20, length);
if (Wait())
{
if (_fullCmd[7] == 0x03)
......
......@@ -25,8 +25,12 @@ public class PuYueRFID_C2S
//DOdata[0] = 0x00;
//DOdata[1] = 0x00;
iomonitorThread = new Thread(new ThreadStart(iomonitor));
//if (autoread)
// iomonitorThread = new Thread(new ThreadStart(iomonitor));
}
/// <summary>
/// 销毁
/// </summary>
~PuYueRFID_C2S()
{
iomonitorrun = false;
......@@ -42,8 +46,8 @@ public class PuYueRFID_C2S
tcpClient.Dispose();
tcpClient = new TcpClient();
tcpClient.ReceiveTimeout = 50;
tcpClient.SendTimeout = 50;
tcpClient.ReceiveTimeout = 1000;
tcpClient.SendTimeout = 100;
lock (tcpClient)
{
try
......@@ -127,11 +131,10 @@ public class PuYueRFID_C2S
{
iomonitorrun = true;
while (iomonitorrun && systemrun)
{
{
try
{
if (ConnectionState_Event != null)
if (ID_Changed_Event != null)
ReadDO();
else
Thread.Sleep(1000);
......@@ -234,7 +237,7 @@ public class PuYueRFID_C2S
/// </summary>
void ReadDO()
{
byte funCode = 0x03;//批量写圈
byte funCode = 0x03;
byte[] startAddress = BitConverter.GetBytes((short)0x0255);
byte startLength = 2;
var seqhead = BitConverter.GetBytes(seq);
......@@ -335,7 +338,169 @@ public class PuYueRFID_C2S
return -1;
}
}
void seqadd()
public bool ReadByte(short address,short length,out byte[] data) {
if (length > 64)
throw new Exception("最大读取64个字节");
data = null;
byte funCode = 0x03;
byte[] startAddress = BitConverter.GetBytes(address);//0x0255
byte[] startLength = BitConverter.GetBytes((short)Math.Ceiling(length / 2d));//2
var seqhead = BitConverter.GetBytes(seq);
byte[] by = new byte[]
{
//事物标识符
seqhead[0],
seqhead[1],
//协议标识符 固定值
0x00,
0x00,
//长度
0x00,
0x06,
//从站地址
station,
//功能码
funCode,
startAddress[1],
startAddress[0], //起始地址
startLength[1],
startLength[0], //读个数
};
by[5] = (byte)(by.Length - 6);
byte[] result = new byte[1000];
int ulength = 0;
lock (tcpClient)
{
try
{
if (tcpClient.Client.Available > 0)
tcpClient.Client.Receive(result);
seqadd();
tcpClient.Client.Send(by);
Thread.Sleep(5);
ulength = tcpClient.Client.Receive(result);
}
catch (SocketException se)
{
return false;
}
}
var newResult = result.ToList().Take(ulength).ToList();
//aa = BitConverter.ToString(newResult);
//Console.WriteLine(aa);
if (newResult.Count>6 && newResult[0] == seqhead[0] && newResult[1] == seqhead[1])
{
if (newResult[7] == 0x03)
{
var recvlen = BitConverter.ToInt16(new byte[] { newResult[5], newResult[4] },0);
if ((recvlen + 6) <= newResult.Count) {
data= newResult.GetRange(9, recvlen-3).ToArray();
Console.WriteLine(BitConverter.ToString(newResult.ToArray()));
return true;
}
}
else if (newResult[7] == 0x83)
{
return false;
}
}
else
{
Thread.Sleep(20);
if (tcpClient.Client.Available > 0)
_ = tcpClient.Client.Receive(result);
}
return false;
}
/// <summary>
/// 写入字节最大 64字
/// </summary>
/// <param name="address"></param>
/// <param name="data"></param>
/// <returns></returns>
public bool WriteByte(short address, byte[] data)
{
if (data.Length > 64)
throw new Exception("最大写入64个字节");
byte funCode = 0x10;
byte[] startAddress = BitConverter.GetBytes(address); //寄存器起始地址;
short registerlength=(short)Math.Ceiling(data.Length / 2d);
byte[] registerAddress = BitConverter.GetBytes(registerlength);
var seqhead = BitConverter.GetBytes(seq);
byte[] by = new byte[]
{
//事物标识符
seqhead[0],
seqhead[1],
//协议标识符 固定值
0x00,
0x00,
//长度
0x00,
0x06,
//从站地址
station,
//功能码
funCode,
startAddress[1],//起始地址高位
startAddress[0], //起始地址低位
registerAddress[1],
registerAddress[0], //寄存器数量
(byte)(registerlength * 2)
};
var ab = by.ToList();
ab.AddRange(data);
if (registerlength * 2 > data.Length)
{
ab.Add(0x0);
}
short datalength = (short)(ab.Count() - 6);
var dl = BitConverter.GetBytes(datalength);
ab[4] = dl[1];
ab[5] = dl[0];
by = ab.ToArray();
lock (tcpClient)
{
try
{
byte[] result = new byte[100];
if (tcpClient.Client.Available > 0)
tcpClient.Client.Receive(result);
seqadd();
tcpClient.Client.Send(by);
var ulength = tcpClient.Client.Receive(result);
var newResult = result.ToList().Take(ulength).ToArray();
if (newResult[0] == seqhead[0] && newResult[1] == seqhead[1])
{
Console.WriteLine(BitConverter.ToString(newResult));
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
}
void seqadd()
{
seq++;
if (seq >= ushort.MaxValue - 10)
......
......@@ -168,12 +168,12 @@ namespace Asa.RFID
return rtn;
}
public bool ReadData(out string value)
public bool ReadData(out string value, short length =4)
{
_dataMode = false;
bool rtn = false;
value = "";
Read(0x0255, 4);
Read(0x0255, length);
if (Wait())
{
if (_fullCmd[7] == 0x03)
......@@ -203,7 +203,20 @@ namespace Asa.RFID
}
return rtn;
}
public bool WriteData(byte[] data)
{
_dataMode = false;
bool rtn = false;
Write(data);
if (Wait())
{
if (_fullCmd[7] == 0x10)
rtn = true;
//else
// Common.log.Info("0x10功能码不匹配");
}
return rtn;
}
......@@ -592,7 +605,55 @@ namespace Asa.RFID
//Common.log.Error(string.Format("Write[{0}] ERROR", IP), ex);
}
}
private void Write(byte[] value)
{
if (!IsConn) return;
int registercount = (int)Math.Ceiling(value.Length / 2d);
//int valuelen = registercount * 2;
byte[] temp;
byte[] buffer = new byte[21];
GetMark();
temp = BitConverter.GetBytes(_mark); //事务处理标识
buffer[0] = temp[1]; //高位
buffer[1] = temp[0]; //低位
buffer[2] = 0;
buffer[3] = 0; //协议标识
buffer[4] = 0;
buffer[5] = 15; //后面字节数
buffer[6] = 0xFF; //主设备
buffer[7] = 0x10; //功能码
buffer[8] = 0x02;
buffer[9] = 0x55; //地址
buffer[10] = 0;
buffer[11] = (byte)registercount; //寄存器个数
buffer[12] = (byte)value.Length; //数据长度
var ab = buffer.ToList();
ab.AddRange(value);
buffer = ab.ToArray();
try
{
_total = false;
//Common.log.Debug("Write[" + IP + "] " + HexBuff(buffer));
if (_client != null)
_client.Send(buffer);
}
catch (Exception ex)
{
IsConn = false;
//Common.log.Error(string.Format("Write[{0}] ERROR", IP), ex);
}
}
private void GetMark()
{
if (_mark == short.MaxValue)
......
......@@ -234,6 +234,11 @@
连接状态变化, 手动连接不触发
</summary>
</member>
<member name="M:PuYueRFID_C2S.Finalize">
<summary>
销毁
</summary>
</member>
<member name="M:PuYueRFID_C2S.Open">
<summary>
打开IO
......@@ -281,5 +286,13 @@
<param name="ms">最后读到的时间ms</param>
<returns>读取成功状态-1读取失败,0没有读到,1读取成功</returns>
</member>
<member name="M:PuYueRFID_C2S.WriteByte(System.Int16,System.Byte[])">
<summary>
写入字节最大 64字
</summary>
<param name="address"></param>
<param name="data"></param>
<returns></returns>
</member>
</members>
</doc>
......@@ -32,6 +32,8 @@ namespace Test
this.components = new System.ComponentModel.Container();
this.textBox1 = new System.Windows.Forms.TextBox();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
......@@ -40,7 +42,7 @@ namespace Test
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox1.Size = new System.Drawing.Size(507, 384);
this.textBox1.Size = new System.Drawing.Size(507, 269);
this.textBox1.TabIndex = 1;
//
// timer1
......@@ -48,11 +50,33 @@ namespace Test
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// button1
//
this.button1.Location = new System.Drawing.Point(46, 318);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 2;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(204, 318);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 3;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(567, 450);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
......@@ -67,6 +91,8 @@ namespace Test
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}
......@@ -22,8 +22,8 @@ namespace Test
PuYueRFID_C2S py;
private void Form1_Load(object sender, EventArgs e)
{
read = new Asa.RFID.ReadAll(new string[] {"192.168.3.7" },"log");
read.Type = Asa.RFID.DeviceType.PuYue;
//read = new Asa.RFID.ReadAll(new string[] {"192.168.3.7" },"log");
//read.Type = Asa.RFID.DeviceType.PuYue;
//read.Start(10001);
//read.Start();
......@@ -60,9 +60,48 @@ namespace Test
private void timer1_Tick(object sender, EventArgs e)
{
int c= py.TryRead(out string id, out double ms);
textBox1.Text = $"{c},{id},{ms}";
//int c= py.TryRead(out string id, out double ms);
//textBox1.Text = $"{c},{id},{ms}";
}
int i = 0;
string txtdata = "aaaaaaaaaaqqqqqqqqqqaaaaaaaaaaqqqqqqqqqzzz";
private void button1_Click(object sender, EventArgs e)
{
i++;
txtdata += i.ToString();
var d = Encoding.ASCII.GetBytes(txtdata);
byte[] data;
while (!py.ReadByte(0x200, 1, out data))
{
textBox1.Text += "1";
Thread.Sleep(100);
}
textBox1.Text = "get tag;";
while (!py.WriteByte(0x255, d)) {
textBox1.Text += "e";
Thread.Sleep(100);
}
Thread.Sleep(500);
while (!py.ReadByte(0x255, (short)d.Length, out data))
{
textBox1.Text += "1";
Thread.Sleep(100);
}
textBox1.Text += Encoding.ASCII.GetString(data);
}
private void button2_Click(object sender, EventArgs e)
{
byte[] data;
while (!py.ReadByte(0x255, 14, out data))
{
textBox1.Text += "1";
Thread.Sleep(100);
}
textBox1.Text += Encoding.ASCII.GetString(data);
}
}
}
......@@ -234,12 +234,22 @@
连接状态变化, 手动连接不触发
</summary>
</member>
<member name="M:PuYueRFID_C2S.Finalize">
<summary>
销毁
</summary>
</member>
<member name="M:PuYueRFID_C2S.Open">
<summary>
打开IO
</summary>
<returns></returns>
</member>
<member name="P:PuYueRFID_C2S.AntennaPower">
<summary>
设置天线功率dBm
</summary>
</member>
<member name="M:PuYueRFID_C2S.Close">
<summary>
关闭IO
......@@ -255,7 +265,7 @@
循环读全部IO
</summary>
</member>
<member name="M:PuYueRFID_C2S.WriteDO(System.Byte,System.Int16)">
<member name="M:PuYueRFID_C2S.WriteDO(System.Int16,System.Int16)">
<summary>
写寄存器
</summary>
......@@ -268,5 +278,21 @@
读全部IO
</summary>
</member>
<member name="M:PuYueRFID_C2S.TryRead(System.String@,System.Double@)">
<summary>
读取rfid
</summary>
<param name="id">读取到的id</param>
<param name="ms">最后读到的时间ms</param>
<returns>读取成功状态-1读取失败,0没有读到,1读取成功</returns>
</member>
<member name="M:PuYueRFID_C2S.WriteByte(System.Int16,System.Byte[])">
<summary>
写入字节最大 64字
</summary>
<param name="address"></param>
<param name="data"></param>
<returns></returns>
</member>
</members>
</doc>
[2022-06-16 09:06:20,750][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 09:06:28,344][5][log:331]DEBUG (192.168.3.7)的数据包解析错误
[2022-06-16 09:06:28,671][5][log:331]DEBUG (192.168.3.7)的数据包解析错误
[2022-06-16 09:06:29,000][5][log:331]DEBUG (192.168.3.7)的数据包解析错误
[2022-06-16 09:06:29,980][5][log:331]DEBUG (192.168.3.7)的数据包解析错误
[2022-06-16 09:06:30,306][5][log:331]DEBUG (192.168.3.7)的数据包解析错误
[2022-06-16 09:11:22,708][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:00:34,447][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:04:22,209][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:04:42,263][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:04:57,070][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:05:31,147][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:06:25,675][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:09:19,312][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:09:46,682][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:10:53,737][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:11:59,962][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:12:52,491][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:13:22,111][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:42:22,133][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:47:27,589][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:51:07,946][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:52:57,744][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:53:57,133][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:54:22,482][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:58:35,867][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 13:44:34,378][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 13:46:20,784][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 13:47:00,960][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 13:51:05,713][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 13:52:03,513][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 13:53:22,768][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 13:55:04,722][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 13:55:15,918][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 13:58:35,849][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 13:59:25,671][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 14:08:34,950][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 14:10:39,122][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 14:11:32,375][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 14:12:34,436][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 14:13:53,997][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 14:14:59,538][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 14:15:49,687][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 14:18:05,190][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 14:19:55,727][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 14:20:19,789][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 14:20:48,928][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 14:22:53,712][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 14:23:26,681][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 14:23:44,790][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 14:24:57,532][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 14:25:22,585][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 14:25:50,771][1][log:78]DEBUG ReadAll server initialization
[2022-06-24 14:32:57,278][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 09:06:20,750][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 09:06:28,344][5][log:331]DEBUG (192.168.3.7)的数据包解析错误
[2022-06-16 09:06:28,671][5][log:331]DEBUG (192.168.3.7)的数据包解析错误
[2022-06-16 09:06:29,000][5][log:331]DEBUG (192.168.3.7)的数据包解析错误
[2022-06-16 09:06:29,980][5][log:331]DEBUG (192.168.3.7)的数据包解析错误
[2022-06-16 09:06:30,306][5][log:331]DEBUG (192.168.3.7)的数据包解析错误
[2022-06-16 09:11:22,708][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:00:34,447][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:04:22,209][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:04:42,263][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:04:57,070][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:05:31,147][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:06:25,675][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:09:19,312][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:09:46,682][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:10:53,737][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:11:59,962][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:12:52,491][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:13:22,111][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:42:22,133][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:47:27,589][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:51:07,946][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:52:57,744][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:53:57,133][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:54:22,482][1][log:78]DEBUG ReadAll server initialization
[2022-06-16 13:58:35,867][1][log:78]DEBUG ReadAll server initialization
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!