Commit 7781a023 gujlg

添加io同步

1 个父辈 3233dcca
...@@ -61,6 +61,7 @@ ...@@ -61,6 +61,7 @@
<Compile Include="acSingleStore\AutomaticBaiting_Partial.cs" /> <Compile Include="acSingleStore\AutomaticBaiting_Partial.cs" />
<Compile Include="acSingleStore\StoreManager.cs" /> <Compile Include="acSingleStore\StoreManager.cs" />
<Compile Include="acSingleStore\AutomaticBaiting.cs" /> <Compile Include="acSingleStore\AutomaticBaiting.cs" />
<Compile Include="DeviceLibrary\IO\AIOBOX\BLL2.cs" />
<Compile Include="DeviceLibrary\halcon\CodeManager.cs" /> <Compile Include="DeviceLibrary\halcon\CodeManager.cs" />
<Compile Include="DeviceLibrary\IO\AIOBOX\AIOBOXManager.cs" /> <Compile Include="DeviceLibrary\IO\AIOBOX\AIOBOXManager.cs" />
<Compile Include="DeviceLibrary\IO\AIOBOX\BLL.cs" /> <Compile Include="DeviceLibrary\IO\AIOBOX\BLL.cs" />
......
/*
* @Description: 用于AIOBOX-32系列一体化IO模块
* @CreateDate: 2019-02-28
* @UpdateDate: 2019-05-21
* @Author: Asa
* @Version: 1.8
*
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections.Generic;
using OnlineStore.Common;
namespace Asa.IOModule
{
/// <summary>
/// AIOBOX操作类,同步通信
/// </summary>
public class AIOBOX2
{
private ushort _flag; //ModBusTCP标识
private Socket _client; //客户端
private Box_Type _type; //类型
private byte[] _addr; //地址
private Box_Sta[] _sta; //状态
private int _unrevd; //没有收到数据的时间
private int _unrevdRemote; //本地还是远程没有收到数据
private bool _readDI; //自动读取DI
private bool _readDO; //自动读取DO
private int _readDISleep; //自动读取DI间隔
private int _readDOSleep; //自动读取DO间隔
private List<string> _log; //日志
//private byte[] _send; //发送的命令
private System.Collections.Concurrent.ConcurrentQueue<byte[]> _receive; //接收的数据
private System.Collections.Concurrent.ConcurrentQueue<byte[]> _send;
private Thread tSend;
private Thread tReceive; //接收信息处理
private Thread tListen; //监听网络
private Thread tTrigger; //触发DI、DO改变事件
private Thread tReadDI; //自动读取DI线程
private Thread tReadDO; //自动读取DO线程
private Thread tLogOut; //日志输出
private bool suspendWrite;
private bool accept;
private AutoResetEvent aWrite;
private AutoResetEvent aReadDI;
private AutoResetEvent aReadDO;
private const int SEND_SLEEP = 30; //发送命令间隔
private const int NET_SLEEP = 30; //接收网络间隔
private const int TRIG_SLEEP = 30; //触发事件间隔
/// <summary>
/// 自动读取DI委托
/// </summary>
/// <param name="box">AIOBOX</param>
/// <param name="sta">所有DI状态</param>
public delegate void DI_Changed(AIOBOX2 box, Box_Sta[] sta);
/// <summary>
/// 自动读取DI事件触发
/// </summary>
public event DI_Changed DI_Changed_Event;
/// <summary>
/// 自动读取DO委托
/// </summary>
/// <param name="box">AIOBOX</param>
/// <param name="sta">所有DO状态</param>
public delegate void DO_Changed(AIOBOX2 box, Box_Sta[] sta);
/// <summary>
/// 自动读取DO事件触发
/// </summary>
public event DO_Changed DO_Changed_Event;
/// <summary>
/// 日志输出
/// </summary>
/// <param name="box"></param>
/// <param name="s"></param>
public delegate void Log_Out(AIOBOX2 box, string[] s);
/// <summary>
/// 日志输出事件
/// </summary>
public event Log_Out Log_Out_Event;
/// <summary>
/// AIOBOX
/// </summary>
public AIOBOX2()
{
_readDI = false;
_readDO = false;
_readDISleep = 100;
_readDOSleep = 100;
_addr = new byte[32];
_sta = new Box_Sta[32];
_log = new List<string>();
Type = Box_Type.DIO_32;
aWrite = new AutoResetEvent(false);
aReadDI = new AutoResetEvent(false);
aReadDO = new AutoResetEvent(false);
}
/// <summary>
/// IP地址
/// </summary>
public string IP { set; get; } = "192.168.1.100";
/// <summary>
/// ModBus端口
/// </summary>
public int Port { set; get; } = 502;
/// <summary>
/// 是否连接
/// </summary>
public bool IsConn { get; private set; } = false;
/// <summary>
/// 错误信息
/// </summary>
public string ErrInfo { get; private set; } = "";
/// <summary>
/// 日志输出
/// </summary>
public bool LogOut { set; get; } = false;
/// <summary>
/// 模块的类型
/// </summary>
public Box_Type Type
{
set
{
_type = value;
if (value == Box_Type.DIO_16)
{
_addr[(int)Box_Addr.DI_1] = 0;
_addr[(int)Box_Addr.DI_2] = 1;
_addr[(int)Box_Addr.DI_3] = 2;
_addr[(int)Box_Addr.DI_4] = 3;
_addr[(int)Box_Addr.DI_5] = 4;
_addr[(int)Box_Addr.DI_6] = 5;
_addr[(int)Box_Addr.DI_7] = 6;
_addr[(int)Box_Addr.DI_8] = 7;
_addr[(int)Box_Addr.DI_9] = 255;
_addr[(int)Box_Addr.DI_10] = 255;
_addr[(int)Box_Addr.DI_11] = 255;
_addr[(int)Box_Addr.DI_12] = 255;
_addr[(int)Box_Addr.DI_13] = 255;
_addr[(int)Box_Addr.DI_14] = 255;
_addr[(int)Box_Addr.DI_15] = 255;
_addr[(int)Box_Addr.DI_16] = 255;
_addr[(int)Box_Addr.DO_1] = 8;
_addr[(int)Box_Addr.DO_2] = 9;
_addr[(int)Box_Addr.DO_3] = 10;
_addr[(int)Box_Addr.DO_4] = 11;
_addr[(int)Box_Addr.DO_5] = 12;
_addr[(int)Box_Addr.DO_6] = 13;
_addr[(int)Box_Addr.DO_7] = 14;
_addr[(int)Box_Addr.DO_8] = 15;
_addr[(int)Box_Addr.DO_9] = 255;
_addr[(int)Box_Addr.DO_10] = 255;
_addr[(int)Box_Addr.DO_11] = 255;
_addr[(int)Box_Addr.DO_12] = 255;
_addr[(int)Box_Addr.DO_13] = 255;
_addr[(int)Box_Addr.DO_14] = 255;
_addr[(int)Box_Addr.DO_15] = 255;
_addr[(int)Box_Addr.DO_16] = 255;
}
else if (value == Box_Type.DIO_32)
{
_addr[(int)Box_Addr.DI_1] = 0;
_addr[(int)Box_Addr.DI_2] = 1;
_addr[(int)Box_Addr.DI_3] = 2;
_addr[(int)Box_Addr.DI_4] = 3;
_addr[(int)Box_Addr.DI_5] = 4;
_addr[(int)Box_Addr.DI_6] = 5;
_addr[(int)Box_Addr.DI_7] = 6;
_addr[(int)Box_Addr.DI_8] = 7;
_addr[(int)Box_Addr.DI_9] = 8;
_addr[(int)Box_Addr.DI_10] = 9;
_addr[(int)Box_Addr.DI_11] = 10;
_addr[(int)Box_Addr.DI_12] = 11;
_addr[(int)Box_Addr.DI_13] = 12;
_addr[(int)Box_Addr.DI_14] = 13;
_addr[(int)Box_Addr.DI_15] = 14;
_addr[(int)Box_Addr.DI_16] = 15;
_addr[(int)Box_Addr.DO_1] = 16;
_addr[(int)Box_Addr.DO_2] = 17;
_addr[(int)Box_Addr.DO_3] = 18;
_addr[(int)Box_Addr.DO_4] = 19;
_addr[(int)Box_Addr.DO_5] = 20;
_addr[(int)Box_Addr.DO_6] = 21;
_addr[(int)Box_Addr.DO_7] = 22;
_addr[(int)Box_Addr.DO_8] = 23;
_addr[(int)Box_Addr.DO_9] = 24;
_addr[(int)Box_Addr.DO_10] = 25;
_addr[(int)Box_Addr.DO_11] = 26;
_addr[(int)Box_Addr.DO_12] = 27;
_addr[(int)Box_Addr.DO_13] = 28;
_addr[(int)Box_Addr.DO_14] = 29;
_addr[(int)Box_Addr.DO_15] = 30;
_addr[(int)Box_Addr.DO_16] = 31;
}
else if (value == Box_Type.DO_16)
{
_addr[(int)Box_Addr.DO_1] = 0;
_addr[(int)Box_Addr.DO_2] = 1;
_addr[(int)Box_Addr.DO_3] = 2;
_addr[(int)Box_Addr.DO_4] = 3;
_addr[(int)Box_Addr.DO_5] = 4;
_addr[(int)Box_Addr.DO_6] = 5;
_addr[(int)Box_Addr.DO_7] = 6;
_addr[(int)Box_Addr.DO_8] = 7;
_addr[(int)Box_Addr.DO_9] = 8;
_addr[(int)Box_Addr.DO_10] = 9;
_addr[(int)Box_Addr.DO_11] = 10;
_addr[(int)Box_Addr.DO_12] = 11;
_addr[(int)Box_Addr.DO_13] = 12;
_addr[(int)Box_Addr.DO_14] = 13;
_addr[(int)Box_Addr.DO_15] = 14;
_addr[(int)Box_Addr.DO_16] = 15;
}
}
get
{
return _type;
}
}
/// <summary>
/// 连接
/// </summary>
/// <returns></returns>
public bool Connect()
{
try
{
//IP合法
string pattern = @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$";
bool rtn = System.Text.RegularExpressions.Regex.IsMatch(IP, pattern);
if (!rtn)
{
ErrInfo = "非法的IP地址 " + IP;
return false;
}
//Ping服务端
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply result = ping.Send(IP, 3000);
if (result.Status != System.Net.NetworkInformation.IPStatus.Success)
{
ErrInfo = "Ping " + IP + " 请求没有回应";
return false;
}
_unrevd = 0;
_unrevdRemote = 0;
_flag = 0;
suspendWrite = false;
accept = false;
_log.Clear();
_send = new System.Collections.Concurrent.ConcurrentQueue<byte[]>();
_receive = new System.Collections.Concurrent.ConcurrentQueue<byte[]>();
//建立连接
_client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);
_client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
_client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1);
_client.Connect(IPAddress.Parse(IP), Port);
Thread.Sleep(100); //需要等待一会才能获取连接状态
tSend = new Thread(new ThreadStart(Send));
tListen = new Thread(new ThreadStart(Listen));
tTrigger = new Thread(new ThreadStart(TriggerDIO));
tReadDI = new Thread(new ThreadStart(AutoReadDI));
tReadDO = new Thread(new ThreadStart(AutoReadDO));
tReceive = new Thread(new ThreadStart(Receive));
tLogOut = new Thread(new ThreadStart(LogPrint));
tSend.Start();
tListen.Start();
tTrigger.Start();
tReceive.Start();
tLogOut.Start();
tReadDI.Start();
tReadDO.Start();
ErrInfo = "OK";
IsConn = true;
return true;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
IsConn = false;
return false;
}
}
/// <summary>
/// 关闭连接
/// </summary>
public void Close()
{
IsConn = false;
if (tListen != null) tListen.Abort();
tListen = null;
if (tReadDI != null) tReadDI.Abort();
tReadDI = null;
if (tReadDO != null) tReadDO.Abort();
tReadDO = null;
if (tTrigger != null) tTrigger.Abort();
tTrigger = null;
if (tLogOut != null) tLogOut.Abort();
tLogOut = null;
if (tSend != null) tSend.Abort();
tSend = null;
if (tReceive != null) tReceive.Abort();
tReceive = null;
if (_client != null)
{
_client.Shutdown(SocketShutdown.Both);
_client.Close();
}
_client = null;
}
/// <summary>
/// 自动读取DI状态,触发DI_Changed_Event
/// </summary>
/// <param name="read">是否自动读取</param>
/// <param name="sleep">间隔,必须大于10ms</param>
public void AutoReadDI(bool read, int sleep)
{
if (_type == Box_Type.DO_16)
{
_readDI = false;
_readDISleep = 10000;
}
else
{
if (sleep < 10)
{
_readDI = false;
_readDISleep = 100;
}
else
{
if (read)
{
_readDI = true;
_readDISleep = sleep;
}
else
{
_readDI = false;
_readDISleep = 100;
}
}
}
}
/// <summary>
/// 自动读取DO状态,触发DO_Changed_Event
/// </summary>
/// <param name="read">是否自动读取</param>
/// <param name="sleep">间隔,必须大于10ms</param>
public void AutoReadDO(bool read, int sleep)
{
if (sleep < 10)
{
_readDO = false;
_readDOSleep = 100;
}
else
{
if (read)
{
_readDO = true;
_readDOSleep = sleep;
}
else
{
_readDO = false;
_readDOSleep = 100;
}
}
}
/// <summary>
/// 相反状态
/// </summary>
/// <param name="sta"></param>
/// <returns></returns>
public Box_Sta ReverseStatus(Box_Sta sta)
{
return sta == Box_Sta.On ? Box_Sta.Off : Box_Sta.On;
}
/// <summary>
/// 读取单个DI
/// </summary>
/// <param name="add"></param>
/// <returns></returns>
public Box_Sta ReadDI(Box_Addr add)
{
return _sta[(int)add];
}
/// <summary>
/// 读取多个DI
/// </summary>
/// <param name="add"></param>
/// <param name="count"></param>
/// <returns></returns>
public Box_Sta[] ReadDI(Box_Addr add, int count)
{
Box_Sta[] sta = new Box_Sta[count];
Array.Copy(_sta, (int)add, sta, 0, count);
return sta;
}
/// <summary>
/// 读取单个DO
/// </summary>
/// <param name="add"></param>
/// <returns></returns>
public Box_Sta ReadDO(Box_Addr add)
{
return _sta[(int)add];
}
/// <summary>
/// 读取多个DO
/// </summary>
/// <param name="add"></param>
/// <param name="count"></param>
/// <returns></returns>
public Box_Sta[] ReadDO(Box_Addr add, int count)
{
Box_Sta[] sta = new Box_Sta[count];
Array.Copy(_sta, (int)add, sta, 0, count);
return sta;
}
/// <summary>
/// 写入单个DO
/// </summary>
/// <param name="add"></param>
/// <param name="sta"></param>
/// <returns></returns>
public bool WriteDO(Box_Addr add, Box_Sta sta)
{
try
{
byte[] data = Command();
byte[] buff = new byte[12];
Array.Copy(data, 0, buff, 0, data.Length);
buff[7] = 5; //功能码
buff[9] = _addr[(int)add]; //地址
buff[10] = (byte)sta; //写入值
suspendWrite = true;
if (_readDI) aWrite.WaitOne();
if (_readDO) aWrite.WaitOne();
accept = false; //接收完数据
int n = 0; //超时时间
int time = 0; //重试次数
while (!accept)
{
if (n == 0)
{
_client.Send(buff);
if (LogOut) AddLog(string.Format("WriteDO({0},{1})", add.ToString(), sta.ToString()), buff);
}
Thread.Sleep(SEND_SLEEP);
n += 5;
if (n >= 1000) //1秒内没有收到返回
{
time++;
n = 0;
}
if (time == 3)
{
ErrInfo = "重试3次都没有响应";
break;
}
}
if (accept)
_log.Add("WriteDO Receive");
else
_log.Add(string.Format("{0:HH:mm:ss:fff} WriteDO 重试3次都没有响应", DateTime.Now));
if (time > 0)
{
return false;
}
else
{
suspendWrite = false;
aReadDI.Set();
aReadDO.Set();
ErrInfo = "OK";
return true;
}
}
catch (Exception ex)
{
ErrInfo = ex.Message;
return false;
}
}
/// <summary>
/// 获取本地IPv4地址
/// </summary>
/// <returns></returns>
public string[] GetLocalIP()
{
List<string> str = new List<string>();
IPAddress[] add = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
foreach (IPAddress ip in add)
{
if (ip.AddressFamily.ToString() == "InterNetwork")
str.Add(ip.ToString());
}
return str.ToArray();
}
private void AddLog(string text, byte[] buff)
{
byte[] bb = new byte[2];
bb[0] = buff[1];
bb[1] = buff[0];
ushort flag = BitConverter.ToUInt16(bb, 0);
string s = string.Format("{0:HH:mm:ss:fff} " + text + " {1} fun={2} len={3}", DateTime.Now, flag, buff[7], buff.Length);
_log.Add(s);
}
private void Send()
{
while (true)
{
bool rtn = _send.TryDequeue(out byte[] result);
if (rtn)
{
try
{
accept = false;
_client.Send(result);
if (LogOut) AddLog("Send", result);
int n = 0;
while (!accept)
{
n += 5;
Thread.Sleep(5);
if (n >= 500)
{
_log.Add("Send Timeout 500ms");
break;
}
}
}
catch (Exception ex)
{
ErrInfo = ex.Message;
break;
}
}
Thread.Sleep(SEND_SLEEP);
}
}
/// <summary>
/// 接收命令
/// </summary>
private void Receive()
{
while (_receive.TryDequeue(out byte[] buff))
{
if (buff[7] == 1)
ReadDO(buff);
else if (buff[7] == 2)
ReadDI(buff);
else if (buff[7] == 5)
{ }
Thread.Sleep(10);
}
}
/// <summary>
/// 读取单个DO
/// </summary>
/// <param name="buff"></param>
private void ReadSingle(byte[] buff)
{
//try
//{
string s;
if (LogOut)
{
byte[] bb = new byte[2];
bb[0] = buff[1];
bb[1] = buff[0];
ushort flag = BitConverter.ToUInt16(bb, 0);
s = string.Format("{0:HH:mm:ss:fff} WriteDO Receive {1}", DateTime.Now, flag);
_log.Add(s);
}
// int n = 0;
// int move = 0;
// byte val = _receive[0][9];
// for (int i = 0; i < 8; i++)
// {
// n = (val & Convert.ToInt32(Math.Pow(2, move))) >> move;
// _sta[i + 16] = n == 1 ? Box_Sta.On : Box_Sta.Off;
// move++;
// }
// if (_receive[0][8] == 2)
// {
// move = 0;
// val = _receive[0][10];
// for (int i = 8; i < 16; i++)
// {
// n = (val & Convert.ToInt32(Math.Pow(2, move))) >> move;
// _sta[i + 16] = n == 1 ? Box_Sta.On : Box_Sta.Off;
// move++;
// }
// }
// ErrInfo = "OK";
//}
//catch (Exception ex)
//{
// ErrInfo = ex.Message;
//}
}
/// <summary>
/// 读取所有DO状态
/// </summary>
/// <param name="buff"></param>
/// <returns></returns>
private bool ReadDO(byte[] buff)
{
try
{
string s;
if (LogOut)
{
byte[] bb = new byte[2];
bb[0] = buff[1];
bb[1] = buff[0];
ushort flag = BitConverter.ToUInt16(bb, 0);
s = string.Format("{0:HH:mm:ss:fff} ReadDO id={1} fun={2} len={3}", DateTime.Now, flag, buff[7], buff.Length);
s += Convert.ToString(buff[9], 2);
if (buff[8] == 2)
s += "," + Convert.ToString(buff[10], 2);
_log.Add(s);
}
int n = 0;
int move = 0;
byte val = buff[9];
for (int i = 0; i < 8; i++)
{
n = (val & Convert.ToInt32(Math.Pow(2, move))) >> move;
_sta[i + 16] = n == 1 ? Box_Sta.On : Box_Sta.Off;
move++;
}
if (buff[8] == 2)
{
move = 0;
val = buff[10];
for (int i = 8; i < 16; i++)
{
n = (val & Convert.ToInt32(Math.Pow(2, move))) >> move;
_sta[i + 16] = n == 1 ? Box_Sta.On : Box_Sta.Off;
move++;
}
}
if (_unrevdRemote == 0) ErrInfo = "OK";
return true;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
LogUtil.error(ex.ToString());
return false;
}
}
/// <summary>
/// 读取所有DI状态
/// </summary>
/// <returns></returns>
private bool ReadDI(byte[] buff)
{
try
{
string s;
if (LogOut)
{
byte[] bb = new byte[2];
bb[0] = buff[1];
bb[1] = buff[0];
ushort flag = BitConverter.ToUInt16(bb, 0);
s = string.Format("{0:HH:mm:ss:fff} ReadDI id={1} fun={2} len={3}", DateTime.Now, flag, buff[7], buff.Length);
s += Convert.ToString(buff[9], 2);
if (buff[8] == 2)
s += "," + Convert.ToString(buff[10], 2);
_log.Add(s);
}
int n = 0;
int move = 0;
byte val = buff[9];
for (int i = 0; i < 8; i++)
{
n = (val & Convert.ToInt32(Math.Pow(2, move))) >> move;
_sta[i] = n == 1 ? Box_Sta.On : Box_Sta.Off;
move++;
}
if (buff[8] == 2)
{
move = 0;
val = buff[10];
for (int i = 8; i < 16; i++)
{
n = (val & Convert.ToInt32(Math.Pow(2, move))) >> move;
_sta[i] = n == 1 ? Box_Sta.On : Box_Sta.Off;
move++;
}
}
if (_unrevdRemote == 0) ErrInfo = "OK";
return true;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
LogUtil.error(ex.ToString());
return false;
}
}
/// <summary>
/// 命令,前7个字节
/// </summary>
/// <returns></returns>
private byte[] Command()
{
byte[] add = BitConverter.GetBytes(++_flag);
byte[] data = new byte[7];
data[0] = add[1];
data[1] = add[0];
data[2] = 0;
data[3] = 0;
data[4] = 0;
data[5] = 0;
data[6] = 1;
if (_flag == ushort.MaxValue) _flag = 0;
return data;
}
/// <summary>
/// 触发DIO改变事件
/// </summary>
private void TriggerDIO()
{
int n;
Box_Sta[] sta = null;
while (true)
{
n = 0;
if (_readDI && DI_Changed_Event != null)
{
if (_type == Box_Type.DIO_16) sta = new Box_Sta[8];
else if (_type == Box_Type.DIO_32) sta = new Box_Sta[16];
Array.Copy(_sta, 0, sta, 0, sta.Length);
DI_Changed_Event.Invoke(this, sta);
Thread.Sleep(TRIG_SLEEP);
n++;
}
if (_readDO && DO_Changed_Event != null)
{
if (_type == Box_Type.DIO_16) sta = new Box_Sta[8];
else if (_type == Box_Type.DIO_32) sta = new Box_Sta[16];
else if (_type == Box_Type.DO_16) sta = new Box_Sta[16];
Array.Copy(_sta, 16, sta, 0, sta.Length);
DO_Changed_Event.Invoke(this, sta);
Thread.Sleep(TRIG_SLEEP);
n++;
}
if (n == 0)
Thread.Sleep(TRIG_SLEEP);
}
}
/// <summary>
/// 日志输出线程
/// </summary>
private void LogPrint()
{
int len = 0;
while (true)
{
if (LogOut && _log.Count > len)
{
len = _log.Count;
string[] ss = new string[len + 1];
_log.CopyTo(0, ss, 0, len);
Log_Out_Event?.Invoke(this, ss);
_log.RemoveRange(0, len);
len = 0;
}
Thread.Sleep(1000);
}
}
/// <summary>
/// 自动读取DI线程
/// </summary>
private void AutoReadDI()
{
while (true)
{
if (IsConn && _readDI)
{
if (suspendWrite)
{
aWrite.Set();
aReadDI.WaitOne();
}
byte[] data = Command();
byte[] buff = new byte[12];
Array.Copy(data, 0, buff, 0, data.Length);
buff[7] = 2; //功能码
buff[9] = _addr[(int)Box_Addr.DI_1]; //地址
if (_type == Box_Type.DIO_16)
buff[11] = 8; //个数
else if (_type == Box_Type.DIO_32)
buff[11] = 16; //个数
_send.Enqueue(buff);
}
Thread.Sleep(_readDISleep);
}
}
/// <summary>
/// 自动读取DO线程
/// </summary>
private void AutoReadDO()
{
while (true)
{
if (IsConn && _readDO)
{
if (suspendWrite)
{
aWrite.Set();
aReadDO.WaitOne();
}
byte[] data = Command();
byte[] buff = new byte[12];
Array.Copy(data, 0, buff, 0, data.Length);
buff[7] = 1; //功能码
buff[9] = _addr[(int)Box_Addr.DO_1]; //地址
if (_type == Box_Type.DIO_16)
buff[11] = 8; //个数
else if (_type == Box_Type.DIO_32)
buff[11] = 16; //个数
else if (_type == Box_Type.DO_16)
buff[11] = 16; //个数
_send.Enqueue(buff);
}
Thread.Sleep(_readDOSleep);
}
}
/// <summary>
/// 监听结果线程
/// </summary>
private void Listen()
{
byte[] bb = new byte[100];
while (true)
{
if (_client.Available > 0)
{
Thread.Sleep(2);
int len = _client.Receive(bb);
byte[] buff = new byte[len];
Array.Copy(bb, buff, len);
_receive.Enqueue(buff);
accept = true;
}
Thread.Sleep(NET_SLEEP);
}
}
}
}
\ No newline at end of file \ No newline at end of file
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!