开发者

Serial port event handler creates an object which is NUll in main code

开发者 https://www.devze.com 2023-04-11 11:01 出处:网络
I am trying to write a simple serial port data logging app which monitors the serial port and writes the data to a file.

I am trying to write a simple serial port data logging app which monitors the serial port and writes the data to a file. If there is a long gap between the data the the file is closed and a new one started. I use a timer to determine when to close the file. and the serial event handler to open the file.

The problem is that when the file is closed the file object seems to be null; The file itself is created by another object as it does other stuff deleted from my example code.

For some reason the object created by the serial event handler is valid but the file within the object is null.

I am a very experienced C and linux kernal programmer but new to C# so the syntax and object nature still catches me out but I cannot see any reason why this should not work unless I am losing an object context somewhere. That might be a problem as there are four serial port ob开发者_StackOverflow社区jects open at the same time writing to four different files.

I already solved my timer problem which wouldn't work when called from the serial handler by chaning from a FOrms.timer to Timers.timer. so I am wondering if its a threading issue.

Any light you can shed would be welcome.

class CommGroup
{
    private SerialPort _comPort;
    private System.Timers.Timer myTimer;
    private ResultsLog logFile = null;

    public CommGroup()
    {
        _comPort = new SerialPort(string name);
        _comPort.PortName = name;
        _comPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
        _comPort.Open();

         myTimer = new System.Timers.Timer();
         myTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimerEventProcessor);
    }

    private void TimerEventProcessor(Object myObject,
                                            EventArgs myEventArgs)
    {
        System.Timers.Timer timer = (System.Timers.Timer) myObject ;
        timer.Stop();

        if (logFile != null)
        {
            logFile.Close(); /* this fails due to null object */
        }
   }

    private void DataReceivedHandler(object sender,SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        if (myTimer.Enabled == false)
        {
            myTimer.Interval = 5000;
            myTimer.Start();
        }
        else
        {
            myTimer.Stop();
            myTimer.Interval = 5000;
            myTimer.Start();
        }

        if (logFile == null)
        {
            logFile = new ResultsLog("Filename");
        }
        logFile.LogResult(sp.ReadExisting());
    }
}

public class ResultsLog
{
    private StreamWriter resultFile;

    public ResultsLog(string filename)
    {
            StreamWriter resultFile = new StreamWriter(filename, true);
    }

    public void Close(string errorname)
    {
        if (this.resultFile != null)
        {
            resultFile.Flush();
            resultFile.Close();
        }
        else
        {
            MessageBox.Show("File NULL Error " + errorname, "File Close");
        }
    }

    public void LogResult(string result)
    {
        if (resultFile != null)
        {
            resultFile.Write(result);
        }
    }
}


Is your resultFile the troublesome null?

You are re-declaring locally in your ResultsLog constructor. Should be:

public ResultsLog(string filename)
{
        resultFile = new StreamWriter(filename, true);
}


Definately could be threading because you have multiple threads accessing the log object without syncronization.

Use lock around your accesses to the varibale and see if it helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消