Ejemplo n.º 1
0
        public bool ExecFC(byte FC, ModbusDataStruct DataToSend, ref ModbusDataStruct DataReceived)
        {
            bool ResponseOK = false;

            Modbus.ModbusDataStruct Response = new ModbusDataStruct();
            byte[] Message = BuildModbusHeader(0, FC, DataToSend.GetAsBytes());
            SendBytes(Message);
            OnRequestSend(IP, DataToSend);

            int ResponseLength = GetResponse(Response);

            ResponseOK = CheckModbusHeader(0, FC, Response.GetAsBytes(), ResponseLength);
            if (ResponseOK & DataReceived != null)
            {
                OnRequestReceived(m_RemoteIpEndPoint.Address.ToString(), Response);
                RemoveModbusHeader(Response);
                switch (FC)
                {
                case 1:
                case 2:
                case 3:
                case 4:
                case 23:
                    DataReceived.SetAsBytes(Response.GetAsBytes(1,
                                                                Response.GetByteCount() - 1), Response.GetByteCount() - 1);//cut off  byte length and return only values
                    break;

                default:
                    DataReceived.SetAsBytes(Response.GetAsBytes(), Response.GetByteCount());
                    break;
                }
            }
            ;
            return(ResponseOK);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// FC16 Write multiple register
        /// FC16-Lengthfields will be added
        /// </summary>
        /// <param name="Register">= StartRegisterOffset</param>
        /// <param name="Data">2*n byte RegisterData</param>
        /// <returns></returns>
        public bool ExecFC16(UInt16 Register, ModbusDataStruct Data)
        {
            bool IsOK = false;

            Modbus.ModbusDataStruct Request = new ModbusDataStruct();
            Request.SetAsBytes(Data.GetAsBytes(), Data.GetByteCount());
            byte[] FCLengthFields = new byte[5];
            FCLengthFields[0] = (byte)((Register >> 8) & 0xFF);
            FCLengthFields[1] = (byte)((Register >> 8));
            FCLengthFields[2] = (byte)((Request.GetWordCount() >> 8) & 0xFF);
            FCLengthFields[3] = (byte)((Request.GetWordCount()) & 0xFF);
            FCLengthFields[4] = (byte)Request.GetByteCount();
            //insert Byte and Word count between RegisterAddr and Data
            Request.InsertByte(0, FCLengthFields, FCLengthFields.Length);
            ModbusDataStruct Answer = new ModbusDataStruct();

            IsOK = ExecFC(16, Request, ref Answer);
            return(IsOK);
        }