Пример #1
0
        public static IModbusMaster CreateSerialMaster(this IModbusFactory factory, SerialSettings settings)
        {
            SerialPort serialPort = new SerialPort()
            {
                PortName  = settings.PortName,
                BaudRate  = settings.BaudRate,
                DataBits  = settings.DataBits,
                Parity    = settings.Parity,
                StopBits  = settings.StopBits,
                Handshake = settings.Handshake
            };

            var adapter = new SerialPortAdapter(serialPort);

            serialPort.Open();

            switch (settings.ModbusType)
            {
            case ModbusType.Rtu:
                return(factory.CreateRtuMaster(adapter));

            case ModbusType.Ascii:
                return(factory.CreateAsciiMaster(adapter));

            default:
                throw new ArgumentException("Serial Settings must be either of type Rtu or Ascii.");
            }
        }
Пример #2
0
        private void UserInitialization()
        {
            _spManager = new SerialPortManager();

            SerialSettings mySerialSettings = _spManager.CurrentSerialSettings;

            //mySerialSettings.PortName = "COM1";
            //serialSettingsBindingSource.DataSource = mySerialSettings;
            //portNameComboBox.DataSource = mySerialSettings.PortNameCollection;
            //baudRateComboBox.DataSource = mySerialSettings.BaudRateCollection;
            //dataBitsComboBox.DataSource = mySerialSettings.DataBitsCollection;
            //parityComboBox.DataSource = Enum.GetValues(typeof(System.IO.Ports.Parity));
            //stopBitsComboBox.DataSource = Enum.GetValues(typeof(System.IO.Ports.StopBits));

            _spManager.NewSerialDataRecieved += new EventHandler <SerialDataEventArgs>(_spManager_NewSerialDataRecieved);
            FormClosing += new FormClosingEventHandler(Form_FormClosing);


            btnStart.Enabled = false;
            btnStop.Enabled  = false;
            BtnOK.Enabled    = false;

            LoadProduct("P002");
            //BtnCloseQueue.Enabled = false;
        }
Пример #3
0
 /// <summary>
 /// Checks if values are up to date and returns true or false if so.
 /// </summary>
 /// <returns></returns>
 private static bool xmlValuesNotSet()
 {
     try
     {
         SerialSettings tmpFileSettings = new SerialSettings();
         XDocument      xmlDoc          = XDocument.Load(fileName);
         XElement       xRootElement    = xmlDoc.Root.Element("COMPort");
         tmpFileSettings.PortName = xRootElement.Element("COMPort_Name").Value;
         tmpFileSettings.BaudRate = int.Parse(xRootElement.Element("Baud_Rate").Value);
         tmpFileSettings.DataBits = int.Parse(xRootElement.Element("Data_Bits").Value);
         tmpFileSettings.Parity   = (Parity)Enum.Parse(typeof(Parity), xRootElement.Element("Parity").Value, true);
         xmlDoc = null;
         //Function reads as if asking if files are not the same.
         //If the values are the same then return false because nothing needs to be changed.
         if (tmpFileSettings == serialSettings)
         {
             return(false);
         }
         return(true);
     }
     catch (IOException e)
     {
         MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         FileStream fs = new FileStream(fileDirectory + "\\" + fileName, FileMode.OpenOrCreate);
         fs.Close();
         fs.Dispose();
         serializeDataToXML(serialSettings);
         return(true);
     }
 }
Пример #4
0
        private void checkSampleCompiles()
        {
            var settings = new SerialSettings()
            {
                PortName = "COM3",
                BaudRate = 57600,
            };

            //Modbus RTU over serial
            using (var master = ModbusMaster.RTU(settings))
            {
                master.WriteCoil(1, 3000, false);
                master.WriteCoils(1, 3001, false, true);
            }
            //Modbus RTU over isolated serial
            using (var master = ModbusMaster.IsolatedRTU(settings))
            {
                master.WriteCoil(1, 3000, false);
                master.WriteCoils(1, 3001, false, true);
            }
            //Modbus TCP over socket
            using (var master = ModbusMaster.TCP("10.77.0.2", 502))
            {
                master.WriteCoils(1, 4, false, true);
            }
        }
