コード例 #1
0
        private bool IsErrorCRC(ResponseBuffer buffer, int messageLength, ModbusRequest request, int timeout)
        {
            var crc = Read(buffer, messageLength, 2, timeout).ToArray();

            return(!CalculateCrc(buffer.Take(messageLength)).SequenceEqual(crc));
        }
コード例 #2
0
        private void RunReceive(ModbusChannel channel)
        {
            lock (responseWaitHandles)
            {
                if (!isReceiving)
                {
                    isReceiving = true;
                    try
                    {
                        var readBuffer = new ResponseBuffer(channel);

                        while (responseWaitHandles.Count > 0)
                        {
                            if (errorBuffer.Count >= 256)
                            {
                                RaiseUnrecognized(channel, errorBuffer.ToArray());
                                errorBuffer.Clear();
                            }

                            readBuffer.Read(2, 0);

                            ushort transactionID = (ushort)((readBuffer[0] << 8) | readBuffer[1]);
                            if (!responseWaitHandles.TryGetValue(transactionID, out var responseWaitHandle))
                            {
                                errorBuffer.AddRange(readBuffer);
                                readBuffer.Clear();
                                continue;
                            }

                            readBuffer.Read(2, responseWaitHandle.Timeout);
                            if (readBuffer[2] != 0 ||
                                readBuffer[3] != 0)
                            {
                                errorBuffer.AddRange(readBuffer);
                                readBuffer.Clear();

                                responseWaitHandle.Response = new ModbusCommErrorResponse(ModbusCommErrorCode.ModbusTcpSymbolError, errorBuffer, responseWaitHandle.Request);
                                continue;
                            }

                            var result = base.DeserializeResponse(readBuffer, responseWaitHandle.Request, responseWaitHandle.Timeout);

                            if (result is ModbusCommErrorResponse responseCommErrorMessage)
                            {
                                errorBuffer.AddRange(readBuffer.Take(4));
                                readBuffer.RemoveRange(0, 4);

                                responseWaitHandle.Response = result;
                                continue;
                            }

                            if (readBuffer.Count - 6 != (ushort)((readBuffer[4] << 8) | readBuffer[5]))
                            {
                                errorBuffer.AddRange(readBuffer.Take(4));
                                readBuffer.RemoveRange(0, 4);

                                responseWaitHandle.Response = new ModbusCommErrorResponse(ModbusCommErrorCode.ResponseTcpLengthDoNotMatch, readBuffer, responseWaitHandle.Request);
                                continue;
                            }


                            if (errorBuffer.Count > 0)
                            {
                                RaiseUnrecognized(channel, errorBuffer.ToArray());
                                errorBuffer.Clear();
                            }

                            responseWaitHandle.ResponseBuffer.AddRange(readBuffer);

                            readBuffer.Clear();
                            responseWaitHandle.Response = result;
                            responseWaitHandle.Set();
                            this.responseWaitHandles.Remove(transactionID);
                        }
                    }
                    catch
                    {
                    }
                    isReceiving = false;
                }
            }
        }