Ejemplo n.º 1
0
 public void Form_Main_FormClosing(object sender, FormClosingEventArgs e)
 {
     if (master != null)
     {
         master.Dispose();
         master = null;
     }
     if (ComWithSqliteServer != null)
     {
         ComWithSqliteServer.Close();
         ComWithSqliteServer = null;
     }
 }
Ejemplo n.º 2
0
 public void DisconnectFromDevice()
 {
     _modbusSerial?.Dispose();
     _modbusSerial = null;
     Port.Close();
     IsConnected = false;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Отключение от устройства
        /// </summary>
        /// <param name="_portName">Имя порта</param>
        /// <param name="_errMessage">Сообщение</param>
        /// <returns></returns>
        internal void DisconnectToDevice()
        {
            if (_serial.IsOpen && _timer.IsEnabled)
            {
                // <Сброс регистров>
                var cleanRegs = new ushort[3] {
                    6, 7, 8
                };
                for (int i = 0; i < cleanRegs.Length; i++)
                {
                    _master.WriteSingleRegister(slaveID, cleanRegs[i], 0);
                }
                //</Сброс регистров>
            }
            _countTimes = 0;

            CleanSeriesWithChart();
            _timer.Stop();
            _serial.Close();
            _serial.Dispose();
            _master.Dispose();
            _errMessage      = "Остановлено";
            _stateSerialPort = "Подключить";
            _queryRegisters  = "Start";
        }
Ejemplo n.º 4
0
 public override void Close()
 {
     base.Close();
     if (master != null)
     {
         master.Dispose();
         master = null;
     }
 }
        protected virtual void Dispose(bool disposing)
        {
            if (isDisposed)
            {
                return;
            }

            if (disposing)
            {
                master?.Dispose();
                serialPort?.Close();
                serialPort?.Dispose();
            }

            isDisposed = true;
        }
Ejemplo n.º 6
0
        private void Dispose(bool dispose)
        {
            if (_disposed)
            {
                return;
            }

            if (dispose)
            {
                if (_port.IsOpen)
                {
                    _port.Close();
                }
                _port.Dispose();
                _modbus.Dispose();
            }

            _disposed = true;
        }
Ejemplo n.º 7
0
        public void Form_Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (ComWithSqlServer != null)
            {
                ComWithSqlServer?.CloseDatabase();
                ComWithSqlServer = null;
            }

            if (UpLoadThread != null)
            {
                UpLoadThread.Abort();
                UpLoadThread = null;
            }

            if (master != null)
            {
                master.Dispose();
                master = null;
            }

            //FileSave.ClosingSaveAppInfo(demarcate, flowMeterStandard, flowMeterWork);
        }
Ejemplo n.º 8
0
        private void timer4_Tick(object sender, EventArgs e)
        {
            try
            {
                //serial_port = new SerialPort("COM" + textBox16.Text);

                // configure serial port
                serial_port.BaudRate = 57600;
                serial_port.DataBits = 8;
                serial_port.Parity   = Parity.None;
                serial_port.StopBits = StopBits.One;
                serial_port.Open();

                ModbusSerialMaster master = ModbusSerialMaster.CreateRtu(serial_port);
                master.Transport.ReadTimeout = Convert.ToInt32(textBox12.Text);
                //master.Transport.WriteTimeout= Convert.ToInt32()

                byte     slaveID          = 2;
                ushort   startAddress     = 0;
                ushort   numOfPoints      = 1;
                ushort[] holding_register = master.ReadHoldingRegisters(slaveID, startAddress, numOfPoints);
                if (holding_register.Count() != 0)
                {
                    //string s = System.Text.Encoding.UTF8.GetString(holding_register, 0, holding_register.Count);
                    label18.Text = holding_register[0].ToString();
                }
                else
                {
                    label18.Text      = "Ошибка!";
                    checkBox4.Checked = false;
                }
                master.Dispose();

                Application.DoEvents();
            }
            catch { label18.Text = "Ошибка!"; }
        }
Ejemplo n.º 9
0
 public void Close()
 {
     ModBus.Dispose();
     f_Port.Close();
 }
