Dial call using C#.net by AT Commands

一笑奈何 提交于 2019-12-07 20:34:56

问题


I want to make a c# program that connects to my mobile phone by usb cable to make just a call. I found how to connect by SerialPort and how to make call by AT Commands, but when I run my program and click to make the call, nothing happens. This is my code, please Help me:

SerialPort SP = new SerialPort("COM3");
SP.BaudRate = 9600;
SP.Parity = Parity.None;
SP.DataBits = 8;
SP.StopBits = StopBits.One;
SP.RtsEnable = true;
SP.DtrEnable = true;
SP.Encoding = System.Text.Encoding.Unicode;
SP.ReceivedBytesThreshold = 1;
SP.NewLine = Environment.NewLine;
SP.Open();
SP.Write("ATDT 0999182542"+ Environment.NewLine);

SP.Close();

回答1:


First of all to see if your modem is connected, send an AT command to the port. If you get a OK as response, then it means your modem is connected.

To make a call the syntax is:

ATDYourphnumber; //Donot forget the ";"

Example: ATD9012345645;

So you should write to the port the same way.

Syntax:

SP.WriteLine("ATD"+phonenumber+";");

You can use WriteLine since that serves the \r\n too.

Update: How to see the response from modem:

After SP.Open( ) ;

string cmd = "AT";
SP.WriteLine(cmd + "\r");
SP.Write(cmd + "\r");
Thread.Sleep(500);
string ss= SP.ReadExisting();
if(ss.EndsWith("\r\nOK\r\n"))
  {
   MessageBox.Show("Modem is connected");
  }



回答2:


@Cdeez Your answer is the best! it works just fine XD I tried but my mistake was not including the "\r" which works as pressing "enter", and you need to press enter for the command to take action. By the way here is my method for calling, and thanks @Cdeez once again !:

private void Call() {
        SerialPort celu = new SerialPort();
        celu.PortName = "COM13"; // You have check what port your phone is using here, and replace it
        celu.Open();
        string cmd = "ATD";  // Here you put your AT command
        string phoneNumber = "784261259"; // Here you put the phone number, for me it worked just with the phone number, not adding any other area code or something like that
        celu.WriteLine(cmd + phoneNumber + ";\r");
        Thread.Sleep(500);
        string ss = celu.ReadExisting();
        if (ss.EndsWith("\r\nOK\r\n"))
        {
            MessageBox.Show("Modem is connected \r Calling : " + phoneNumber);
        }
        celu.Close();
    }



回答3:


What about COM port logging tools. Do you use it? Did your commands send to COM port?

As far as I know dial command is ATD[Number]; so try to rewrite your code as follows:

    SP.Write("ATD0999182542;"+ Environment.NewLine);

Try to use this library: GSM Communication Library



来源:https://stackoverflow.com/questions/12320944/dial-call-using-c-net-by-at-commands

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