System.Timers.Timer 用法 [英] System.Timers.Timer Usage

查看:108
本文介绍了System.Timers.Timer 用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了这个问题的目的,我完整地包括了我的一类:

For the purposes of this question I'm including a class of mine in its entirety:

public class SerialPortConnection
{
    private SerialPort serialPort;
    private string ping;
    double failOut;
    bool isReceiving;

    public SerialPortConnection(string comPort = "Com1", int baud = 9600, System.IO.Ports.Parity parity = System.IO.Ports.Parity.None, int dataBits = 8, System.IO.Ports.StopBits stopBits = System.IO.Ports.StopBits.One, string ping = "*IDN?", double failOut = 2)
    {
        this.ping = ping;
        this.failOut = failOut * 1000;

        try
        {
            serialPort = new SerialPort(comPort, baud, parity, dataBits, stopBits);
        }
        catch (Exception e)
        {
            throw e;
        }
    }

    //Open Serial Connection. Returns False If Unable To Open.
    public bool OpenSerialConnection()
    {
        //Opens Initial Connection:
        try
        {
            serialPort.Open();
            serialPort.Write("REMOTE\r");
        }
        catch (Exception e)
        {
            throw e;
        }

        serialPort.Write(ping + "\r");
        var testReceived = "";
        isReceiving = true;

        Timer StopWatch = new Timer(failOut);
        StopWatch.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        StopWatch.Interval = failOut;
        StopWatch.Enabled = true;

        while (isReceiving == true)
        {
            try
            {
                testReceived += serialPort.ReadExisting();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        StopWatch.Dispose();

        if (testReceived.Contains('>'))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public string WriteSerialConnection(string SerialCommand)
    {
        try
        {
            serialPort.Write(String.Format(SerialCommand + "\r"));
            var received = "";
            bool isReceiving = true;

            Timer StopWatch = new Timer(failOut);
            StopWatch.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            StopWatch.Interval = failOut;
            StopWatch.Enabled = true;

            while (isReceiving == true)
            {
                try
                {
                    received += serialPort.ReadExisting();
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            if (received.Contains('>'))
            {
                return received;
            }
            else
            {
                received = "Error: No Data Received From Device";
                return received;
            }

            StopWatch.Dispose();
        }
        catch (Exception e)
        {
            throw e;
        }        
    }

    //Closes Serial Connection. Returns False If Unable To Close.
    public bool CloseSerialConnection()
    {
        try
        {
            serialPort.Write("LOCAL\r");
            serialPort.Close();
            return true;
        }
        catch (Exception e)
        {
            throw e;
        }
    }

    private void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        isReceiving = false;
    }
}

我在这里尝试做的是让循环运行一段设定的时间(在这种情况下为两秒),因为连接到我正在使用的串行端口的设备是不可预测的.我不知道我会从中收到什么数据,也不知道需要多长时间.这是无法修复的,这是我必须处理的事情.目前,我最好的选择是等待一段时间并检查我收到的数据以获取结束令牌 (">").我什至在课堂上也试过像这样连接计时器:

What I'm attempting to do here is keep a loop running for a set amount of time (two seconds in this case) because the device connected to the serial port I'm working with is unpredictable. I don't know what data I will receive from it and I don't know how long it will take. That can't be fixed and is something I have to work with. My best option, currently, is to wait a set amount of time and check the data I've received for an end token (">"). I've tried wiring up a timer even in the class like so:

Timer StopWatch = new Timer(failOut * 1000);
StopWatch.Elapsed += new ElapsedEventHandler(OnTimedEvent);
StopWatch.Interval = failOut;
StopWatch.Enabled = true;

但它似乎不起作用.事件本身看起来像这样:

But it doesn't appear to work. The event itself looks like so:

private void OnTimedEvent(object source, ElapsedEventArgs e)
{
    isReceiving = false;
}

我的目标是切断与 isReceiving 相关的循环:

My objective is to cut the loop isReceiving is tied to:

(while isReceiving == true)
{
    //Do Something
}

但它似乎不起作用.我想我完全误解了计时器的功能,但我之前有过实现它的建议.我究竟做错了什么?如果我只是完全滥用它,我可以用什么来代替计时器?正如我所说,我别无选择,只能等待一段时间并检查我收到的内容.除了等待并希望我得到一些东西之外,无法以任何方式避免或处理这种情况.

But it doesn't appear to work. I assume I've completely misunderstood the function of the timer but I've had suggestions before to implement it. What am I doing wrong? If I'm just completely misusing it, what can I use instead of a timer? As I've said, I've no choice but to wait a set amount of time and check what I've received. That can't be avoided or handled in any way other than waiting and hoping I get something.

也许我最好澄清一下.OnTimedEvent 事件正在触发,变量设置为 false,但它不会切断循环,因为 isReceiving 未设置为 false.

Maybe it's best I clarify this. The OnTimedEvent event is firing and the variable is set to false but it doesn't cut the loop as isReceiving isn't getting set to false.

编辑 2:

先生Passant 的回答非常有效,除非我遇到了一个奇怪的错误.由于我不认为这是他的答案中的问题,更有可能是硬件缺陷,或者其他一些奇怪和晦涩的东西,我将他的答案标记为已接受.我建议任何选择实施他的答案的人也可以查看我在此处提交的问题:

Mr. Passant's answer works beautifully barring a strange error I'm encountering. As I don't believe it's a problem within his answer, it's more likely that it's a hardware flaw, or something else strange and obscure along those lines, I'm leaving his answer marked as accepted. I recommend anyone that chooses to implement his answer also view the question I have submitted here:

表观IO.Ports.C# 中的串行端口缺陷或可能的硬件缺陷

推荐答案

你让自己太难了.只需将 SerialPort.NewLine 属性更改为>".并使用 SerialPort.ReadLine() 读取响应.如果需要,您仍然可以使用超时,分配 SerialPort.ReadTimeout 属性并准备捕获 TimeoutException.

You are making it too difficult on yourself. Simply change the SerialPort.NewLine property to ">". And use SerialPort.ReadLine() to read the response. You can still use a timeout if you need it, assign the SerialPort.ReadTimeout property and be prepared to catch the TimeoutException.

这篇关于System.Timers.Timer 用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