Commit a366373a 刘韬

1

1 个父辈 20ecde30
此文件类型无法预览
...@@ -396,6 +396,7 @@ public class PuYueRFID_C2S ...@@ -396,6 +396,7 @@ public class PuYueRFID_C2S
catch (SocketException exception1) catch (SocketException exception1)
{ {
errmsg = exception1.ToString(); errmsg = exception1.ToString();
Open();
return false; return false;
} }
catch (Exception ex) catch (Exception ex)
......
...@@ -20,10 +20,8 @@ public class PuYueRFID_FR540SP_C2S ...@@ -20,10 +20,8 @@ public class PuYueRFID_FR540SP_C2S
/// 连接状态变化, 手动连接不触发 /// 连接状态变化, 手动连接不触发
/// </summary> /// </summary>
public event EventHandler<bool> ConnectionState_Event; public event EventHandler<bool> ConnectionState_Event;
public PuYueRFID_FR540SP_C2S(string ip, bool automode = false) public PuYueRFID_FR540SP_C2S()
{ {
this.IP = ip;
Automode = automode;
//DOdata[0] = 0x00; //DOdata[0] = 0x00;
//DOdata[1] = 0x00; //DOdata[1] = 0x00;
//if (autoread) //if (autoread)
...@@ -42,8 +40,10 @@ public class PuYueRFID_FR540SP_C2S ...@@ -42,8 +40,10 @@ public class PuYueRFID_FR540SP_C2S
/// 打开IO /// 打开IO
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public bool Open() public bool Open(string ip, bool automode = false)
{ {
this.IP = ip;
Automode = automode;
tcpClient.Dispose(); tcpClient.Dispose();
tcpClient = new TcpClient(); tcpClient = new TcpClient();
...@@ -61,8 +61,8 @@ public class PuYueRFID_FR540SP_C2S ...@@ -61,8 +61,8 @@ public class PuYueRFID_FR540SP_C2S
if (Automode) if (Automode)
{ {
iomonitorrun = true; iomonitorrun = true;
iomonitorThread = new Thread(new ThreadStart(iomonitor)); //iomonitorThread = new Thread(new ThreadStart(iomonitor));
iomonitorThread.Start(); //iomonitorThread.Start();
} }
ConnectionState_Event?.Invoke(this, true); ConnectionState_Event?.Invoke(this, true);
return true; return true;
...@@ -124,7 +124,7 @@ public class PuYueRFID_FR540SP_C2S ...@@ -124,7 +124,7 @@ public class PuYueRFID_FR540SP_C2S
/// </summary> /// </summary>
public bool IsConn public bool IsConn
{ {
get => iomonitorrun && systemrun && tcpClient.Connected; get => tcpClient.Connected;
} }
bool iomonitorrun = false; bool iomonitorrun = false;
...@@ -156,7 +156,7 @@ public class PuYueRFID_FR540SP_C2S ...@@ -156,7 +156,7 @@ public class PuYueRFID_FR540SP_C2S
do do
{ {
Thread.Sleep(1000); Thread.Sleep(1000);
} while (systemrun && !Open()); } while (systemrun && !Open(IP,Automode));
} }
} }
...@@ -275,7 +275,7 @@ public class PuYueRFID_FR540SP_C2S ...@@ -275,7 +275,7 @@ public class PuYueRFID_FR540SP_C2S
{ {
seqadd(); seqadd();
tcpClient.Client.Send(by); tcpClient.Client.Send(by);
Thread.Sleep(5); Thread.Sleep(10);
ulength = tcpClient.Client.Receive(result); ulength = tcpClient.Client.Receive(result);
} }
...@@ -339,7 +339,7 @@ public class PuYueRFID_FR540SP_C2S ...@@ -339,7 +339,7 @@ public class PuYueRFID_FR540SP_C2S
} }
catch catch
{ {
Open(); Open(IP,Automode);
return -1; return -1;
} }
} }
...@@ -387,12 +387,13 @@ public class PuYueRFID_FR540SP_C2S ...@@ -387,12 +387,13 @@ public class PuYueRFID_FR540SP_C2S
seqadd(); seqadd();
tcpClient.Client.Send(by); tcpClient.Client.Send(by);
Thread.Sleep(5); Thread.Sleep(10);
ulength = tcpClient.Client.Receive(result); ulength = tcpClient.Client.Receive(result);
} }
catch (SocketException se) catch (SocketException se)
{ {
Open(IP, Automode);
return false; return false;
} }
} }
...@@ -486,11 +487,13 @@ public class PuYueRFID_FR540SP_C2S ...@@ -486,11 +487,13 @@ public class PuYueRFID_FR540SP_C2S
seqadd(); seqadd();
tcpClient.Client.Send(by); tcpClient.Client.Send(by);
Thread.Sleep(10);
var ulength = tcpClient.Client.Receive(result); var ulength = tcpClient.Client.Receive(result);
var newResult = result.ToList().Take(ulength).ToArray(); var newResult = result.ToList().Take(ulength).ToArray();
if (newResult[0] == seqhead[0] && newResult[1] == seqhead[1]) if (newResult[0] == seqhead[0] && newResult[1] == seqhead[1])
{ {
//if (newResult[7] == 0x90)
// return false;
Console.WriteLine(BitConverter.ToString(newResult)); Console.WriteLine(BitConverter.ToString(newResult));
return true; return true;
} }
...@@ -501,11 +504,39 @@ public class PuYueRFID_FR540SP_C2S ...@@ -501,11 +504,39 @@ public class PuYueRFID_FR540SP_C2S
} }
catch catch
{ {
Open(IP, Automode);
return false; return false;
} }
} }
} }
void seqadd()
public bool WriteEPC(byte[] data)
{
bool rtn = WriteByte(0x227, data);
return rtn;
}
public bool ReadEPC(short len,out byte[] value)
{
var rtn = ReadByte(0x0227, len, out value);
return rtn;
}
/// <summary>
/// 16进制转换
/// </summary>
/// <param name="buff"></param>
/// <returns></returns>
public string HexBuff(byte[] buff)
{
string s = "";
if (buff == null) return s;
for (int i = 0; i < buff.Length; i++)
s += buff[i].ToString("X2") + " ";
return s;
}
void seqadd()
{ {
seq++; seq++;
if (seq >= ushort.MaxValue - 10) if (seq >= ushort.MaxValue - 10)
......
...@@ -543,6 +543,68 @@ namespace Asa.RFID ...@@ -543,6 +543,68 @@ namespace Asa.RFID
} }
} }
private void Write(short address, byte[] value)
{
if (!IsConn) return;
byte[] temp;
byte[] buffer = new byte[10];
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; //功能码
temp = BitConverter.GetBytes(address); //寄存器起始地址
buffer[8] = temp[1]; //高位
buffer[9] = temp[0]; //低位
//buffer[10] = 0;
//buffer[11] = 4; //寄存器个数
//buffer[12] = 8; //数据长度
//buffer[13] = 0x5A;
//buffer[14] = (byte)value[0];
//buffer[15] = Convert.ToByte(value.Substring(1));
//buffer[16] = 0;
//buffer[17] = 0;
//buffer[18] = 0x4A;
//buffer[19] = 0;
//buffer[20] = 0;
List<byte> lb = new List<byte>(buffer);
List<byte> vd = new List<byte>(value);
if (value.Length % 2 != 0)
vd.Add(0);
var registerlen = value.Length / 2;
var regb = BitConverter.GetBytes(registerlen);
lb.Add(regb[1]);
lb.Add(regb[0]);
lb.Add((byte)(registerlen * 2));
lb.AddRange(vd);
lb[5] = (byte)(lb.Count - 6);
buffer = lb.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);
}
}
/// <summary> /// <summary>
/// 写入保持寄存器,0x10功能码 /// 写入保持寄存器,0x10功能码
......
...@@ -239,7 +239,7 @@ ...@@ -239,7 +239,7 @@
销毁 销毁
</summary> </summary>
</member> </member>
<member name="M:PuYueRFID_FR540SP_C2S.Open"> <member name="M:PuYueRFID_FR540SP_C2S.Open(System.String,System.Boolean)">
<summary> <summary>
打开IO 打开IO
</summary> </summary>
...@@ -294,6 +294,13 @@ ...@@ -294,6 +294,13 @@
<param name="data"></param> <param name="data"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:PuYueRFID_FR540SP_C2S.HexBuff(System.Byte[])">
<summary>
16进制转换
</summary>
<param name="buff"></param>
<returns></returns>
</member>
<member name="E:PuYueRFID_C2S.ConnectionState_Event"> <member name="E:PuYueRFID_C2S.ConnectionState_Event">
<summary> <summary>
连接状态变化, 手动连接不触发 连接状态变化, 手动连接不触发
......
...@@ -8,10 +8,10 @@ C:\Neotel\Program\RFID\RFID_PuYue\obj\Debug\RFID_PuYue.csprojAssemblyReference.c ...@@ -8,10 +8,10 @@ C:\Neotel\Program\RFID\RFID_PuYue\obj\Debug\RFID_PuYue.csprojAssemblyReference.c
D:\rick\vs\RFID\RFID_PuYue\bin\Debug\Asa.RFID.PuYue.xml D:\rick\vs\RFID\RFID_PuYue\bin\Debug\Asa.RFID.PuYue.xml
D:\rick\vs\RFID\RFID_PuYue\bin\Debug\Asa.RFID.PuYue.dll D:\rick\vs\RFID\RFID_PuYue\bin\Debug\Asa.RFID.PuYue.dll
D:\rick\vs\RFID\RFID_PuYue\bin\Debug\Asa.RFID.PuYue.pdb D:\rick\vs\RFID\RFID_PuYue\bin\Debug\Asa.RFID.PuYue.pdb
D:\rick\vs\RFID\RFID_PuYue\obj\Debug\RFID_PuYue.csproj.AssemblyReference.cache
D:\rick\vs\RFID\RFID_PuYue\obj\Debug\RFID_PuYue.csproj.CoreCompileInputs.cache D:\rick\vs\RFID\RFID_PuYue\obj\Debug\RFID_PuYue.csproj.CoreCompileInputs.cache
D:\rick\vs\RFID\RFID_PuYue\obj\Debug\Asa.RFID.PuYue.dll D:\rick\vs\RFID\RFID_PuYue\obj\Debug\Asa.RFID.PuYue.dll
D:\rick\vs\RFID\RFID_PuYue\obj\Debug\Asa.RFID.PuYue.pdb D:\rick\vs\RFID\RFID_PuYue\obj\Debug\Asa.RFID.PuYue.pdb
D:\rick\vs\RFID\RFID_PuYue\bin\Debug\log4net.dll D:\rick\vs\RFID\RFID_PuYue\bin\Debug\log4net.dll
D:\rick\vs\RFID\RFID_PuYue\bin\Debug\log4net.xml D:\rick\vs\RFID\RFID_PuYue\bin\Debug\log4net.xml
D:\rick\vs\RFID\RFID_PuYue\obj\Debug\RFID_PuYue.csproj.CopyComplete D:\rick\vs\RFID\RFID_PuYue\obj\Debug\RFID_PuYue.csproj.CopyComplete
D:\rick\vs\RFID\RFID_PuYue\obj\Debug\RFID_PuYue.csproj.AssemblyReference.cache
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
...@@ -14,6 +16,20 @@ namespace RFID_PuYue_Debug ...@@ -14,6 +16,20 @@ namespace RFID_PuYue_Debug
[STAThread] [STAThread]
static void Main() static void Main()
{ {
//PuYueRFID_FR540SP_C2S rfid = new PuYueRFID_FR540SP_C2S();
//rfid.Open("192.168.3.7");
//var epc = "20418002Z-203";
//epc = epc.PadRight(16);
//var result=rfid.WriteEPC(Encoding.ASCII.GetBytes(epc));
//Thread.Sleep(500);
//for (int i = 0; i < 5; i++)
//{
// if (rfid.ReadEPC(16, out byte[] data))
// {
// var txt = Encoding.ASCII.GetString(data);
// break;
// }
//}
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FrmMain()); Application.Run(new FrmMain());
......
...@@ -239,7 +239,7 @@ ...@@ -239,7 +239,7 @@
销毁 销毁
</summary> </summary>
</member> </member>
<member name="M:PuYueRFID_FR540SP_C2S.Open"> <member name="M:PuYueRFID_FR540SP_C2S.Open(System.String,System.Boolean)">
<summary> <summary>
打开IO 打开IO
</summary> </summary>
...@@ -294,6 +294,13 @@ ...@@ -294,6 +294,13 @@
<param name="data"></param> <param name="data"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:PuYueRFID_FR540SP_C2S.HexBuff(System.Byte[])">
<summary>
16进制转换
</summary>
<param name="buff"></param>
<returns></returns>
</member>
<member name="E:PuYueRFID_C2S.ConnectionState_Event"> <member name="E:PuYueRFID_C2S.ConnectionState_Event">
<summary> <summary>
连接状态变化, 手动连接不触发 连接状态变化, 手动连接不触发
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!