Ejemplo n.º 1
0
        /// <summary>
        /// write data to address  Type of ReadByte
        /// </summary>
        /// <param name="station">plc station</param>
        /// <param name="address">address, for example: M100, D100, DW100</param>
        /// <param name="value">source value</param>
        /// <returns>command bytes</returns>
        private static OperateResult <byte[]> BuildWriteByteCommand(byte station, string address, byte[] value)
        {
            var analysisResult = XGBFastEnet.AnalysisAddress(address);

            if (!analysisResult.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(analysisResult));
            }

            List <byte> command = new List <byte>( );

            command.Add(0x05);      // ENQ
            command.AddRange(SoftBasic.BuildAsciiBytesFrom(station));
            command.Add(0x77);      // command w
            command.Add(0x53);      // command type: SB
            command.Add(0x42);
            command.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)analysisResult.Content.Length));
            command.AddRange(Encoding.ASCII.GetBytes(analysisResult.Content));
            command.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)value.Length));
            command.AddRange(SoftBasic.BytesToAsciiBytes(value));
            command.Add(0x04);      // EOT

            int sum = 0;

            for (int i = 0; i < command.Count; i++)
            {
                sum += command[i];
            }
            command.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)sum));

            return(OperateResult.CreateSuccessResult(command.ToArray( )));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 往指定的地址里写入bool数据对象
        /// </summary>
        /// <param name="address">西门子的地址信息</param>
        /// <param name="value">值</param>
        /// <returns>是否成功的结果</returns>
        public OperateResult Write(string address, bool value)
        {
            OperateResult <string> analysis = XGBFastEnet.AnalysisAddress(address, false);

            if (!analysis.IsSuccess)
            {
                return(analysis);
            }

            // to do, this is not right
            int startIndex = CheckAddress(analysis.Content.Substring(3));

            switch (analysis.Content[1])
            {
            case 'P': pBuffer.SetBool(value, startIndex); return(OperateResult.CreateSuccessResult());

            case 'Q': qBuffer.SetBool(value, startIndex); return(OperateResult.CreateSuccessResult());

            case 'M': mBuffer.SetBool(value, startIndex); return(OperateResult.CreateSuccessResult());

            case 'D': dBuffer.SetBool(value, startIndex); return(OperateResult.CreateSuccessResult());

            default: return(new OperateResult(StringResources.Language.NotSupportedDataType));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 读取自定义的寄存器的值
        /// </summary>
        /// <param name="address">起始地址,示例:"I100","M100"</param>
        /// <param name="length">数据长度</param>
        /// <exception cref="IndexOutOfRangeException"></exception>
        /// <returns>byte数组值</returns>
        public override OperateResult <byte[]> Read(string address, ushort length)
        {
            int startIndex = 0;

            string[] fullAddressSplit       = null;
            string   Dmain                  = string.Empty;
            string   AddressSplit           = string.Empty;
            OperateResult <string> analysis = XGBFastEnet.AnalysisAddress(address, true);

            if (!analysis.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(analysis));
            }
            if (address.IndexOf(".") > 0)//BitOnByte and BitOnWord
            {
                Dmain        = address.Substring(0, 1);
                AddressSplit = address.Remove(0, 2);
                switch (Dmain)
                {
                case "I":
                case "Q":
                case "U":
                {
                    fullAddressSplit = AddressSplit.Split(new char[1] {
                            '.'
                        }, 4);
                    startIndex = CheckAddress(fullAddressSplit[3]);
                    break;
                }

                default:    // MB---DB-----
                {
                    fullAddressSplit = AddressSplit.Split(new char[1] {
                            '.'
                        }, 2);
                    startIndex = CheckAddress(fullAddressSplit[0].Substring(2));
                    break;
                }
                }
            }
            else
            {
                startIndex = CheckAddress(analysis.Content.Substring(3));
            }

            switch (analysis.Content[1])
            {
            case 'I':
            case 'Q':
            case 'U':
            case 'P':
                return(OperateResult.CreateSuccessResult(SoftBasic.BoolArrayToByte(pBuffer.GetBool(startIndex, length *= 8))));

            case 'M': return(OperateResult.CreateSuccessResult(SoftBasic.BoolArrayToByte(mBuffer.GetBool(startIndex, length *= 8))));

            case 'D': return(OperateResult.CreateSuccessResult(dBuffer.GetBytes(startIndex == 0 ? startIndex : startIndex *= 2, length)));

            default: return(new OperateResult <byte[]>(StringResources.Language.NotSupportedDataType));
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 写入自定义的数据到数据内存中去
        /// </summary>
        /// <param name="address">地址</param>
        /// <param name="value">数据值</param>
        /// <returns>是否写入成功的结果对象</returns>
        public override OperateResult Write(string address, byte[] value)
        {
            int startIndex = 0;

            OperateResult <string> analysis = XGBFastEnet.AnalysisAddress(address);

            if (!analysis.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(analysis));
            }

            startIndex = CheckAddress(analysis.Content.Substring(3));


            switch (analysis.Content[1])
            {
            case 'I':
            case 'Q':
            case 'U':
            case 'P':
                pBuffer.SetBytes(value, startIndex); return(OperateResult.CreateSuccessResult());

            case 'M': mBuffer.SetBool(value[0] == 1 ? true : false, startIndex); return(OperateResult.CreateSuccessResult());

            case 'D': dBuffer.SetBytes(value, startIndex == 0 ? startIndex : startIndex *= 2); return(OperateResult.CreateSuccessResult());

            default: return(new OperateResult <byte[]>(StringResources.Language.NotSupportedDataType));
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 读取指定地址的bool数据对象
        /// </summary>
        /// <param name="address">西门子的地址信息</param>
        /// <returns>带有成功标志的结果对象</returns>
        public OperateResult <bool> ReadBool(string address)
        {
            OperateResult <string> analysis = XGBFastEnet.AnalysisAddress(address, true);

            if (!analysis.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <bool>(analysis));
            }

            // to do, this is not right
            int startIndex = CheckAddress(analysis.Content.Substring(3));

            switch (analysis.Content[1])
            {
            case 'P': return(OperateResult.CreateSuccessResult(pBuffer.GetBool(startIndex)));

            case 'Q': return(OperateResult.CreateSuccessResult(qBuffer.GetBool(startIndex)));

            case 'M': return(OperateResult.CreateSuccessResult(mBuffer.GetBool(startIndex)));

            case 'D': return(OperateResult.CreateSuccessResult(dBuffer.GetBool(startIndex)));

            default: return(new OperateResult <bool>(StringResources.Language.NotSupportedDataType));
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// One reading address  Type of ReadByte
        /// </summary>
        /// <param name="station">plc station</param>
        /// <param name="address">address, for example: MX100, DW100, TW100</param>
        /// <param name="length">read length</param>
        /// <returns></returns>
        private static OperateResult <byte[]> BuildReadOneCommand(byte station, string address, ushort length)
        {
            var analysisResult = XGBFastEnet.AnalysisAddress(address, true);

            if (!analysisResult.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(analysisResult));
            }

            List <byte> command = new List <byte>();

            command.Add(0x05);    // ENQ
            command.AddRange(SoftBasic.BuildAsciiBytesFrom(station));
            command.Add(0x72);    // command r
            command.Add(0x53);    // command type: SS
            command.Add(0x53);
            command.Add(0x01);    // Number of blocks
            command.Add(0x00);
            command.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)analysisResult.Content.Length));
            command.AddRange(Encoding.ASCII.GetBytes(analysisResult.Content));
            command.Add(0x04);    // EOT

            int sum = 0;

            for (int i = 0; i < command.Count; i++)
            {
                sum += command[i];
            }
            command.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)sum));

            return(OperateResult.CreateSuccessResult(command.ToArray()));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 读取自定义的寄存器的值
        /// </summary>
        /// <param name="address">起始地址,示例:"I100","M100"</param>
        /// <param name="length">数据长度</param>
        /// <exception cref="IndexOutOfRangeException"></exception>
        /// <returns>byte数组值</returns>
        public override OperateResult <byte[]> Read(string address, ushort length)
        {
            int startIndex = 0;

            OperateResult <string> analysis = XGBFastEnet.AnalysisAddress(address);

            if (!analysis.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(analysis));
            }
            startIndex = CheckAddress(analysis.Content.Substring(3));
            switch (analysis.Content[1])
            {
            case 'I':
            case 'Q':
            case 'U':
            case 'P':
                return(OperateResult.CreateSuccessResult(SoftBasic.BoolArrayToByte(pBuffer.GetBool(startIndex, length *= 8))));

            case 'M': return(OperateResult.CreateSuccessResult(SoftBasic.BoolArrayToByte(mBuffer.GetBool(startIndex, length *= 8))));

            case 'D': return(OperateResult.CreateSuccessResult(dBuffer.GetBytes(startIndex == 0 ? startIndex : startIndex *= 2, length)));

            default: return(new OperateResult <byte[]>(StringResources.Language.NotSupportedDataType));
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Continuous writing to inverter device (W)
        /// </summary>
        /// <param name="station">plc station</param>
        /// <param name="address">address, for example: 0100 </param>
        /// <param name="value">source value</param>
        /// <returns>command bytes</returns>
        private static OperateResult <byte[]> BuildWriteByteCommand(byte station, string address, byte[] value)
        {
            var analysisResult = XGBFastEnet.AnalysisAddress(address);

            if (!analysisResult.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(analysisResult));
            }

            List <byte> command = new List <byte>();

            command.Add(0x05);    // ENQ
            command.AddRange(SoftBasic.BuildAsciiBytesFrom(station));
            command.Add(0x57);    // command W  Write inverter variable of Word.
            command.Add(0x06);    // Device Length
            command.AddRange(Encoding.ASCII.GetBytes(analysisResult.Content));
            command.AddRange(SoftBasic.BytesToAsciiBytes(value));

            int sum = 0;

            for (int i = 1; i < command.Count; i++)
            {
                sum += command[i];
            }
            command.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)sum));

            command.Add(0x04);    // EOT



            return(OperateResult.CreateSuccessResult(command.ToArray()));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// write data to address  Type of ReadByte
        /// </summary>
        /// <param name="station">plc station</param>
        /// <param name="address">address, for example: M100, D100, DW100</param>
        /// <param name="value">source value</param>
        /// <returns>command bytes</returns>
        private static OperateResult <byte[]> BuildWriteByteCommand(byte station, string address, byte[] value)
        {
            var analysisResult = XGBFastEnet.AnalysisAddress(address, false);

            if (!analysisResult.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(analysisResult));
            }
            var DataTypeResult = XGBFastEnet.GetDataTypeToAddress(address);

            if (!DataTypeResult.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(DataTypeResult));
            }

            List <byte> command = new List <byte>();

            command.Add(0x05);    // ENQ
            command.AddRange(SoftBasic.BuildAsciiBytesFrom(station));
            command.Add(0x77);    // command w
            command.Add(0x53);    // command type: S
            switch (DataTypeResult.Content)
            {
            case "Bit":
            case "Byte":
                command.Add(0x53);        // command type: SS
                command.Add(0x30);        // Number of blocks
                command.Add(0x31);
                command.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)analysisResult.Content.Length));
                command.AddRange(Encoding.ASCII.GetBytes(analysisResult.Content));
                break;

            case "Word":
            case "DWord":
            case "LWord":
            case "Continuous":
                command.Add(0x42);           // command type: SB
                command.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)analysisResult.Content.Length));
                command.AddRange(Encoding.ASCII.GetBytes(analysisResult.Content));
                command.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)value.Length));
                break;

            default: break;
            }
            command.AddRange(SoftBasic.BytesToAsciiBytes(value));
            command.Add(0x04);    // EOT
            int sum = 0;

            for (int i = 0; i < command.Count; i++)
            {
                sum += command[i];
            }
            command.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)sum));

            return(OperateResult.CreateSuccessResult(command.ToArray()));
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 写入自定义的数据到数据内存中去
        /// </summary>
        /// <param name="address">地址</param>
        /// <param name="value">数据值</param>
        /// <returns>是否写入成功的结果对象</returns>
        public override OperateResult Write(string address, byte[] value)
        {
            OperateResult <string> analysis = XGBFastEnet.AnalysisAddress(address, false);

            if (!analysis.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(analysis));
            }

            switch (analysis.Content[0])
            {
            case 'P': inputBuffer.SetBytes(value, int.Parse(analysis.Content) / 8); return(OperateResult.CreateSuccessResult( ));

            case 'Q': outputBuffer.SetBytes(value, int.Parse(analysis.Content) / 8); return(OperateResult.CreateSuccessResult( ));

            case 'M': memeryBuffer.SetBytes(value, int.Parse(analysis.Content) / 8); return(OperateResult.CreateSuccessResult( ));

            case 'D': dbBlockBuffer.SetBytes(value, int.Parse(analysis.Content) / 8); return(OperateResult.CreateSuccessResult( ));

            default: return(new OperateResult <byte[]>(StringResources.Language.NotSupportedDataType));
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 读取自定义的寄存器的值
        /// </summary>
        /// <param name="address">起始地址,示例:"I100","M100"</param>
        /// <param name="length">数据长度</param>
        /// <exception cref="IndexOutOfRangeException"></exception>
        /// <returns>byte数组值</returns>
        public override OperateResult <byte[]> Read(string address, ushort length)
        {
            OperateResult <string> analysis = XGBFastEnet.AnalysisAddress(address, true);

            if (!analysis.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(analysis));
            }

            switch (analysis.Content[0])
            {
            case 'P': return(OperateResult.CreateSuccessResult(inputBuffer.GetBytes(int.Parse(analysis.Content) / 8, length)));

            case 'Q': return(OperateResult.CreateSuccessResult(outputBuffer.GetBytes(int.Parse(analysis.Content) / 8, length)));

            case 'M': return(OperateResult.CreateSuccessResult(memeryBuffer.GetBytes(int.Parse(analysis.Content) / 8, length)));

            case 'D': return(OperateResult.CreateSuccessResult(dbBlockBuffer.GetBytes(int.Parse(analysis.Content) / 8, length)));

            default: return(new OperateResult <byte[]>(StringResources.Language.NotSupportedDataType));
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// 往指定的地址里写入bool数据对象
        /// </summary>
        /// <param name="address">西门子的地址信息</param>
        /// <param name="value">值</param>
        /// <returns>是否成功的结果</returns>
        public OperateResult Write(string address, bool value)
        {
            OperateResult <string> analysis = XGBFastEnet.AnalysisAddress(address, false);

            if (!analysis.IsSuccess)
            {
                return(analysis);
            }

            switch (analysis.Content[1])
            {
            case 'P': inputBuffer.SetBool(value, int.Parse(analysis.Content.Remove(0, 3))); return(OperateResult.CreateSuccessResult());

            case 'Q': outputBuffer.SetBool(value, int.Parse(analysis.Content.Remove(0, 3))); return(OperateResult.CreateSuccessResult());

            case 'M': memeryBuffer.SetBool(value, int.Parse(analysis.Content.Remove(0, 3))); return(OperateResult.CreateSuccessResult());

            case 'D': dbBlockBuffer.SetBool(value, int.Parse(analysis.Content.Remove(0, 3))); return(OperateResult.CreateSuccessResult());

            default: return(new OperateResult(StringResources.Language.NotSupportedDataType));
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// 读取指定地址的bool数据对象
        /// </summary>
        /// <param name="address">西门子的地址信息</param>
        /// <returns>带有成功标志的结果对象</returns>
        public OperateResult <bool> ReadBool(string address)
        {
            OperateResult <string> analysis = XGBFastEnet.AnalysisAddress(address, true);

            if (!analysis.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <bool>(analysis));
            }

            switch (analysis.Content[1])
            {
            case 'P': return(OperateResult.CreateSuccessResult(inputBuffer.GetBool(int.Parse(analysis.Content.Remove(0, 3)))));

            case 'Q': return(OperateResult.CreateSuccessResult(outputBuffer.GetBool(int.Parse(analysis.Content.Remove(0, 3)))));

            case 'M': return(OperateResult.CreateSuccessResult(memeryBuffer.GetBool(int.Parse(analysis.Content.Remove(0, 3)))));

            case 'D': return(OperateResult.CreateSuccessResult(dbBlockBuffer.GetBool(int.Parse(analysis.Content.Remove(0, 3)))));

            default: return(new OperateResult <bool>(StringResources.Language.NotSupportedDataType));
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// 读取自定义的寄存器的值
        /// </summary>
        /// <param name="address">起始地址,示例:"I100","M100"</param>
        /// <param name="length">数据长度</param>
        /// <exception cref="IndexOutOfRangeException"></exception>
        /// <returns>byte数组值</returns>
        public override OperateResult <byte[]> Read(string address, ushort length)
        {
            OperateResult <string> analysis = XGBFastEnet.AnalysisAddress(address, true);

            if (!analysis.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(analysis));
            }

            int startIndex = int.Parse(analysis.Content.Substring(3));

            switch (analysis.Content[1])
            {
            case 'P': return(OperateResult.CreateSuccessResult(pBuffer.GetBytes(startIndex, length)));

            case 'Q': return(OperateResult.CreateSuccessResult(qBuffer.GetBytes(startIndex, length)));

            case 'M': return(OperateResult.CreateSuccessResult(mBuffer.GetBytes(startIndex, length)));

            case 'D': return(OperateResult.CreateSuccessResult(dBuffer.GetBytes(startIndex == 0 ? startIndex : startIndex *= 2, length)));

            default: return(new OperateResult <byte[]>(StringResources.Language.NotSupportedDataType));
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// 写入自定义的数据到数据内存中去
        /// </summary>
        /// <param name="address">地址</param>
        /// <param name="value">数据值</param>
        /// <returns>是否写入成功的结果对象</returns>
        public override OperateResult Write(string address, byte[] value)
        {
            OperateResult <string> analysis = XGBFastEnet.AnalysisAddress(address, false);

            if (!analysis.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(analysis));
            }

            int startIndex = int.Parse(analysis.Content.Substring(3));

            switch (analysis.Content[1])
            {
            case 'P': pBuffer.SetBytes(value, startIndex); return(OperateResult.CreateSuccessResult( ));

            case 'Q': qBuffer.SetBytes(value, startIndex); return(OperateResult.CreateSuccessResult( ));

            case 'M': mBuffer.SetBytes(value, startIndex); return(OperateResult.CreateSuccessResult( ));

            case 'D': dBuffer.SetBytes(value, startIndex); return(OperateResult.CreateSuccessResult( ));

            default: return(new OperateResult <byte[]>(StringResources.Language.NotSupportedDataType));
            }
        }
Ejemplo n.º 16
0
        /// <summary>
        /// 写入自定义的数据到数据内存中去
        /// </summary>
        /// <param name="address">地址</param>
        /// <param name="value">数据值</param>
        /// <returns>是否写入成功的结果对象</returns>
        public override OperateResult Write(string address, byte[] value)
        {
            int startIndex = 0;

            string[] fullAddressSplit       = null;
            string   Dmain                  = string.Empty;
            string   AddressSplit           = string.Empty;
            OperateResult <string> analysis = XGBFastEnet.AnalysisAddress(address, false);

            if (!analysis.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(analysis));
            }
            if (address.IndexOf(".") > 0)//BitOnByte and BitOnWord
            {
                Dmain        = address.Substring(0, 1);
                AddressSplit = address.Remove(0, 2);
                switch (Dmain)
                {
                case "I":
                case "Q":
                case "U":
                {
                    fullAddressSplit = AddressSplit.Split(new char[1] {
                            '.'
                        }, 4);
                    startIndex = CheckAddress(fullAddressSplit[3]);
                    break;
                }

                default:    // MB---DB-----
                {
                    fullAddressSplit = AddressSplit.Split(new char[1] {
                            '.'
                        }, 2);
                    startIndex = CheckAddress(fullAddressSplit[0]);
                    break;
                }
                }
            }
            else
            {
                startIndex = CheckAddress(analysis.Content.Substring(3));
            }


            switch (analysis.Content[1])
            {
            case 'I':
            case 'Q':
            case 'U':
            case 'P':
                pBuffer.SetBytes(value, startIndex); return(OperateResult.CreateSuccessResult());

            case 'M': mBuffer.SetBool(value[0] == 1 ? true : false, startIndex); return(OperateResult.CreateSuccessResult());

            case 'D': dBuffer.SetBytes(value, startIndex == 0 ? startIndex : startIndex *= 2); return(OperateResult.CreateSuccessResult());

            default: return(new OperateResult <byte[]>(StringResources.Language.NotSupportedDataType));
            }
        }