首页 新闻 会员 周边 捐助

C#怎么在Comm_DataReceived外写函数?

0
[已关闭问题] 关闭于 2017-03-06 14:17
  1. 串口操作只是“一发、一收”,类似:comm.Write("指令1")→读取数据→比较数据→得出结果(pass\fail)→清除刚才收到的数据→comm.Write("指令2")→读取数据→比较数据→得出结果(pass\fail)。
  2. 我不想在Comm_DataReceived写逻辑判断,想写个逻辑判断函数,然后再调用。可是,string b 如果不在Comm_DataReceived里面,就是显示上下文不存在“b”,我在怎么处理???
  3. 部分代码
    复制代码
     1  private void Comm_DataReceived(object sender, SerialDataReceivedEventArgs e)
     2         {
     3             int n = comm.BytesToRead;
     4             byte[] buf = new byte[n];   
     5             comm.Read(buf, 0, n);
     6             builder.Clear();
     7             this.Invoke((EventHandler)(delegate
     8             {
     9                 builder.Append(Encoding.UTF8.GetString(buf));
    10          string b = (Encoding.UTF8.GetString(buf)).ToString();      
    11                 if (b.Contains("T78, 110.2014"))
    12             {
    13                 textBox1.Text = b;
    14                textBox2.Text = "pass";
    15                 builder.Clear();
    16               Thread.Sleep(1000);
    17                comm.Write("T79" + "\r");
    18                 }
    19     else if (b.Contains("T79,2GIG-b 3.42"))
    20               {
    21                 textBox3.Text = b;
    22                 textBox4.Text = "pass";
    23             builder.Clear();
    24                Thread.Sleep(1000);
    25                  comm.Write("T80" + "\r");
    26    else if (b.Contains("T80,13451"))
    27               {
    28                textBox5.Text = b;
    29                textBox6.Text = "pass";
    30                 builder.Clear();
    31                 Thread.Sleep(1000);
    32                 comm.Write("T90,2" + "\r");
    33               }
    34 
    35            }
    36 。。。。。
    复制代码

     


    li
某的主页 | 菜鸟二级 | 园豆:278
提问于:2017-03-05 10:35
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册