private bool ParseReadSend(FinsAddrInfo addrInfo, ref List <byte> sendmsg) { byte[] command = new byte[8]; // 指令 读 command[0] = PLCConvert.HighByte(Convert.ToUInt16(addrInfo.Operator)); command[1] = PLCConvert.LowByte(Convert.ToUInt16(addrInfo.Operator)); // 地址 command[2] = addrInfo.Area; // 区域 command[3] = addrInfo.HighByte; // 地址-整数位 command[4] = addrInfo.LowByte; command[5] = addrInfo.Decimal; // 地址-小数位 // 长度 command[6] = PLCConvert.HighByte(Convert.ToUInt16(addrInfo.FinsCount)); command[7] = PLCConvert.LowByte(Convert.ToUInt16(addrInfo.FinsCount)); var length = -8 + header.Count + 8; sendmsg.AddRange(header); sendmsg.RemoveRange(4, 4); // 数据段长度设置 sendmsg.InsertRange(4, PLCConvert.ArrayByte((UInt32)length)); sendmsg.AddRange(command); // 添加command return(true); }
private bool ParseWriteSend <T>(FinsAddrInfo addrInfo, List <T> values, ref List <byte> sendmsg) { List <byte> command = new List <byte>(8); // 指令 读 command[0] = PLCConvert.HighByte(Convert.ToUInt16(addrInfo.Operator)); command[1] = PLCConvert.LowByte(Convert.ToUInt16(addrInfo.Operator)); // 地址 command[2] = addrInfo.Area; // 区域 command[3] = addrInfo.HighByte; // 地址-整数位 command[4] = addrInfo.LowByte; command[5] = addrInfo.Decimal; // 地址-小数位 // 长度 command[6] = PLCConvert.HighByte(Convert.ToUInt16(addrInfo.Count)); command[7] = PLCConvert.LowByte(Convert.ToUInt16(addrInfo.Count)); // 数据 EValueType valueType = EValueType._BOOL; if (!PLCConvert.AddrValueType(typeof(T).Name, ref valueType)) { return(false); } List <byte> data = new List <byte>(); switch (valueType) { case EValueType._UNKNOW: return(false); case EValueType._BOOL: foreach (var i in values) { data.AddRange(BitConverter.GetBytes((Byte)(Object)i).Reverse()); } break; case EValueType._INT16: foreach (var i in values) { data.AddRange(BitConverter.GetBytes((Int16)(Object)i).Reverse()); } break; case EValueType._INT32: foreach (var i in values) { data.AddRange(BitConverter.GetBytes((Int32)(Object)i).Reverse()); } break; case EValueType._INT64: foreach (var i in values) { data.AddRange(BitConverter.GetBytes((Int64)(Object)i).Reverse()); } break; case EValueType._UINT16: foreach (var i in values) { data.AddRange(BitConverter.GetBytes((UInt16)(Object)i).Reverse()); } break; case EValueType._UINT32: foreach (var i in values) { data.AddRange(BitConverter.GetBytes((UInt32)(Object)i).Reverse()); } break; case EValueType._UINT64: foreach (var i in values) { data.AddRange(BitConverter.GetBytes((UInt64)(Object)i).Reverse()); } break; case EValueType._FLOAT: foreach (var i in values) { data.AddRange(BitConverter.GetBytes((Single)(Object)i).Reverse()); } break; case EValueType._DOUBLE: foreach (var i in values) { data.AddRange(BitConverter.GetBytes((Double)(Object)i).Reverse()); } break; } command.AddRange(data); var length = -8 + header.Count + command.Count + data.Count; var length_read = data.Count; sendmsg.AddRange(header); sendmsg.RemoveRange(4, 4); // 数据段长度设置 sendmsg.InsertRange(4, PLCConvert.ArrayByte((UInt32)length)); sendmsg.AddRange(command); // 添加command sendmsg.AddRange(data); // 添加data return(true); }
// 这个正则不熟,要测试 private bool ParseAddrInfo(string addr, UInt16 count, string valueType, EFinsOperate opt, ref FinsAddrInfo addrInfo) { addrInfo.Address = addr; addrInfo.Operator = opt; addrInfo.Count = count; if (!PLCConvert.AddrValueType(valueType, ref addrInfo.ValueType)) { return(false); } var matchArea = Regex.Matches(addr, @"[A-Za-z]+"); if (matchArea.Count != 1) { return(false); } var sarea = matchArea[0].Value; var matchNum = Regex.Matches(addr, @"([1-9]\d*\.?\d*)|(0\.\d*[1-9])"); if (matchNum.Count != 1) { return(false); } var snum = matchNum[0].Value; if (!PLCConvert.FinsAddrArea(sarea, ref addrInfo.Area)) { return(false); } if (!PLCConvert.FinsAddrNum(snum, ref addrInfo.HighByte, ref addrInfo.LowByte, ref addrInfo.Decimal)) { return(false); } switch (addrInfo.ValueType) { case EValueType._UNKNOW: return(false); case EValueType._BOOL: addrInfo.FinsCount = (UInt16)((addrInfo.Decimal + addrInfo.Count) / 16 + 1); break; case EValueType._INT16: addrInfo.FinsCount = addrInfo.Count; break; case EValueType._INT32: addrInfo.FinsCount = (UInt16)(addrInfo.Count * 2); break; case EValueType._INT64: addrInfo.FinsCount = (UInt16)(addrInfo.Count * 4); break; default: break; } var readCount = (addrInfo.Decimal + addrInfo.Count) / 16; return(true); }