1

该程序旨在打开两个 COM 端口并每隔 1 秒将数据从一个端口发送到另一个端口。用户输入哪个端口发送和哪个端口接收。我遇到的问题是 ElapsedEventHandler() 接受了具有两个默认参数的 OnTimedEvent() 函数。我希望 OnTimedEvent() 函数向发送端口写入一些内容并在接收端口上读取它,然后将其显示到控制台。显然我的代码不会像现在这样工作,因为端口和消息不在 OnTimedEvent() 的范围内。我该怎么做才能使该功能按我想要的方式工作?提前致谢。

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);
    }
}
4

2 回答 2

2

我建议System.Threading.Timer改为使用,您可以将状态对象传递给传递给TimerCallback.

于 2013-04-19T20:16:10.587 回答
0

您可以使用以下选项之一:

01 - 只需使用辅助对象

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

02 - 或使用封闭课程

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;
}
于 2021-11-24T16:45:10.913 回答