C#通过串口模拟交通灯程序

     前阵子,一个同学让我帮他写个单片机控制交通灯的程序,我帮他写了程序之后,改进和完善了他的要求,使得我们可以通过PC端直接模拟这个系统。在完成的过程中,也遇到过不少问题,很多收获,这里写出来分享下。

     本文要实现的目标是与单片机通讯,将我们需要模拟的信息传递给单片机程序;界面上能实时显示交通灯的状况。

  因此,主要有两点,需要重点考虑:一是把界面的信息传递到单片机,一是可以不断更新界面的显示。这里我使用串口发送信息;使用定时器刷新界面内容。如图所示:

    C#通过串口模拟交通灯程序_第1张图片

  程序中首先是串口的使用,在C#中需要包含using System.IO.Ports;

//定义串口
SerialPort sp = null;
//串口的状态
bool isOpen = false;

//全局变量,表示红绿黄灯亮时间
 int  totalTimeg = 40;
 int  totalTimer= 60;
 int  totalTimey= 20;
 int _timeg= 40;
 int _timer= 60;
 int _timey= 20;

//设置串口的属性
sp = new SerialPort();
//设置串口名
sp.PortName = "COM1";
//设置串口的波特率
sp.BaudRate = 9600;
//打开串口
sp.Open();
isOpen = true;

接下来就是发送数据了,使用SerialPort类的成员函数可以实现:

//向串口发送数据
       public void SendCommand(string CommandString)
       {
              byte[] WriteBuffer =Encoding.ASCII.GetBytes(CommandString);
              sp.Write(WriteBuffer, 0, WriteBuffer.Length);
       }

  这样就把信息发给单片机了,CommandString为发送的字符串,这里是以ASCII码发送的。

   然后就是定时器程序,可以进行仿真模拟:

