Beispiel #1
0
 protected bool OpenTCPClient()
 {
     if (_online)
     {
         return(false);
     }
     CommBase.CommBaseSettings commBaseSettings = CommSettings();
     commBaseSettings.Port = _mainSettings.ComPortName;
     _TCP            = new TCPClientImp(_mainSettings.RemoteIP, _mainSettings.RemotePort, _log);
     _auto           = false;
     _TCP.RecvedMsg += OnRecvedMsg;
     try {
         _TCP.ConnectServer();
     } catch (Exception ex) {
         _log.TraceFatal(string.Format("Can't open {0}:{1}! Reason: {2}", _mainSettings.RemoteIP, _mainSettings.RemotePort, ex.Message));
         return(false);
     }
     _online = true;
     if (AfterOpen())
     {
         _auto = commBaseSettings.AutoReopen;
         return(true);
     }
     else
     {
         Close();
         return(false);
     }
 }
Beispiel #2
0
 protected bool OpenSerialPort()
 {
     if (_online)
     {
         return(false);
     }
     CommBase.CommBaseSettings commBaseSettings = CommSettings();
     _serial = new SerialPortClass(commBaseSettings.Port, commBaseSettings.BaudRate, (Parity)commBaseSettings.Parity, commBaseSettings.DataBits, (StopBits)commBaseSettings.StopBits)
     {
         WriteTimeout = 5000 // 发送超时设为5s
     };
     _writeCount           = 0;
     _auto                 = false;
     _serial.DataReceived += SerialDataReceived;
     try {
         if (_serial.OpenPort())
         {
             _online = true;
             if (AfterOpen())
             {
                 _auto = commBaseSettings.AutoReopen;
                 return(true);
             }
             else
             {
                 Close();
                 return(false);
             }
         }
         else
         {
             return(false);
         }
     } catch (Exception ex) {
         _log.TraceFatal(string.Format("Can't open {0}! Reason: {1}", commBaseSettings.Port, ex.Message));
         return(false);
     }
 }