Пример #1
0
 /// <summary>
 /// Extract the typical header for a request command (server-side)
 /// </summary>
 /// <param name="command"></param>
 /// <param name="body"></param>
 internal static void PopRequestHeader(
     ModbusCommand command,
     ByteArrayReader body)
 {
     command.Offset = body.ReadUInt16BE();
     command.Count = body.ReadInt16BE();
 }
Пример #2
0
        //public static readonly ModbusCommandCodec[] CommandCodecs = new ModbusCommandCodec[36];



        /// <summary>
        /// Append the typical header for a request command (master-side)
        /// </summary>
        /// <param name="command"></param>
        /// <param name="body"></param>
        internal static void PushRequestHeader(
            ModbusCommand command,
            ByteArrayWriter body)
        {
            body.WriteUInt16BE((ushort)command.Offset);
            body.WriteInt16BE((short)command.Count);
        }
Пример #3
0
 public override void ClientEncode(
     ModbusCommand command,
     ByteArrayWriter body)
 {
     ModbusCodecBase.PushRequestHeader(
         command,
         body);
 }
Пример #4
0
 public override void ClientDecode(
     ModbusCommand command,
     ByteArrayReader body)
 {
     var count = body.ReadByte() / 2;
     command.Data = new ushort[count];
     for (int i = 0; i < count; i++)
         command.Data[i] = body.ReadUInt16BE();
 }
Пример #5
0
        public override void ServerEncode(
            ModbusCommand command,
            ByteArrayWriter body)
        {
            body.WriteByte(_device);
            var count = command.Count;
            body.WriteByte((byte)(count));
            body.WriteByte(0xFF);
            body.WriteByte(0xFF);
            body.WriteInt32BE(_address);

            if (_category == 0)
            {
                for (int i = 0; i < count/2; i++)
                {
                    UInt16 v = (UInt16) GetRandomNumber(0, 25);
                    byte h = (byte)(v >> 8);
                    byte l = (byte)(v &0x00FF);
                    UInt16 t = (UInt16)((l << 8) + h);
                    body.WriteUInt16BE(t);
                }
            }
            else if (_category == 2)
            {
                for (int i = 0; i < count / 2; i++)
                {
                    body.WriteUInt16BE((UInt16)GetRandomNumber(0, 5));
                }

            }
            else 
            {
                for (int i = 0; i < count / 2; i++)
                {
                    body.WriteUInt16BE((UInt16)GetRandomNumber(0, 50));
                }

            }

        }
Пример #6
0
 private void BtnWriteSingleCoilClick(object sender, EventArgs e)
 {
     try
     {
         var command = new ModbusCommand(ModbusCommand.FuncWriteCoil)
         {
             Offset = StartAddress,
             Count = 1,
             TransId = _transactionId++,
             Data = new ushort[1]
         };
         command.Data[0] = (ushort)(_registerData[StartAddress] & 0x0100);
         var result = _driver.ExecuteGeneric(_portClient, command);
         AppendLog(result.Status == CommResponse.Ack
                       ? String.Format("Write succeeded: Function code:{0}", ModbusCommand.FuncWriteCoil)
                       : String.Format("Failed to execute Write: Error code:{0}", result.Status));
     }
     catch (Exception ex)
     {
         AppendLog(ex.Message);
     }
 }
Пример #7
0
 private void ExecuteWriteCommand(byte function)
 {
     try
     {
         var command = new ModbusCommand(function)
                           {
                               Offset = StartAddress,
                               Count = DataLength,
                               TransId = _transactionId++,
                               Data = new ushort[DataLength]
                           };
         for (int i = 0; i < DataLength; i++)
         {
             var index = StartAddress + i;
             if (index > _registerData.Length)
             {
                 break;
             }
             command.Data[i] = _registerData[index];
         }
         var result = _driver.ExecuteGeneric(_portClient, command);
         AppendLog(result.Status == CommResponse.Ack
                       ? String.Format("Write succeeded: Function code:{0}", function)
                       : String.Format("Failed to execute Write: Error code:{0}", result.Status));
     }
     catch (Exception ex)
     {
         AppendLog(ex.Message);
     }
 }
