繁体   English   中英

在C#中使用SerialPort的System.TimeoutException

[英]System.TimeoutException using SerialPort in C#

我有一个将命令发送到串行端口列表并在RichTextBox中显示返回值的系统。 当我有10个串行端口时,该程序运行良好,现在我正在使用约60个串行端口,它返回以下错误:

Problem signature:
Problem Event Name: CLR20r3
Problem Signature 01:   jporttestot.exe
Problem Signature 02:   1.0.0.0
Problem Signature 03:   51db05c7
Problem Signature 04:   System
Problem Signature 05:   4.0.0.0
Problem Signature 06:   4ba1dff4
Problem Signature 07:   3140
Problem Signature 08:   b3
Problem Signature 09:   System.TimeoutException
OS Version: 6.1.7601.2.1.0.256.1
Locale ID:  1046
Additional Information 1:   0a9e
Additional Information 2:   0a9e372d3b4ad19135b953a78882e789
Additional Information 3:   0a9e
Additional Information 4:   0a9e372d3b4ad19135b953a78882e789
Read our privacy statement online:
http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409
If the online privacy statement is not available, please read our privacy statement offline:
C:\Windows\system32\en-US\erofflps.txt

下面是我的代码:

 public partial class Form1 : Form
        {
            List<SerialPort> serialPort = new List<SerialPort>();


            private delegate void SetTextDeleg(string text);

            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                int baudRate = Convert.ToInt32(txtBaudRate.Text);
                var portNames = SerialPort.GetPortNames();
                foreach (var port in portNames)
                {
                    SerialPort sp;
                    sp = new SerialPort(port, baudRate, Parity.None, 8, StopBits.One);
                    sp.Handshake = Handshake.None;
                    sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
                    sp.ReadTimeout = 500;
                    sp.WriteTimeout = 500;

                    serialPort.Add(sp);
                    listPorts.Items.Add(port);
                }
            }

            private void listPorts_SelectedIndexChanged(object sender, EventArgs e)
            {

            }

            private void button1_Click_1(object sender, EventArgs e)
            {
                boxLogs.Text = "";
                String comando = txtComando.Text.Trim();

                foreach (var sp in serialPort)
                {
                    // Open port
                    try
                    {
                        if (!sp.IsOpen)
                            sp.Open();

                        //sp.Write("at\r\n");
                        sp.Write("at\r\n");
                    }
                    catch (Exception ex)
                    {
                        boxLogs.Text = "Erro ao abrir/escrever na porta serial :: " + ex.Message +"\n";
                    }
                }
            }

            void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                Thread.Sleep(500);
                SerialPort sp = (SerialPort)sender;
                string data = sp.ReadLine();
                data = sp.ReadLine();
                this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { sp.PortName + " - Retorno: " + data });
            }

            private void si_DataReceived(string data)
            {
                String retorno = data.Trim();
                boxLogs.Text += retorno + "\n";
            }

            private void txtComando_TextChanged(object sender, EventArgs e)
            {

            }

            private void txtBaudRate_TextChanged(object sender, EventArgs e)
            {

            }

        }

当您有太多SerialPort对象时,您在其上调用Write()时端口可能仍未打开。 系统需要一些时间才能实际完成操作。 在Open()之后尝试Thread.Sleep()一段时间,看看会发生什么(尽管您的UI会冻结)。 也许是这样吗?

 if (!sp.IsOpen)  
{
    sp.Open();
    Thread.Sleep(200);
    sp.Write("at\r\n");  
}

顺便说一句,您在哪里调用Close()? MSDN指出:

“对于任何应用程序,最佳实践都是在调用Close方法之后等待一段时间,然后再尝试调用Open方法,因为端口可能不会立即关闭。”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM