コード例 #1
0
        public void WriteBytes(byte[] data)
        {
            //create instance for the TCP/IP and Serial port classes
            NetworkClient client  = new NetworkClient(); //for TCP
            IOPort        comPort = new IOPort();        //for COMM port

            //get the comm settings for the connection
            string[]           settingsArray = _commSettings.Split(',');
            IOPort.ComSettings commSettings  = new IOPort.ComSettings();
            commSettings.BaudRate  = settingsArray[0].ToInt();
            commSettings.ParityBit = Parity.Even;
            commSettings.DataBits  = settingsArray[2].ToInt16();
            commSettings.StopBit   = StopBits.One;
            try
            {
                //if the device is attached to network
                if (_isNetworkAttached)
                {
                    //send data using TCP/IP
                    client.TcpClient(_ipAddress, _port, data);
                }
                else
                {
                    //send data using Serial port
                    LogBook.Write(this.SensorFactoryID + " " + this.SensorAlarmID + " " + "Sending data using UDP Port: " + _port.ToString());
                    comPort.Handshake("COM" + _port.ToString(), commSettings, DataRecievedFromCom);
                    comPort.SerialPortOutput(data);
                }
            }
            catch (Exception ex)
            {
                LogBook.Write(ex, this.SensorFactoryID + " " + this.SensorAlarmID + " " + "NotifyEngine-MessageBoard");
                throw ex;
            }
        }
コード例 #2
0
        private void SendToBoard(string messageData)
        {
            LogBook.Write("Enter send to board method");
            //to communicate using TCP/IP
            NetworkClient client = new NetworkClient();
            //to communicate using serial port
            IOPort ioPort = new IOPort();

            if (_isNetworkAttached)
            {
                //send message to message board using TCP/IP
                //client.TcpClient(_ipAddress, _port, messageData);
            }
            else
            {
                string[]           settingsArray = _commSettings.Split(',');
                IOPort.ComSettings commSettings  = new IOPort.ComSettings();
                commSettings.BaudRate  = settingsArray[0].ToInt();
                commSettings.ParityBit = Parity.None;
                commSettings.DataBits  = settingsArray[3].ToInt16();
                commSettings.StopBit   = StopBits.One;

                ioPort.Handshake(_port.ToString(), commSettings, DataRecievedFromCom);
                ioPort.SerialPortOutput(messageData);

                //send message to message board using serial port
                //ioPort.SerialPort(_port.ToString(),commSettings, messageData,null);
            }
            LogBook.Write("Message sent to message board.");
            LogBook.Write("Exit send to board method");
        }
