using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;
namespace NDSD_TouchSocket
{
public class SendAnalysis
{
///
/// DO启动操作数据头
///
private string DOperateHead = "01 05 00 ";
///
/// DO启动操作数据尾
///
private string DOpenData = " FF 00 ";
///
/// DO关闭操作数据尾
///
private string DCloseData = " 00 00 ";
///
/// DO计时启动操作数据头
///
private string DTimeOperateHead = " 01 07 00 ";
///
/// DO计时启动操作码
///
private string DTimeOpenData = " FF 00 ";
public byte[] DTimeOpenDataAnalysis(byte port, int delay)
{
byte[] bytes = IntToDoubleByte(delay);
Array.Reverse(bytes);
string CrcData = DTimeOperateHead + port.ToString("X2") + DTimeOpenData + bytes.ToHexStrFromByte();
return CrcCombine(CrcData);
}
///
/// DO开启
///
///
///
public byte[] DOpenDataAnalysis(byte port)
{
string CrcData = DOperateHead + port.ToString("X2") + DOpenData; //结合
return CrcCombine(CrcData);
}
///
/// DO关闭
///
///
///
public byte[] DCloseDataAnlysis(byte port)
{
string CrcData = DOperateHead + port.ToString("X2") + DCloseData; //结合
return CrcCombine(CrcData);
}
///
/// 结合CRC16
///
///
///
public byte[] CrcCombine(string CrcData)
{
byte[] body = CrcData.ToBytesFromHexString(); //byte的校验体
int crc = CRC16.CalculateCRC(body, body.Length); //int32CRC值
return body.Concat(IntToDoubleByte(crc)).ToArray();
}
///
/// int转换为两位Byte数字
///
///
///
public byte[] IntToDoubleByte(int i)
{
byte highByte = (byte)(i >> 8); // 获取高字节
byte lowByte = (byte)(i & 0xFF); // 获取低字节
byte[] bytes = { lowByte, highByte };
return bytes;
}
}
}