/// <summary>
 /// 读取设备的float类型的数组
 /// </summary>
 /// <param name="address">起始地址</param>
 /// <param name="length">数组长度</param>
 /// <returns>带成功标志的结果数据对象</returns>
 public OperateResult <float[]> ReadFloat(string address, ushort length)
 {
     return(ByteTransformHelper.GetResultFromBytes(Read(address, (ushort)(length * WordLength * 2)), m => ByteTransform.TransSingle(m, 0, length)));
 }
Exemplo n.º 2
0
 /// <summary>
 /// 向设备中写入double数组,返回是否写入成功
 /// </summary>
 /// <param name="address">数据地址</param>
 /// <param name="values">实际数据</param>
 /// <returns>返回写入结果</returns>
 /// <example>
 /// 以下为三菱的连接对象示例,其他的设备读写情况参照下面的代码:
 /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\NetworkDeviceBase.cs" region="WriteDoubleArray" title="Double类型示例" />
 /// </example>
 public OperateResult Write(string address, double[] values)
 {
     return(Write(address, ByteTransform.TransByte(values)));
 }
 /// <summary>
 /// 读取设备的short类型的数组
 /// </summary>
 /// <param name="address">起始地址</param>
 /// <param name="length">数组长度</param>
 /// <returns>带成功标志的结果数据对象</returns>
 public OperateResult <short[]> ReadInt16(string address, ushort length)
 {
     return(ByteTransformHelper.GetResultFromBytes(Read(address, (ushort)(length * WordLength)), m => ByteTransform.TransInt16(m, 0, length)));
 }
Exemplo n.º 4
0
 /// <summary>
 /// 向寄存器中写入字符串,编码格式为Unicode
 /// </summary>
 /// <param name="address">要写入的数据地址</param>
 /// <param name="value">要写入的实际数据</param>
 /// <returns>返回写入结果</returns>
 public OperateResult WriteUnicodeString(string address, string value)
 {
     byte[] temp = ByteTransform.TransByte(value, Encoding.Unicode);
     return(Write(address, temp));
 }
Exemplo n.º 5
0
 /// <summary>
 /// 向寄存器中写入指定长度的字符串,超出截断,不够补0,编码格式为Unicode
 /// </summary>
 /// <param name="address">要写入的数据地址</param>
 /// <param name="value">要写入的实际数据</param>
 /// <param name="length">指定的字符串长度,必须大于0</param>
 /// <returns>返回写入结果</returns>
 public OperateResult WriteUnicodeString(string address, string value, int length)
 {
     byte[] temp = ByteTransform.TransByte(value, Encoding.Unicode);
     temp = SoftBasic.ArrayExpandToLength(temp, length * 2);
     return(Write(address, temp));
 }
 /// <summary>
 /// 读取设备的字符串数据,编码为ASCII
 /// </summary>
 /// <param name="address">起始地址</param>
 /// <param name="length">地址长度</param>
 /// <returns>带成功标志的结果数据对象</returns>
 public OperateResult <string> ReadString(string address, ushort length)
 {
     return(ByteTransformHelper.GetResultFromBytes(Read(address, length), m => ByteTransform.TransString(m, 0, m.Length, Encoding.ASCII)));
 }
 /// <summary>
 /// 读取设备的uint类型的数组
 /// </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="ReadUInt32Array" title="UInt32类型示例" />
 /// </example>
 public virtual OperateResult <uint[]> ReadUInt32(string address, ushort length)
 {
     return(ByteTransformHelper.GetResultFromBytes(Read(address, (ushort)(length * WordLength * 2)), m => ByteTransform.TransUInt32(m, 0, length)));
 }
Exemplo n.º 8
0
 /// <summary>
 /// 读取设备的double类型的数组
 /// </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="ReadDoubleArray" title="Double类型示例" />
 /// </example>
 public override OperateResult <double[]> ReadDouble(string address, ushort length)
 {
     return(ByteTransformHelper.GetResultFromBytes(Read(address, length), m => ByteTransform.TransDouble(m, 0, length)));
 }
Exemplo n.º 9
0
 /// <summary>
 /// 向设备中写入float数组,返回是否写入成功
 /// </summary>
 /// <param name="address">数据地址</param>
 /// <param name="values">实际数据</param>
 /// <returns>返回写入结果</returns>
 /// <example>
 /// 以下为三菱的连接对象示例,其他的设备读写情况参照下面的代码:
 /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\NetworkDeviceBase.cs" region="WriteFloatArray" title="Float类型示例" />
 /// </example>
 public override OperateResult Write(string address, float[] values)
 {
     return(WriteTag(address, AllenBradleyHelper.CIP_Type_Real, ByteTransform.TransByte(values), values.Length));
 }
Exemplo n.º 10
0
 /// <summary>
 /// 向设备中写入uint数据,返回是否写入成功
 /// </summary>
 /// <param name="address">数据地址</param>
 /// <param name="value">实际数据</param>
 /// <returns>返回写入结果</returns>
 public override OperateResult Write(string address, uint value)
 {
     return(WriteTag(address, AllenBradleyHelper.CIP_Type_DWord, ByteTransform.TransByte(value)));
 }
Exemplo n.º 11
0
 /// <summary>
 /// 读取设备的ulong类型的数组
 /// </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="ReadUInt64Array" title="UInt64类型示例" />
 /// </example>
 public override OperateResult <ulong[]> ReadUInt64(string address, ushort length)
 {
     return(ByteTransformHelper.GetResultFromBytes(Read(address, length), m => ByteTransform.TransUInt64(m, 0, length)));
 }
Exemplo n.º 12
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);
                }
            }
        }
Exemplo n.º 13
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);
                }
            }
        }
