Error on Windows Form That Run Serial Port

Daniel Mouyal 21 Reputation points
2021-10-22T14:41:30.713+00:00

Hello to All Helper I make a small project that runs windows form with a serial port select and run a command all good but when I add if and else to this form I get error

this is the project


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;
using System.Diagnostics;
using System.IO.Ports;

namespace Daniel_Mouyal____Ultimate_Tool
{
public partial class Samsung_Factory_Reset : Form
{

    public Samsung_Factory_Reset()  
    {  

        InitializeComponent();  
        getallports();  
    }  
    void getallports()  

    {  
        string[] ports = SerialPort.GetPortNames();  
        cmbPort.Items.AddRange(ports);  
    }  
    private void guna2Button1_Click(object sender, EventArgs e)  
    {  
        cmbPort.Text = "";  
    }  

    private void pictureBox1_Click(object sender, EventArgs e)  
    {  

    }  

    private void Exit_Click(object sender, EventArgs e)  
    {  
        this.Close();  
    }  

    private void btnFactoryReset_Click(object sender, EventArgs e)  
    {  


        if (cmbPort.Text == "")  
            MessageBox.Show("Please Select Port");  



        else  

        {  
        }  
          

       System.IO.Ports.SerialPort sport = new System.IO.Ports.SerialPort(cmbPort.Text, 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);  

        try  


        {  
            sport.Open();  
            sport.Write("AT+FACTORST=0,0\r\n");  
        }  
        catch (Exception ex)  
        {  
            MessageBox.Show(ex.ToString());  


        }  



        sport.Close();  

    }  
       
    

    private void btnExit_Click(object sender, EventArgs e)  
    {  
        this.Close();  
    }  
}  
}  

this is the error that I get

142860-error.jpg

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,811 questions
0 comments No comments
{count} votes

1 additional answer

Sort by: Most helpful
  1. Daniel Mouyal 21 Reputation points
    2021-10-23T16:10:19.837+00:00

    Thanks it's work you are really help me

    0 comments No comments