public bool Motor5(int index, ushort id1, int value)
{
const int function = 4;
try
{
var byteList = new List
{
// 地址 1个字节,取低位
(byte)(id1 % 256),
// 功能码 1个字节 取低位
(byte)(function % 256),
// 起始地址2个字节 高位
0x00,
0x02,
0x00,
0x02
};
// 校验码
byteList = CRC16(byteList);
// 发送报文
port.Write(byteList.ToArray(), 0, byteList.Count);
Thread.Sleep(50);
var buffer = new byte[port.BytesToRead];
port.Read(buffer, 0, buffer.Length);
if (RecordTimes > 30)
{
RecordTimes = 0;
return true;
}
RecordTimes++;
if (buffer[2] == 04)
{
var value3 = value <= 65535
? buffer[3] * 256 + buffer[4]
: 65535 + buffer[3] * 256 + buffer[4] + buffer[6];
if (value == value3)
{
RecordTimes = 0;
return true;
}
}
return false;
}
catch (Exception)
{
return false;
}
}