// // 
// // (c) 2010 eedv holger@eiboeck.de
// // 
using System;
using System.IO.Ports;
using Modbus.Device;

namespace KaltgasTool
{
    public static class DeviceFactory
    {
        private static SerialPort _port;

        public static ModbusSerialMaster Create(string comport, int baudrate, int databits, int parity, int stopbits)
        {
            if (_port != null)
                _port.Close();

            _port = new SerialPort(comport)
                        {
                            BaudRate = baudrate,
                            DataBits = databits,
                            Parity = ((Parity) parity),
                            StopBits = ((StopBits) stopbits),
                            ReadTimeout = 2000,
                            WriteTimeout = 2000
                        };

            try
            {
                if (!_port.IsOpen)
                    _port.Open();
            }
            catch (Exception)
            {

                return null;
            }

            return ModbusSerialMaster.CreateRtu(_port);
        }
    }
}