Пример #8
0
 private void ExecuteReadCommand(byte function)
 {
     try
     {
         var command = new ModbusCommand(function) {Offset = StartAddress, Count = DataLength, TransId = _transactionId++};
         var result = _driver.ExecuteGeneric(_portClient, command);
         if (result.Status == CommResponse.Ack)
         {
             command.Data.CopyTo(_registerData, StartAddress);
             UpdateDataTable();
             AppendLog(String.Format("Read succeeded: Function code:{0}.", function));
         }
         else
         {
             AppendLog(String.Format("Failed to execute Read: Error code:{0}", result.Status));
         }
     }
     catch (Exception ex)
     {
         AppendLog(ex.Message);
     }
 }
Пример #9
0
 private void DoWrite(ModbusCommand command)
 {
     var dataAddress = command.Offset;
     if (dataAddress < StartAddress || dataAddress > StartAddress + _displayCtrlCount)
     {
         AppendLog(String.Format("Received address is not within viewable range, Received address:{0}.", dataAddress));
         return;
     }
     if ((command.Count + dataAddress) > _registerData.Length)
     {
         AppendLog(String.Format("Received address is not within viewable range, Received address:{0}.", dataAddress));
         return;
     }
     command.Data.CopyTo(_registerData, dataAddress);
     UpdateDataTable();
     AppendLog(String.Format("Received data: Function code:{0}.", command.FunctionCode));
 }
Пример #10
0
        private void DoRead(ModbusCommand command)
        {
            for (int i = 0; i < command.Count; i++)
                command.Data[i] = _registerData[command.Offset + i];
            AppendLog(String.Format("Sent data: Function code:{0}.", command.FunctionCode));

        }
Пример #11
0
 private void IllegalFunction(ModbusCommand command)
 {
     AppendLog(String.Format("Illegal Function, expecting function code {0}.", FunctionCode));
     command.ExceptionCode = ModbusCommand.ErrorIllegalFunction;
 }
Пример #12
0
        public override void ServerDecode(
            ModbusCommand command,
            ByteArrayReader body)
        {

            _device = body.ReadByte();
            var data_len = body.ReadByte();
            body.ReadByte();
            body.ReadByte();
            _address = body.ReadInt32BE();
            _dayIndex = _address & 0x03FF;
            _category = (_address & 0xFC00) >> 10;
            command.Count = data_len;
            command.Data = new ushort[command.Count];
        }
Пример #13
0
 /// <summary>
 /// Helper for packing the discrete data outgoing as a bit-array
 /// </summary>
 /// <param name="command"></param>
 /// <param name="body"></param>
 internal static void PushDiscretes(
     ModbusCommand command,
     ByteArrayWriter body)
 {
     var count = ((byte)((command.Count + 7) / 8));
     var wholeWords = command.Count / 16;
     var remainingBits = command.Count % 16;
     body.WriteByte(count);
     int k;
     for (k = 0; k < wholeWords; k++)
     {
         var hb = (byte)(command.Data[k] >> 8);
         var lb = (byte)(command.Data[k] & 0x00FF);
         body.WriteByte(hb);
         body.WriteByte(lb);
     }
     if (remainingBits > 0)
     {
         byte bitMask = 1;
         byte cell = 0;
         byte currentByte = (byte)(command.Data[k] >> 8);
         for (int j = 0; j < remainingBits; j++)
         {
             if (j == 8)
             {
                 body.WriteByte(cell);
                 currentByte = (byte)(command.Data[k] & 0x00FF);
                 bitMask = 1;
                 cell = 0;
             }
             cell |= (byte)(currentByte & bitMask);
             bitMask = (byte)(bitMask << 1);
         }
         body.WriteByte(cell);
     }
 }
