public static HashSet <ReadOperandsRequest> CreateMultiple(UnitronicsPLC plc, Dictionary <OperandType, HashSet <ushort> > operandAddresses) { HashSet <ReadOperandsRequest> requests = new HashSet <ReadOperandsRequest>(); ushort maximumBufferSize = (ushort)(plc.BufferSize - Response.HeaderLength - Response.FooterLength); ReadOperandsRequest request = null; foreach (OperandType operand in operandAddresses.Keys.OrderBy(type => (ushort)type)) { foreach (ushort address in operandAddresses[operand].OrderBy(address => address)) { if (request == null) { request = new ReadOperandsRequest(plc.UnitID); } else if (request.newOperandAddressFitsBuffer(operand, address, maximumBufferSize) == false) { requests.Add(request); request = new ReadOperandsRequest(plc.UnitID); } request.addOperandAddress(plc, operand, address); } } if (request != null && request.OperandRequests.Count > 0 && request.OperandRequests.Values.Any(request => request.Count > 0)) { requests.Add(request); } return(requests); }
public static ReadOperandsRequest CreateNew(UnitronicsPLC plc, OperandType type, ushort startAddress, byte length) { if (length > MaximumOperandsLength) { throw new ArgumentOutOfRangeException(nameof(length), "The Number of Operands to Read cannot be greater than the Maximum Value '" + MaximumOperandsLength + "'"); } return(new ReadOperandsRequest(plc.UnitID, Commands.ReadOperands[type], type, startAddress, length)); }
private void addOperandAddress(UnitronicsPLC plc, OperandType type, ushort address) { ushort maximumAddress = plc.GetMaximumOperandAddress(type) ?? ushort.MaxValue; if (isBitOperandType(type)) { if (_operandRequests.ContainsKey(type) == false) { VectorialOperandRequest newRequest = new VectorialOperandRequest(type, address, 8); if (newRequest.EndAddress > maximumAddress) { newRequest.StartAddress = (ushort)(maximumAddress - 7); } _operandRequests.Add(type, newRequest); return; } if (_operandRequests.TryGetValue(type, out IOperandRequest genericRequest) && genericRequest is VectorialOperandRequest existingRequest) { if (address >= existingRequest.StartAddress && address > existingRequest.EndAddress) { existingRequest.Count = (ushort)(address - existingRequest.StartAddress + 1); } if (existingRequest.Count % 8 != 0) { existingRequest.Count = (ushort)(existingRequest.Count + (8 - (existingRequest.Count % 8))); } if (existingRequest.EndAddress > maximumAddress) { existingRequest.StartAddress -= (ushort)(existingRequest.EndAddress - maximumAddress); } } } else { if (_operandRequests.ContainsKey(type) == false) { _operandRequests.Add(type, new NonVectorialOperandRequest(type)); } if (_operandRequests.TryGetValue(type, out IOperandRequest genericRequest) && genericRequest is NonVectorialOperandRequest request && request.Addresses.Contains(address) == false) { request.Addresses.Add(address); } } }
public static HashSet <ReadOperandsRequest> CreateMultiple(UnitronicsPLC plc, Dictionary <OperandType, HashSet <ushort> > operandAddresses) { HashSet <ReadOperandsRequest> requests = new HashSet <ReadOperandsRequest>(); foreach (OperandType operand in operandAddresses.Keys) { byte operandLength = calculateOperandMessageLength(operand); int maximumReceiveLength = plc.BufferSize - Response.STX.Length - Response.UnitIDLength - Response.CommandLength - Response.CRCLength - Response.ETXLength; ReadOperandsRequest request = null; foreach (ushort address in operandAddresses[operand].OrderBy(address => address)) { if (request != null) { int newLength = address - request.StartAddress + 1; if (newLength * operandLength > maximumReceiveLength || newLength > MaximumOperandsLength) { requests.Add(request); request = CreateNew(plc, operand, address, 1); } else { request.Length = (byte)newLength; } } else { request = CreateNew(plc, operand, address, 1); } } if (request != null && request.Length > 0) { requests.Add(request); } } return(requests); }
public static ReadClockRequest CreateNew(UnitronicsPLC plc) { return(new ReadClockRequest(plc.UnitID, Commands.ReadClock)); }
public static GetIdentificationRequest CreateNew(UnitronicsPLC plc) { return(new GetIdentificationRequest(plc.UnitID, Commands.GetIdentification)); }
public static WriteOperandRequest CreateNew(UnitronicsPLC plc, OperandType type, ushort address, object value) { return(new WriteOperandRequest(plc.UnitID, type, address, value)); }
public static WriteClockRequest CreateNew(UnitronicsPLC plc, DateTime newDateTime) { return(new WriteClockRequest(plc.UnitID, Commands.GetIdentification, newDateTime)); }