C# serialport1在上下文中不存在 使用System.Collections.Generic; 使用系统组件模型; 使用制度; 使用系统数据; 使用系统图; 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 使用System.Windows.Forms; 使用System.IO.Ports; 命名空间Windows窗体应用程序21 { 公共部分类Form1:Form { 公共表格1() { 初始化组件(); } 私有无效标签1_单击(对象发送方,事件参数e) { } 私有无效按钮2\u单击(对象发送者,事件参数e) { button1.BackColor=颜色。红色; 标签1.Text=“已占用”; } private void serialPort1\u DataReceived(对象发送方,System.IO.Ports.SerialDataReceivedEventArgs e) { Invoke(neweventhandler(DoUpdate)); } 私有void DoUpdate(对象s、事件参数e) { string InputString=serialPort1.ReadExisting(); string[]data=InputString.Split(','); textBox1.Text=数据[0]; textBox2.Text=数据[1]; int b; int d; bool result=Int32.TryParse(数据[0],输出b); bool-sonuc=Int32.TryParse(数据[1],输出d); 如果(结果) { 如果(b

C# serialport1在上下文中不存在 使用System.Collections.Generic; 使用系统组件模型; 使用制度; 使用系统数据; 使用系统图; 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 使用System.Windows.Forms; 使用System.IO.Ports; 命名空间Windows窗体应用程序21 { 公共部分类Form1:Form { 公共表格1() { 初始化组件(); } 私有无效标签1_单击(对象发送方,事件参数e) { } 私有无效按钮2\u单击(对象发送者,事件参数e) { button1.BackColor=颜色。红色; 标签1.Text=“已占用”; } private void serialPort1\u DataReceived(对象发送方,System.IO.Ports.SerialDataReceivedEventArgs e) { Invoke(neweventhandler(DoUpdate)); } 私有void DoUpdate(对象s、事件参数e) { string InputString=serialPort1.ReadExisting(); string[]data=InputString.Split(','); textBox1.Text=数据[0]; textBox2.Text=数据[1]; int b; int d; bool result=Int32.TryParse(数据[0],输出b); bool-sonuc=Int32.TryParse(数据[1],输出d); 如果(结果) { 如果(b,c#,C#,我收到一个错误: “serialport1在上下文中不存在” 我能做什么?每当我想编译时,我都会得到上面提到的错误。 我必须添加任何库吗? 我正在使用visual Studio 2010在哪里设置serialport1值的代码?请看文档中的示例:(假设)在DoUpdate()的末尾有一个结束块引号(*/),并且缺少一个右大括号()。DoUpdate()(intb=…)的最后两行可能应该注释掉或删除?是否在设计器中添加了serialPort1?也许把它改名为其他东西?谢谢你它成功了 using S

我收到一个错误:

“serialport1在上下文中不存在”

我能做什么?每当我想编译时,我都会得到上面提到的错误。
我必须添加任何库吗?

我正在使用visual Studio 2010

在哪里设置
serialport1
值的代码?请看文档中的示例:(假设)在
DoUpdate()
的末尾有一个结束块引号(
*/
),并且缺少一个右大括号(
)。
DoUpdate()
intb=…
)的最后两行可能应该注释掉或删除?是否在设计器中添加了
serialPort1
?也许把它改名为其他东西?谢谢你它成功了
using System.Collections.Generic;
using System.ComponentModel;
using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApplication21
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            button1.BackColor = Color.Red;
            label1.Text = " OCCUPIED";
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
          this.Invoke(new EventHandler(DoUpdate));
        }

        private void DoUpdate(object s, EventArgs e)
        {
            string InputString = serialPort1.ReadExisting();
            string[] data = InputString.Split(',');

            textBox1.Text = data[0];
            textBox2.Text = data[1];

           int b;
           int d;

           bool result = Int32.TryParse(data[0], out b);
           bool sonuc = Int32.TryParse(data[1], out d);
           if (result)
           {
               if (b < 224)
               {
                   button1.BackColor = Color.Red;
                   label1.Text = " OCCUPIED";
               }
               else
               {
                   button1.BackColor = Color.Lime;
                   label1.Text = "AVAILABLE";
               }
           }

           if (sonuc)
           {
               if (d< 224)
               {
                   button1.BackColor = Color.Red;
                   label1.Text = " OCCUPIED";
               }
               else
               {
                   button1.BackColor = Color.Lime;
                   label1.Text = "AVAILABLE";
               }
           }

        int b = Int16.Parse(a);
        textBox1.Text= a.ToString();*/

        private void button3_Click(object sender, EventArgs e)
        {
            listBox1.Visible = true;
            listBox1.Items.Add("Değerler alınıyor!");

            if (!serialPort1.IsOpen)
            {
                serialPort1.PortName = "COM11";
                serialPort1.Open();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            serialPort1.Close();
            listBox1.Items.Add("Durduruldu!");
        }
    }
}