Answered How to call a function from Main Form

Abdul Hayee

Member
Joined
Mar 31, 2020
Messages
24
Programming Experience
Beginner
I want to use a serial port as a class. whenever user Click on Connect button , function will be call and Open the serial Port
whenever Disconnect button is pressed, function will be call and Close the serial Port.

Can you guide me where i am wrong?

Main Program:
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 serial_testing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            Port_Initializer();
        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            Port_Closer();
        }
    }
}

MyClass:
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace serial_testing
{
    class MyClass
    {
        public SerialPort serialPort1 = null;
        
        public void Port_Initializer()
        {
            serialPort1.PortName = "COM10";
            serialPort1.BaudRate = 9600;
            serialPort1.Open();
        }

        public void Port_Closer()
        {
            serialPort1.Close();
        }
    }
}

serial_program.png


Thanks
 
I tried sometime like belows but it is giving an error

Main Program:
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 serial_testing
{
    public partial class Form1 : Form
    {
        MyClass serial_setting = new MyClass();

        string portName = null;
        Int32 baudRate = 0;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            //portName = cBoxComPort.Text;
            //baudRate = Convert.ToInt32(cBoxBaudRate.Text);
            //serial_setting.Port_Initializer("COM10",9600);

            portName = "COM10";
            baudRate = 9600;
            serial_setting.Port_Initializer(portName,baudRate);
        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            serial_setting.Port_Closer();
        }
    }
}

MyClass:
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace serial_testing
{
    class MyClass
    {
        public SerialPort serialPort1 = null;
        
        public void Port_Initializer(string PortNo,int baudRate)
        {
            serialPort1.PortName = PortNo;
            serialPort1.BaudRate = baudRate;
            serialPort1.Open();
        }

        public void Port_Closer()
        {
            serialPort1.Close();
        }
    }
}

exception handler.png


what this error is about?
 
1605270908419.png


You haven't created a SerialPort object and assigned that to the serialPort1 field.
 
Solution
Back
Top Bottom