private IMessage ReceiveMessage(PComB pComB, List <List <byte> > readDataTablesDataRequestBytes, string parentID)
        {
            if (this.BreakFlag)
            {
                throw new ComDriveExceptions("Request aborted by user",
                                             ComDriveExceptions.ComDriveException.AbortedByUser);
            }

            GuidClass guid = new GuidClass();

            lock (guid)
            {
                Channel.Send(pComB.MessageToPLC as byte[], ReceiveBytes, guid, parentID,
                             "Binary Protocol - Read/Write Data Tables", PlcGuid);
                Monitor.Wait(guid);
            }

            if (this.BreakFlag)
            {
                throw new ComDriveExceptions("Request aborted by user",
                                             ComDriveExceptions.ComDriveException.AbortedByUser);
            }

            PlcResponseMessage plcResponseMessage;

            lock (_lockObj)
            {
                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);
            }

            pComB.DisAssembleBinaryResult(plcResponseMessage.responseBytesMessage, null);

            return(pComB.MessageFromPLC);
        }
        private void ReadWriteOperandsCommand(ref ReadWriteRequest[] values, string parentID, byte messageEnumerator)
        {
            PComB pComB = new PComB();
            List <List <byte> > dataRequestsBytes = new List <List <byte> >();

            #region Group Requests by type.

            List <ReadOperands>  operandReads  = new List <ReadOperands>();
            List <WriteOperands> operandWrites = new List <WriteOperands>();

            for (int i = 0; i < values.Length; i++)
            {
                if (values[i] is ReadOperands)
                {
                    operandReads.Add(values[i] as ReadOperands);
                }
                if (values[i] is WriteOperands)
                {
                    operandWrites.Add(values[i] as WriteOperands);
                }
            }

            #endregion

            #region Detail Area Header

            int totalNumberOfReads  = 0;
            int totalNumberOfWrites = 0;

            foreach (ReadWriteRequest operand in values)
            {
                if (operand is ReadOperands)
                {
                    totalNumberOfReads += (operand as ReadOperands).NumberOfOperands;
                    dataRequestsBytes.Add(GetDataRequestsBytesForReadOperands(operand as ReadOperands));
                }

                if (operand is WriteOperands)
                {
                    totalNumberOfWrites += (operand as WriteOperands).NumberOfOperands;
                    dataRequestsBytes.Add(GetDataRequestsBytesForWriteOperands(operand as WriteOperands));
                }
            }

            byte[] command_detailes = new byte[4];

            byte[] NoOfReads  = BitConverter.GetBytes(totalNumberOfReads);
            byte[] NoOfWrites = BitConverter.GetBytes(totalNumberOfWrites);

            command_detailes[0] = NoOfReads[0];
            command_detailes[1] = NoOfReads[1];

            command_detailes[2] = NoOfWrites[0];
            command_detailes[3] = NoOfWrites[1];

            #endregion

            if (operandReads.Count > 0 || operandWrites.Count > 0)
            {
                pComB.BuildBinaryCommand((byte)UnitId, BinaryCommand.ReadWrite, command_detailes,
                                         values.ToList(), dataRequestsBytes);

                if (Channel != null)
                {
                    if (this.BreakFlag)
                    {
                        throw new ComDriveExceptions("Request aborted by user",
                                                     ComDriveExceptions.ComDriveException.AbortedByUser);
                    }

                    GuidClass guid = new GuidClass();

                    lock (guid)
                    {
                        Channel.Send(pComB.MessageToPLC as byte[], ReceiveBytes, guid, parentID,
                                     "Binary Protocol - Read/Write Operands (80)", PlcGuid, messageEnumerator);
                        Monitor.Wait(guid);
                    }

                    if (this.BreakFlag)
                    {
                        throw new ComDriveExceptions("Request aborted by user",
                                                     ComDriveExceptions.ComDriveException.AbortedByUser);
                    }

                    PlcResponseMessage plcResponseMessage;

                    lock (_lockObj)
                    {
                        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);
                    }

                    // receivedMessage[13] shows if there was occured an error.
                    if (plcResponseMessage.responseBytesMessage[13] == 0)
                    {
                        pComB.DisAssembleBinaryResult(plcResponseMessage.responseBytesMessage, dataRequestsBytes);
                    }
                }
            }
            else
            {
                //System.Diagnostics.Debug.Assert(false);
            }
        }
Esempio n. 3
0
        private void ReadOperations(ref List <ReadWriteRequest> pdataRequestsForRead, string parentID)
        {
            List <ReadWriteRequest> dataRequestsForRead = null;

            dataRequestsForRead = pdataRequestsForRead.OrderBy(dr => GetOperandSize(dr)).ToList();

            List <List <byte> > dataRequestBytes = new List <List <byte> >();

            for (int i = 0; i < dataRequestsForRead.Count; i++)
            {
                if (dataRequestsForRead[i] is ReadOperands)
                {
                    dataRequestBytes.Add(GetDataRequestBytes(dataRequestsForRead[i] as ReadOperands));
                }
            }

            //set number of read requests in position 4 and 5 of command_details which in header will be 18 and 19
            byte[] command_detailes = new byte[6];
            byte[] count            = BitConverter.GetBytes(dataRequestsForRead.Count);
            command_detailes[4] = count[0];
            command_detailes[5] = count[1];

            PComB pComB = new PComB();

            pComB.BuildBinaryCommand((byte)UnitId, BinaryCommand.ReadOperands, command_detailes,
                                     dataRequestsForRead, dataRequestBytes);

            if (this.BreakFlag)
            {
                throw new ComDriveExceptions("Request aborted by user",
                                             ComDriveExceptions.ComDriveException.AbortedByUser);
            }

            GuidClass guid = new GuidClass();

            lock (guid)
            {
                Channel.Send(pComB.MessageToPLC as byte[], ReceiveBytes, guid, parentID,
                             "Binary Protocol - Read Operands (77)", PlcGuid);
                Monitor.Wait(guid);
            }

            if (this.BreakFlag)
            {
                throw new ComDriveExceptions("Request aborted by user",
                                             ComDriveExceptions.ComDriveException.AbortedByUser);
            }

            PlcResponseMessage plcResponseMessage;

            lock (_lockObj)
            {
                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);
            }

            pComB.DisAssembleBinaryResult(plcResponseMessage.responseBytesMessage, dataRequestBytes);
        }