private byte[] WriteOneRegisterBack(byte[] modbus)
        {
            try
            {
                ushort address  = ByteTransform.TransUInt16(modbus, 2);
                short  ValueOld = ReadInt16(address.ToString( )).Content;
                // 写入到寄存器
                Write(address.ToString( ), modbus[4], modbus[5]);
                short ValueNew = ReadInt16(address.ToString( )).Content;
                // 触发写入请求
                OnRegisterBeforWrite(address, ValueOld, ValueNew);

                return(CreateWriteBack(modbus));
            }
            catch (Exception ex)
            {
                LogNet?.WriteException(ToString( ), StringResources.Language.ModbusTcpWriteRegisterException, ex);
                return(CreateExceptionBack(modbus, ModbusInfo.FunctionCodeReadWriteException));
            }
        }
Esempio n. 2
0
        private byte[] WriteOneCoilBack(byte[] modbus)
        {
            try
            {
                ushort address = ByteTransform.TransUInt16(modbus, 2);

                if (modbus[4] == 0xFF && modbus[5] == 0x00)
                {
                    WriteCoil(address.ToString( ), true);
                }
                else if (modbus[4] == 0x00 && modbus[5] == 0x00)
                {
                    WriteCoil(address.ToString( ), false);
                }
                return(CreateWriteBack(modbus));
            }
            catch (Exception ex)
            {
                LogNet?.WriteException(ToString( ), StringResources.Language.ModbusTcpWriteCoilException, ex);
                return(CreateExceptionBack(modbus, ModbusInfo.FunctionCodeReadWriteException));
            }
        }
Esempio n. 3
0
        private byte[] ReadByCommand(byte[] command)
        {
            if (command[3] == 0x01)
            {
                // 位读取
                int startIndex = command[9] * 65536 + command[10] * 256 + command[11];
                switch (command[8])
                {
                case 0x81: return(PackReadBitCommandBack(inputBuffer.GetBool(startIndex)));

                case 0x82: return(PackReadBitCommandBack(outputBuffer.GetBool(startIndex)));

                case 0x83: return(PackReadBitCommandBack(memeryBuffer.GetBool(startIndex)));

                case 0x84: return(PackReadBitCommandBack(dbBlockBuffer.GetBool(startIndex)));

                default: throw new Exception(StringResources.Language.NotSupportedDataType);
                }
            }
            else
            {
                // 字读取
                ushort length     = ByteTransform.TransUInt16(command, 4);
                int    startIndex = (command[9] * 65536 + command[10] * 256 + command[11]) / 8;
                switch (command[8])
                {
                case 0x81: return(PackReadWordCommandBack(inputBuffer.GetBytes(startIndex, length)));

                case 0x82: return(PackReadWordCommandBack(outputBuffer.GetBytes(startIndex, length)));

                case 0x83: return(PackReadWordCommandBack(memeryBuffer.GetBytes(startIndex, length)));

                case 0x84: return(PackReadWordCommandBack(dbBlockBuffer.GetBytes(startIndex, length)));

                default: throw new Exception(StringResources.Language.NotSupportedDataType);
                }
            }
        }
Esempio n. 4
0
 /// <summary>
 /// 读取PLC的ushort类型的数组 -> An array that reads the ushort type of the PLC
 /// </summary>
 /// <param name="address">节点的名称 -> Name of the node </param>
 /// <param name="length">数组长度 -> Array length </param>
 /// <returns>带有结果对象的结果数据 -> Result data with result info </returns>
 /// <example>
 /// 以下为三菱的连接对象示例,其他的设备读写情况参照下面的代码:
 /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\NetworkDeviceBase.cs" region="ReadUInt16Array" title="UInt16类型示例" />
 /// </example>
 public override OperateResult <ushort[]> ReadUInt16(string address, ushort length)
 {
     return(ByteTransformHelper.GetResultFromBytes(Read(address, length), m => ByteTransform.TransUInt16(m, 0, length)));
 }
Esempio n. 5
0
 /// <summary>
 /// 读取设备的ushort类型的数组
 /// </summary>
 /// <param name="address">起始地址</param>
 /// <param name="length">数组长度</param>
 /// <returns>带成功标志的结果数据对象</returns>
 /// <example>
 /// 以下为三菱的连接对象示例,其他的设备读写情况参照下面的代码:
 /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\NetworkDeviceBase.cs" region="ReadUInt16Array" title="UInt16类型示例" />
 /// </example>
 public virtual OperateResult <ushort[]> ReadUInt16(string address, ushort length)
 {
     return(ByteTransformHelper.GetResultFromBytes(Read(address, (ushort)(length * WordLength)), m => ByteTransform.TransUInt16(m, 0, length)));
 }
