Пример #1
0
        private void ParseRxString(List <int> rxByteList)
        {
            if (rxByteList.Count <= 1)
            {
                IncreaseCommunicationErrorsCount();
                return;
            }

            if (rxByteList[0].Equals(255) && rxByteList[1].Equals(255))
            {
                if (rxByteList[2].Equals(255) && rxByteList[3].Equals(255))
                {
                    // Response
                    var size = rxByteList[5];
                    var cmd  = rxByteList[6];

                    var data = string.Join(",", rxByteList);
                    if (MTRCommunication != null)
                    {
                        var eventArgs = new MTRCommandEventArgs {
                            Command = cmd.ToString(), Data = data, Identifier = @"IN", DebugText = "debug"
                        };
                        MTRCommunication(this, eventArgs);
                    }

                    switch (cmd)
                    {
                    case 77:     // 'M':
                        if (rxByteList.Count < 230 || size != 230)
                        {
                            return;
                        }
                        HandleMTRDataMessage(data);
                        break;

                    case 83:     //'S':
                        if (rxByteList.Count < 59 || size != 59)
                        {
                            return;
                        }
                        HandleMTRStatusMessage(data);
                        break;

                    default:
                        IncreaseCommunicationErrorsCount();
                        break;
                    }
                }
                else
                {
                    if (rxByteList.Count < 217)
                    {
                        return;
                    }
                    HandleMTRResponseMessage(rxByteList);
                }
            }
        }
Пример #2
0
        private void SendData(string cmd)
        {
            if (string.IsNullOrEmpty(cmd))
            {
                return;
            }

            WaitForReply(cmd);

            _waitAck = true;

            try
            {
                _serialPort.Write(cmd + @"\r");
            }
            catch (Exception ex)
            {
                if (MTRCommunication != null)
                {
                    var infoArgs = CreateInfoArgs(@"Serial port data sending failed for the command beneath this line, check exception from application log");
                    MTRCommunication(this, infoArgs);
                }
                // TODO some logging
                //Logger.AddMessageToLogQueue(@"Serial port data sending failed");
                //Logger.FlushLogQueue();
                return;
            }

            if (MTRCommunication != null)
            {
                var command = cmd.Substring(0, 1);
                var data    = string.Empty;
                if (cmd.Length > 1)
                {
                    data = cmd.Substring(1);
                }
                var eventArgs = new MTRCommandEventArgs {
                    Command = command, Data = data, Identifier = @"OUT", DebugText = "debug"
                };
                MTRCommunication(this, eventArgs);
            }
        }