C#Modbus RTU读取数据长度不同

C#Modbus RTU读取数据长度不同,c#,C#,我希望我能解释。很抱歉,有些文本使用不同的语言,我认为这与此问题无关。我正在尝试从Modbus RTU模式传感器读取数据。我还创建了一个读取程序和CRC检查。数据读取成功,但响应长度不同,如:- 长度决定REC的第5和第6个值,如上文所述:-(1E)或(1A) 我的代码是 public bool RsTransmitRec(string msg) { bool result = false; string outmsg = ms

我希望我能解释。很抱歉,有些文本使用不同的语言,我认为这与此问题无关。我正在尝试从Modbus RTU模式传感器读取数据。我还创建了一个读取程序和CRC检查。数据读取成功,但响应长度不同,如:-

长度决定REC的第5和第6个值,如上文所述:-(1E)或(1A)

我的代码是

 public bool RsTransmitRec(string msg)
        {
            bool result = false;
            string outmsg = msg;

            spExec = true;
            try
            {
                sp.DiscardInBuffer();
                sp.DiscardOutBuffer();
                sp.Write(globalFrame, 0, globalFrame.Length);
                result = true;
                MainLogTextDisp("TR<" + outmsg + ">", Color.Blue); //TR command
                result = ResRecieve();
            }
            catch (Exception)
            {
                result = false;
                spExec = false;
            }
            spExec = true;
            return (result);
        }
public bool RsTransmitRec(字符串消息)
{
布尔结果=假;
字符串outmsg=msg;
spExec=真;
尝试
{
丢弃缓冲液;
sp.discardbuffer();
sp.Write(globalFrame,0,globalFrame.Length);
结果=真;
MainLogTextDisp(“TR”,Color.Blue);//TR命令
结果=重新接收();
}
捕获(例外)
{
结果=假;
spExec=false;
}
spExec=真;
返回(结果);
}
上面的代码用于传输,下面的代码用于响应

 public bool ResRecieve()
        {
            bool result = false;
            tOutRec.TimeOutSet(0x100000);  //Start timeout monitoring                   
            while (true)
            {
                try
                {
                    if (sp.BytesToRead > 5)//If I change number here when I     try to read holding register it will be the small length
                    {
                        byte[] bufferReceiver = new byte[this.sp.BytesToRead];
                        sp.Read(bufferReceiver, 0, sp.BytesToRead);
                        MainLogTextDisp("REC<" + dterr_chk.Display(bufferReceiver) + ">", Color.Blue);
                        sp.DiscardInBuffer();
                        sp.DiscardOutBuffer();
                        _recDoubleBuffer = dterr_chk.Display(bufferReceiver);
                        result = true;
                        break;
                    }
                    tOutRec.TimerInc();
                    if (tOutRec.TimeOutCheck() == true)
                    {
                        MainLogTextDisp("受信タイムアウト", Color.Red);
                        result = false;
                        break;
                    }
                }
                catch (Exception ex)
                {
                    //何かエラーが起きたら
                    MainLogTextDisp(ex.Message, Color.Red);
                    EventAction(REC_ERROR);
                }
            }
            return (result);
        }
public bool resreceive()
{
布尔结果=假;
tOutRec.timestart(0x100000);//启动超时监视
while(true)
{
尝试
{
if(sp.BytesToRead>5)//当我尝试读取保持寄存器时,如果我在这里更改数字,那么它将是较小的长度
{
byte[]bufferReceiver=新字节[this.sp.BytesToRead];
sp.Read(缓冲接收器,0,sp.BytesToRead);
MainLogTextDisp(“REC”,颜色为蓝色);
丢弃缓冲液;
sp.discardbuffer();
_recDoubleBuffer=dterr\u chk.Display(bufferReceiver);
结果=真;
打破
}
tOutRec.TimerInc();
if(tOutRec.TimeOutCheck()==true)
{
MainLogTextDisp(“受信タイムアウト", 颜色(红色);
结果=假;
打破
}
}
捕获(例外情况除外)
{
//何かエラーが起きたら
MainLogTextDisp(例如消息,颜色为红色);
事件操作(记录错误);
}
}
返回(结果);
}
删除

DiscardInBuffer();
DiscardOutBuffer();
在你的密码里,这对我很有帮助