Пример #5
0
        private void MainForm_Load(object sender, EventArgs e)
        {
            /// <summary>
            /// Автозапуск программы с заданными портами
            /// </summary>
            SerialSettings mySerialSettings = _spManager.CurrentSerialSettings;

            serialSettingsBindingSource.DataSource = mySerialSettings;
            if (mySerialSettings.PortNameCollection.Contains(mySerialSettings.inPortNemeDefault))
            {
                inPortNameComboBox.Text = mySerialSettings.inPortNemeDefault;
            }
            if (mySerialSettings.PortNameCollection.Contains(mySerialSettings.outPortNemeDefault))
            {
                outPortNameComboBox.Text = mySerialSettings.outPortNemeDefault;
            }
            if (inPortNameComboBox.Text == mySerialSettings.inPortNemeDefault && outPortNameComboBox.Text == mySerialSettings.outPortNemeDefault)
            {
                btnStart.Enabled = false;
                btnStop.Enabled  = true;
                _spManager.StartListening();
                this.WindowState            = FormWindowState.Minimized;
                notifyIcon1.BalloonTipText  = "Редирект портов запущен автоматически!";
                notifyIcon1.BalloonTipIcon  = ToolTipIcon.Info;
                notifyIcon1.BalloonTipTitle = "Внимание!";
                notifyIcon1.ShowBalloonTip(500);
            }
        }
Пример #6
0
        private void UserInitialization()
        {
            //StreamWriter file = new StreamWriter("C:/Users/HP/Desktop/NXP/tab.csv");
            _spManager = new SerialPortManager(this);
            port1      = new SerialPortManager(this);

            count = 0;
            SerialSettings mySerialSettings = _spManager.CurrentSerialSettings;

            serialSettingsBindingSource.DataSource = mySerialSettings;
            portNameComboBox.DataSource            = mySerialSettings.PortNameCollection;
            baudRateComboBox.DataSource            = mySerialSettings.BaudRateCollection;
            dataBitsComboBox.DataSource            = mySerialSettings.DataBitsCollection;
            parityComboBox.DataSource   = Enum.GetValues(typeof(System.IO.Ports.Parity));
            stopBitsComboBox.DataSource = Enum.GetValues(typeof(System.IO.Ports.StopBits));

            folderBrowserDialog1.Disposed    += new EventHandler(folderBrowserDialog1_disposed);
            _spManager.NewSerialDataRecieved += new EventHandler <SerialDataEventArgs>(_spManager_NewSerialDataRecieved);
            this.FormClosing += new FormClosingEventHandler(MainForm_FormClosing);

            /*
             * //gyro
             * port1._serialPort.BaudRate = 115200;
             * port1._serialPort.PortName = "COM33";
             * port1._serialPort.Parity = System.IO.Ports.Parity.None;
             * port1._serialPort.DataBits = 8;
             * port1._serialPort.StopBits = System.IO.Ports.StopBits.One;
             *
             *
             * port1._serialPort.Open();
             */
        }
Пример #7
0
        /*
         * User custom initialization.
         */
        private void UserInitialization()
        {
            // New serial port manager
            _spManager = new SerialPortManager();

            // Read current serial port settings
            SerialSettings mySerialSettings = _spManager.CurrentSerialSettings;

            // Bind serial port & user interface data sources with serial port settings
            serialSettingsBindingSource.DataSource = mySerialSettings;
            portNameComboBox.DataSource            = mySerialSettings.PortNameCollection;
            baudRateComboBox.DataSource            = mySerialSettings.BaudRateCollection;
            dataBitsComboBox.DataSource            = mySerialSettings.DataBitsCollection;
            parityComboBox.DataSource   = Enum.GetValues(typeof(System.IO.Ports.Parity));
            stopBitsComboBox.DataSource = Enum.GetValues(typeof(System.IO.Ports.StopBits));

            // Add evnet handling functions to serial port manager
            _spManager.NewSerialDataRecieved += new EventHandler <SerialDataEventArgs>(_spManager_NewSerialDataRecieved);
            _spManager.ErrorHandler          += new EventHandler <EventArgs>(_spManager_ErrorHandler);

            this.FormClosing += new FormClosingEventHandler(MainForm_FormClosing);

            // Diable "Disconnect" button
            btnStop.Enabled = false;

            // Add empty point to make chart visible before first sample arrived
            chart1.Series[0].Points.AddXY(double.NaN, double.NaN);
        }
Пример #8
0
 public MasterDto()
 {
     Config   = new MasterConfig();
     Controls = new List <SerializableMap> ();
     Serial   = new SerialSettings();
     Name     = "New Session";
 }
Пример #9
0
 protected override void OnLoadDefaults()
 {
     index         = 0;
     outputType    = DigitalOutputType.Toggle;
     portName      = "";
     this.phrase   = string.Empty;
     this.settings = new SerialSettings(9600, Parity.Even, StopBits.One, 8);
 }
Пример #10
0
 private void timer1_Tick(object sender, EventArgs e)
 {
     //UpdateTable();
     if (xmlManager._serialSettings != mySerialSettings)
     {
         mySerialSettings = xmlManager._serialSettings;
     }
 }
Пример #11
0
        private void CreateSerialMaster(SerialSettings settings)
        {
            var    master   = _modbusFactory.CreateSerialMaster(settings);
            string masterId = settings.PortName;

            AddMaster(masterId, master);
            _ea.GetEvent <NewModbusMasterEvent>().Publish(masterId);
        }
        private List <CategoryItem> CreateCategories()
        {
            var categories = new List <CategoryItem>();

#pragma warning disable IDE0008 // Use explicit type
            foreach (var t in Enum.GetValues(typeof(DeviceType)))
            {
#pragma warning restore IDE0008

                string       name     = t.ToString();
                CategoryItem category = new CategoryItem()
                {
                    Category = "Devices", Name = name,
                };

                switch (name)
                {
                case nameof(DeviceType.Display):
                    var dui = new DisplaySettings();
                    dui.ItemChanged   += SettingsItemChanged;
                    category.UIElement = dui;
                    break;

                case nameof(DeviceType.InputSwitch):
                    var iui = new InputSwitchSettings();
                    iui.ItemChanged   += SettingsItemChanged;
                    category.UIElement = iui;
                    break;

                case nameof(DeviceType.MediaInfo):
                    var mui = new MediaInfoSettings();
                    mui.ItemChanged   += SettingsItemChanged;
                    category.UIElement = mui;
                    break;

                case nameof(DeviceType.Processor):
                    var pui = new ProcessorSettings();
                    pui.ItemChanged   += SettingsItemChanged;
                    category.UIElement = pui;
                    break;

                case nameof(DeviceType.Serial):
                    var sui = new SerialSettings();
                    sui.ItemChanged   += SettingsItemChanged;
                    category.UIElement = sui;
                    break;

                case nameof(DeviceType.Source):
                    var srui = new SourceSettings();
                    srui.ItemChanged  += SettingsItemChanged;
                    category.UIElement = srui;
                    break;
                }
                categories.Add(category);
            }

            return(categories);
        }
Пример #13
0
        public SerialManager(string name, SerialSettings settings)
        {
            input  = new ConcurrentQueue <byte[]>();
            serial = new SerialPort(name);
            settings.CopyTo(serial);
            serial.Open();

            reader = Task.Factory.StartNew(ReadLoop, TaskCreationOptions.LongRunning);
        }
Пример #14
0
        public void OnSaved(WpfConfiguration configurationControl)
        {
            var config = configurationControl as SerialListenConfig;

            this.port     = config.Selector.PortName;
            this.settings = config.Selector.Settings;

            this.phrase = config.Phrase;
        }
Пример #15
0
        private void UserInitialization()
        {
            SerialSettings mySerialSettings = _spManager.CurrentSerialSettings;

            bindingSource1.DataSource         = mySerialSettings;
            comboBoxAdv1.DataSource           = mySerialSettings.PortNameCollection;
            comboBoxAdv2.DataSource           = mySerialSettings.BaudRateCollection;
            _spManager.NewSerialDataRecieved += _spManager_NewSerialDataRecieved;
        }
