简体   繁体   中英

passing arguments to ElapsedEventHandler C#

This program is designed to open two COM ports and send data from one to the other ever 1 second. The user inputs which port transmits and which port recieves. The problem I am having is with the ElapsedEventHandler() takes in the OnTimedEvent() function which has two default arguments. I want the OnTimedEvent() function to write something to the send port and read it in on the recieve port, then display it to the console. Obviously my code will not work the way I have it now because the ports and message are not in the scope of OnTimedEvent(). What can I do to make that function work the way I want? Thanks in advance.

using System;
using System.IO.Ports;
using System.Timers;

public class serial_test1
{
    public static void Main(string[] args)
    {
        string sender;
        string recver;
        string buff_out;
        string message;
        SerialPort send_port;
        SerialPort recv_port;

        if (args.Length == 1)
        {
            sender = "UART";
            recver = "USB";
            message = args[0];
        }
        else if (args.Length == 2)
        {
            sender = args[0];
            message = args[1];
            if (sender == "USB")
            {
                recver = "UART";
            }
            else
            {
                recver = "USB";
            }
        }
        else
        {
            sender = "UART";
            recver = "USB";
            message = "TEST MESSAGE";
        }


        int baud = 115200;
        int data_bits = 8;
        Parity parity = Parity.None;
        StopBits stop_bits = StopBits.One;

        buff_out = message;

        SerialPort UARTport = new SerialPort("COM1", baud, parity, data_bits, stop_bits);
        SerialPort USBport = new SerialPort("COM7", baud, parity, data_bits, stop_bits);

        UARTport.Open();
        USBport.Open();


        if (sender == "USB")
        {
            send_port = USBport;
            recv_port = UARTport;
        }
        if (sender == "UART")
        {
            send_port = UARTport;
            recv_port = USBport;
        }

        string header = "from " + sender + " port to " + recver + " port";
        Console.WriteLine(header);

        Timer aTimer = new System.Timers.Timer();
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        // Set the Interval to 1 second.
        aTimer.Interval = 1000;
        aTimer.Enabled = true;

        UARTport.Close();
        USBport.Close();
        Console.ReadLine();
    }

    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        string buff_in;
        send_port.WriteLine(buff_out);
        buff_in = recv_port.ReadLine();
        Console.WriteLine(buff_in);
    }
}

我建议改用System.Threading.Timer ,您可以将状态对象传递到构造函数中,该构造函数传递给TimerCallback

You can use one of these options:

01 - Simply use an auxiliar object

static string _value;
static void MyElapsedMethod(object sender, ElapsedEventArgs e)
{
    Console.WriteLine(_value);
}

02 - Or use a closed class

class CustomTimer : System.Timers.Timer
{
    public string Data;
}

private void StartTimer()
{
    var timer = new CustomTimer
    {
        Interval = 3000,
        Data = "Foo Bar"
    };

    timer.Elapsed += timer_Elapsed;
    timer.Start();
}

void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    string data = ((CustomTimer)sender).Data;
}

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