Skip to content

Commit

Permalink
W
Browse files Browse the repository at this point in the history
  • Loading branch information
Monika1313 committed Oct 26, 2023
1 parent de3732a commit 384b0b3
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions Modules/Wu.CommTool.Modules.ModbusRtu/Models/ModbusRtuModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -743,27 +743,36 @@ private void ReceiveMessage(object sender, SerialDataReceivedEventArgs e)

var crcOk = IsModbusCrcOk(frame);//先验证前8字节是否能校验成功

//0x10请求帧 帧长度需要根据帧的实际情况计算 长度=9+N 从站ID(1) 功能码(1) 起始地址(2) 寄存器数量(2) 字节数(1) 寄存器值(n) 校验码(2)
if (!crcOk && frame[1] == 0x10 && frameCache.Count >= (frame[6] + 9))
{
frame = frameCache.Take(frame[6] + 9).ToList();
}
else if (!crcOk && frame[1] == 0x10 && frameCache.Count < (frame[6] + 9))
//若8字节校验未通过, 则根据功能码再次解析
if (!crcOk)
{
continue;
}
//0x10请求帧 帧长度需要根据帧的实际情况计算 长度=9+N 从站ID(1) 功能码(1) 起始地址(2) 寄存器数量(2) 字节数(1) 寄存器值(n) 校验码(2)
if (frame[1] == 0x10 && frameCache.Count >= (frame[6] + 9))
{
frame = frameCache.Take(frame[6] + 9).ToList();
}
else if (frame[1] == 0x10 && frameCache.Count < (frame[6] + 9))
{
continue;
}

//0x03响应帧 从站ID(1) 功能码(1) 字节数(1) 寄存器值(N*×2) 校验码(2)
if (frame[1] == 0x03 && frameCache.Count >= (frame[2] + 5))
{
frame = frameCache.Take(frame[2] + 5).ToList();
}
else if (frame[1] == 0x03 && frameCache.Count < (frame[2] + 5))
{
continue;
}

//TODO 0x03和0x10粘包问题已处理 其他功能码的未做

//0x03响应帧 从站ID(1) 功能码(1) 字节数(1) 寄存器值(N*×2) 校验码(2)
if (!crcOk && frame[1] == 0x03 && frameCache.Count >= (frame[2] + 5))
{
frame = frameCache.Take(frame[2] + 5).ToList();
}
else if(!crcOk && frame[1] == 0x03 && frameCache.Count < (frame[2] + 5))
{
continue;
}




//若前面8字节验证失败, 则根据功能码截取不同帧长度后 再次验证
if (!crcOk)
{
Expand Down

0 comments on commit 384b0b3

Please sign in to comment.