Beispiel #1
0
        public Boolean Open()
        {
            LastError = new ModuleError(ModuleErrorCode.None);

            TraceInfo(String.Format("Открытие {0}", LinkName));

            if (State == LinkState.Opened)
            {
                TraceWarn(String.Format("Уже открыт {0}", LinkName));

                return(true);
            }

            State = LinkState.Opening;

            if (OnOpen())
            {
                State = LinkState.Opened;

                return(true);
            }

            State = LinkState.Closed;

            return(false);
        }
Beispiel #2
0
 public Boolean SendBytes(Byte[] buffer)
 {
     if (State == LinkState.Opened)
     {
         Boolean result = OnSend(buffer);
         if (result)
         {
             TraceDump(DebugLevel.Out, LinkName, buffer);
         }
         return(result);
     }
     else
     {
         // Передача данных в закрытый порт
         LastError = new ModuleError(ModuleErrorCode.SendToClosed);
     }
     return(false);
 }
Beispiel #3
0
        protected override Boolean OnClose()
        {
            try
            {
                _serialPort.Close();
                NotifyClose(String.Format("{0} закрыт", PortName));

                return(true);
            }
            catch (IOException ex)
            {
                LastError = new ModuleError(ModuleErrorCode.Exception, ex.Message);
            }
            catch (Exception ex)
            {
                LastError = new ModuleError(ModuleErrorCode.Exception, ex.Message);
            }

            NotifyError(String.Format("Ошибка закрытия {0}: {1}", PortName, LastError.Message));

            return(false);
        }
Beispiel #4
0
        protected override Boolean OnOpen()
        {
            try
            {
                _serialPort.Open();
                NotifyOpen(String.Format("{0} открыт, скорость {1}", PortName, Baudrate));

                return(true);
            }
            catch (UnauthorizedAccessException)
            {
                LastError = new ModuleError(ModuleErrorCode.AccessDenied);
            }
            catch (ArgumentOutOfRangeException)
            {
                LastError = new ModuleError(ModuleErrorCode.WrongParam);
            }
            catch (ArgumentException)
            {
                LastError = new ModuleError(ModuleErrorCode.WrongPortName);
            }
            catch (InvalidOperationException)
            {
                LastError = new ModuleError(ModuleErrorCode.AlreadyOpen);
            }
            catch (IOException)
            {
                LastError = new ModuleError(ModuleErrorCode.PortNotPresent);
            }
            catch (Exception ex)
            {
                LastError = new ModuleError(ModuleErrorCode.Exception, ex.Message);
            }

            NotifyError(String.Format("Ошибка открытия {0}: {1}", PortName, LastError.Message));

            return(false);
        }
Beispiel #5
0
        protected override Boolean OnSend(Byte[] buffer)
        {
            try
            {
                _serialPort.Write(buffer, 0, buffer.Length);

                return(true);
            }
            catch (ArgumentNullException)
            {
                LastError = new ModuleError(ModuleErrorCode.BufferIsNull);
            }
            catch (InvalidOperationException)
            {
                LastError = new ModuleError(ModuleErrorCode.NotOpened);
            }
            catch (ArgumentOutOfRangeException)
            {
                LastError = new ModuleError(ModuleErrorCode.WrongParam, "count=" + buffer.Length.ToString());
            }
            catch (ArgumentException)
            {
                LastError = new ModuleError(ModuleErrorCode.WrongParam, "count=" + buffer.Length.ToString());
            }
            catch (TimeoutException ex)
            {
                LastError = new ModuleError(ModuleErrorCode.Exception, ex.Message);
            }
            catch (Exception ex)
            {
                LastError = new ModuleError(ModuleErrorCode.Exception, ex.Message);
            }

            NotifyError(String.Format("Ошибка отправки {0}: {1}", PortName, LastError.Message));

            return(false);
        }