コード例 #3
0
        public void SendData()
        {
            SetCurrentState(true);
            SetNewState();


            LogBook.Write(this.FactoryID + " " + this.SensorAlarmID + " " + "Sending data to switch, switch bit mask value: " + SwitchBitMask + " data value :" + _lastValue.ToString());
            //if the configuration setting has the "Reset Notification Condition if Sensor Returns To Normal Status:" as checked
            //then IsDynamicNotificationCleared is set to true
            if (IsDynamicNotificationCleared != null && IsDynamicNotificationCleared == true)
            {
                LogBook.Write(this.FactoryID + " " + this.SensorAlarmID + " " + "Dynamic notifications have to be cleared, previous switch value: " + SwitchBitMask + " data value :" + _lastValue.ToString());

                _switchBitMask = 0;
                SetNewState();

                ResponseContent += "switch: [" + this.SwitchName + "] " + (this.IpAddress.ToStr().Trim() != string.Empty ? "IP:" : " ") + this.IpAddress + ", Current state " + CurrentState + ", New State:" + NewState + ".\r\n";

                LogBook.Write(this.FactoryID + " " + this.SensorAlarmID + " " + "Resetting switch value: current relay state:" + CurrentState + ", New State :" + NewState);
            }


            //construct the relay command
            string sBuf = "R" + String.Format("{0:x2}", _lastValue) + ((char)13).ToString();

            try
            {
                //create instances of TCP/IP and Serial port
                NetworkClient client  = new NetworkClient(); //for TCP
                IOPort        comPort = new IOPort();        //for COMM port
                //if the device is attached to network
                if (_isNetworkAttached)
                {
                    LogBook.Write(this.FactoryID + " " + this.SensorAlarmID + " " + "Sending data using TCP IPAddress :" + _ipAddress.ToString() + "Port: " + _ipPort.ToString());
                    //send data using TCP Client
                    client.TcpClient(_ipAddress, _ipPort, Encoding.ASCII.GetBytes(sBuf));
                    LogBook.Write(this.FactoryID + " " + this.SensorAlarmID + " " + "Data sent using TCP IPAddress :" + _ipAddress.ToString() + "Port: " + _ipPort.ToString());
                }
                else
                {
                    LogBook.Write(this.FactoryID + " " + this.SensorAlarmID + " " + "Sending data using UDP Port: " + _comPort.ToString());
                    //ping the com port
                    comPort.Handshake(_comPort.ToString(), _comSettings, DataRecieveFromCom);
                    LogBook.Write(this.FactoryID + " " + this.SensorAlarmID + " " + "Handshake complete at port: " + _comPort.ToString());
                    LogBook.Write(this.FactoryID + " " + this.SensorAlarmID + " " + "Sending data to port: " + _comPort.ToString());
                    //send data using serial port
                    comPort.SerialPortOutput(Encoding.ASCII.GetBytes(sBuf));
                    LogBook.Write(this.FactoryID + " " + this.SensorAlarmID + " " + "Data sent to port: " + _comPort.ToString());
                }
            }
            catch (Exception ex)
            {
                LogBook.Write(ex.Message);
                throw ex;
            }
        }
