0

我有一个向串行端口列表发送命令并在 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)
            {

            }

        }
4

1 回答 1

0

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

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

顺便说一句,你在哪里调用 Close()?MSDN 状态:

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

于 2013-07-09T07:14:24.540 回答