Click here to Skip to main content
15,171,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends

I am trying to send SMS Using huawei dongle, now i am able to send SMS only When User Select the correct com port number from the combo box which are loaded at the form load event,

Now My Question How Can I make the Combo box Load the only port number which huawei dongle is connected?

This My Code Which I have Done

C#
using System;
using System.Collections.Generic;
using System.Threading;
using System.IO.Ports;
using System.Windows.Forms;
using System.Management;
using System.Management.Instrumentation;

namespace gi
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SerialPort s;
        private void button1_Click(object sender, EventArgs e)
        {

            string comport = cboPorts.Text;
            GetPort(comport);
        }
        public void GetPort(string comport)
        {
           // if (this.s == null)
            {
                this.s = new SerialPort();
                this.s.PortName = comport;
                this.s.Open();
                this.s.BaudRate = 9600;
                this.s.Parity = Parity.None;
                this.s.DataBits = 8;
                this.s.StopBits = StopBits.One;
                //this.s.Handshake = Handshake.RequestToSend;
                this.s.DtrEnable = true;
                this.s.RtsEnable = true;
                //this.s.RtsEnable = true;
                this.s.NewLine = System.Environment.NewLine;
                this.s.WriteLine("AT" + (char)(13));
                //string tt = s.ReadLine();
               // if(s.ReadLine()!="AT/r/r")
                {
                    Thread.Sleep(2000);
                    this.s.WriteLine("AT+CMGF=1" + (char)(13));
                    Thread.Sleep(3000);
                    this.s.WriteLine("AT+CMGS=\"" + 9916518522 + "\"");
                    Thread.Sleep(5000);
                    this.s.WriteLine(">" + "le" + (char)(26));
                    this.s.Close();
                    MessageBox.Show("Sent");
                }
                //else
                //{
                //    MessageBox.Show("Dervice Nt");
                //}
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();


            foreach (string port in ports)
            {
                cboPorts.Items.Add(port);
            }


        }
    }
}
Posted

1 solution

instead of selecting manually use for loop & check by sending on every com port & if the com port does not consists the dongle catch it in try catch and do not show a message.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900