| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1643 人关注过本帖
标题:用下面这段程序做串口调试界面,为什么会出现警告“类名?不是该语言的有效 ...
只看楼主 加入收藏
liqiang123
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2015-8-15
结帖率:100%
收藏
 问题点数:0 回复次数:0 
用下面这段程序做串口调试界面,为什么会出现警告“类名?不是该语言的有效标示符“
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 1; i <= 20; i++)
            {
                comboBox1.Items.Add("COM" + i.ToString());

            }
            comboBox1.SelectedIndex = 0;

            for (int i = 0; i <= 20; i++)
            {
                string str =  Convert.ToString(i,16);
                if(str.Length == 1)//根据字符串的长度来决定要不要给它加个“0”
                    str = "0" + str;
                str = "0x" + str;
                comboBox1.Items.Add(str);
               
            }
             //串口发送的最小单位是字节
        }
              

        private void button1_Click(object sender, EventArgs e)
        {
            
           
            if (serialPort1.IsOpen)//端口如果是关闭着的,那么就打开端口
            {
                try
                {        
                    //设置串口名称
                    serialPort1.PortName = comboBox1.Text;
                    serialPort1.Open();
                    button1.Text = "关闭端口";
                }
                catch
                {
                    MessageBox.Show("端口打开失败");
                }
            }
            else//如果是打开着的,那就关闭端口
            {
                try
                {
                    serialPort1.Close();
                    button1.Text = "打开端口";
                }
                catch
                {
                    MessageBox.Show("端口关闭失败");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //发送数据
            byte [] data = new byte[1];
            String str ;
            try
    {            
        str = comboBox2.Text;
            //定义一个数组
           str =  str.Substring(2,2);
            data [0]= Convert.ToByte(str,16);
            //数据转换
    }
    catch
    {
        MessageBox.Show("提示用户,请使用合理的数据");
        
    }
            
            data[0] = 1;
            if (serialPort1.IsOpen)
            {

            try
            {
                serialPort1.Write(data, 0, 1);//发送数据
            }
            catch
            {
               MessageBox.Show(端口发送失败,请检查端口);
            }
            }

           else //端口没打开的
            {
                    MessageBox.Show("端口没打开,发送失败");
                }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
搜索更多相关主题的帖子: private public 标示 
2015-08-18 23:46
快速回复:用下面这段程序做串口调试界面,为什么会出现警告“类名?不是该语言的 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015633 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved