Friday, May 1, 2015

One serial port in multiple forms.

Hi, 

I would like to ask you folks about passing one serial port to multiple forms in one project. I acomplished to passed it from "main" form to "Form1" but I don't know how to handle it by buttons, combobox, etc. I want to change serial port parameters in seperate form. I'm quite new in c# and i can't figure it out.

Form "main"

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RS232
{
    public partial class mainform : Form
    {
        Form1 frm1;
        Form2 frm2;
         
        public mainform()
        {
            InitializeComponent();
            this.Text = "Mitasubaszi controller";
            frm1 = new Form1(sport);
            frm2 = new Form2();            
        }      
       
        public void toolStripMenuItem9_Click(object sender, EventArgs e)
        {
            DialogResult resultquit;
            resultquit = MessageBox.Show("Are you sure?","Warning", MessageBoxButtons.YesNo);
            if (resultquit == System.Windows.Forms.DialogResult.Yes)
            {
                if (sport.IsOpen) sport.Close();
                Close();
            }           
        
        }

        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            frm1.ShowDialog();   
        }
       

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            frm2.ShowDialog();
        }
    }
}

Form "Form1"

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Timers;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RS232
{
       
    public partial class Form1 : Form
    {
        
        int brate;
        
     
        public Form1(System.IO.Ports.SerialPort main)
        {
            InitializeComponent();
            initvalue(main);                     
        }      


        public void initvalue(System.IO.Ports.SerialPort initport)
        {   
            boxbaud.SelectedIndex = 5;
            boxdata.SelectedIndex = 3;
            boxpar.SelectedIndex = 0;
            boxstop.SelectedIndex = 2;
            checkDTR.Checked = true;
            checkRTS.Checked = true;
            initport.BaudRate = 9600;
            initport.DataBits = 8;
            initport.PortName = "COM1";
            initport.DtrEnable = true;
            initport.RtsEnable = true;
            initport.Parity = System.IO.Ports.Parity.Even;
            initport.StopBits = System.IO.Ports.StopBits.Two;           
        }    

        public void button1_Click(object sender, EventArgs e)
        {
            this.Close();            
        }
                
        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void DTR_CheckedChanged(object sender, EventArgs e)
        {
            if (checkDTR.Checked) .DtrEnable = true;
            else .DtrEnable = false;            
        }

        public void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (.IsOpen) .Close();
                int comvalue = Convert.ToInt32(numericUpDown1.Value);
                string comport = String.Format("COM{0}", comvalue);
                
            }
            catch
            {
                MessageBox.Show("Port does not exist");
            }
        }

        public void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (boxbaud.SelectedIndex == -1) MessageBox.Show("Baud Rate is not selected", "Warning");
            else
            {
                brate = Convert.ToInt32(boxbaud.SelectedItem);
                
            }
            
        }

        private void button7_Click(object sender, EventArgs e)
        {
            string stat;
            if (.IsOpen) stat = "Open";
            else stat = "Close";
            string br = Convert.ToString(.BaudRate);
            string dts = Convert.ToString(.DtrEnable);
            string com = Convert.ToString(.PortName);
            string par = Convert.ToString(.Parity);
            string dat = Convert.ToString(.DataBits);
            string rts = Convert.ToString(.RtsEnable);
            string stop = Convert.ToString(.StopBits);
            string text = string.Format("Status: {0}\nPort: {1}\nBaud rate: {2}\nData bits: {3}\nParity: {4}\nStop bits: {5}\nDTS: {6}\nRTS {7}", stat, com, br, dat, par, stop, dts, rts);
            MessageBox.Show(text);
        }
                    
        private void checkRTS_CheckedChanged(object sender, EventArgs e)
        {
            if (checkRTS.Checked) .RtsEnable = true;
            else .RtsEnable = false;
        }

        private void boxpar_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (boxpar.SelectedItem == "Even") .Parity = System.IO.Ports.Parity.Even;
            if (boxpar.SelectedItem == "Mark") .Parity = System.IO.Ports.Parity.Mark;
            if (boxpar.SelectedItem == "None") .Parity = System.IO.Ports.Parity.None;
            if (boxpar.SelectedItem == "Odd") .Parity = System.IO.Ports.Parity.Odd;
            if (boxpar.SelectedItem == "Space") .Parity = System.IO.Ports.Parity.Space;
        }

        private void boxdata_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (boxdata.SelectedIndex == -1) MessageBox.Show("Data bits are not selected", "Warning");
            else
            {
                int bdata = Convert.ToInt32(boxdata.SelectedItem);
                .DataBits = bdata;
            }
        }

        private void boxstop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (boxstop.SelectedItem == "1") .StopBits = System.IO.Ports.StopBits.One;
            if (boxstop.SelectedItem == "1.5") .StopBits = System.IO.Ports.StopBits.OnePointFive;
            if (boxstop.SelectedItem == "2") .StopBits = System.IO.Ports.StopBits.Two;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (!.IsOpen) .Open();
            }
            catch
            {
                MessageBox.Show("Invalid port parameters");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (.IsOpen) .Close(); 
        }  

     
    }
}

No comments:

Post a Comment