コード例 #1
0
ファイル: ModbusHelper.cs プロジェクト: RSG-XGame/ScanSystem
        private ModbusRequest SendWrite(ModbusPackage package, ModbusFunctions functionCode, bool isInternal)
        {
            ModbusRequest request = null;

            if (package.CountVariables > 0)
            {
                request            = new ModbusRequest();
                request.IsInternal = isInternal;
                request.MBAPHeader.TransactionId = GetTransactionId();
                request.MBAPHeader.Length        = 0;
                request.MBAPHeader.UnitId        = UnitId;
                request.PDU.FunctionCode         = functionCode;

                List <byte> pdu = new List <byte>();
                pdu.AddRange(BitConverter.GetBytes(Convert.ToUInt16(package.StartRegister)).Reverse());
                pdu.AddRange(BitConverter.GetBytes(Convert.ToUInt16(package.CountRegisters)).Reverse());

                switch (functionCode)
                {
                case ModbusFunctions.ForceSingleCoil:
                case ModbusFunctions.PresetSingleRegister:
                    request.MBAPHeader.Length = 6;
                    break;

                case ModbusFunctions.ForceMultipleCoils:
                case ModbusFunctions.PresetMultipleRegisters:
                    pdu.Add(Convert.ToByte(package.SizeInBytes));
                    pdu.AddRange(package.GetData());
                    request.MBAPHeader.Length = (ushort)(pdu.Count + 2);
                    break;
                }
                request.PDU.Data = pdu.ToArray();
            }
            return(request);
        }
コード例 #2
0
ファイル: ModbusHelper.cs プロジェクト: RSG-XGame/ScanSystem
        private ModbusRequest SendRead(ModbusPackage package, ModbusFunctions functionCode, bool isInternal)
        {
            ModbusRequest request = null;

            if (package.CountVariables > 0)
            {
                request            = new ModbusRequest();
                request.IsInternal = isInternal;
                request.MBAPHeader.TransactionId = GetTransactionId();
                request.MBAPHeader.Length        = 6;
                request.MBAPHeader.UnitId        = UnitId;
                request.PDU.FunctionCode         = functionCode;
                request.PDU.Data = new byte[4];
                ConvertingToBytes(package.StartRegister, request.PDU.Data, 0);
                ConvertingToBytes(package.CountRegisters, request.PDU.Data, 2);
            }
            return(request);
        }