Call and Play wav file using usb modem c#

990 views Asked by At

I have written a code to dial a number using USB modem connected and want to play a .wav file on the serial port connected.

My code is making the call but when I try to transfer voice using AT command it always through the exception some time "Access denied on port."(but if able to call why access denied) and some time "Device not connected"(but call is on going. How do I reconnect my device while call is going on.)

Below is the code I am using, please take a look.

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);
            }
        }
    }
1

There are 1 answers

2
dnhat On

GSM modem have 2 ports , 1 for comment , 1 for voice , select voice port again .