我编写了一个代码来使用连接的 USB 调制解调器拨打号码,并希望在连接的串行端口上播放 .wav 文件。
我的代码正在拨打电话,但是当我尝试使用 AT 命令传输语音时,它总是通过异常有时“端口被拒绝。”(但如果能够调用为什么访问被拒绝)和有时“设备未连接”(但通话正在进行。如何在通话进行时重新连接我的设备。)
下面是我正在使用的代码,请看一下。
public static void MakeCall(string number, string filepath, string PORT = "COM3")
{
SerialPort serialPort = new SerialPort();
serialPort.PortName = PORT;
serialPort.BaudRate = 9600;
serialPort.Parity = Parity.None;
serialPort.DataBits = 8;
serialPort.StopBits = StopBits.One;
serialPort.Handshake = Handshake.RequestToSend;
serialPort.DtrEnable = true;
serialPort.RtsEnable = true;
serialPort.NewLine = System.Environment.NewLine;
serialPort.Open();
Console.WriteLine("OPEN");
if (serialPort.IsOpen)
{
Console.WriteLine("OPENED");
serialPort.WriteLine(@"AT" + (char)(13));
Thread.Sleep(200);
//serialPort.WriteLine("AT+CMGF=1" + (char)(13));
Thread.Sleep(200);
serialPort.WriteLine(@"ATD" + number + ";\r");
Thread.Sleep(30000);
serialPort.Write("AT+VTX" + System.Convert.ToChar(13).ToString());
bool MSwitch = false;
byte[] buffer = new byte[20000];
FileStream strm = new FileStream(filepath, System.IO.FileMode.Open);
MemoryStream ms = new MemoryStream();
int count = ms.Read(buffer, 44, buffer.Length - 44);
BinaryReader rdr = new BinaryReader(strm);
while (!MSwitch)
{
byte[] bt = new byte[1024];
bt = rdr.ReadBytes(1024);
if (bt.Length == 0)
{
MSwitch = true;
break;
}
//This line through exception but not in first loop iteration but after few iterations
serialPort.Write(bt, 0, bt.Length);
}
}
}