Пример #16
0
        public void SerialSettingsConstructorTest()
        {
            var sp1 = new SerialPort();

            Assert.AreEqual("COM1", sp1.PortName);
            Assert.AreEqual(9600, sp1.BaudRate);
            Assert.AreEqual(8, sp1.DataBits);
            Assert.AreEqual(Parity.None, sp1.Parity);
            Assert.AreEqual(StopBits.One, sp1.StopBits);
            Assert.AreEqual(Handshake.None, sp1.Handshake);

            var ss1 = new SerialSettings();

            Assert.AreEqual("COM1", ss1.PortName);
            Assert.AreEqual(9600, ss1.BaudRate);
            Assert.AreEqual(8, ss1.DataBits);
            Assert.AreEqual(Parity.None, ss1.Parity);
            Assert.AreEqual(StopBits.One, ss1.StopBits);
            Assert.AreEqual(Handshake.None, ss1.Handshake);

            var ss2 = new SerialSettings("COM0");

            Assert.AreEqual("COM0", ss2.PortName);
            Assert.AreEqual(9600, ss2.BaudRate);
            Assert.AreEqual(8, ss2.DataBits);
            Assert.AreEqual(Parity.None, ss2.Parity);
            Assert.AreEqual(StopBits.One, ss2.StopBits);
            Assert.AreEqual(Handshake.None, ss2.Handshake);

            var sp2 = new SerialPort();

            sp2.PortName  = "COM0";
            sp2.BaudRate  = 1200;
            sp2.DataBits  = 7;
            sp2.Parity    = Parity.Even;
            sp2.StopBits  = StopBits.Two;
            sp2.Handshake = Handshake.XOnXOff;

            var ss3 = new SerialSettings(sp2);

            Assert.AreEqual("COM0", ss3.PortName);
            Assert.AreEqual(1200, ss3.BaudRate);
            Assert.AreEqual(7, ss3.DataBits);
            Assert.AreEqual(Parity.Even, ss3.Parity);
            Assert.AreEqual(StopBits.Two, ss3.StopBits);
            Assert.AreEqual(Handshake.XOnXOff, ss3.Handshake);

            var ss4 = new SerialSettings(ss3);

            Assert.AreEqual("COM0", ss4.PortName);
            Assert.AreEqual(1200, ss4.BaudRate);
            Assert.AreEqual(7, ss4.DataBits);
            Assert.AreEqual(Parity.Even, ss4.Parity);
            Assert.AreEqual(StopBits.Two, ss4.StopBits);
            Assert.AreEqual(Handshake.XOnXOff, ss4.Handshake);
        }
Пример #17
0
        public SerialListenConfig(string portName, SerialSettings settings, string phrase)
        {
            InitializeComponent();

            Selector.PortName = portName;
            Selector.Settings = settings;

            Selector.CanSaveChanged += new EventHandler(this.Selector_CanSaveChanged);

            this.Phrase = phrase;
        }
Пример #18
0
        public void Edit(SerialSettings oldObject)
        {
            Text = "Serial Port Settings";
            var newObject = oldObject.Clone();

            propertyGrid.SelectedObject = newObject;
            if (ShowDialog() == DialogResult.OK)
            {
                newObject.CopyTo(oldObject);
            }
        }
Пример #19
0
        public void SerialExceptionOnReadTest()
        {
            var ss = new SerialSettings("COM0");

            using (var sp = new SerialProcess(ss))
            {
                Assert.Throws <SerialException>(() =>
                {
                    sp.Read(-1, -1, 200);
                });
            }
        }
Пример #20
0
        public MainForm()
        {
            InitializeComponent();
            _spManager = new SerialPortManager();
            SerialSettings mySerialSettings = _spManager.CurrentSerialSettings;

            serialSettingsBindingSource.DataSource = mySerialSettings;
            ComboboxCOM.DataSource            = (mySerialSettings.PortNameCollection.Length > 0) ? mySerialSettings.PortNameCollection : new string[] { "Chưa có" };
            _spManager.NewSerialDataRecieved += new EventHandler <SerialDataEventArgs>(_spManager_NewSerialDataRecieved);
            this.FormClosing += new FormClosingEventHandler(MainForm_FormClosing);
            ComboBoxSampleRate.DataSource = SampleRateList;
        }