Esempio n. 6
0
        private byte[] WriteByMessage(byte[] command)
        {
            if (command[2] == 0x01)
            {
                // 位写入
                ushort length     = ByteTransform.TransUInt16(command, 8);
                int    startIndex = (command[6] * 65536 + command[5] * 256 + command[4]);

                if (command[7] == MelsecMcDataType.M.DataCode)
                {
                    byte[] buffer = MelsecMcNet.ExtractActualData(SoftBasic.BytesArrayRemoveBegin(command, 10), true).Content;
                    mBuffer.SetBytes(buffer.Take(length).ToArray( ), startIndex);
                    return(new byte[0]);
                }
                else if (command[7] == MelsecMcDataType.X.DataCode)
                {
                    byte[] buffer = MelsecMcNet.ExtractActualData(SoftBasic.BytesArrayRemoveBegin(command, 10), true).Content;
                    xBuffer.SetBytes(buffer.Take(length).ToArray( ), startIndex);
                    return(new byte[0]);
                }
                else if (command[7] == MelsecMcDataType.Y.DataCode)
                {
                    byte[] buffer = MelsecMcNet.ExtractActualData(SoftBasic.BytesArrayRemoveBegin(command, 10), true).Content;
                    yBuffer.SetBytes(buffer.Take(length).ToArray( ), startIndex);
                    return(new byte[0]);
                }
                else
                {
                    throw new Exception(StringResources.Language.NotSupportedDataType);
                }
            }
            else
            {
                // 字写入
                ushort length     = ByteTransform.TransUInt16(command, 8);
                int    startIndex = (command[6] * 65536 + command[5] * 256 + command[4]);

                if (command[7] == MelsecMcDataType.M.DataCode)
                {
                    byte[] buffer = SoftBasic.ByteToBoolArray(SoftBasic.BytesArrayRemoveBegin(command, 10)).Select(m => m ? (byte)1 : (byte)0).ToArray( );
                    mBuffer.SetBytes(buffer, startIndex);
                    return(new byte[0]);
                }
                else if (command[7] == MelsecMcDataType.X.DataCode)
                {
                    byte[] buffer = SoftBasic.ByteToBoolArray(SoftBasic.BytesArrayRemoveBegin(command, 10)).Select(m => m ? (byte)1 : (byte)0).ToArray( );
                    xBuffer.SetBytes(buffer, startIndex);
                    return(new byte[0]);
                }
                else if (command[7] == MelsecMcDataType.Y.DataCode)
                {
                    byte[] buffer = SoftBasic.ByteToBoolArray(SoftBasic.BytesArrayRemoveBegin(command, 10)).Select(m => m ? (byte)1 : (byte)0).ToArray( );
                    yBuffer.SetBytes(buffer, startIndex);
                    return(new byte[0]);
                }
                else if (command[7] == MelsecMcDataType.D.DataCode)
                {
                    dBuffer.SetBytes(SoftBasic.BytesArrayRemoveBegin(command, 10), startIndex * 2);
                    return(new byte[0]);
                }
                else if (command[7] == MelsecMcDataType.W.DataCode)
                {
                    wBuffer.SetBytes(SoftBasic.BytesArrayRemoveBegin(command, 10), startIndex * 2);
                    return(new byte[0]);
                }
                else
                {
                    throw new Exception(StringResources.Language.NotSupportedDataType);
                }
            }
        }
Esempio n. 7
0
        private byte[] ReadByCommand(byte[] command)
        {
            if (command[2] == 0x01)
            {
                // 位读取
                ushort length     = ByteTransform.TransUInt16(command, 8);
                int    startIndex = (command[6] * 65536 + command[5] * 256 + command[4]);

                if (command[7] == MelsecMcDataType.M.DataCode)
                {
                    byte[] buffer = mBuffer.GetBytes(startIndex, length);
                    return(MelsecHelper.TransBoolArrayToByteData(buffer));
                }
                else if (command[7] == MelsecMcDataType.X.DataCode)
                {
                    byte[] buffer = xBuffer.GetBytes(startIndex, length);
                    return(MelsecHelper.TransBoolArrayToByteData(buffer));
                }
                else if (command[7] == MelsecMcDataType.Y.DataCode)
                {
                    byte[] buffer = yBuffer.GetBytes(startIndex, length);
                    return(MelsecHelper.TransBoolArrayToByteData(buffer));
                }
                else
                {
                    throw new Exception(StringResources.Language.NotSupportedDataType);
                }
            }
            else
            {
                // 字读取
                ushort length     = ByteTransform.TransUInt16(command, 8);
                int    startIndex = (command[6] * 65536 + command[5] * 256 + command[4]);
                if (command[7] == MelsecMcDataType.M.DataCode)
                {
                    bool[] buffer = mBuffer.GetBytes(startIndex, length * 16).Select(m => m != 0x00).ToArray( );
                    return(SoftBasic.BoolArrayToByte(buffer));
                }
                else if (command[7] == MelsecMcDataType.X.DataCode)
                {
                    bool[] buffer = xBuffer.GetBytes(startIndex, length * 16).Select(m => m != 0x00).ToArray( );
                    return(SoftBasic.BoolArrayToByte(buffer));
                }
                else if (command[7] == MelsecMcDataType.Y.DataCode)
                {
                    bool[] buffer = yBuffer.GetBytes(startIndex, length * 16).Select(m => m != 0x00).ToArray( );
                    return(SoftBasic.BoolArrayToByte(buffer));
                }
                else if (command[7] == MelsecMcDataType.D.DataCode)
                {
                    return(dBuffer.GetBytes(startIndex * 2, length * 2));
                }
                else if (command[7] == MelsecMcDataType.W.DataCode)
                {
                    return(wBuffer.GetBytes(startIndex * 2, length * 2));
                }
                else
                {
                    throw new Exception(StringResources.Language.NotSupportedDataType);
                }
            }
        }