gpt4 book ai didi

C# SerialPort DataReceived 事件未更新主 UI

转载 作者:行者123 更新时间:2023-12-03 13:20:58 30 4
gpt4 key购买 nike

我目前正在研究一个完全用 c# 编写的防盗报警解决方案。该程序与基于 USB 串行端口的 IO 板通信,该 IO 板具有硬连线到其中的警报传感器。我遇到了一个问题,即 DataReceived 事件无法更新主 UI,除非我从 DataReceived 事件中调用一个名为 TextLog 的子(见末尾)。奇怪的是,来自查询区域 1 的 DataReceived 事件能够更新主 UI,但不能更新区域 2 或 3。此外,如果我在执行串行端口写入的行插入断点,它会按预期工作。

值得一提的是这些全局变量:

string ioCardRxString = "";

bool[] arrGlobalZoneStatus = new bool[4];

通过从设置文件中读取设置来打开串行端口(一切正常)。
private void OpenIOComPort()
{
bool error = false;

else
{
// Set the port's settings
spIOCard.PortName = Settings1.Default.ioComPort;
spIOCard.BaudRate = int.Parse(Settings1.Default.ioBaudRate);
spIOCard.DataBits = int.Parse(Settings1.Default.ioDataBits);
spIOCard.StopBits = (System.IO.Ports.StopBits)Enum.Parse(typeof(System.IO.Ports.StopBits), Settings1.Default.ioStopBits);
spIOCard.Parity = (System.IO.Ports.Parity)Enum.Parse(typeof(System.IO.Ports.Parity), Settings1.Default.ioParity);
spIOCard.Handshake = (System.IO.Ports.Handshake)Enum.Parse(typeof(System.IO.Ports.Handshake), Settings1.Default.ioHandshake);

try
{
// Open the port
spIOCard.Open();
}
catch (UnauthorizedAccessException) { error = true; }
catch (System.IO.IOException) { error = true; }
catch (ArgumentException) { error = true; }

//On error, advise the user
if (error)
{
MessageBox.Show("Could not open the I/O Board COM port.");
globalIOCardError = true;
}

if (!error)
{
globalIOCardError = false;
// Do Nothing
}

}
}

定时器每 500 毫秒运行一次,它向串行端口写入 3 个命令,每个命令相隔 25 毫秒(硬件限制)。这些命令查询 IO 板以确定每个警报传感器的状态。
private void tmrAuditSensors_Tick(object sender, EventArgs e) 
{
try
{
if (globalIOCardError == false && Settings1.Default.disableSensorAudit == false)
{
if (Settings1.Default.zone1Armed == true)
{
spIOCard.Write("~in01~");
System.Threading.Thread.Sleep(25);;
}

if (Settings1.Default.zone2Armed == true)
{
spIOCard.Write("~in02~");
System.Threading.Thread.Sleep(25);;
}

if (Settings1.Default.zone3Armed == true)
{
spIOCard.Write("~in03~");
System.Threading.Thread.Sleep(25);;
}

//Applicable results will appear on serial data received event
}
}
catch
{
textLog("There was a problem writing to the serial port, check and restart app");
emergencyHalt();
}
}

Serial Port DataReceived 事件读取返回的字符串并写入全局 bool 数组。如果该区域打开 (=1),则为 True,如果该区域关闭 (=0),则为 False。注意:星号线。
private void spIO_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
ioCardRxString = spIOCard.ReadExisting();
textLog(ioCardRxString); //*Cannot make it work without this

if (ioCardRxString.Contains("in05=1") == true)
{
arrGlobalZoneStatus[1] = true;
}

if (ioCardRxString.Contains("in05=0") == true)
{
arrGlobalZoneStatus[1] = false;
}

if (ioCardRxString.Contains("in01=1") == true)
{
arrGlobalZoneStatus[2] = true;
}

if (ioCardRxString.Contains("in01=0") == true)
{
arrGlobalZoneStatus[2] = false;
}