Пример #21
0
 private static void RegisterInteractions()
 {
     Interactions.SerialSettings.RegisterHandler(
         async interaction =>
     {
         var dialog = new SerialSettings {
             ViewModel = new SerialSettingsViewModel()
         };
         await dialog.ShowDialog(_mainWindow);
         _mainWindow.Focus();
     });
 }
Пример #22
0
        public void EofExceptionOnReadTest()
        {
            var ss = new SerialSettings("COM98");

            using (var sp = new SerialProcess(ss))
            {
                Process.GetProcessById(sp.Pid).Kill();
                Assert.Throws <EndOfStreamException>(() =>
                {
                    sp.Read(-1, -1, 200);
                });
            }
        }
Пример #23
0
        public void EofExceptionOnWriteTest()
        {
            var ss = new SerialSettings("COM99");

            using (var sp = new SerialProcess(ss))
            {
                Process.GetProcessById(sp.Pid).Kill();
                Assert.Throws <EndOfStreamException>(() =>
                {
                    sp.Write(new byte[0]);
                });
            }
        }
Пример #24
0
        public void SerialSettingsCopyTest()
        {
            var sp1 = new SerialPort();

            sp1.PortName  = "COM0";
            sp1.BaudRate  = 1200;
            sp1.DataBits  = 7;
            sp1.Parity    = Parity.Even;
            sp1.StopBits  = StopBits.Two;
            sp1.Handshake = Handshake.XOnXOff;

            var ss1 = new SerialSettings();

            ss1.CopyFrom(sp1);
            Assert.AreEqual("COM0", ss1.PortName);
            Assert.AreEqual(1200, ss1.BaudRate);
            Assert.AreEqual(7, ss1.DataBits);
            Assert.AreEqual(Parity.Even, ss1.Parity);
            Assert.AreEqual(StopBits.Two, ss1.StopBits);
            Assert.AreEqual(Handshake.XOnXOff, ss1.Handshake);

            var sp2 = new SerialPort();

            ss1.CopyTo(sp2);
            Assert.AreEqual("COM0", sp2.PortName);
            Assert.AreEqual(1200, sp2.BaudRate);
            Assert.AreEqual(7, sp2.DataBits);
            Assert.AreEqual(Parity.Even, sp2.Parity);
            Assert.AreEqual(StopBits.Two, sp2.StopBits);
            Assert.AreEqual(Handshake.XOnXOff, sp2.Handshake);

            var ss3 = new SerialSettings();

            ss3.CopyFrom(ss1);
            Assert.AreEqual("COM0", ss3.PortName);
            Assert.AreEqual(1200, ss3.BaudRate);
            Assert.AreEqual(7, ss3.DataBits);
            Assert.AreEqual(Parity.Even, ss3.Parity);
            Assert.AreEqual(StopBits.Two, ss3.StopBits);
            Assert.AreEqual(Handshake.XOnXOff, ss3.Handshake);

            var ss4 = new SerialSettings();

            ss1.CopyTo(ss4);
            Assert.AreEqual("COM0", ss4.PortName);
            Assert.AreEqual(1200, ss4.BaudRate);
            Assert.AreEqual(7, ss4.DataBits);
            Assert.AreEqual(Parity.Even, ss4.Parity);
            Assert.AreEqual(StopBits.Two, ss4.StopBits);
            Assert.AreEqual(Handshake.XOnXOff, ss4.Handshake);
        }
Пример #25
0
        public void OnSave()
        {
            this.PortName = PortBox.SelectedValue.ToString();

            // Set the settings (validate baud rate and databits as you go)

            // This should work because we validated already
            int      baud     = int.Parse(BaudBox.Text);
            Parity   parity   = (Parity)Enum.Parse(typeof(Parity), ParityBox.SelectedValue.ToString());
            StopBits stopBits = (StopBits)Enum.Parse(typeof(StopBits), StopBitsBox.SelectedValue.ToString());
            int      dataBits = int.Parse(DataBitsBox.Text);

            this.Settings = new SerialSettings(baud, parity, stopBits, dataBits);
        }
