使用usb调制解调器调用和播放wav文件c#

时间:2017-01-02 04:27:06

标签: c# serial-port at-command usb-modeswitch

我已经编写了一个代码,用于连接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);
            }
        }
    }

0 个答案:

没有答案