Пример #14
0
        /// <summary>
        /// Helper for unpacking discrete data incoming as a bit-array
        /// </summary>
        /// <param name="command"></param>
        /// <param name="body"></param>
        internal static void PopDiscretes(
            ModbusCommand command,
            ByteArrayReader body)
        {
            var byteCount = body.ReadByte();

            var count = command.Count;
            command.Data = new ushort[count];
            command.QueryTotalLength += (byteCount + 1);

            int k = 0;
            while (body.EndOfBuffer == false)
            {
                if (command.Count <= k)
                    break;
                byte hb = body.CanRead(1) ? body.ReadByte() : (byte)0;
                byte lb = body.CanRead(1) ? body.ReadByte() : (byte)0;
                command.Data[k++] = (ushort)((hb << 8) | lb);
                //int n = count <= 8 ? count : 8;
                //count -= n;
                //for (int i = 0; i < n; i++)
                //    command.Data[k++] = (ushort)(cell & (1 << i));
            }
        }
Пример #15
0
        CommResponse IProtocolCodec.ServerDecode(CommDataBase data)
        {
            var server = (ModbusServer)data.OwnerProtocol;
            var incoming = data.IncomingData;

            //validate header first
            if (incoming.Length < 6)
                goto LabelUnknown;

            //transaction-id
            var transId = incoming.ReadUInt16BE();

            //protocol-identifier
            var protId = incoming.ReadInt16BE();
            if (protId != 0)
                goto LabelIgnore;

            //message length
            var length = incoming.ReadInt16BE();

            if (incoming.Length < (length + 6))
                goto LabelUnknown;

            //address
            var address = incoming.ReadByte();

            if (address == server.Address)
            {
                //function code
                var fncode = incoming.ReadByte();
                if (!CommandCodecs.ContainsKey(fncode))
                {
                    return new CommResponse(null, CommResponse.Unknown);
                }

                //create a new command
                var command = new ModbusCommand(fncode);
                data.UserData = command;
                command.TransId = transId;

                //
                var body = new ByteArrayReader(incoming.ReadToEnd());

                //encode the command body, if applies
                var codec = CommandCodecs[fncode];
                if (codec != null)
                    codec.ServerDecode(command, body);

                return new CommResponse(
                    data,
                    CommResponse.Ack);
            }

            //exception
        LabelIgnore:
            return new CommResponse(
                data,
                CommResponse.Ignore);

        LabelUnknown:
            return new CommResponse(
                data,
                CommResponse.Unknown);
        }
Пример #16
0
        CommResponse IProtocolCodec.ServerDecode(CommDataBase data)
        {
            var server   = (ModbusServer)data.OwnerProtocol;
            var incoming = data.IncomingData;

            //validate header first
            if (incoming.Length < 6)
            {
                goto LabelUnknown;
            }

            //transaction-id
            var transId = incoming.ReadUInt16BE();

            //protocol-identifier
            var protId = incoming.ReadInt16BE();

            if (protId != 0)
            {
                goto LabelIgnore;
            }

            //message length
            var length = incoming.ReadInt16BE();

            if (incoming.Length < (length + 6))
            {
                goto LabelUnknown;
            }

            //address
            var address = incoming.ReadByte();

            if (address == server.Address)
            {
                //function code
                var fncode = incoming.ReadByte();
                if (!CommandCodecs.ContainsKey(fncode))
                {
                    return(new CommResponse(null, CommResponse.Unknown));
                }

                //create a new command
                var command = new ModbusCommand(fncode);
                data.UserData   = command;
                command.TransId = transId;

                //
                var body = new ByteArrayReader(incoming.ReadToEnd());

                //encode the command body, if applies
                var codec = CommandCodecs[fncode];
                if (codec != null)
                {
                    codec.ServerDecode(command, body);
                }

                return(new CommResponse(
                           data,
                           CommResponse.Ack));
            }

            //exception
LabelIgnore:
            return(new CommResponse(
                       data,
                       CommResponse.Ignore));

LabelUnknown:
            return(new CommResponse(
                       data,
                       CommResponse.Unknown));
        }