Пример #1
0
        /// <summary>
        /// This Task handles all the types of Modbus Functions
        /// </summary>
        /// <returns>The operation.</returns>
        /// <param name="functionType">Function type.</param>
        /// <param name="slaveAddress">Slave address.</param>
        /// <param name="nrRegisters">Nr registers.</param>
        /// <param name="selectedRegisterAddress">Selected register address.</param>
        /// <param name="dataToWrite">Data to write.</param>
        public async Task <ReceivedData> ModbusOperation(ModbusFunctionType functionType, byte slaveAddress, byte[] nrRegisters, byte[] selectedRegisterAddress, byte[] dataToWrite)
        {
            frameToSend = new ModbusFrame();

            if (functionType == ModbusFunctionType.WriteCoil || functionType == ModbusFunctionType.WriteHoldingRegister)
            {
                frameToSend = new ModbusFrame(slaveAddress, functionType, selectedRegisterAddress, null, dataToWrite);
            }
            else
            {
                frameToSend = new ModbusFrame(slaveAddress, functionType, selectedRegisterAddress, nrRegisters, null);
            }


            //We send the message to the Slave Device
            TransmissionState b = await SendMessage(frameToSend.BytePacketToSend);

            sizeOfMessage = CalculateExpectedSize();



            ReceivedData responseData = new ReceivedData();

            //If the message was transmitted right, then proceed with the listening
            //of the response
            if (b == TransmissionState.OK)
            {
                System.Diagnostics.Debug.WriteLine("MessageSended: " + b.ToString());

                //TO-DO Implement the retry mechanism
                //int retries = 3;
                //responseData = await ListenMessages();

                responseData = await ListenMessages();
            }
            else
            {
                responseData.state = TransmissionState.ErrorSendMessage;
            }

            if (responseData.state == TransmissionState.OK)
            {
                frameReceived = new ModbusFrame(responseData.data);
            }
            else
            {
                frameReceived = new ModbusFrame();
            }

            return(responseData);
        }
Пример #2
0
 /// <summary>
 /// Initializes a new instance of the ModbusFrame class
 /// based on the informations given in input by the user.
 /// It can be used to the Request to Read or Write
 /// </summary>
 /// <param name="slaveAddress">Slave address.</param>
 /// <param name="ft">Ft.</param>
 /// <param name="registerAddr">Register address.</param>
 /// <param name="nrRegisters">Nr registers.</param>
 /// <param name="dataToWrite">Data to write.</param>
 /// <param name="request">If set to <c>true</c> request.</param>
 public ModbusFrame(byte slaveAddress, ModbusFunctionType ft, byte[] registerAddr, byte[] nrRegisters, byte[] dataToWrite, bool request = true)
 {
     this.SlaveAddress      = slaveAddress;
     this.FunctionType      = ft;
     this.FunctionCode      = functionCodesRequests[ft];
     this.RegisterAddress   = registerAddr;
     this.NumberOfRegisters = nrRegisters;
     this.DataToWrite       = dataToWrite;
     this.IsRequestFrame    = request;
     this.PacketInBytes     = BuildBytesFrame();
     this.CRC = ModRTU_CRC();
     this.BuildPacketToSend();
     this.SetFrameType();
     this.State = ModbusFrameState.Correct;
 }