我正在使用 WPF 应用程序进行串行通信,其中通过在主窗口(GUI)中获取用户的规范来创建串行端口,并将端口作为参数发送到后台工作人员。我的问题是我的主窗口线程中有一个端口的 datareceived 事件,用于从串行端口读取样本数据并在 BG 线程中进行连续读取。当使用我作为参数发送给 BG 工作人员的端口时,我应该定义一个新的 datareceived 事件还是同样的一个工作?
private void SerialThread_DoWork(object Sender, DoWorkEventArgs e )
{
BGargs args = e.Argument as BGargs;
SerialPort BGport = args.PORT;
string MODE = args.MODE;
string filePath = args.filepath;
BGport.DataReceived +=new SerialDataReceivedEventHandler(BGport_DataReceived);
Dispatcher.BeginInvoke((Action)delegate() { run_button.IsEnabled = false; });
switch (MODE)
{
case "EXT_trigger":
while (SerialThread.CancellationPending)
{
FileStream file = new FileStream(filePath, FileMode.Append, FileAccess.Write);
using (StreamWriter Writer = new StreamWriter(file))
{
//code to continuously trigger and read data and then write to file
}
}
break;
}
}