private static string getROWORequestsPropertiesString(ReadWriteRequest readWriteRequest) { string result = String.Empty; // The string results format is: // RequestType:OperandType-StartAddress-NumberOfOperands. where // ':' - is the delimitator for the requestType // '-' - is the delimitator between request properties. // '.' - used to mark the end of the request properties. // Example: RO:MI-1-10. means Read 10 MI from address 1. if (readWriteRequest is ReadOperands) { ReadOperands ro = readWriteRequest as ReadOperands; result += "RO:" + ro.OperandType.ToString() + "-" + ro.StartAddress.ToString() + "-" + ro.NumberOfOperands.ToString() + "."; } else { WriteOperands wo = readWriteRequest as WriteOperands; result += "WO" + wo.OperandType.ToString() + "-" + wo.StartAddress.ToString() + "-" + wo.NumberOfOperands.ToString() + "."; } return(result); }
private bool receiveMessageSizeFitsThePLCBuffer(ReadWriteRequest operand) { ReadOperands readOperands = operand as ReadOperands; int operandSize = Utils.GetOperandSizeByCommandCode(readOperands.OperandType.ToString()); int receiveDataSize = readOperands.NumberOfOperands * operandSize; int receiveMessageSize = Utils.Lengths.LENGTH_ASCII_RECEIVE_MESSAGE + receiveDataSize; return(receiveMessageSize <= PLCVersion.PlcBuffer && readOperands.NumberOfOperands <= 255); }
private void ReadWriteReadOperand(ReadWriteRequest operand, string parentID, bool suppressEthernetHeader) { ReadOperands readOperands = operand as ReadOperands; GuidClass guid = new GuidClass(); pcomA.BuildAsciiCommand(UnitId, Utils.ASCIIOperandTypes[Enum.GetName(typeof(OperandTypes), readOperands.OperandType)] .CommandCodeForRead, (operand as ReadOperands).StartAddress, readOperands.NumberOfOperands, null, readOperands.TimerValueFormat); string readMessage = pcomA.MessageToPLC as string; if (this.BreakFlag) { throw new ComDriveExceptions("Request aborted by user", ComDriveExceptions.ComDriveException.AbortedByUser); } lock (guid) { Channel.Send(readMessage, ReceiveString, guid, parentID, "ASCII Protocol - Read Operands", PlcGuid, suppressEthernetHeader); Monitor.Wait(guid); } if (this.BreakFlag) { throw new ComDriveExceptions("Request aborted by user", ComDriveExceptions.ComDriveException.AbortedByUser); } PlcResponseMessage plcResponseMessage; { plcResponseMessage = m_responseMessageQueue[guid]; m_responseMessageQueue.Remove(guid); } if (plcResponseMessage.comException == CommunicationException.Timeout) { throw new ComDriveExceptions("Cannot communicate with the PLC with the specified UnitID!", ComDriveExceptions.ComDriveException.CommunicationTimeout); } pcomA.DisAssembleAsciiResult(plcResponseMessage.response, UnitId, Utils.ASCIIOperandTypes[Enum.GetName(typeof(OperandTypes), readOperands.OperandType)] .CommandCodeForRead, (operand as ReadOperands).StartAddress, readOperands.NumberOfOperands, null, readOperands.TimerValueFormat); if (pcomA.MessageFromPLC != null) { operand.ResponseValues = (pcomA.MessageFromPLC as AbstractASCIIMessage).ArrayValues; //Values; } }
private void _receiveSizeExceedPLCBUffer(ReadWriteRequest readWriteRequest) { ReadOperands ro = readWriteRequest as ReadOperands; ushort availableReceiveSize = (ushort)(plcBuffer - bytesNoToReceive - (Utils.Lengths.LENGTH_WR_DETAILS)); ushort availableNoOfOperands = (ushort)(availableReceiveSize / operandSize); if (availableNoOfOperands > 255) { availableReceiveSize -= (ushort)((availableNoOfOperands / 255) * (Utils.Lengths.LENGTH_WR_DETAILS + 1)); availableReceiveSize -= 1; availableReceiveSize -= (ushort)((availableNoOfOperands % 255) % 2); availableNoOfOperands = (ushort)(availableReceiveSize / operandSize); } ushort noOfOperands = 0; ushort startAddress = ro.StartAddress; int requestNo = 0; ReadOperands remainingRo = new ReadOperands { NumberOfOperands = (ushort)(ro.NumberOfOperands - availableNoOfOperands), OperandType = ro.OperandType, StartAddress = (ushort)(ro.StartAddress + availableNoOfOperands), TimerValueFormat = ro.TimerValueFormat }; while (availableNoOfOperands > 0) { if (availableNoOfOperands > 255) { noOfOperands = 255; availableNoOfOperands -= noOfOperands; } else { noOfOperands = availableNoOfOperands; availableNoOfOperands = 0; } requestNo++; ReadOperands tmpRO = new ReadOperands(noOfOperands, ro.OperandType, startAddress, ro.TimerValueFormat); requestsList.Add(tmpRO); startAddress += noOfOperands; _updateSplitDetailsList(); } ReadWriteRequest[] tmpReq = requestsList.ToArray(); allRequestsList.Add(tmpReq.ToList()); _resetRequestsList(); AddNewRequest(remainingRo, reqPosition, true); }
public ReadWriteRequest[] init() { // ReadWriteRequest r1 = new ReadOperands { NumberOfOperands = 8, OperandType = OperandTypes.MI, StartAddress = 0, }; return(new ReadWriteRequest[] { r1 }); }
private void _addRequestsWhenNoOfOperandsExceed255(ReadWriteRequest readWriteRequest) { ushort startAddress; ushort noOfOperands; ushort remainingNoOfOperands; if (readWriteRequest is ReadOperands) { ReadOperands ro = readWriteRequest as ReadOperands; remainingNoOfOperands = ro.NumberOfOperands; startAddress = ro.StartAddress; noOfOperands = 255; while (remainingNoOfOperands > 0) { requestsList.Add(new ReadOperands(noOfOperands, ro.OperandType, startAddress, ro.TimerValueFormat)); startAddress += noOfOperands; remainingNoOfOperands -= noOfOperands; if (remainingNoOfOperands <= 255) { noOfOperands = remainingNoOfOperands; } _updateSplitDetailsList(); } } else { WriteOperands wo = readWriteRequest as WriteOperands; remainingNoOfOperands = wo.NumberOfOperands; startAddress = wo.StartAddress; noOfOperands = 255; while (remainingNoOfOperands > 0) { object[] values = new object[noOfOperands]; Array.Copy(wo.Values, startAddress - wo.StartAddress, values, 0, noOfOperands); requestsList.Add(new WriteOperands(noOfOperands, wo.OperandType, startAddress, values, wo.TimerValueFormat)); startAddress += noOfOperands; remainingNoOfOperands -= noOfOperands; if (remainingNoOfOperands <= 255) { noOfOperands = remainingNoOfOperands; } _updateSplitDetailsList(); } } }
private List <byte> GetDataRequestBytes(ReadOperands readOperandsExecuter) { bool isVectorial = true; List <byte> results = new List <byte>(); results.AddRange(BitConverter.GetBytes(readOperandsExecuter.NumberOfOperands)); OperandType operandType = Enum.GetName(typeof(OperandTypes), readOperandsExecuter.OperandType) .GetOperandTypeByName(); results.Add(isVectorial ? operandType.VectorialValue : operandType.ByteValue); results.Add(255); results.AddRange(BitConverter.GetBytes(readOperandsExecuter.StartAddress)); return(results); }
private List <byte> GetDataRequestsBytesForReadOperands(ReadOperands readOperandsExecuter) { List <byte> results = new List <byte>(); results.AddRange( BitConverter.GetBytes((readOperandsExecuter.NumberOfOperands > 0) ? (UInt16)1 : (UInt16)0)); results.AddRange(BitConverter.GetBytes((UInt16)0)); if (readOperandsExecuter.NumberOfOperands > 0) { results.AddRange(UpdateReadDataRequest( readOperandsExecuter.OperandType.ToString().GetOperandIdByNameForFullBinary(), readOperandsExecuter.NumberOfOperands, readOperandsExecuter.StartAddress)); } return(results); }
private int GetReadOperandReceiveSize(ReadWriteRequest readWriteRequest) { ReadOperands readOperands = readWriteRequest as ReadOperands; int operandSize = readOperands.OperandType.GetOperandSizeByOperandTypeForFullBinarry(); int result = 0; if (operandSize == 1) { result = (readOperands.NumberOfOperands / 8); if (readOperands.NumberOfOperands % 8 != 0) { result++; } } else { result = operandSize * readOperands.NumberOfOperands; } return(result); }
private List <ReadOperands> GetSplitedReadOperands(ReadWriteRequest operand) { ReadOperands readOpearnd = operand as ReadOperands; List <ReadOperands> results = new List <ReadOperands>(); int operandSize = Utils.GetOperandSizeByCommandCode(readOpearnd.OperandType.ToString()); ushort startAddress = readOpearnd.StartAddress; ushort remainingNoOfOperands = readOpearnd.NumberOfOperands; int splitMaxDataSize = PLCVersion.PlcBuffer - Utils.Lengths.LENGTH_ASCII_RECEIVE_MESSAGE; ushort splitMaxNoOfOperands = (ushort)(splitMaxDataSize / operandSize); if (splitMaxNoOfOperands > 255) { splitMaxNoOfOperands = 255; } bool bDone = false; while (!bDone) { if (remainingNoOfOperands <= splitMaxNoOfOperands) { results.Add(new ReadOperands(remainingNoOfOperands, readOpearnd.OperandType, startAddress, readOpearnd.TimerValueFormat)); bDone = true; } else { results.Add(new ReadOperands(splitMaxNoOfOperands, readOpearnd.OperandType, startAddress, readOpearnd.TimerValueFormat)); startAddress += splitMaxNoOfOperands; remainingNoOfOperands -= splitMaxNoOfOperands; } } return(results); }
private void _getRequestBytesSize(ReadWriteRequest readWriteRequest) { ushort noOfOperands; int noOf255Splits, noOfSplits; if (readWriteRequest is WriteOperands) { WriteOperands wo = readWriteRequest as WriteOperands; noOfOperands = wo.NumberOfOperands; operandSize = wo.OperandType.GetOperandSizeByOperandTypeForFullBinarry(); noOf255Splits = noOfOperands / 255; noOfSplits = noOfOperands % 255; if (noOfOperands > 255) { requestBytesNoToSend = (ushort)(noOf255Splits * Utils.Lengths.LENGTH_WR_DETAILS + noOf255Splits * 255 * operandSize); requestBytesNoToSend += (ushort)((operandSize % 2) * (noOf255Splits + noOfSplits % 2)); if (noOfSplits > 0) { requestBytesNoToSend += (ushort)(Utils.Lengths.LENGTH_WR_DETAILS + noOfSplits * operandSize); } } else { requestBytesNoToSend = (ushort)(operandSize * noOfOperands + Utils.Lengths.LENGTH_WR_DETAILS); requestBytesNoToSend += (ushort)(requestBytesNoToSend % 2); } requestBytesNoToReceive = 0; } else { ReadOperands ro = readWriteRequest as ReadOperands; noOfOperands = ro.NumberOfOperands; operandSize = ro.OperandType.GetOperandSizeByOperandTypeForFullBinarry(); if (noOfOperands > 255) { noOf255Splits = (noOfOperands / 255); noOfSplits = (noOfOperands % 255); requestBytesNoToSend = (ushort)(noOf255Splits * Utils.Lengths.LENGTH_WR_DETAILS); requestBytesNoToReceive = (ushort)(noOf255Splits * Utils.Lengths.LENGTH_WR_DETAILS + noOf255Splits * 255 * operandSize); requestBytesNoToReceive += (ushort)((operandSize % 2) * (noOf255Splits + noOfSplits % 2)); if (noOfSplits != 0) { requestBytesNoToSend += Utils.Lengths.LENGTH_WR_DETAILS; requestBytesNoToReceive += (ushort)(Utils.Lengths.LENGTH_WR_DETAILS + noOfSplits * operandSize); } } else { requestBytesNoToReceive = (ushort)(operandSize * noOfOperands + Utils.Lengths.LENGTH_WR_DETAILS); requestBytesNoToReceive += (ushort)(requestBytesNoToReceive % 2); requestBytesNoToSend = Utils.Lengths.LENGTH_WR_DETAILS; } } }
//Staticly type for needed Application public ReadWriteRequest[] init() { //Unusedwl ser ReadWriteRequest r1 = new ReadOperands { NumberOfOperands = 1, OperandType = OperandTypes.MI, StartAddress = 1501, }; ReadWriteRequest r2 = new ReadOperands { NumberOfOperands = 1, OperandType = OperandTypes.MI, StartAddress = 56, }; //Hydr Vorlauf+Rück % Kom Vor+Rück --> Spliten ReadWriteRequest r3 = new ReadOperands { NumberOfOperands = 4, OperandType = OperandTypes.MI, StartAddress = 1631, }; //Hydr. --> Spliten ReadWriteRequest r4 = new ReadOperands { NumberOfOperands = 2, OperandType = OperandTypes.MI, StartAddress = 29, }; //Aussentemp ReadWriteRequest r5 = new ReadOperands { NumberOfOperands = 1, OperandType = OperandTypes.MI, StartAddress = 1523, }; //Fehler ReadWriteRequest r6 = new ReadOperands { NumberOfOperands = 1, OperandType = OperandTypes.MB, StartAddress = 2300, }; //Daten Kühlung aktiv ReadWriteRequest r7 = new ReadOperands { NumberOfOperands = 1, OperandType = OperandTypes.SB, StartAddress = 3, }; //Luft MO ReadWriteRequest r8 = new ReadOperands { NumberOfOperands = 1, OperandType = OperandTypes.MB, StartAddress = 1807, }; //Luft SP1 ReadWriteRequest r9 = new ReadOperands { NumberOfOperands = 1, OperandType = OperandTypes.MB, StartAddress = 1805, }; //Luft SC ReadWriteRequest r10 = new ReadOperands { NumberOfOperands = 1, OperandType = OperandTypes.MB, StartAddress = 1801, }; //Luft Versorgung ReadWriteRequest r11 = new ReadOperands { NumberOfOperands = 1, OperandType = OperandTypes.MB, StartAddress = 1811, }; //Luft SP2 ReadWriteRequest r12 = new ReadOperands { NumberOfOperands = 1, OperandType = OperandTypes.MB, StartAddress = 1803, }; //Luft FB ReadWriteRequest r13 = new ReadOperands { NumberOfOperands = 1, OperandType = OperandTypes.MB, StartAddress = 1809, }; //Temp Klebeplatz ReadWriteRequest r14 = new ReadOperands { NumberOfOperands = 1, OperandType = OperandTypes.MI, StartAddress = 1561, }; return(new ReadWriteRequest[] { r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14 }); }
internal void CheckReadWriteRequests(ReadWriteRequest[] values) { foreach (ReadWriteRequest rw in values) { if (rw is ReadOperands) { ReadOperands ro = rw as ReadOperands; if (ro == null) { throw new ComDriveExceptions("Read Operand Request cannot be Null", ComDriveExceptions.ComDriveException.UserInputException); } if (ro.NumberOfOperands <= 0) { throw new ComDriveExceptions("The number of Operands to read cannot be less than 1", ComDriveExceptions.ComDriveException.UserInputException); } if ((ro.StartAddress < 0) || (ro.StartAddress + ro.NumberOfOperands > m_plcVersion.OperandCount(ro.OperandType))) { throw new ComDriveExceptions( "Operand start address and end must be non-negative and less than the operand count on the PLC", ComDriveExceptions.ComDriveException.OperandAddressOutOfRange); } } else if (rw is WriteOperands) { WriteOperands wo = rw as WriteOperands; if (wo == null) { throw new ComDriveExceptions("Write Operand Request cannot be Null", ComDriveExceptions.ComDriveException.UserInputException); } if (wo.NumberOfOperands <= 0) { throw new ComDriveExceptions("The number of Operands to write cannot be less than 1", ComDriveExceptions.ComDriveException.UserInputException); } if (wo.Values == null) { throw new ComDriveExceptions("Values cannot be Null in a Write Operands request", ComDriveExceptions.ComDriveException.UserInputException); } if (wo.NumberOfOperands > wo.Values.Length) { throw new ComDriveExceptions( "The number of Operands to write is larger than the number of values that were entered", ComDriveExceptions.ComDriveException.UserInputException); } if ((wo.StartAddress < 0) || (wo.StartAddress + wo.NumberOfOperands > m_plcVersion.OperandCount(wo.OperandType))) { throw new ComDriveExceptions( "Operand start address and end must be non-negative and less than the operand count on the PLC", ComDriveExceptions.ComDriveException.OperandAddressOutOfRange); } } else if (rw is ReadDataTables) { ReadDataTables rdt = rw as ReadDataTables; if (rdt == null) { throw new ComDriveExceptions("Read DataTables Request cannot be Null", ComDriveExceptions.ComDriveException.UserInputException); } if ((rdt.NumberOfBytesToReadInRow <= 0) && (rdt.NumberOfRowsToRead <= 0) && (rdt.RowSizeInBytes <= 0) && (rdt.StartAddress < 0)) { throw new ComDriveExceptions("Invalid request parameters in Read DataTables Request", ComDriveExceptions.ComDriveException.UserInputException); } } else if (rw is WriteDataTables) { WriteDataTables wdt = rw as WriteDataTables; if (wdt == null) { throw new ComDriveExceptions("Write DataTables Request cannot be Null", ComDriveExceptions.ComDriveException.UserInputException); } if ((wdt.NumberOfBytesToWriteInRow <= 0) && (wdt.NumberOfRowsToWrite <= 0) && (wdt.RowSizeInBytes <= 0) && (wdt.StartAddress < 0)) { throw new ComDriveExceptions("Invalid request parameters in Read DataTables Request", ComDriveExceptions.ComDriveException.UserInputException); } } else if (rw == null) { throw new ComDriveExceptions("One or more ReadWriteRequest object are Null", ComDriveExceptions.ComDriveException.UserInputException); } else if (rw is BinaryRequest) { // Nothing specific to check } else { throw new ComDriveExceptions("Unsupported request", ComDriveExceptions.ComDriveException.UnsupportedCommand); } } }
private void SplitAndReadReadOperands(ref List <ReadWriteRequest> readWriteRequest, string parentID) { if (this.BreakFlag) { throw new ComDriveExceptions("Request aborted by user", ComDriveExceptions.ComDriveException.AbortedByUser); } if (readWriteRequest.Count == 1) //only 1 request that exceed the plcBuffer { ReadOperands readOperands = readWriteRequest[0] as ReadOperands; List <ReadWriteRequest> splitRequests = new List <ReadWriteRequest>(); List <object> responseValues = new List <object>(); int operandSize = readOperands.OperandType.GetOperandSizeByOperandTypeForFullBinarry(); ushort maxNoOfOperands = (ushort)((PLCVersion.PlcBuffer - Utils.Lengths.LENGTH_HEADER_AND_FOOTER) / operandSize); ushort startAddress = (readWriteRequest[0] as ReadOperands).StartAddress; ushort remainingNoOfOperands = (ushort)(readOperands.NumberOfOperands); ushort remainingStartAddress = startAddress; while (remainingNoOfOperands > maxNoOfOperands) { if (this.BreakFlag) { throw new ComDriveExceptions("Request aborted by user", ComDriveExceptions.ComDriveException.AbortedByUser); } splitRequests.Add(new ReadOperands(maxNoOfOperands, readOperands.OperandType, remainingStartAddress, readOperands.TimerValueFormat)); ReadOperations(ref splitRequests, parentID); if (this.BreakFlag) { throw new ComDriveExceptions("Request aborted by user", ComDriveExceptions.ComDriveException.AbortedByUser); } responseValues.AddRange((splitRequests[0].ResponseValues) as object[]); splitRequests.Clear(); remainingNoOfOperands -= maxNoOfOperands; remainingStartAddress += maxNoOfOperands; } if (remainingNoOfOperands > 0) { if (this.BreakFlag) { throw new ComDriveExceptions("Request aborted by user", ComDriveExceptions.ComDriveException.AbortedByUser); } splitRequests.Add(new ReadOperands(remainingNoOfOperands, readOperands.OperandType, remainingStartAddress, readOperands.TimerValueFormat)); ReadOperations(ref splitRequests, parentID); responseValues.AddRange((splitRequests[0].ResponseValues) as object[]); } readWriteRequest[0].ResponseValues = responseValues.ToArray(); } else { //this is called only at the end of userRequests and when //joinRequests contains more than 1 requests if (this.BreakFlag) { throw new ComDriveExceptions("Request aborted by user", ComDriveExceptions.ComDriveException.AbortedByUser); } ReadOperations(ref readWriteRequest, parentID); } }