GsmCommMain - No Phone Connected Error in C# (Sending SMS using broadband stick)

戏子无情 提交于 2019-12-20 03:43:35

问题


I want to use a broadband stick and create a C# application in VB.net 2012 that can send SMS message to single / multiple mobile phones. I'm currently connected to a COM Port, but I could not send any message. Here is my whole code:

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 GsmComm.GsmCommunication;
using GsmComm.PduConverter;
using GsmComm.Server;
using GsmComm.Interfaces;

namespace myTEXT
{
public partial class Form1 : Form
{
    private GsmCommMain comm;


    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    }

    private void cmdSend_Click(object sender, EventArgs e)
    {
            SmsSubmitPdu pdu;
            byte dcs = (byte)DataCodingScheme.GeneralCoding.Alpha7BitDefault;
            pdu = new SmsSubmitPdu(txtMessage.Text, txtPhoneNumber.Text, dcs);

                comm.SendMessage(pdu);

    }

    private void cmdConnect_Click(object sender, EventArgs e)
    {

        if (txtCommPort.Text == "")
        {
            MessageBox.Show("Invalid Port Number!");

        }

        else
        {
            comm = new GsmCommMain(txtCommPort.Text, 9600, 150);
            Cursor.Current = Cursors.Default;

                Cursor.Current = Cursors.WaitCursor;
                comm.Open();
                Cursor.Current = Cursors.Default;
                MessageBox.Show("Connected!");              


        }
    }
}
}

When I run my program, I can connect to a certain port (for my case, COM22). But when I try to send my message, it has an error

CommException was unhandled

No phone connected.

What to do?

Thanks for the help! Happy coding ^_^


回答1:


After opening the port, make sure also that you are connected to the device that will send the SMS. Here is the code...

private void cmdConnect_Click(object sender, EventArgs e)
{
        if (txtCommPort.Text == "")
        {
            MessageBox.Show("Invalid Port Number!");
        }
        else
        {
            comm = new GsmCommMain(txtCommPort.Text, 9600, 150);
            Cursor.Current = Cursors.Default;

            Cursor.Current = Cursors.WaitCursor;
            comm.Open();
            Cursor.Current = Cursors.Default;

            if (!comm.IsConnected())
            {
                MessageBox.Show("No phone connected.");
            }
            else
            {
                MessageBox.Show("Connected!");
            }
        }
}



回答2:


Might be too late but i created a method that the serial port where gsm device is connected before connecting.you can also save the com22 directly to your code,thats if you won't move the device to another com.after getting the com where the device is you can save it to app setting,it another way to work it around.



来源:https://stackoverflow.com/questions/43339749/gsmcommmain-no-phone-connected-error-in-c-sharp-sending-sms-using-broadband-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!