///
/// 操作继电器
///
/// 表示打开还是关闭
/// 操作第几路继电器(从1到16)
private void OperationRelay(bool isOpenOrClose ,int Ch)
{
byte[] sBytes = new byte[] { 0xCC, 0xDD, 0xA1, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x9E, 0x3C };
if (Ch <= 8)
{
sBytes[4] = 0;
sBytes[6] = 0;
sBytes[5] = (byte)(0x01 << (Ch - 1));
sBytes[7] = (byte)(0x01 << (Ch - 1));
}
else
{
sBytes[4] = (byte)(0x01 << (Ch - 9));
sBytes[6] = (byte)(0x01 << (Ch - 9));
sBytes[5] = 0;
sBytes[7] = 0;
}
if (isOpenOrClose==false)
{
sBytes[4] = 0;
sBytes[5] = 0;
}
sBytes[8] = SumCheck(sBytes, 2, 6);
sBytes[9] = SumCheck(sBytes, 2, 7);
string str = BitConverter.ToString(sBytes).Replace("-", " ");
SendUdpMessage(sBytes);
Debug.Log(str);
}
public void BtnOpenAllClick()
{
byte[] sBytes = new byte[] { 0xCC, 0xDD, 0xA1, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x9E, 0x3C };
SendUdpMessage(sBytes);
}
public void BtnCloseAllClick()
{
byte[] sBytes = new byte[] { 0xCC, 0xDD, 0xA1, 0x01, 0x00, 0x00, 0xFF, 0xFF, 0xA0, 0x40 };
SendUdpMessage(sBytes);
}
补充和校验的方法
- ///
- /// 和校验
- ///
- ///
- ///
- ///
- ///
- private byte SumCheck(byte[] cBytes, int startIndex, int cLenth)
- {
- byte check = 0;
- if (cBytes.Length > 1)
- {
- for (int i = startIndex; i < startIndex + cLenth; i++)
- {
- check = (byte)((cBytes[i] + check) & 0xFF);
- }
- }
- return check;
- }