| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3111 人关注过本帖
标题:用C#,做一个简单的串口调试助手时,程序出现了错误(附上程序及错误提示) ...
只看楼主 加入收藏
liqiang123
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2015-8-15
结帖率:100%
收藏
 问题点数:0 回复次数:0 
用C#,做一个简单的串口调试助手时,程序出现了错误(附上程序及错误提示),希望大神能指点,好让我有提高
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using

namespace 串口调试助手
{
    public partial class Form1 : Form
    {
        SerialPort sp = null;//声明一个串口类
        bool isOpen = false;//打开串口标志位
        bool isSetProperty = false;//属性设置标志位
        bool isHex = false;//十六进制显示标志位
        public Form1()
        {
            InitializeComponent();//窗口初始化,net自动生成
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.MaximumSize = this.Size;
            this.MinimumSize = this.Size;
            this.MaximizeBox = false;
            for (int i = 0; i < 10; i++)//最大支持到串口10,可根据自己需求增加
            {
                cbxCOMPort.Items.Add("COM" + (i + 1).ToString());
            }
            cbxCOMPort.SelectedIndex = 0;
            //列出常用的波特率
            cbxBaudRate.Items.Add("1200");
            cbxBaudRate.Items.Add("2400");
            cbxBaudRate.Items.Add("4800");
            cbxBaudRate.Items.Add("9600");
            cbxBaudRate.Items.Add("19200");
            cbxBaudRate.Items.Add("38400");
            cbxBaudRate.Items.Add("43000");
            cbxBaudRate.Items.Add("56000");
            cbxBaudRate.Items.Add("57600");
            cbxBaudRate.Items.Add("115200");
            cbxBaudRate.SelectedIndex = 5;
            //列出停止位
            cbxStopBits.Items.Add("0");
            cbxStopBits.Items.Add("1");
            cbxStopBits.Items.Add("1.5");
            cbxStopBits.Items.Add("2");
            cbxStopBits.SelectedIndex = 1;
            //列出数据位
            cbxDataBits.Items.Add("8");
            cbxDataBits.Items.Add("7");
            cbxDataBits.Items.Add("6");
            cbxDataBits.Items.Add("5");
            cbxDataBits.SelectedIndex = 0;
            //列出奇偶检验位
            cbxParitv.Items.Add("无");
            cbxParitv.Items.Add("奇校验");
            cbxParitv.Items.Add("偶校验");
            cbxParitv.SelectedIndex = 0;
            //默认为Char显示
            rbnChar.Checked = true;
        }
        private void btnCheckCOM_Click(object sender, EventArgs e)//检测哪些串口可用
        {
            bool comExistence = false; //有可用串口标志位
            cbxCOMPort.Items.Clear();//清除当前串口号中的所有串口名称
            for (int i = 0; i < 10; i++)
            {
                try
                {
                    SerialPort sp = new SerialPort("COM" + (i + 1).ToString());
                    sp.Open();
                    sp.Close();
                    cbxCOMPort.Items.Add("COM" + (i + 1).ToString());
                    comExistence = true;
                }
                catch (Exception)
                {
                    continue;
                }
            }
            if (comExistence)
            {
                cbxCOMPort.SelectedIndex = 0;//使ListBox显示第1个添加的索引
            }
            else
            {
                MessageBox.Show("没有找到可用的串口!", "错误提示");
            }
        }
        private bool CheckPortSetting()//检查串口是否设置
        {
            if (cbxCOMPort.Text.Trim() == "") return false;
            if (cbxBaudRate.Text.Trim() == "") return false;
            if (cbxDataBits.Text.Trim() == "") return false;
            if (cbxParitv.Text.Trim() == "") return false;
            if (cbxStopBits.Text.Trim() == "") return false;
            return true;
        }
        private bool CheckSendData()
        {
            if (tbxSendData.Text.Trim() == "") return false;
            return true;
        }
        private void SetPortProperty()//设置串口的属性
        {
            sp = new SerialPort();
            sp.PortName = cbxCOMPort.Text.Trim();//设置串口名
            sp.BaudRate = Convert.Tolnt32(cbxBaudRate.Text.Trim());//设置串口波特率
            float f = Convert.ToSingle(cbxStopBits.Text.Trim());//设置停止位
            if (f == 0)
            {
                sp.StopBits = StopBits.None;
            }
            else if (f == 1.5)
            {
                sp.StopBits = StopBits.OnePointFive;
            }
            else if (f == 1)
            {
                sp.StopBits = StopBits.One;
            }
            else if (f == 2)
            {
                sp.StopBits = StopBits.Two;
            }
            else
            {
                sp.StopBits = StopBits.One;
            }
            sp.DataBits = Convert.ToInt16(cbxDataBits.Text.Trim);//设置数据位
            string s = cbxParitv.Text.Trim();//设置奇偶校验位
            if (("无" )== 0)
            {
                sp.Parity = Parity.None;
            }
            else if (("奇校验") == 0)
            {
                sp.Parity = Parity.Odd;
            }
            else if (("偶校验") == 0)
            {
                sp.Parity = Parity.Even;
            }
            else
            {
                sp.Parity = Parity.None;
            }
            sp.ReadTimeout = -1;//设置超时读取时间
            sp.RtsEnable = true;
            //定义DataRecevied事件,当串口收到数据后触发事件
            sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
            if (rbnHex.Checked)
            {
                isHex = true;
            }
            else
            {
                isHex = false;
            }
        }
        private void btnSend_Click(object sender, EventArgs e)//发送串口数据
        {
            if (isOpen)//写串口数据
            {
                try
                {
                    sp.WriteLine(tbxSendData.Text);
                }
                catch (Exception)
                {
                    MessageBox.Show("发送数据时发生错误!", "错误提示");
                    return;
                }
            }
            else
            {
                MessageBox.Show("串口未打开!", "错误提示");
                return;
            }
            if (!CheckSendData())//检测要发送的数据
            {
                MessageBox.Show("请输入要发送的数据!", "错误提示");
                return;
            }
        }
        private void btnOpenCom_Click(object sender, EventArgs e)
        {
            if (isOpen == false)
            {
                if (!CheckPortSetting())//检测串口设置
                {
                    MessageBox.Show("串口未设置!", "错误提示");
                    return;
                }
                if (!isSetProperty)//串口未设置则设置串口
                {
                    SetPortProperty();
                    isSetProperty = true;
                }
                try//打开串口
                {
                    sp.Open();
                    isOpen = true;
                    btnOpenCom.Text = "关闭串口";
                    //串口打开后则相关的串口设置按钮便不可再用
                    cbxCOMPort.Enabled = false;
                    cbxBaudRate.Enabled = false;
                    cbxDataBits.Enabled = false;
                    cbxParitv.Enabled = false;
                    cbxStopBits.Enabled = false;
                    rbnChar.Enabled = false;
                    rbnHex.Enabled = false;
                }
                catch (Exception)
                {
                    //串口打开失败后,响应标志取消
                    isSetProperty = false;
                    isOpen = false;
                    MessageBox.Show("串口无效或已被占用!", "错误提示");
                }
            }
            else
            {
                try//打开串口
                {
                    sp.Close();
                    isOpen = false;
                    isSetProperty = false;
                    btnOpenCom.Text = "打开串口";
                    //关闭串口后,串口设置选项便可以继续使用
                    cbxCOMPort.Enabled = true;
                    cbxBaudRate.Enabled = true;
                    cbxDataBits.Enabled = true;
                    cbxParitv.Enabled = true;
                    cbxStopBits.Enabled = true;
                    rbnChar.Enabled = true;
                    rbnHex.Enabled = true;
                }
                catch (Exception)
                {
                    btnOpenCom.Text = "关闭串口时发生错误";
                }
            }
        }
        private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            System.Threading.Thread.Sleep(100);//延时100ms等待接受完整数据
            //this.Invoke 就是跨线程访问ui的方法,也是本文的范例
            this.Invoke(EventHandler )(delegate
            {
                if(isHex == false)
                {
                    tbxRecvData.Text+=sp.ReadLine();
                }
                else
                {
                    Byte[]ReceivedData = new Byte[sp.BytesToRead];//创建接受字节数组
                    sp.Read(ReceivedData,0,ReceivedData.Length);//读取所接受到的数据
                    String RecvDataText = null;
                    for(int i = 0;i<ReceivedData.Length -1;i++)
                    {
                       RecvData Text +=("0x" + ReceivedData[i].ToString("X2")+"");
                    }
                   /tbxRecvData.Text += RecvData Text;
                }
                sp.DiscardInBuffer();//丢弃接受缓存区数据
            });
        }
        private void btnCleanData_Click(object sender, EventArgs e)
        {
            tbxRecvData.Text = "";
            tbxSendData.Text = "";
        }
    }

}