if (ioCardRxString.Contains("in17=1") == true)
{
arrGlobalZoneStatus[3] = true;
}

if (ioCardRxString.Contains("in17=0") == true)
{
arrGlobalZoneStatus[3] = false;
}

}

另外,另一个计时器会定期(每 250 毫秒)检查每个数组成员的内容,然后相应地通过一些颜色更改和文本更新来更新主 UI。
 private void tmrCheckZoneStatus_Tick(object sender, EventArgs e)
{
if (arrGlobalZoneStatus[1] == true)
{
button10.BackColor = System.Drawing.Color.Red;
textLog(button10.Text + " was activated");

if (globalFullAlarmSet || globalNightAlarmSet || globalDoorsAlarmSet)
{
this.BeginInvoke(new EventHandler(delegate { checkAndActivateRelays(1); }));
}
}

if (arrGlobalZoneStatus[1] == false)
{
button10.BackColor = System.Drawing.Color.Gray;
}

if (arrGlobalZoneStatus[2] == true)
{
button11.BackColor = System.Drawing.Color.Red;
textLog(button11.Text + " was activated"); ;

if (globalFullAlarmSet || globalNightAlarmSet || globalDoorsAlarmSet)
{
this.BeginInvoke(new EventHandler(delegate { checkAndActivateRelays(2); }));
}
}

if (arrGlobalZoneStatus[2] == false)
{
button11.BackColor = System.Drawing.Color.Gray;
}

if (arrGlobalZoneStatus[3] == true)
{
button12.BackColor = System.Drawing.Color.Red;
textLog(button12.Text + " was activated");

if (globalFullAlarmSet || globalNightAlarmSet || globalDoorsAlarmSet)
{
this.BeginInvoke(new EventHandler(delegate { checkAndActivateRelays(3); }));
}
}

if (arrGlobalZoneStatus[3] == false)
{
button12.BackColor = System.Drawing.Color.Gray;
}

}

文本日志子:
public void textLog(string logEntry)
{
textLines++;
try
{
if (this.txtLog.InvokeRequired)
{
ChangeTextCallback MethodCallback = new ChangeTextCallback(textLog);
this.Invoke(MethodCallback, new object[] { logEntry });
}
else
{
if (!logEntry.Contains("?"))
{
txtLog.Text = txtLog.Text + DateTime.Now + " >: " + logEntry + "\r\n";
txtLog.SelectionStart = txtLog.Text.Length;
txtLog.ScrollToCaret();

if (textLines > 3000)
{
txtLog.Clear();
textLines = 0;
textLog("Text log cleared");
}

System.IO.StreamWriter sw = new System.IO.StreamWriter(logFile, true);
try
{
sw.WriteLine(DateTime.Now + " >: " + logEntry);
}
catch (Exception ex)
{
//
}

sw.Close();
}
}
}
catch
{
//
}


}

我想我需要在某个地方合并调用/委托(delegate),但作为一个菜鸟,我有点摸不着头脑。您的帮助将不胜感激。

谢谢

最佳答案

我没有通读所有内容,但这应该会让您走上正确的道路:

SerialPort.DataReceived 事件在单独的线程上引发。如果您需要异步处理数据的接收,并在其上使用 GUI 执行某些操作,您可以执行以下操作:

private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) {
var port = (SerialPort)sender;
string data = port.ReadExisting();

UpdateGui(data);
}

private void UpdateGui(string data) {
if (this.InvokeRequired) {
this.Invoke(new Action( d => UpdateGui(d) ));
return;
}

this.txtBox1.Text = data;
}

现在,这就是说....你真的想使用 DataReceived吗? ?听起来您(主机)正在启动与外部板的所有通信。如果是这种情况,那么我建议您改用同步(阻塞)读取:

1. Write the request out the port
2. Call read() with the expected number of bytes
3. Process the reply.

关于C# SerialPort DataReceived 事件未更新主 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12240205/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com