按命令使用c#。net进行拨号呼叫。

时间:2023-01-29 15:29:18

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:

我想做一个c#程序,用usb线连接到我的手机上,只打一个电话。我找到了如何通过SerialPort进行连接,以及如何通过AT命令进行调用,但是当我运行程序并单击以进行调用时,什么也没有发生。这是我的代码,请帮助我:

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();

3 个解决方案

#1


4  

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.

首先,要查看调制解调器是否已连接,请向端口发送AT命令。如果你得到一个OK作为响应,那么它意味着你的调制解调器是连接的。

To make a call the syntax is:

调用的语法是:

ATDYourphnumber; //Donot forget the ";"

ATDYourphnumber;/ /不要忘了”;“

Example: ATD9012345645;

例如:ATD9012345645;

So you should write to the port the same way.

所以你应该以同样的方式写信给港口。

Syntax:

语法:

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

SP.WriteLine(ATD + phonenumber +”;“);

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

你也可以使用书写,因为它也适用于\r\n。

Update: How to see the response from modem:

更新:如何查看来自调制解调器的响应:

After SP.Open( ) ;

后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


1  

@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 !:

@Cdeez你的回答是最好的!它工作得很好,我尝试了XD,但是我的错误不包括“\r”,它工作时按下“enter”,您需要按enter命令才能采取行动。顺便说一下,这是我的通话方式,再次感谢@Cdeez !

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


0  

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

COM端口日志工具呢?你使用它吗?您的命令发送到COM端口了吗?

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

据我所知拨号命令是ATD[Number];所以试着重写你的代码如下:

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

Try to use this library: GSM Communication Library

尝试使用这个库:GSM通信库

#1


4  

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.

首先,要查看调制解调器是否已连接,请向端口发送AT命令。如果你得到一个OK作为响应,那么它意味着你的调制解调器是连接的。

To make a call the syntax is:

调用的语法是:

ATDYourphnumber; //Donot forget the ";"

ATDYourphnumber;/ /不要忘了”;“

Example: ATD9012345645;

例如:ATD9012345645;

So you should write to the port the same way.

所以你应该以同样的方式写信给港口。

Syntax:

语法:

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

SP.WriteLine(ATD + phonenumber +”;“);

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

你也可以使用书写,因为它也适用于\r\n。

Update: How to see the response from modem:

更新:如何查看来自调制解调器的响应:

After SP.Open( ) ;

后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


1  

@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 !:

@Cdeez你的回答是最好的!它工作得很好,我尝试了XD,但是我的错误不包括“\r”,它工作时按下“enter”,您需要按enter命令才能采取行动。顺便说一下,这是我的通话方式,再次感谢@Cdeez !

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


0  

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

COM端口日志工具呢?你使用它吗?您的命令发送到COM端口了吗?

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

据我所知拨号命令是ATD[Number];所以试着重写你的代码如下:

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

Try to use this library: GSM Communication Library

尝试使用这个库:GSM通信库