示例#1
0
 //根据modbus响应的对象转成字节数组
 public static byte[] ToBytes(ModbusRTUResponse request)
 {
     return(new byte[0]);
 }
示例#2
0
            //根据字节数组转成modbus响应上来的对象
            public static ModbusRTUResponse FromBytes(byte[] bytes, out string errMsg)
            {
                errMsg = "";
                try
                {
                    switch (bytes[1])
                    {
                    case 0x03:
                    {
                        if (bytes.Length != 7)
                        {
                            return(null);
                        }
                        //读寄存器回应
                        ModbusRTUResponse response = new ModbusRTUResponse();
                        response.PlcAddress = bytes[0];
                        response.Type       = ResponseType.Read;
                        // 含义:寄存器的个数*2
                        response.DataLength = bytes[2];
                        response.Values     = ByteUtil.ToUshorts(bytes, 3, response.DataLength);
                        response.CRC        = BitConverter.ToUInt16(bytes, 5);   // CRC 无需反转高低位
                        if (response.DataLength == 0x02)
                        {
                            // 针对单个寄存器
                            response.RegisterValue = response.Values[0];
                            response.RegisterCount = 0x01;
                        }
                        return(response);
                    }

                    case 0x10:
                    {
                        if (bytes.Length != 8)
                        {
                            return(null);
                        }
                        // 目前只有单个寄存器写入
                        ModbusRTUResponse response = new ModbusRTUResponse();
                        response.PlcAddress           = bytes[0];
                        response.Type                 = ResponseType.Write;
                        response.RegisterBeginAddress = ByteUtil.ToUshort(bytes, 2);
                        response.RegisterValue        = ByteUtil.ToUshort(bytes, 4);
                        response.CRC = BitConverter.ToUInt16(bytes, 6);          // CRC 无需反转高低位
                        return(response);
                    }

                    case 0x83:
                    {
                        if (bytes.Length != 5)
                        {
                            return(null);
                        }
                        // 读单个寄存器请求出错
                        ModbusRTUResponse response = new ModbusRTUResponse();
                        response.PlcAddress = bytes[0];
                        response.Type       = ResponseType.ReadError;
                        response.errMsg     = "读寄存器请求出错--" + GetErrMSg(bytes[2]);
                        response.CRC        = BitConverter.ToUInt16(bytes, 3);   // CRC 无需反转高低位
                        return(response);
                    }

                    case 0x86:
                    {
                        if (bytes.Length != 5)
                        {
                            return(null);
                        }
                        // 写单个寄存器请求出错
                        ModbusRTUResponse response = new ModbusRTUResponse();
                        response.PlcAddress = bytes[0];
                        response.Type       = ResponseType.WriteError;
                        response.errMsg     = "写寄存器请求出错--" + GetErrMSg(bytes[2]);
                        response.CRC        = BitConverter.ToUInt16(bytes, 3);   // CRC 无需反转高低位
                        return(response);
                    }
                    }
                }
                catch (Exception e)
                {
                    errMsg = e.Message;
                    return(null);
                }
                errMsg = "字节数组转modbus回应对象出现未知的功能码";
                return(null);
            }