public override void ReadValue(Command command, Action <int, PointValueType, object> onValueReceive) { string[] deviceAddressInfo = command.DeviceAddress.Split('/'); var deviceIP = deviceAddressInfo[0]; var port = int.Parse(deviceAddressInfo[1]); PointDescription[] points = new PointDescription[command.Points.Length]; for (int i = 0; i < points.Length; i++) { points[i] = new PointDescription(command.Points[i], i); } //找出相邻的点 List <PointGroup> groups = new List <PointGroup>(); points = (from m in points orderby m.Function, m.Address select m).ToArray(); PointGroup currentGroup = new PointGroup(); groups.Add(currentGroup); foreach (var point in points) { if (currentGroup.Points.Count == 0) { currentGroup.Function = point.Function; currentGroup.StartAddress = point.Address; currentGroup.Points.Add(point); } else { if (point.Address - currentGroup.Points.Last().Address == 1 && point.Function == currentGroup.Function) { currentGroup.Points.Add(point); } else { currentGroup = new PointGroup(); groups.Add(currentGroup); currentGroup.Function = point.Function; currentGroup.StartAddress = point.Address; currentGroup.Points.Add(point); } } } Way.Lib.NetStream client = new Way.Lib.NetStream(deviceIP, port); readValues(client, groups); //把值通过stream发送出去 for (int i = 0; i < points.Length; i++) { onValueReceive(points[i].Index, PointValueType.Short, points[i].Value); } client.Dispose(); }
public override bool[] WriteValue(Command command) { string[] deviceAddressInfo = command.DeviceAddress.Split('/'); var deviceIP = deviceAddressInfo[0]; var port = int.Parse(deviceAddressInfo[1]); PointDescription[] points = new PointDescription[command.Points.Length]; for (int i = 0; i < points.Length; i++) { points[i] = new PointDescription(command.Points[i], i); points[i].Value = Convert.ToInt16(Convert.ToDouble(command.Values[i])); if (points[i].Function == FunctionCode.ReadCoilStatus) { points[i].Function = FunctionCode.WriteCoilStatus; } if (points[i].Function == FunctionCode.ReadHoldingRegister) { points[i].Function = FunctionCode.WriteHoldingRegister; } } Way.Lib.NetStream client = new Way.Lib.NetStream(deviceIP, port); bool[] result = new bool[points.Length]; foreach (var point in points) { try { WriteValuePackage package = new WriteValuePackage(point.Function); package.Address = point.Address; package.Value = point.Value; var cmd = package.BuildCommand(); client.Write(cmd); result[point.Index] = package.ParseAnswer((len) => { return(client.ReceiveDatas(len)); }); } catch { client = new Way.Lib.NetStream(deviceIP, port); } } client.Dispose(); return(result); }