Пример #26
0
        public CMS50DPlus()
        {
            portManager = new SerialPortManager();
            SerialSettings settings = portManager.CurrentSerialSettings;

            settings.BaudRate = 19200;
            settings.PortName = settings.PortNameCollection[1];
            settings.Parity   = Parity.Odd;
            settings.DataBits = 8;
            settings.StopBits = StopBits.One;

            portManager.NewSerialDataRecieved += PortManager_NewSerialDataRecieved;
            portManager.StartListening();
        }
Пример #27
0
        private void PortConfig()
        {
            ClosePort();
            var dialog = new SerialSettings();

            if (dialog.ShowDialog() == true)
            {
                _port = dialog.Port;
                _port.DataReceived += _port_DataReceived;
                _port.Open();
                _portopen       = true;
                PortStatus.Text = _port.PortName;
            }
        }
Пример #28
0
        public static void serializeDataToXML(SerialSettings _spSettings)
        {
            serialSettings = _spSettings;
            XmlTextWriter writer = new XmlTextWriter(fileName, Encoding.UTF8);

            writer.WriteStartDocument(true);
            writer.Formatting  = Formatting.Indented;
            writer.Indentation = 2;
            writer.WriteStartElement("COMPort_Values");
            createNode(writer);
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
        }
Пример #29
0
        private void UserInitialization()
        {
            _spManager = new SerialPortManager();
            SerialSettings mySerialSettings = _spManager.CurrentSerialSettings;

            serialSettingsBindingSource.DataSource = mySerialSettings;
            portNameComboBox.DataSource            = mySerialSettings.PortNameCollection;
            baudRateComboBox.DataSource            = mySerialSettings.BaudRateCollection;
            dataBitsComboBox.DataSource            = mySerialSettings.DataBitsCollection;
            parityComboBox.DataSource   = Enum.GetValues(typeof(System.IO.Ports.Parity));
            stopBitsComboBox.DataSource = Enum.GetValues(typeof(System.IO.Ports.StopBits));

            _spManager.NewSerialDataRecieved += new EventHandler <SerialDataEventArgs>(_spManager_NewSerialDataRecieved);
            this.FormClosing += new FormClosingEventHandler(MainForm_FormClosing);
        }
Пример #30
0
        private Func <IDNP3Manager, IChannel> GetSerialFunctor(TimeSpan min, TimeSpan max)
        {
            var name = this.comboBoxSerialDeviceName.Text;
            var baud = Decimal.ToInt32(this.numericUpDownBaud.Value);

            var dataBits = Decimal.ToInt32(this.numericUpDownDataBits.Value);
            var parity   = (Parity)comboBoxParity.SelectedValue;
            var flow     = (FlowControl)comboBoxFlowControl.SelectedValue;
            var stopBits = (StopBits)comboBoxStopBits.SelectedValue;

            var flags = logLevelControl1.Filters.Flags;
            var ss    = new SerialSettings(name, baud, dataBits, stopBits, parity, flow);

            return((IDNP3Manager manager) => manager.AddSerial(this.textBoxID.Text, flags, min, max, ss));
        }
Пример #31
0
        private Func<IDNP3Manager, IChannel> GetSerialFunctor(TimeSpan min, TimeSpan max)
        {
            var name = this.comboBoxSerialDeviceName.Text;
            var baud = Decimal.ToInt32(this.numericUpDownBaud.Value);

            var dataBits = Decimal.ToInt32(this.numericUpDownDataBits.Value);
            var parity = (Parity)comboBoxParity.SelectedValue;
            var flow = (FlowControl) comboBoxFlowControl.SelectedValue;
            var stopBits = (StopBits) comboBoxStopBits.SelectedValue;

            var flags = logLevelControl1.Filters.Flags;
            var retry = new ChannelRetry(min, max);
            var ss = new SerialSettings(name, baud, dataBits, stopBits, parity, flow);
            return (IDNP3Manager manager) => manager.AddSerial(this.textBoxID.Text, flags, retry, ss);
        }