错误    1    应输入 ;    C:\Users\Administrator\Documents\Visual Studio 2008\Projects\串口调试助手\串口调试助手\Form1.cs    266    38    串口调试助手
错误    2    无效的表达式项“+=”    C:\Users\Administrator\Documents\Visual Studio 2008\Projects\串口调试助手\串口调试助手\Form1.cs    266    38    串口调试助手
错误    3    应输入 ;    C:\Users\Administrator\Documents\Visual Studio 2008\Projects\串口调试助手\串口调试助手\Form1.cs    268    49    串口调试助手
错误    4    “System.Convert”并不包含“Tolnt32”的定义    C:\Users\Administrator\Documents\Visual Studio 2008\Projects\串口调试助手\串口调试助手\Form1.cs    112    35    串口调试助手
错误    5    与“System.Convert.ToInt16(object)”最匹配的重载方法具有一些无效参数    C:\Users\Administrator\Documents\Visual Studio 2008\Projects\串口调试助手\串口调试助手\Form1.cs    134    27    串口调试助手
错误    6    参数“1”: 无法从“方法组”转换为“object”    C:\Users\Administrator\Documents\Visual Studio 2008\Projects\串口调试助手\串口调试助手\Form1.cs    134    43    串口调试助手

错误    7    “串口调试助手.Form1”不包含“btnOpenCom_Click_1”的定义,并且找不到可接受类型为“串口调试助手.Form1”的第一个参数的扩展方法“btnOpenCom_Click_1”(是否缺少 using 指令或程序集引用?)    C:\Users\Administrator\Documents\Visual Studio 2008\Projects\串口调试助手\串口调试助手\Form1.Designer.cs    90    67    串口调试助手
搜索更多相关主题的帖子: 十六进制 public false null 
2015-08-24 16:01
快速回复:用C#,做一个简单的串口调试助手时,程序出现了错误(附上程序及错误提 ...
数据加载中...
 
   



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

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