2

我必须在 C# 中做一个应用程序才能从 gsm 调制解调器呼叫电话号码并发送 SMS。我设法使用 AT 命令完成拨号和发送 SMS 的部分,现在我必须在有人回答时注入语音文件称呼。这是我的代码

using System;
using System.Collections.Generic;
using System.Text;
using System;
using System.IO.Ports;
using System.Threading;
using System.Speech.Synthesis;
using System.Collections.ObjectModel;

  public class PortChat
{
// static bool _continue;
 static SerialPort _serialPort;

 public static void Main()
{

    //string message;
    StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
    // Create a new SerialPort object with default settings.
    _serialPort = new SerialPort();

    // Allow the user to set the appropriate properties.
    _serialPort.PortName = SetPortName(_serialPort.PortName);
    _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
    _serialPort.Parity = SetPortParity(_serialPort.Parity);
    _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
    _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
    _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

    // Set the read/write timeouts
    _serialPort.ReadTimeout = 500;
    _serialPort.WriteTimeout = 500;

    _serialPort.Open();

    // readThread.Start();


    string phonenr = "";
    string mesaj;
    if (!_serialPort.IsOpen)
    {
        _serialPort.Open();
    }
    _serialPort.WriteLine("AT\r");

    {
        Console.WriteLine("Enter the phone number:", phonenr);
        phonenr = Console.ReadLine();
        _serialPort.WriteLine("ATD" + phonenr + ";" + "\r");            
        Console.WriteLine("Ring...");
        Thread.Sleep(1000);                               
        Speak(1);
        Console.WriteLine("Enter the message:");
        mesaj= Console.ReadLine();
        _serialPort.WriteLine("AT+CMGF=1\r");
        _serialPort.WriteLine("AT+CMGS=\"" + phonenr + "\"\r");
        _serialPort.WriteLine(mesaj + '\x001a');
         Thread.Sleep(500);
         Console.WriteLine("Message sent...");     
        _serialPort.DiscardInBuffer();
        _serialPort.DiscardOutBuffer();
        _serialPort.Close();


        _serialPort.Close();

        Console.ReadLine();
    }
}


static void Speak(int voiceIdx)
{
    string text = "";
    SpeechSynthesizer syn = new SpeechSynthesizer();
    ReadOnlyCollection<InstalledVoice> voices = syn.GetInstalledVoices();
    if (voiceIdx < 0 || voiceIdx >= voices.Count)
    {
        Console.WriteLine("voice index out of range from [0-{0}]", voices.Count);
        return;
    }

    syn.SelectVoice(voices[voiceIdx].VoiceInfo.Name);
    //tranforms the following given text in voice
    syn.Speak("the address is : constitution street number 69");
    // here you can enter the text to be transformed in voice
    Console.WriteLine("Enter the text:");
    text = Console.ReadLine();
    syn.Speak(text);
}



static void ListVoice()
{
    SpeechSynthesizer syn = new SpeechSynthesizer();
    ReadOnlyCollection<InstalledVoice> voices = syn.GetInstalledVoices();
    foreach (InstalledVoice voice in voices)
    {
        Console.WriteLine(voice.VoiceInfo.Description);
    }
}



public static string SetPortName(string defaultPortName)
{
    string portName;

    Console.WriteLine("Available Ports:");
    foreach (string s in SerialPort.GetPortNames())
    {
        Console.WriteLine("   {0}", s);
    }

    Console.Write("COM port({0}): ", defaultPortName);
    portName = Console.ReadLine();

    if (portName == "")
    {
        portName = defaultPortName;
    }
    return portName;
}

public static int SetPortBaudRate(int defaultPortBaudRate)
{
    string baudRate;

    Console.Write("Baud Rate({0}): ", defaultPortBaudRate);
    baudRate = Console.ReadLine();

    if (baudRate == "")
    {
        baudRate = defaultPortBaudRate.ToString();
    }

    return int.Parse(baudRate);
}

public static Parity SetPortParity(Parity defaultPortParity)
{
    string parity;

    Console.WriteLine("Available Parity options:");
    foreach (string s in Enum.GetNames(typeof(Parity)))
    {
        Console.WriteLine("   {0}", s);
    }

    Console.Write("Parity({0}):", defaultPortParity.ToString());
    parity = Console.ReadLine();

    if (parity == "")
    {
        parity = defaultPortParity.ToString();
    }

    return (Parity)Enum.Parse(typeof(Parity), parity);
}

public static int SetPortDataBits(int defaultPortDataBits)
{
    string dataBits;

    Console.Write("Data Bits({0}): ", defaultPortDataBits);
    dataBits = Console.ReadLine();

    if (dataBits == "")
    {
        dataBits = defaultPortDataBits.ToString();
    }

    return int.Parse(dataBits);
}

public static StopBits SetPortStopBits(StopBits defaultPortStopBits)
{
    string stopBits;

    Console.WriteLine("Available Stop Bits options:");
    foreach (string s in Enum.GetNames(typeof(StopBits)))
    {
        Console.WriteLine("   {0}", s);
    }

    Console.Write("Stop Bits({0}):", defaultPortStopBits.ToString());
    stopBits = Console.ReadLine();

    if (stopBits == "")
    {
        stopBits = defaultPortStopBits.ToString();
    }

    return (StopBits)Enum.Parse(typeof(StopBits), stopBits);
}

public static Handshake SetPortHandshake(Handshake defaultPortHandshake)
{
    string handshake;

    Console.WriteLine("Available Handshake options:");
    foreach (string s in Enum.GetNames(typeof(Handshake)))
    {
        Console.WriteLine("   {0}", s);
    }

    Console.Write("Stop Bits({0}):", defaultPortHandshake.ToString());
    handshake = Console.ReadLine();

    if (handshake == "")
    {
        handshake = defaultPortHandshake.ToString();
    }

    return (Handshake)Enum.Parse(typeof(Handshake), handshake);
}
}
4

0 回答 0