当信号从1和0不断变化的机器发出时,如何保持警报保持红色?

发布于 2025-01-07 08:47:32 字数 620 浏览 0 评论 0原文

我有一个代码,用于指示机器发出的警报,当机器遇到问题时,机器的警报会闪烁以显示...

当警报闪烁时,输出将在1和0之间不断变化当然我的视觉C#会写一个if else语句来显示闹钟响了,如果alarm=1,那么闹钟图片会变成红色,否则alarm=0,那么闹钟图片会变成绿色。

但是当接收到来自机器的信号时,它在1和0之间闪烁,并且我在视觉c#中的警报图片也会在红色和绿色之间不断变化,我该怎么办,这样当警报闪烁时,我的警报图片在视觉上c# 一直显示红色?因为一旦警报响起,我也需要存储在数据库中。 这是我的 if else 语句...

if (bn4 == "1" || bn4 == "0")
{   
    if ((cmdstop4 == 1) || bn4 == "0")
    {
        alarm.Stop();
        pictureBox6.Show();
        pictureBox14.Hide();
        bn4 = "0";

    }
    if (bn4 == "1")
    {
        alarm.PlayLooping();
        pictureBox14.Show();
        pictureBox6.Hide();
    }
}

i have a code that using to indicate the alarm from a machine, and the alarm of the machine will be flash to show when the machine facing some problem...

when it flashing the alarm, the output will be keep changing between 1 and 0. and of course my visual C# will write a if else statement to show that the alarm is ringing, if alarm=1, then alarm picture will become red, else alarm=0, then alarm picture will become green.

but when receive the signal from the machine was flash between 1 and 0, and my alarm picture at the visual c# will also keep changing between red and green, what could i do so that when the alarm is flashing, and my alarm picture in visual c# just keep showing red? because i need to store in the database too once alarm is sound.
here is my if else statement...

if (bn4 == "1" || bn4 == "0")
{   
    if ((cmdstop4 == 1) || bn4 == "0")
    {
        alarm.Stop();
        pictureBox6.Show();
        pictureBox14.Hide();
        bn4 = "0";

    }
    if (bn4 == "1")
    {
        alarm.PlayLooping();
        pictureBox14.Show();
        pictureBox6.Hide();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安稳善良 2025-01-14 08:47:32

您可以使用哨兵值来确定之前是否已触发警报。它不需要很复杂,只需将一个 int 设置为 0(如果警报关闭)和 1(如果警报已触发)。

// this sentinal should be declared somewhere useful, in the alarm would make most sense 
int alarmTriggered = 0; 

if (bn4 == "1" || bn4 == "0") 
{    
    if ((cmdstop4 == 1) || (alarmTriggered == 0 && bn4 == "0")) 
    { 
        alarmTriggered = 0;
        alarm.Stop(); 
        pictureBox6.Show(); 
        pictureBox14.Hide(); 
        bn4 = "0"; 

    } 
    if (bn4 == "1" || alarmTriggered == 1) 
    { 
        alarmTriggered = 1;
        alarm.PlayLooping(); 
        pictureBox14.Show(); 
        pictureBox6.Hide(); 
    } 
} 

You could use a sentinal value to determine if the alarm has been previously triggered. It doesn't need to be complex, just an int set to 0 if the alarm is off and 1 if the alarm has been triggered.

// this sentinal should be declared somewhere useful, in the alarm would make most sense 
int alarmTriggered = 0; 

if (bn4 == "1" || bn4 == "0") 
{    
    if ((cmdstop4 == 1) || (alarmTriggered == 0 && bn4 == "0")) 
    { 
        alarmTriggered = 0;
        alarm.Stop(); 
        pictureBox6.Show(); 
        pictureBox14.Hide(); 
        bn4 = "0"; 

    } 
    if (bn4 == "1" || alarmTriggered == 1) 
    { 
        alarmTriggered = 1;
        alarm.PlayLooping(); 
        pictureBox14.Show(); 
        pictureBox6.Hide(); 
    } 
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文