问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO.Ports;namespace串口助手{publicpartialclassForm1:Form{SerialPortsp=null;//声明一个串口类boolisOpen=false;//打开串口标志位boolisSetProperty=false;//属性设置标志位boolisHex=false;//十六进制显示标志位publicForm1(){InitializeComponent();//窗口初始化,net自动生成}privatevoidForm1_Load(objectsender,EventArgse){this.MaximumSize=this.Size;this.MinimumSize=this.Size;this.MaximizeBox=false;for(inti=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;//列出奇偶校验位cbxParity.Items.Add("无");cbxParity.Items.Add("奇校验");cbxParity.Items.Add("偶校验");cbxParity.SelectedIndex=0;//默认为Char显示rbnChar.Checked=true;}privatevoidbtnCheckCOM_Click(objectsender,EventArgse)///检测哪些串口可用{boolcomExistence=false;//有可用串口标志位cbxCOMPort.Items.Clear();//清除当前串口号中的所有串口名称for(inti=0;i<10;i++){try{SerialPortsp=newSerialPort("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("没有找到可用串口!","错误提示");}}privateboolCheckPortSetting();//检查串口是否设置//这里的括号全都‘(’无效{if(cbxCOMPort.Text.Trim()=="")returnfalse;if(cbxBaudRate.Text.Trim()=="")returnfalse;if(cbxDataBits.Text.Trim()=="")returnfalse;if(cbxParity.Text.Trim()=="")returnfalse;if(cbxStopBits.Text.Trim()=="")returnfalse;returntrue;}privateboolCheckSendData()//从这里开始就boolvoid出错了{if(tbxSendData.Text.Trim()=="")returnfalse;returntrue;}privatevoidSetPortProperty()//设置串口的属性{sp=newSerialPort();sp.PortName=cbxCOMPort.Text.Trim();//设置串口名sp.BaudRate=Convert.ToInt32(cbxBaudRate.Text.Trim());//设置串口的波特率floatf=Convert.ToSingle(cbxStopBits.Text.Trim());//设置停止位if(f==0){sp.StopBits=StopBits.None;}elseif(f==1.5){sp.StopBits=StopBits.OnePointFive;}elseif(f==1){sp.StopBits=StopBits.One;}elseif(f==2){sp.StopBits=StopBits.Two;}else{sp.StopBits=StopBits.One;}sp.DataBits=Convert.ToInt16(cbxDataBits.Text.Trim());//设置数据位strings=cbxParity.Text.Trim();//设置奇偶校验位if(s.CompareTo("无")==0){sp.Parity=Parity.None;}elseif(s.CompareTo("奇校验")==0){sp.Parity=Parity.Odd;}elseif(s.CompareTo("偶校验")==0){sp.Parity=Parity.Even;}else{sp.Parity=Parity.None;}sp.ReadTimeout=-1;//设置超时读取时间sp.RtsEnable=true;//定义DataReceived事件,当串口收到数据后触发事件sp.DataReceived+=newSerialDataReceivedEventHandler(sp_DataReceived);if(rbnHex.Checked){isHex=true;}else{isHex=false;}}privatevoidbtnSend_Click(objectsender,EventArgse)//发送串口数据{if(isOpen)//写串口数据{try{sp.WriteLine(tbxSendData.Text);}catch(Exception){MessageBox.Show("发送数据时发生错误!","错误提示");return;}}else{MessageBox.Show("串口未打开!","错误提示");return;}if(!CheckSendData())//检测要发送的数据{MessageBox.Show("请输入要发送的数据!","错误提示");return;}}privateboolbtnOpenCom_Click(objectsender,EventArgse){{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;cbxParity.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;cbxParity.Enabled=true;cbxStopBits.Enabled=true;rbnChar.Enabled=true;rbnHex.Enabled=true;}catch(Exception){lblStatus.Text="关闭串口时发生错误";}}}}privatevoidsp_DataReceived(objectsender,SerialDataReceivedEventArgse);{{System.Threading.Thread.Sleep(100);//延时100ms等待接收完数据//this.Invoke就是跨线程访问ui的方法,也是本文的范例this.Invoke((EventHandler)(delegate{if(isHex==false){tbxRecvData.Text+=sp.ReadLine();}else{byte[ReceivedData]=newbyte[sp.BytesToRead];//创建接收字节数组sp.Read(ReceivedData,0,ReceivedData.Length);//读取所接收到的数据StringRecvDataText=null;for(inti=0;i<ReceivedData.Length-1;i++){RecvDataText+=("0x"+ReceivedData[i].ToString("X2")+"");}tbxRecvData.Text+=RecvDataText;}sp.DiscardInBuffer();//丢弃接收缓冲区数据}));}}privatevoidbtnCleanData_Click(objectsender,EventArgse){tbxRecvData.Text="";tbxSendData.Text="";}}}
解决方案
解决方案二:
我需要大神的帮助因为这个一点没学过,老师就是赶鸭子上架
解决方案三:
我的天,就没大神么。