/// <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)); } }
/// <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)); } }
/// <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())); }
/// <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 analysisDataTypeResult = XGBFastEnet.AnalysisAddressDataType(address); if (!analysisDataTypeResult.IsSuccess) { return(OperateResult.CreateFailedResult <byte[]>(analysisDataTypeResult)); } var lSDataType = (XGBFastEnet.LsDataType)Enum.Parse(typeof(XGBFastEnet.LsDataType), analysisDataTypeResult.Content); 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 (lSDataType) { case XGBFastEnet.LsDataType.Bit: case XGBFastEnet.LsDataType.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 XGBFastEnet.LsDataType.Word: case XGBFastEnet.LsDataType.DWord: case XGBFastEnet.LsDataType.LWord: case XGBFastEnet.LsDataType.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())); }
/// <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 = CheckAddress(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.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)); } }
/// <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 = CheckAddress(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(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)); } }