コード例 #4
0
        /// <summary>
        /// Send Mail
        /// </summary>
        /// <returns></returns>
        public NotifyComResponse Send(INotifyObject notifyObject)
        {
            notifyComResponse = new NotifyComResponse();
            try
            {
                _snppServer     = notifyObject.NotifierSettings["SNPPServer"].ToStr();
                _snppPort       = notifyObject.NotifierSettings["SNPPPort"].ToInt();
                _pagerSubject   = notifyObject.NotifierSettings["PagerSubject"].ToStr();
                _pagerToAddress = notifyObject.NotifierSettings["ToAddress"].ToStr();
                _name           = notifyObject.NotifierSettings["Name"].ToStr();
                _pagerBody      = notifyObject.NotificationData.ToStr();



                /*Send notification to Pager using SNPP Server*/
                try
                {
                    if (notifyObject.NotifierSettings["DeliveryMethod"].ToInt() != 1)
                    {
                        string phone        = notifyObject.NotifierSettings["ToAddress"].ToStr();
                        int    pagerDelay   = notifyObject.NotifierSettings["PagerDelay"].ToInt();
                        string pagerMessage = notifyObject.NotifierSettings["PagerMessage"].ToStr();
                        string comPortId    = notifyObject.NotifierSettings["PagerComPort"].ToStr();

                        Int16 attemptCount = notifyObject.NotifierSettings["AttemptCount"].ToInt16();
                        attemptCount++;
                        notifyObject.NotifierSettings["AttemptCount"] = attemptCount;
                        notifyObject.NotifierSettings["LastSentTime"] = DateTime.Now;

                        Message = " Attempt: " + attemptCount.ToString();

                        sendPageTime = DateTime.Now;

                        /*
                         * "2400,n,8,1"
                         * 1st baud rate
                         * 2nd parity
                         * 3rd data bits
                         * 4th stop bit*/


                        IOPort.ComSettings comSettings = new IOPort.ComSettings();
                        comSettings.BaudRate  = 2400;
                        comSettings.ParityBit = System.IO.Ports.Parity.Even;
                        comSettings.DataBits  = 8;
                        comSettings.StopBit   = System.IO.Ports.StopBits.One;



                        int n = 0;
                        //COMcmds[n++] = "1~+~.1";
                        //COMcmds[n++] = ".3~+~.1";
                        //COMcmds[n++] = ".3~+~.1";

                        //COMcmds[n++] = ".5~ATH~3";          // Hang-up if connected
                        COMcmds[n++] = ".5~ATH~3";          // Hang-up if connected
                        COMcmds[n++] = ".5~ATZ~3";          // Reset modem
                        COMcmds[n++] = ".5~ATL0M0~3";       // Disable modem speaker / sounds
                        COMcmds[n++] = ".5~ATE0~3";         //Turn off echoing

                        if (notifyObject.NotifierSettings["COMportInitString"].ToStr().Length > 0)
                        {
                            COMcmds[n++] = ".5~" + notifyObject.NotifierSettings["COMportInitString"].ToStr() + "~1";
                        }

                        //If Len(InitString) Then COMcmds[n] = ".5~" & InitString & "~1";
                        COMcmds[n++] = ".5~ATDT" + phone + new System.Text.StringBuilder().Append(',', pagerDelay).ToString() + pagerMessage + "~40";
                        //COMcmds[n++] = "0~+~.1";
                        //COMcmds[n++] = ".3~+~.1";
                        //COMcmds[n++] = ".3~+~.1";
                        COMcmds[n++] = ".5~ATH~3";
                        nCOMcmds     = n;

                        comPort = new IOPort();//for COMM port


                        //ping the com port
                        comPort.Handshake("COM" + comPortId, comSettings, DataRecieveFromCom);


                        SendCommand();
                    }
                    else
                    {
                        //snnpObj = new KTGUtil.SNPP();
                        using (SNPPWrapper snppObj = new SNPPWrapper())
                        {
                            /*SNPP Host*/
                            snppObj.Host = _snppServer;

                            /*SNPP Port Number*/
                            snppObj.Port = _snppPort;

                            /*Subject*/
                            snppObj.Subject = _pagerSubject;

                            /*Body*/
                            snppObj.Body = _pagerBody;

                            /*Pager To Address*/
                            snppObj.SendTo = _pagerToAddress;

                            /*Send information to pager*/
                            int res = snppObj.Send();
                            ProcessCompleted = true;



                            if (res == 0)
                            {
                                /*Record notify response*/
                                notifyComResponse.IsError         = false;
                                notifyComResponse.IsSucceeded     = true;
                                notifyComResponse.ResponseContent = "Page to [" + _name + "] " + _pagerToAddress + ", sent successfully.";
                            }
                            else
                            {
                                notifyComResponse.IsError         = true;
                                notifyComResponse.IsSucceeded     = false;
                                notifyComResponse.ResponseContent = "Page to [" + _name + "] " + _pagerToAddress + ", Error:." + snppObj.LastErrorText;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    /*Record notify response*/
                    notifyComResponse.IsError         = true;
                    notifyComResponse.IsSucceeded     = false;
                    notifyComResponse.ResponseContent = "Page to [" + _name + "] " + _pagerToAddress + ", Failed.";
                    Message         += "Page to [" + _name + "] " + _pagerToAddress + ", Failed (" + ex.Message + ").";
                    ProcessCompleted = true;
                    /*Debug Object values for reference*/
                    LogBook.Debug(notifyComResponse, this);

                    /*Write exception log*/
                    LogBook.Write("Error has occurred while sending pager to ." + _pagerToAddress, ex, "CooperAtkins.NotificationServer.NotifyEngine.PagerHelper");
                }
            }
            catch (Exception ex)
            {
                /*Record notify response*/
                notifyComResponse.IsError         = true;
                notifyComResponse.IsSucceeded     = true;
                notifyComResponse.ResponseContent = "Page to [" + _name + "] " + _pagerToAddress + ", Failed.";
                Message         += "Page to [" + _name + "] " + _pagerToAddress + ", Failed (" + ex.Message + ").";
                ProcessCompleted = true;
                /*Debug Object values for reference*/
                LogBook.Debug(notifyComResponse, this);

                /*Write exception log*/
                LogBook.Write("Error has occurred while preparing SNPP setting.", ex, "CooperAtkins.NotificationServer.NotifyEngine.PagerHelper");
            }
            return(notifyComResponse);
        }