Ejemplo n.º 10
0
        private void timer4_Tick(object sender, EventArgs e)
        {
            try
            {
                SerialPort serial_port = new SerialPort("COM" + textBox16.Text);
                serial_port.BaudRate = 57600;
                serial_port.DataBits = 8;
                serial_port.Parity   = Parity.None;
                serial_port.StopBits = StopBits.One;
                serial_port.Open();

                ModbusSerialMaster master = ModbusSerialMaster.CreateRtu(serial_port);
                master.Transport.ReadTimeout = Convert.ToInt32(textBox12.Text);
                //master.Transport.WriteTimeout= Convert.ToInt32()

                byte     slaveID          = 2;
                ushort   startAddress     = 0;
                ushort   numOfPoints      = 1;
                ushort[] holding_register = master.ReadHoldingRegisters(slaveID, startAddress, numOfPoints);
                if (holding_register.Count() != 0)
                {
                    //string s = System.Text.Encoding.UTF8.GetString(holding_register, 0, holding_register.Count);
                    textBox17.Text = holding_register[0].ToString();
                    //Передача нового значения
                    if ((checkBox1.Checked == true) & (holding_register[0] <= (Convert.ToInt32(textBox2.Text))))
                    {
                        show_alarm("Отсечка кода:" + holding_register[0]);
                    }
                    else
                    {
                        Wkr = holding_register[0];
                    }
                    firt_wav = true;
                    try
                    {
                        this.Player.Stop();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error playing sound");
                    }
                }
                else
                {
                    //Если Передача последнего значения, при ошибке ==TRUE
                    if (checkBox7.Checked == true)
                    {
                        show_alarm("Ошибка ответа");
                    }
                    else
                    {
                        Wkr = 0;
                        show_alarm("Ошибка ответа");
                    }
                }
                master.Dispose();
                serial_port.Close();

                //Application.DoEvents();
            }
            catch {
                if (checkBox7.Checked == true)
                {
                    show_alarm("Ошибка передачи");
                }
                else
                {
                    Wkr = 0;
                    show_alarm("Ошибка передачи");
                }
            }
        }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            try{
                /*
                 *  Connecting via Serial
                 */
                SerialPort serialPort = new SerialPort(); //Create a new SerialPort object.
                serialPort.PortName  = "COM5";            //PC port
                serialPort.BaudRate  = 19200;             //baud rate
                serialPort.DataBits  = 8;
                serialPort.Parity    = Parity.None;
                serialPort.StopBits  = StopBits.One;
                serialPort.RtsEnable = true; //false for RS232, true for RS48
                serialPort.Open();

                ModbusSerialMaster master = ModbusSerialMaster.CreateRtu(serialPort); //or .CreateAscii(serialPort)
                master.Transport.ReadTimeout = 1000;                                  //ms


                {
                    // read multiple int16 values
                    string[] totusTemps =
                    {
                        "Thermal/AmbientTemp",
                        "Thermal/AmbientTemp/1hAvg",
                        "Thermal/AmbientHumidity",
                        "Thermal/AmbientHumidity/1hAvg",
                        "Thermal/TopOilTemp",
                        "Thermal/TopOilTemp/1hAvg",
                        "Thermal/BottomOilTemp",
                        "Thermal/BottomOilTemp/1hAvg",
                        "Thermal/TapChangerTemp",
                        "Thermal/TapChangerTemp/1hAvg"
                    };

                    {
                        //read int16 temperatures
                        ushort   numInputs    = 10;
                        byte     slaveID      = 1;
                        ushort   startAddress = 1000;                                                        //select address from Totus Modbus table
                        ushort[] temps        = master.ReadInputRegisters(slaveID, startAddress, numInputs); //*2 because we are reading 2byte unsigned short that needs converted to 4 byte floats

                        for (int i = 0; i < numInputs; i++)
                        {
                            Console.WriteLine("{0} = {1}°C", totusTemps[i], (float)temps[i] / 10); // divide by 10 as specified in Scaling column
                        }
                    }
                }
                {
                    //read alarms
                    byte   slaveID      = 1;
                    ushort numInputs    = 2;
                    ushort startAddress = 100;
                    bool[] alarms       = master.ReadInputs(slaveID, startAddress, numInputs);

                    string[] totusAlarms =
                    {
                        "ALARM/System/HL/State",
                        "ALARM/System/HHLL/State"
                    };
                    for (int i = 0; i < numInputs; i++)
                    {
                        Console.WriteLine("{0} = {1}", totusAlarms[i], alarms[i]);
                    }
                }
                {
                    //read DGA float32 gases
                    string[] totusDGA =
                    {
                        "DGA/SourceA/CH4",
                        "DGA/SourceA/C2H6",
                        "DGA/SourceA/C2H4",
                        "DGA/SourceA/C2H2",
                        "DGA/SourceA/CO",
                        "DGA/SourceA/CO2",
                        "DGA/SourceA/O2",
                        "DGA/SourceA/N2",
                        "DGA/SourceA/H2",
                        "DGA/SourceA/H2O",
                        "DGA/SourceA/TDCG",
                        "DGA/SourceA/THC"
                    };
                    byte     slaveID      = 1;
                    ushort   numInputs    = 12;
                    ushort   startAddress = 2200;                                                                      //select address from Totus Modbus table
                    ushort[] inputsdga    = master.ReadInputRegisters(slaveID, startAddress, (ushort)(numInputs * 2)); //*2 because we are reading 2byte unsigned short that needs converted to 4 byte floats

                    for (int i = 0; i < numInputs; i++)
                    {
                        Console.WriteLine("Float32 {0} {1} = {2} ppm", startAddress + i * 2, totusDGA[i], Convert2Float(inputsdga[i * 2], inputsdga[i * 2 + 1]));
                    }
                }
                master.Dispose();
            }
            catch (Exception exception)
            {
                //Connection exception
                //No response from server.
                //The server maybe close the connection, or response timeout.
                Console.WriteLine(exception.Message);
            }

            Console.Read();
        }
Ejemplo n.º 12
0
        public void ResetConnection()
        {
            _master?.Dispose();

            _serialPort?.Close();
        }