private voidtimer1_Tick(object sender, EventArgs e)
   {
        int nSelect =comboBox7.SelectedIndex + 1;
        switch (nSelect)
        {
            case 1:
               textBox1.Text =_timeg.ToString();
               textBox3.Text =_timeg.ToString();
               pictureBox1.BackColor =Color.Green;
               pictureBox3.BackColor =Color.Green;
               _timeg--;
               textBox2.Text =_timer.ToString();
               pictureBox2.BackColor =Color.Red;
               _timer--;
               textBox4.Visible = false;
               pictureBox4.Visible =false;
               if (_timeg < 0)
               {
                       textBox1.Text =_timey.ToString();
                       textBox3.Text =_timey.ToString();
                       pictureBox1.BackColor = Color.Yellow;
                       pictureBox3.BackColor =Color.Yellow;
                       _timey--;
               }
               if (_timey < 0)
               {
                       textBox1.Text =totalTimer.ToString();
                       textBox3.Text =totalTimer.ToString();
                       pictureBox1.BackColor =Color.Red;
                       pictureBox3.BackColor =Color.Red;
                       totalTimer--;
                       textBox2.Text =totalTimeg.ToString();
                       pictureBox2.BackColor =Color.Green;
                       totalTimeg--;
               }
               if (totalTimeg < 0)
               {
                       textBox1.Text =totalTimer.ToString();
                       textBox3.Text =totalTimer.ToString();
                       pictureBox1.BackColor =Color.Red;
                       pictureBox3.BackColor =Color.Red;
                       totalTimer--;
                       textBox2.Text =totalTimey.ToString();
                       pictureBox2.BackColor =Color.Yellow;
                       totalTimey--;
              }
              if (totalTimey < 0)
              {
                       totalTimeg = 40;
                       totalTimer = 60;
                       totalTimey = 20;
                       _timeg = 40;
                       _timer = 60;
                       _timey = 20;
             }
             break;
         case 2:
                   textBox1.Text =_timeg.ToString();
                   pictureBox1.BackColor =Color.Green;
                   _timeg--;
                   textBox2.Text = _timer.ToString();
                   pictureBox2.BackColor =Color.Red;
                   _timer--;
                   textBox3.Visible = false;
                   pictureBox3.Visible =false;
                   textBox4.Visible = false;
                   pictureBox4.Visible =false;
                   if (_timeg < 0)
                   {
                       textBox1.Text =_timey.ToString();
                       pictureBox1.BackColor =Color.Yellow;
                       _timey--;

                   }
                   if (_timey < 0)
                   {
                       textBox1.Text =totalTimer.ToString();
                       pictureBox1.BackColor =Color.Red;
                       totalTimer--;
                       textBox2.Text =totalTimeg.ToString();
                       pictureBox2.BackColor =Color.Green;
                       totalTimeg--;
                   }
                   if (totalTimeg < 0)
                   {
                       textBox1.Text = totalTimer.ToString();
                       pictureBox1.BackColor =Color.Red;
                       totalTimer--;
                       textBox2.Text =totalTimey.ToString();
                       pictureBox2.BackColor =Color.Yellow;
                       totalTimey--;
                   }
                   if (totalTimey < 0)
                   {
                       totalTimeg = 40;
                       totalTimer = 60;
                       totalTimey = 20;
                       _timeg = 40;
                       _timer = 60;
                       _timey = 20;
                   }
                   break;
         case 3:
                  textBox1.Text =_timeg.ToString();
                   textBox3.Text = _timeg.ToString();
                   pictureBox1.BackColor =Color.Green;
                   pictureBox3.BackColor =Color.Green;
                   _timeg--;
                   textBox2.Text =_timer.ToString();
                   textBox4.Text =_timer.ToString();
                   pictureBox2.BackColor =Color.Red;
                   pictureBox4.BackColor =Color.Red;
                   _timer--;
                   if (_timeg < 0)
                   {
                       textBox1.Text = _timey.ToString();
                       textBox3.Text =_timey.ToString();
                       pictureBox1.BackColor =Color.Yellow;
                       pictureBox3.BackColor =Color.Yellow;
                       _timey--;
                   }
                   if (_timey < 0)
                   {
                       textBox1.Text =totalTimer.ToString();
                       textBox3.Text =totalTimer.ToString();
                       pictureBox1.BackColor =Color.Red;
                       pictureBox3.BackColor =Color.Red;
                       totalTimer--;
                       textBox2.Text =totalTimeg.ToString();
                       textBox4.Text =totalTimeg.ToString();
                       pictureBox2.BackColor =Color.Green;
                       pictureBox4.BackColor =Color.Green;
                       totalTimeg--;
                   }
                   if (totalTimeg < 0)
                   {
                       textBox1.Text =totalTimer.ToString();
                       textBox3.Text =totalTimer.ToString();
                       pictureBox1.BackColor =Color.Red;
                       pictureBox3.BackColor =Color.Red;
                       totalTimer--;
                       textBox2.Text =totalTimey.ToString();
                       textBox4.Text =totalTimey.ToString();
                       pictureBox2.BackColor =Color.Yellow;
                       pictureBox4.BackColor =Color.Yellow;
                       totalTimey--;
                   }
                   if (totalTimey < 0)
                   {
                       totalTimeg = 40;
                       totalTimer = 60;
                       totalTimey = 20;
                       _timeg = 40;
                       _timer = 60;
                       _timey = 20;
                   }
                   break;
              case 4:
                  textBox4.Text =_timeg.ToString();
                   pictureBox4.BackColor =Color.Green;
                   _timeg--;
                   textBox2.Text =_timer.ToString();
                   pictureBox2.BackColor =Color.Red;
                   _timer--;
                   textBox3.Visible = false;
                   pictureBox3.Visible =false;
                   textBox1.Visible = false;
                   pictureBox1.Visible =false;
                   if (_timeg < 0)
                   {
                       textBox4.Text = _timey.ToString();
                       pictureBox4.BackColor =Color.Yellow;
                       _timey--;
                   }
                   if (_timey < 0)
                   {
                       textBox4.Text =totalTimer.ToString();
                       pictureBox4.BackColor =Color.Red;
                       totalTimer--;
                       textBox2.Text =totalTimeg.ToString();
                       pictureBox2.BackColor =Color.Green;
                       totalTimeg--;
                   }
                   if (totalTimeg < 0)
                   {
                       textBox4.Text =totalTimer.ToString();
                       pictureBox4.BackColor =Color.Red;
                       totalTimer--;
                       textBox2.Text =totalTimey.ToString();
                       pictureBox2.BackColor =Color.Yellow;
                       totalTimey--;
                   }
                   if (totalTimey < 0)
                   {
                       totalTimeg = 40;
                       totalTimer = 60;
                       totalTimey = 20;
                       _timeg = 40;
                       _timer = 60;
                       _timey = 20;
                   }
                   break;
               case 5:
                   textBox1.Text =_timeg.ToString();
                   textBox3.Text =_timeg.ToString();
                   pictureBox1.BackColor =Color.Green;
                   pictureBox3.BackColor =Color.Green;
                   _timeg--;
                   textBox4.Text =_timer.ToString();
                   pictureBox4.BackColor =Color.Red;
                   _timer--;
                   textBox2.Visible = false;
                   pictureBox2.Visible =false;
                   if (_timeg < 0)
                   {
                       textBox1.Text =_timey.ToString();
                       textBox3.Text =_timey.ToString();
                       pictureBox1.BackColor =Color.Yellow;
                       pictureBox3.BackColor =Color.Yellow;
                       _timey--;
                   }
                   if (_timey < 0)
                   {
                       textBox1.Text =totalTimer.ToString();
                       textBox3.Text =totalTimer.ToString();
                       pictureBox1.BackColor =Color.Red;
                       pictureBox3.BackColor =Color.Red;
                       totalTimer--;
                       textBox4.Text = totalTimeg.ToString();
                       pictureBox4.BackColor =Color.Green;
                       totalTimeg--;
                   }
                   if (totalTimeg < 0)
                   {
                       textBox1.Text =totalTimer.ToString();
                       textBox3.Text =totalTimer.ToString();
                       pictureBox1.BackColor =Color.Red;
                       pictureBox3.BackColor =Color.Red;
                       totalTimer--;
                       textBox4.Text =totalTimey.ToString();
                       pictureBox4.BackColor =Color.Yellow;
                       totalTimey--;
                   }
                   if (totalTimey < 0)
                   {
                       totalTimeg = 40;
                       totalTimer = 60;
                       totalTimey = 20;
                       _timeg = 40;
                       _timer = 60;
                       _timey = 20;
                   }
                   break;
              case 6:
                   textBox2.Visible = false;
                   pictureBox2.Visible =false;
                   textBox3.Visible = false;
                   pictureBox3.Visible =false;
                   textBox1.Visible = false;
                   pictureBox1.Visible =false;
                   textBox4.Text =_timeg.ToString();
                   pictureBox4.BackColor =Color.Green;
                   _timeg--;
                   if (_timeg < 0)
                   {
                       textBox4.Text =_timey.ToString();
                       pictureBox4.BackColor =Color.Yellow;
                       _timey--;
                   }
                   if (_timey < 0)
                   {
                       textBox4.Text =totalTimer.ToString();
                       pictureBox4.BackColor =Color.Red;
                       totalTimer--;
                   }
                   if (totalTimeg < 0)
                   {
                       textBox4.Text =totalTimer.ToString();
                       pictureBox4.BackColor =Color.Red;
                       totalTimer--;
                   }
                   if (totalTimey < 0)
                   {
                       totalTimeg = 40;
                       totalTimer = 60;
                       totalTimey = 20;
                       _timeg = 40;
                       _timer = 60;
                       _timey = 20;
                   }
                   break;
             case 7:
                   textBox1.Text =_timeg.ToString();
                   pictureBox1.BackColor =Color.Green;
                   _timeg--;
                   textBox4.Text =_timer.ToString();
                   pictureBox4.BackColor =Color.Red;
                   _timer--;
                   textBox3.Visible = false;
                   pictureBox3.Visible =false;
                   textBox2.Visible = false;
                   pictureBox2.Visible =false;
                   if (_timeg < 0)
                   {
                       textBox1.Text =_timey.ToString();
                       pictureBox1.BackColor =Color.Yellow;
                       _timey--;
                   }
                   if (_timey < 0)
                   {
                       textBox1.Text =totalTimer.ToString();
                       pictureBox1.BackColor =Color.Red;
                       totalTimer--;
                       textBox4.Text =totalTimeg.ToString();
                       pictureBox4.BackColor =Color.Green;
                       totalTimeg--;
                   }
                   if (totalTimeg < 0)
                   {
                       textBox1.Text =totalTimer.ToString();
                       pictureBox1.BackColor =Color.Red;
                       totalTimer--;
                       textBox4.Text =totalTimey.ToString();
                       pictureBox4.BackColor =Color.Yellow;
                       totalTimey--;
                   }
                   if (totalTimey < 0)
                   {
                       totalTimeg = 40;
                       totalTimer = 60;
                       totalTimey = 20;
                       _timeg = 40;
                       _timer = 60;
                       _timey = 20;
                   }
                   break;
           }
       }
         以实现动态循环显示。

感觉要实现动态循环显示,这部分还是有些复杂,逻辑上不好表示出来。我的程序可能也需要优化,或许使用线程处理会简洁些吧,欢迎大家指正。

你可能感兴趣的:(Small,projects)