简体   繁体   中英

C# Serial Port Threading, application crashes after some time

I'm writing a program that reads in data from COM port and shows on screen. When data is read less frequently, it works perfectly fine and can run all day. However, when data is sending in every second, the program crashes every 30 min. The error code is c0000005 access violation.

     void Start()
     {
     _serialPort = new SerialPort(TempPortName, 9600, Parity.None, 8, StopBits.One);        
    _serialPort.Handshake = Handshake.None;
     _serialPort.ReadTimeout = 1000;
     _serialPort.ReadBufferSize = 100000;
    readThread = new Thread(Read);
     readThread.Start();        
     }

void Read()
    {
    string MessageSection = "";
    char[] end = {'\r'};    

    //string MessageSection = "";
    while (readData)
    {           
    Thread.Sleep(10);
        if (_serialPort.IsOpen)
        {

            string message = "test"; 

         try
                {
                    message = ((char)(_serialPort.ReadByte())).ToString();

                }
                catch
                {
                    message = "";
                }

                if (message != "")
                {

                    message = MessageSection + message; 

                    string[] MessageArray = message.Split(end);
                    if (!message.EndsWith("\r"))
                    {
                        MessageSection = MessageArray[MessageArray.Length - 1];                    
                    }
                    else
                    {
        if(message.Length>0)
        {
            doSomething();
        }
                        MessageSection = "";
                    }
                }
        else {
        }

        }
        else
        {
            try
            {
                _serialPort.Open();

            }

            catch
            {
                print("Error"); 
            }
        }
    }

    }

I'm using Unity 3D, is it about buffersize?

It's not the decent way you are doing the Serial Port communication using the while loop . You are register SerialPort.DataReceived Event to receive in-comming data on the port.

Here is the sample code from MSDN

using System;
using System.IO.Ports;

class PortDataReceived
{
    public static void Main()
    {
        SerialPort mySerialPort = new SerialPort("COM1");

        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;

        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

        mySerialPort.Open();

        Console.WriteLine("Press any key to continue...");
        Console.WriteLine();
        Console.ReadKey();
        mySerialPort.Close();
    }

    private static void DataReceivedHandler(
                        object sender,
                        SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();
        Console.WriteLine("Data Received:");
        Console.Write(indata);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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