C# 使用猫拨打电话

这篇博客介绍了如何在C#中实现电话拨号功能,通过简单的UI设计,包括一个文本框和启动按钮,结合CommPort.cs类,实现拨号操作。内容来源于网络并进行了精简,适用于辅助拨打电话的需求。
摘要由CSDN通过智能技术生成

主窗口一个textbox与btnstart按钮

代码是使用别人!只是去掉部分不用的!只用于拨号!用于辅助打电话!

form1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Drawing;

using System.Threading;



namespace DialUp
{
    public partial class Form1 : Form
    {
        static String str11;
        public Form1()
        {
            InitializeComponent();
        }
    

        private void button1_Click(object sender, EventArgs e)
        {
            if (btnStart.Text == "开始")
            {

                str11 = textBox1.Text;
                btnStart.Text = "停止";
                timer2.Enabled = true;

                int ModemCount = int.Parse(GetConfig("ComCount"));
                for (int i = 0; i < ModemCount; i++)
                {
                    queue.Enqueue(i + 1);
                }
            }
            else
            {
                btnStart.Text = "开始";
                timer2.Enabled = false;

                while (queueThread.Count > 0)
                {
                    try
                    {
                        Thread t = queueThread.Dequeue();
                        t.Join();
                        t.Abort();
                    }
                    catch { }
                }
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (queue.Count > 0)
            {
                Thread t = new Thread(Dial);
                t.Start();

                queueThread.Enqueue(t);
            }
        }
        /// <summary>任务队列</summary>               
        private static System.Collections.Generic.Queue<int> queue = new Queue<int>();
        /// <summary>线程队列 </summary>
        private static System.Collections.Generic.Queue<Thread> queueThread = new Queue<Thread>();
        private static System.Text.StringBuilder sbMsg = new StringBuilder();
        private static void Dial()
        {
            int index = queue.Dequeue();
            sbMsg.Insert(0, "启动端口" + index + "\r\n");

            try
            {

                CommPort port = new CommPort();
                port.PortNum = GetConfig(string.Format("Com_{0}_PortNum", index));//端口名称
                port.BaudRate = int.Parse(GetConfig(string.Format("Com_{0}_BaudRate", index)));             //串口通信波特率 ,每秒位数
                port.ByteSize = byte.Parse(GetConfig(string.Format("Com_{0}_ByteSize", index)));            //数据位 
                port.Parity = byte.Parse(GetConfig(string.Format("Com_{0}_Parity", index)));              //奇偶校验 
                port.StopBits = byte.Parse(GetConfig(string.Format("Com_{0}_StopBits", index)));            //停止位 
                port.ReadTimeout = 100;                                 //读超时时间


                if (!port.Opened)
                    port.Open();//打开串口 

                //初始化modem
                string[] strInit = GetConfig(string.Format("Com_{0}_Init", index)).Split('');
                foreach (string str in strInit)
                {
                    if (str == string.Empty) continue;
                    string strData = str + "\r";
                    port.Write(System.Text.Encoding.ASCII.GetBytes(strData));
                    //Thread.Sleep(1000);
                    port.Read(200);
                }

                //拨号 
                //string strDial = GetConfig(string.Format("Com_{0}_Dial", index)) + "\r";
                string strDial = "ATDT" + str11 + "\r";
                int dCount = int.Parse(GetConfig(string.Format("Com_{0}_dCount", index)));
                int wCount = int.Parse(GetConfig(string.Format("Com_{0}_wCount", index)));

                //挂机指令
                string strShutdown = GetConfig(string.Format("Com_{0}_Shutdown", index)) + "\r";

                bool isConntion = false;
                for (int k = 0; k < dCount; k++)
                {
                    //拨号
                    //port.Write(System.Text.Encoding.ASCII.GetBytes(strShutdown));  //拨号前挂机
                    port.Write(System.Text.Encoding.ASCII.GetBytes(strDial));

                    //等待20秒
                    for (int j = 0; j < wCount; j++)
                    {
                        string strRe = System.Text.Encoding.ASCII.GetString(port.Read(1000));

                        sbMsg.Insert(0, "端口" + index + "拨号" + k + "/" + j + "\r\n");


                        //应答有NO DIALTONE字符表示拨号失败 ,busy表示忙音
                        if (strRe.Index
[DllImport("phone.dll")] private static extern IntPtr PhoneMakeCall(ref PhoneMakeCallInfo ppmci); /// /// Dials the specified phone number. /// /// Phone number to dial. public static void MakeCall(string PhoneNumber) { MakeCall(PhoneNumber, false); } /// /// Dials the specified phone number. /// /// Phone number to dial. /// Prompts the user before the call is placed. unsafe public static void MakeCall(string PhoneNumber, bool PromptBeforeCall) { IntPtr res; PhoneNumber += '\0'; char[] cPhoneNumber = PhoneNumber.ToCharArray(); fixed (char* pAddr = cPhoneNumber) { PhoneMakeCallInfo info = new PhoneMakeCallInfo(); info.cbSize = (IntPtr)Marshal.SizeOf(info); info.pszDestAddress = (IntPtr)pAddr; if (PromptBeforeCall) { info.dwFlags = (IntPtr)PMCF_PROMPTBEFORECALLING; } else { info.dwFlags = (IntPtr)PMCF_DEFAULT; } res = PhoneMakeCall(ref info); } } } /// /// Reads information from the Subscriber Identity Module (SIM) /// public class Sim { private static long SERVICE_PROVIDER = 0x00006F46; [StructLayout(LayoutKind.Sequential)] private struct SimRecord { public IntPtr cbSize; public IntPtr dwParams; public IntPtr dwRecordType; public IntPtr dwItemCount; public IntPtr dwSize; } [DllImport("sms.dll")] private static extern IntPtr SmsGetPhoneNumber(IntPtr psmsaAddress); [DllImport("cellcore.dll")] private static extern IntPtr SimInitialize(IntPtr dwFlags, IntPtr lpfnCallBack, IntPtr dwParam, out IntPtr lphSim); [DllImport("cellcore.dll")] private static extern IntPtr SimGetRecordInfo(IntPtr hSim, IntPtr dwAddress, ref SimRecord lpSimRecordInfo); [DllImport("cellcore.dll")] private static extern IntPtr SimReadRecord(IntPtr hSim, IntPtr dwAddress, IntPtr dwRecordType, IntPtr dwIndex, byte[] lpData, IntPtr dwBufferSize, ref IntPtr lpdwBytesRead); [DllImport("cellcore.dll")] private static extern IntPtr SimDeinitialize(IntPtr hSim); /// /// Gets the phone number from the SIM. /// /// PhoneAddress structure with phone number. unsafe public static PhoneAddress GetPhoneNumber() { PhoneAddress phoneaddr = new PhoneAddress(); Byte[] buffer = new Byte[516]; fixed (byte* pAddr = buffer) { IntPtr res = SmsGetPhoneNumber((IntPtr)pAddr); if (res != IntPtr.Zero) throw new Exception("Could not get phone number from SIM"); byte* pCurrent = pAddr; phoneaddr.AddressType = (AddressType)Marshal.ReadInt32((IntPtr)pCurrent); pCurrent += Marshal.SizeOf(phoneaddr.AddressType); phoneaddr.Address = Marshal.PtrToStringUni((IntPtr)pCurrent); } return phoneaddr; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值