Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 串口数据阵列提取_C#_Arraylist_Serial Port - Fatal编程技术网

C# 串口数据阵列提取

C# 串口数据阵列提取,c#,arraylist,serial-port,C#,Arraylist,Serial Port,我知道在c#串行端口的接收和写入方面有很多问题,但我没有在我的案例中找到有用的解决方案。我用c#windows应用程序编写了一个小程序。此应用程序允许用户使用mobile miscall登录。提取从串行端口接收的数据时遇到问题。我想从输出中提取手机号码,但运气不好。下面是我的代码,请告诉我如何从输出中提取手机号码并将其显示在密码字段中。谢谢 SerialPort port = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.On

我知道在c#串行端口的接收和写入方面有很多问题,但我没有在我的案例中找到有用的解决方案。我用c#windows应用程序编写了一个小程序。此应用程序允许用户使用mobile miscall登录。提取从串行端口接收的数据时遇到问题。我想从输出中提取手机号码,但运气不好。下面是我的代码,请告诉我如何从输出中提取手机号码并将其显示在密码字段中。谢谢

    SerialPort port = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);
    private void Login_Load(object sender, EventArgs e)
    {
        port.RtsEnable = true;
        port.DtrEnable = true;
        port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
        if (!port.IsOpen) port.Open();
        port.Write("AT+CLIP=1" + Environment.NewLine);
    }

    delegate void SetTextCallback(string text);

    private void SetText(string text)
    {
        // InvokeRequired required compares the thread ID of the
        // calling thread to the thread ID of the creating thread.
        // If these threads are different, it returns true.
        if (this.txtPassword.InvokeRequired)
        {
            SetTextCallback d = new SetTextCallback(SetText);
            this.Invoke(d, new object[] { text });
        }
        else
        {
            this.txtPassword.Text = text;
        }
    }

    protected void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        var currentPort = (SerialPort)sender;
        string result = currentPort.ReadExisting();
        MessageBox.Show(result);
        //if (!result.Contains("\r\nRING\r\n")) return;
        //currentPort.Write("AT+CHUP" + Environment.NewLine);
        //txtPassword.Text = result.Split('\"')[1];
        //SetText(result.Split('\"')[1]);
        //port.Write("AT+CHUP" + Environment.NewLine);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        LoginUser();
    }
从端口接收的数据如下所示。我想从中提取手机号码。我是C#的新手:

戒指 +剪辑:“032232323423”,161,“0”,“0” 当我试图打印这段代码时:SetText(result.Split(“\”)[1]);
它给我错误[索引超出范围]。

在DataReceived事件中,您可以这样读:

byte[] buffer = new buffer[1024];
int readBytes = port.Read(buffer, 0, port.BytesToRead);
int firstMark = result.IndexOf('\"');
result.SubString(firstMark, result.IndexOf('\"', firstMark + 1) - firstMark);
无论如何,我想你可以得到这样的电话号码:

byte[] buffer = new buffer[1024];
int readBytes = port.Read(buffer, 0, port.BytesToRead);
int firstMark = result.IndexOf('\"');
result.SubString(firstMark, result.IndexOf('\"', firstMark + 1) - firstMark);

使用ReadLine()而不是ReadExisting()。由于字符串中没有``字符,索引超出范围。我尝试了ReadLine()而不是ReadExisting(),但运气不佳。还有其他想法吗?