Exemplo n.º 14
0
        private byte[] WriteByMessage(byte[] packCommand)
        {
            int NameLength   = packCommand[28];
            int RequestCount = BitConverter.ToUInt16(packCommand, 30 + NameLength);
            int _byte2       = 12 + (int)RequestCount;
            var result       = new List <byte>();
            var data5        = new byte[30];

            Array.Copy(packCommand, 0, data5, 0, 30);
            data5[9]  = 0x11;
            data5[10] = 0x01;
            data5[12] = 0xA0;
            data5[13] = 0x11;
            data5[16] = (byte)_byte2;
            data5[18] = 0x03;
            data5[20] = 89;
            data5[21] = 0;
            data5[22] = 20;
            data5[23] = 0;
            data5[24] = 0x08;
            data5[25] = 0x01;
            data5[26] = 0;
            data5[27] = 0;
            data5[28] = 1;
            data5[29] = 0;
            result.AddRange(data5);
            result.AddRange(BitConverter.GetBytes((ushort)RequestCount));

            var DeviceAddress = Encoding.ASCII.GetString(packCommand, 30, NameLength);
            var AddressLength = BitConverter.ToUInt16(packCommand, 30 + NameLength);
            var startIndex    = CheckAddress(DeviceAddress.Remove(0, 3));

            if (DeviceAddress.Substring(1, 2) == "DW" || DeviceAddress.Substring(1, 2) == "DB")
            {
                byte[] data = ByteTransform.TransByte(packCommand, 30 + NameLength + AddressLength, RequestCount);

                switch (DeviceAddress[1])
                {
                case 'C': inputBuffer.SetBytes(data, startIndex); break;

                case 'T': outputBuffer.SetBytes(data, startIndex); break;

                case 'M': memeryBuffer.SetBytes(data, startIndex); break;

                case 'D': dbBlockBuffer.SetBytes(data, startIndex); break;

                default: throw new Exception(StringResources.Language.NotSupportedDataType);
                }
                result.AddRange(data);
                return(result.ToArray());
            }
            else
            {
                bool value = BitConverter.ToBoolean(packCommand, 30 + NameLength + AddressLength);

                switch (DeviceAddress[1])
                {
                //case 'M': inputBuffer.SetBool(value, startIndex); break;
                //case 'M': outputBuffer.SetBool(value, startIndex); break;
                case 'M': memeryBuffer.SetBool(value, startIndex); break;

                case 'D': dbBlockBuffer.SetBool(value, startIndex); break;

                default: throw new Exception(StringResources.Language.NotSupportedDataType);
                }
                result.AddRange(new byte[] { 0, 0 });
                return(result.ToArray());
            }
        }
 /// <summary>
 /// 读取设备的ulong类型的数组
 /// </summary>
 /// <param name="address">起始地址</param>
 /// <param name="length">数组长度</param>
 /// <returns>带成功标志的结果数据对象</returns>
 public OperateResult <ulong[]> ReadUInt64(string address, ushort length)
 {
     return(ByteTransformHelper.GetResultFromBytes(Read(address, (ushort)(length * WordLength * 4)), m => ByteTransform.TransUInt64(m, 0, length)));
 }
Exemplo n.º 16
0
 /// <summary>
 /// 向设备中写入string数据,返回是否写入成功,该string类型是针对PLC的DINT类型,长度自动扩充到8
 /// </summary>
 /// <param name="address">数据地址</param>
 /// <param name="value">实际数据</param>
 /// <returns>返回写入结果</returns>
 public override OperateResult Write(string address, string value)
 {
     return(WriteTag(address, AllenBradleyHelper.CIP_Type_DWord, BasicFramework.SoftBasic.ArrayExpandToLength(ByteTransform.TransByte(value, Encoding.ASCII), 8)));
 }
 /// <summary>
 /// 读取设备的double类型的数组
 /// </summary>
 /// <param name="address">起始地址</param>
 /// <param name="length">数组长度</param>
 /// <returns>带成功标志的结果数据对象</returns>
 public OperateResult <double[]> ReadDouble(string address, ushort length)
 {
     return(ByteTransformHelper.GetResultFromBytes(Read(address, (ushort)(length * WordLength * 4)), m => ByteTransform.TransDouble(m, 0, length)));
 }
Exemplo n.º 18
0
 /// <summary>
 /// 向PLC中写入指定长度的字符串,超出截断,不够补0,编码格式为ASCII
 /// </summary>
 /// <param name="address">要写入的数据地址</param>
 /// <param name="value">要写入的实际数据</param>
 /// <param name="length">指定的字符串长度,必须大于0</param>
 /// <returns>写入结果</returns>
 public OperateResult Write(string address, string value, int length)
 {
     byte[] temp = ByteTransform.TransByte(value, Encoding.ASCII);
     temp = BasicFramework.SoftBasic.ArrayExpandToLength(temp, length);
     return(Write(address, temp));
 }
 /// <summary>
 /// 向设备中写入long数组,返回是否写入成功
 /// </summary>
 /// <param name="address">数据地址</param>
 /// <param name="values">实际数据</param>
 /// <returns>返回写入结果</returns>
 public virtual OperateResult Write(string address, long[] values)
 {
     return(Write(address, ByteTransform.TransByte(values)));
 }
Exemplo n.º 20
0
 /// <summary>
 /// 向PLC中字软元件写入字符串,编码格式为ASCII
 /// </summary>
 /// <param name="address">要写入的数据地址</param>
 /// <param name="value">要写入的实际数据</param>
 /// <returns>返回读取结果</returns>
 public OperateResult Write(string address, string value)
 {
     byte[] temp = ByteTransform.TransByte(value, Encoding.ASCII);
     temp = SoftBasic.ArrayExpandToLengthEven(temp);
     return(Write(address, temp));
 }