/// <summary> /// Converts from float to DWord (DBD) /// </summary> /// <param name="input"></param> /// <returns></returns> public static UInt32 ConvertToUInt(this float input) { uint output; output = DWord.FromByteArray(Single.ToByteArray(input)); return(output); }
/// <summary> /// Given a S7 variable type (Bool, Word, DWord, etc.), it converts the bytes in the appropriate C# format. /// </summary> /// <param name="varType"></param> /// <param name="bytes"></param> /// <param name="varCount"></param> /// <param name="bitAdr"></param> /// <returns></returns> private object ParseBytes(VarType varType, byte[] bytes, int varCount, byte bitAdr = 0) { if (bytes == null || bytes.Length == 0) { return(null); } switch (varType) { case VarType.Byte: if (varCount == 1) { return(bytes[0]); } else { return(bytes); } case VarType.Word: if (varCount == 1) { return(Word.FromByteArray(bytes)); } else { return(Word.ToArray(bytes)); } case VarType.Int: if (varCount == 1) { return(Int.FromByteArray(bytes)); } else { return(Int.ToArray(bytes)); } case VarType.DWord: if (varCount == 1) { return(DWord.FromByteArray(bytes)); } else { return(DWord.ToArray(bytes)); } case VarType.DInt: if (varCount == 1) { return(DInt.FromByteArray(bytes)); } else { return(DInt.ToArray(bytes)); } case VarType.Real: if (varCount == 1) { return(Types.Single.FromByteArray(bytes)); } else { return(Types.Single.ToArray(bytes)); } case VarType.String: return(Types.String.FromByteArray(bytes)); case VarType.StringEx: return(StringEx.FromByteArray(bytes)); case VarType.Timer: if (varCount == 1) { return(Timer.FromByteArray(bytes)); } else { return(Timer.ToArray(bytes)); } case VarType.Counter: if (varCount == 1) { return(Counter.FromByteArray(bytes)); } else { return(Counter.ToArray(bytes)); } case VarType.Bit: if (varCount == 1) { if (bitAdr > 7) { return(null); } else { return(Bit.FromByte(bytes[0], bitAdr)); } } else { return(Bit.ToBitArray(bytes)); } default: return(null); } }
public bool WriteItems(Tag tag, object value) { lock (this) { try { if (!IsConnected()) { return(false); } if (tag.TagType == "string") //在创建TagType时已经转为小写 { var pLcAddress = DbAddress[tag.Address]; var strValue = value.ToString(); var strlen = (byte)strValue.Length; var valueByte = new byte[strlen + 1]; var strbyte = Encoding.ASCII.GetBytes(strValue); if (strbyte.Length != strlen) { throw new InvalidOperationException("写入" + tag.Address + "出错,解析的字符串长度与GetBytes数组长度不一致!"); } if (strlen > pLcAddress.BitOffset) { throw new InvalidOperationException("写入" + tag.Address + "出错,输入的字符串的长度过长!"); } valueByte[0] = strlen; strbyte.CopyTo(valueByte, 1); _plc.Write(DataType.DataBlock, pLcAddress.DBBlockID, pLcAddress.Offset + 1, valueByte); /*if (result != ErrorCode.NoError) * throw new InvalidOperationException(result + "\n" + "Tag: " + tag.Address);*/ return(true); } //2020/6/11 ByteArray if (tag.TagType == "bytearray") //在创建TagType时已经转为小写 { var pLcAddress = DbAddress[tag.Address]; var strValue = value.ToString(); var strlen = strValue.Length; var valueByte = new byte[strlen]; var strbyte = Encoding.ASCII.GetBytes(strValue); if (strbyte.Length != strlen) { throw new InvalidOperationException("写入" + tag.Address + "出错,解析的字符串长度与GetBytes数组长度不一致!"); } if (strlen != pLcAddress.BitOffset) { throw new InvalidOperationException("写入" + tag.Address + "出错,输入的字符串长度与定义长度不匹配!"); } strbyte.CopyTo(valueByte, 0); _plc.WriteBytes(DataType.DataBlock, pLcAddress.DBBlockID, pLcAddress.Offset, valueByte); /*if (result != ErrorCode.NoError) * throw new InvalidOperationException(result + "\n" + "Tag: " + tag.Address);*/ return(true); } else { switch (value) { case double d: { var bytes = Double.ToByteArray(d); value = DWord.FromByteArray(bytes); break; } case bool b: value = b ? 1 : 0; break; } _plc.Write(tag.Address, value); /*if (result != ErrorCode.NoError) * throw new InvalidOperationException(result + "\n" + "Tag: " + tag.Address);*/ return(true); } } catch (Exception ex) { //LOG.Error(string.Format("S7 Driver写入地址{0},值{1},出错{2}", tag.Address, value,ex)); Log.Error($"S7 Driver writes address:{tag.Address}, value:{value}, Message:{ex}"); return(false); } } }