Exemple #1
0
        private void HandleDataMessage(MessageHeader header, SecsMessage secsMessage)
        {
            var systemByte = header.SystemBytes;

            if (header.DeviceId != DeviceId && secsMessage.S != 9 && secsMessage.F != 1)
            {
                Logger.Error("Received Unrecognized Device Id Message");
                SecsMessage replyWrongDeviceIdMsg = new SecsMessage(
                    9,
                    1,
                    false,
                    Resources.S9F1,
                    Item.B(header.EncodeTo(new byte[10])));// 将header作为消息体返回
                SendDataMessageAsync(replyWrongDeviceIdMsg, NewSystemId);

                if (_replyExpectedMsgs.TryGetValue(systemByte, out var ar1))
                {
                    ar1.HandleReplyMessage(replyWrongDeviceIdMsg);
                }
                return;
            }

            if (secsMessage.F % 2 != 0)
            {
                if (secsMessage.S != 9)
                {
                    _taskFactory.StartNew((wrapper) =>
                    {
                        PrimaryMessageReceived?.Invoke(this, wrapper as PrimaryMessageWrapper);
                    }, new PrimaryMessageWrapper(this, header, secsMessage));
                    return;
                }
                // Error message
                var headerBytes = secsMessage.SecsItem.GetValues <byte>();// 解析出MessageHeader的Bytes
                systemByte = BitConverter.ToInt32(new byte[] { headerBytes[9], headerBytes[8], headerBytes[7], headerBytes[6] }, 0);
            }

            if (_replyExpectedMsgs.TryGetValue(systemByte, out var ar))
            {
                ar.HandleReplyMessage(secsMessage);
            }
        }
Exemple #2
0
        internal Task <SecsMessage> SendDataMessageAsync(SecsMessage secsMessage, int systemByte)
        {
            if (State != ConnectionState.Selected)
            {
                throw new SecsException("Device is not selected");
            }

            var token = new TaskCompletionSourceToken(secsMessage, systemByte, MessageType.DataMessage);

            if (secsMessage.ReplyExpected)
            {
                _replyExpectedMsgs[systemByte] = token;
            }

            var header = new MessageHeader()
            {
                S             = secsMessage.S,
                F             = secsMessage.F,
                ReplyExpected = secsMessage.ReplyExpected,
                DeviceId      = DeviceId,
                SystemBytes   = systemByte,
            };
            var bufferList = secsMessage.RawDatas.Value;

            bufferList[1] = new ArraySegment <byte>(header.EncodeTo(new byte[10]));
            var sendEventArgs = new SocketAsyncEventArgs
            {
                BufferList = bufferList,
                UserToken  = token,
            };

            sendEventArgs.Completed += SendDataMessage_Completed;
            if (!_socket.SendAsync(sendEventArgs))
            {
                SendDataMessage_Completed(_socket, sendEventArgs);
            }
            return(token.Task);
        }
Exemple #3
0
 /// <summary>
 /// Asynchronously send message to device .
 /// </summary>
 /// <param name="secsMessage"></param>
 /// <param name="systemId"></param>
 /// <returns>null</returns>
 public Task <SecsMessage> SendAsync(SecsMessage secsMessage, int systemId) => SendDataMessageAsync(secsMessage, systemId);
Exemple #4
0
 /// <summary>
 /// Asynchronously send message to device .
 /// </summary>
 /// <param name="secsMessage">primary message</param>
 /// <returns>senondary</returns>
 public Task <SecsMessage> SendAsync(SecsMessage secsMessage) => SendAsync(secsMessage, NewSystemId);
Exemple #5
0
 public SecsException(SecsMessage secsMessage, string description) : base(description)
 {
     this.secsMessage = secsMessage;
 }