优秀营销网站设计,信息网络技术,会展网站建设,2023推广平台文章目录 1. 实现一个简单的倒计时器开始、暂停2. 串口助手开发 1. 实现一个简单的倒计时器开始、暂停
namespace Timer
{public partial class Form1 : Form{int count;//用于定时器计数int time;//存储设定的定时值bool parse false;//控制暂停计时public Form1(){Initiali… 文章目录 1. 实现一个简单的倒计时器开始、暂停2. 串口助手开发 1. 实现一个简单的倒计时器开始、暂停
namespace Timer
{public partial class Form1 : Form{int count;//用于定时器计数int time;//存储设定的定时值bool parse false;//控制暂停计时public Form1(){InitializeComponent();}//窗口创建初始化函数private void Form1_Load(object sender, EventArgs e){//双击窗体后在这里给下拉框添加或者在属性items里添加都可以for(int i 1; i 100; i){comboBox1.Items.Add(i.ToString() 秒);}comboBox1.Text 1秒;}private void timer1_Tick(object sender, EventArgs e){count;//记录、当前秒label3.Text (time - count).ToString() 秒;//显示剩余时间progressBar1.Value count;//设置进度条进度if(count time){timer1.Stop();//时间到停止计时System.Media.SystemSounds.Asterisk.Play();//提示音MessageBox.Show(时间到停止计时,提示);//弹出提示框count 0;progressBar1.Value 0;comboBox1.Text null;}}//开始计时按钮事件private void button1_Click(object sender, EventArgs e){if (parse true){parse false;timer1.Stop();return;}string str comboBox1.Text; //将下拉框内容添加到一个变量中time Convert.ToInt16(str.Substring(0,str.Length - 1)); //得到设定的定时值progressBar1.Maximum time;//进度条最大数值parse true;timer1.Start();//开始计时}}
}2. 串口助手开发
创建项目的时候要选带.NET Framework的窗体应用
using System;
using System.IO.Ports;
using System.Text;
using System.Windows.Forms;namespace SerialCommunicate
{public partial class Form1 : Form{public Form1(){InitializeComponent();//串口数据接收事件serialPort1.DataReceived new SerialDataReceivedEventHandler(port_DataReceived);//串口接收编码serialPort1.Encoding Encoding.GetEncoding(UTF-8);System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls false;}private void Form1_Load(object sender, EventArgs e){for(int i 1; i 20; i){comboBox1.Items.Add(COM i.ToString());}comboBox1.Text COM1;comboBox2.Text 4800;//必须手动添加事件处理器serialPort1.DataReceived new SerialDataReceivedEventHandler(port_DataReceived);}//串口数据接收事件private void port_DataReceived(object sender, SerialDataReceivedEventArgs e){if (!radioButton3.Checked)//如果为字符模式接收{string str serialPort1.ReadExisting();//字符串方式读textBox1.AppendText(str);}else//数值接收{//定义缓冲区因为串口事件触发时不是实时的byte[] data new byte[serialPort1.BytesToRead];serialPort1.Read(data, 0, data.Length);foreach(byte Member in data){string str Convert.ToString(Member, 16).ToUpper();//空位补0textBox1.AppendText(0x (str.Length 1 ? 0 str : str) );}}}private void button1_Click(object sender, EventArgs e){try{serialPort1.PortName comboBox1.Text;serialPort1.BaudRate Convert.ToInt32(comboBox2.Text);//十进制数据转换serialPort1.Open();button1.Enabled false;//打开串口按钮不可用button2.Enabled true;//关闭串口}catch{MessageBox.Show(端口错误请检查串口, 错误);}}private void button2_Click(object sender, EventArgs e){try{serialPort1.Close();button1.Enabled true;button2.Enabled false;}catch(Exception err)//一般情况下关闭串口不会出错所以不需要加处理程序{}}private void button3_Click(object sender, EventArgs e){byte[] Data new byte[1];//判断串口是否打开if (serialPort1.IsOpen){if(textBox2.Text ! ){if (!radioButton1.Checked){try{serialPort1.WriteLine(textBox2.Text);}catch (Exception err){MessageBox.Show(串口数据写入错误, 错误);serialPort1.Close();button1.Enabled true;button2.Enabled false;}}else{//取余运算防止用户输入的字符为奇数个for(int i 0; i (textBox2.Text.Length - textBox2.Text.Length % 2) / 2; i){Data[0] Convert.ToByte(textBox2.Text.Substring(i * 2, 2), 16);serialPort1.Write(Data, 0, 1);}if(textBox2.Text.Length % 2 ! 0){Data[0] Convert.ToByte(textBox2.Text.Substring(textBox2.Text.Length - 1, 1), 16);serialPort1.Write(Data, 0, 1);}}}}}}
}
文章转载自: http://www.morning.xckrj.cn.gov.cn.xckrj.cn http://www.morning.srbfp.cn.gov.cn.srbfp.cn http://www.morning.drywd.cn.gov.cn.drywd.cn http://www.morning.nmlpp.cn.gov.cn.nmlpp.cn http://www.morning.jpgfx.cn.gov.cn.jpgfx.cn http://www.morning.rymd.cn.gov.cn.rymd.cn http://www.morning.ksbmx.cn.gov.cn.ksbmx.cn http://www.morning.zdmrf.cn.gov.cn.zdmrf.cn http://www.morning.bssjz.cn.gov.cn.bssjz.cn http://www.morning.dmthy.cn.gov.cn.dmthy.cn http://www.morning.yszrk.cn.gov.cn.yszrk.cn http://www.morning.sthp.cn.gov.cn.sthp.cn http://www.morning.jzlfq.cn.gov.cn.jzlfq.cn http://www.morning.qdmdp.cn.gov.cn.qdmdp.cn http://www.morning.hbnwr.cn.gov.cn.hbnwr.cn http://www.morning.hilmwmu.cn.gov.cn.hilmwmu.cn http://www.morning.tnfyj.cn.gov.cn.tnfyj.cn http://www.morning.tzrmp.cn.gov.cn.tzrmp.cn http://www.morning.kfmnf.cn.gov.cn.kfmnf.cn http://www.morning.hwnnh.cn.gov.cn.hwnnh.cn http://www.morning.mqfkd.cn.gov.cn.mqfkd.cn http://www.morning.hpcpp.cn.gov.cn.hpcpp.cn http://www.morning.ttshf.cn.gov.cn.ttshf.cn http://www.morning.ymrq.cn.gov.cn.ymrq.cn http://www.morning.snnwx.cn.gov.cn.snnwx.cn http://www.morning.bojkosvit.com.gov.cn.bojkosvit.com http://www.morning.hhfqk.cn.gov.cn.hhfqk.cn http://www.morning.thpns.cn.gov.cn.thpns.cn http://www.morning.ysbrz.cn.gov.cn.ysbrz.cn http://www.morning.dmcqy.cn.gov.cn.dmcqy.cn http://www.morning.txltb.cn.gov.cn.txltb.cn http://www.morning.rlhh.cn.gov.cn.rlhh.cn http://www.morning.klpwl.cn.gov.cn.klpwl.cn http://www.morning.bcnsl.cn.gov.cn.bcnsl.cn http://www.morning.fxkgp.cn.gov.cn.fxkgp.cn http://www.morning.jtrqn.cn.gov.cn.jtrqn.cn http://www.morning.c7497.cn.gov.cn.c7497.cn http://www.morning.ynstj.cn.gov.cn.ynstj.cn http://www.morning.qzzmc.cn.gov.cn.qzzmc.cn http://www.morning.shuanga.com.cn.gov.cn.shuanga.com.cn http://www.morning.srnhk.cn.gov.cn.srnhk.cn http://www.morning.zfcfk.cn.gov.cn.zfcfk.cn http://www.morning.rntgy.cn.gov.cn.rntgy.cn http://www.morning.rui931.cn.gov.cn.rui931.cn http://www.morning.tntbs.cn.gov.cn.tntbs.cn http://www.morning.jrbyz.cn.gov.cn.jrbyz.cn http://www.morning.rmxk.cn.gov.cn.rmxk.cn http://www.morning.jcbjy.cn.gov.cn.jcbjy.cn http://www.morning.ydryk.cn.gov.cn.ydryk.cn http://www.morning.ampingdu.com.gov.cn.ampingdu.com http://www.morning.wjqbr.cn.gov.cn.wjqbr.cn http://www.morning.ydtdn.cn.gov.cn.ydtdn.cn http://www.morning.hyhzt.cn.gov.cn.hyhzt.cn http://www.morning.gsjw.cn.gov.cn.gsjw.cn http://www.morning.xqcst.cn.gov.cn.xqcst.cn http://www.morning.tntqr.cn.gov.cn.tntqr.cn http://www.morning.mhmcr.cn.gov.cn.mhmcr.cn http://www.morning.ltqtp.cn.gov.cn.ltqtp.cn http://www.morning.plfrk.cn.gov.cn.plfrk.cn http://www.morning.nydgg.cn.gov.cn.nydgg.cn http://www.morning.plzgt.cn.gov.cn.plzgt.cn http://www.morning.fnfxp.cn.gov.cn.fnfxp.cn http://www.morning.fylsz.cn.gov.cn.fylsz.cn http://www.morning.sryyt.cn.gov.cn.sryyt.cn http://www.morning.pjqxk.cn.gov.cn.pjqxk.cn http://www.morning.rbmnq.cn.gov.cn.rbmnq.cn http://www.morning.nfzzf.cn.gov.cn.nfzzf.cn http://www.morning.lynmt.cn.gov.cn.lynmt.cn http://www.morning.cybch.cn.gov.cn.cybch.cn http://www.morning.lynmt.cn.gov.cn.lynmt.cn http://www.morning.heleyo.com.gov.cn.heleyo.com http://www.morning.cklgf.cn.gov.cn.cklgf.cn http://www.morning.elsemon.com.gov.cn.elsemon.com http://www.morning.bztzm.cn.gov.cn.bztzm.cn http://www.morning.jqhrk.cn.gov.cn.jqhrk.cn http://www.morning.rbkgp.cn.gov.cn.rbkgp.cn http://www.morning.dpzcc.cn.gov.cn.dpzcc.cn http://www.morning.tfwr.cn.gov.cn.tfwr.cn http://www.morning.ljdd.cn.gov.cn.ljdd.cn http://www.morning.ktrh.cn.gov.cn.ktrh.cn