实例介绍
【实例简介】
【实例截图】
【核心代码】
using System; using System.Diagnostics; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.IO.Ports; using System.Data.SqlClient; using System.Threading; using System.Collections; namespace MCS20130131327 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } int TX, RX; bool flag = true; bool button1_flag = false; private void button1_mcs_Click(object sender, EventArgs e) { if (flag) { button1_mcs.Text = " 关闭串口"; pictureBox1.Visible = true; pictureBox2.Visible = false; ser_mcs.PortName = mcs_combo_com.ToString(); ser_mcs.PortName = mcs_combo_com.Text; ser_mcs.BaudRate = mcs_combo_bo.SelectedIndex*mcs_combo_bo.SelectedIndex* 300; ser_mcs.Parity =0; ser_mcs.DataBits = 8; flag = false; button1_flag = true; //ser_mcs.Open(); try { ser_mcs.Open(); } catch (Exception ex) { MessageBox.Show("错误:" ex.Message); flag = true; button1_mcs.Text = " 打开串口"; pictureBox1.Visible = false; pictureBox2.Visible = true; button1_flag = false; } } else { button1_mcs.Text = " 打开串口"; pictureBox1.Visible = false; pictureBox2.Visible = true; flag = true; button1_flag = false; ser_mcs.Close(); } } private void button1_Click(object sender, EventArgs e) { textBox1.Text= ""; } private void Form1_Load(object sender, EventArgs e) { int i = 1; for (i = 1; i <= 16; i ) { mcs_combo_com.Items.Add("COM" i.ToString()); } for (i = 300; i <= 115200; i=i*2) { mcs_combo_bo.Items.Add(i.ToString()); } mcs_combo_com.SelectedIndex = 2; mcs_combo_bo.SelectedIndex = 5; mcs_combo_jiao.SelectedIndex = 0; mcs_combo_num.SelectedIndex = 0; mcs_combo_stop.SelectedIndex = 0; DateTime now = DateTime.Now; label11.Text= now.Year.ToString(); label11.Text = "/"; label11.Text = now.Month.ToString(); label11.Text = "/"; label11.Text = now.Day.ToString(); label11.Text = " "; label11.Text = now.Hour.ToString(); label11.Text = ":"; label11.Text = now.Minute.ToString(); label11.Text = ":"; label11.Text = now.Second.ToString(); checkBox4_flag = true; checkBox8_flag = true; } private void ser_mcs_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; //int ReceiveNums = ser_mcs.BytesToRead; string str = ser_mcs.ReadExisting().ToString(); RX = str.Length; label9.Text = Convert.ToString(RX); string a = "", b = "-"; a = str; List<byte> l = Encoding.ASCII.GetBytes(a).ToList<byte>(); byte[] data = l.ToArray(); a = (BitConverter.ToString(data)); for (int i = 0; i < a.Length; i ) { if (a[i] == b[0]) { a = a.Remove(i, 1); a = a.Insert(i, " "); } } if (checkBox1_flag == true) textBox1.Text = textBox1.Text a; else textBox1.Text = textBox1.Text str; if (checkBox3_flag) { if (textBox1.Text.Length >= 1000) textBox1.Text = ""; } if (checkBox8_flag) { textBox1.Text = "[" label11.Text "]"; } if (checkBox4_flag) { textBox1.Text = "\r\n"; } } private void button2_Click(object sender, EventArgs e) { string str = ""; str = textBox2.Text; if (checkBox6_flag) { str = "\r\n"; } string a = "", b = "-"; a = textBox2.Text; List<byte> l = Encoding.ASCII.GetBytes(a).ToList<byte>(); byte[] data = l.ToArray(); a = (BitConverter.ToString(data)); for (int i = 0; i < a.Length; i ) { if (a[i] == b[0]) { a = a.Remove(i, 1); a = a.Insert(i, " "); } } if (button1_flag == true) if (checkBox5_flag == true) { ser_mcs.Write(a); //a = a.Replace(" ",""); TX = a.Length; label10.Text = Convert.ToString(TX); } else { ser_mcs.Write(str); TX = str.Length; label10.Text = Convert.ToString(TX); } else MessageBox.Show("尚未打开串口!"); } private void button4_Click(object sender, EventArgs e) { Process pr = new Process();//声明一个进程类对象 pr.StartInfo.FileName = "calc.exe"; pr.Start();//运行 } int count = 3; private void button3_Click(object sender, EventArgs e) { System.Windows.Forms.SaveFileDialog objSave = new System.Windows.Forms.SaveFileDialog(); objSave.Filter = "(*.txt)|*.txt|" "(*.*)|*.*"; objSave.FileName = "文件" DateTime.Now.ToString("yyyyMMddHHmm") ".txt"; if (objSave.ShowDialog() == DialogResult.OK) { StreamWriter FileWriter = new StreamWriter(objSave.FileName, true); //写文件 FileWriter.Write(this.textBox1.Text);//将字符串写入 FileWriter.Close(); //关闭StreamWriter对象 } } private void button5_Click(object sender, EventArgs e) { OpenFileDialog open = new OpenFileDialog(); if (open.ShowDialog(this) == System.Windows.Forms.DialogResult.OK) { string lujing = open.FileName; textBox2.Text = System.IO.File.ReadAllText(lujing, Encoding.Default); } } bool checkBox2_flag = true; private void checkBox2_CheckedChanged(object sender, EventArgs e) { if (checkBox2_flag) { textBox1.BackColor = Color.Black; textBox1.ForeColor = Color.FromArgb(224, 224, 224); checkBox2_flag = false; } else { textBox1.BackColor = Color.FromArgb(224, 224, 224); textBox1.ForeColor = Color.Black; checkBox2_flag = true; } } bool checkBox3_flag = false; private void checkBox3_CheckedChanged(object sender, EventArgs e) { if (checkBox3_flag) { checkBox3_flag = false; } else { checkBox3_flag = true; } } bool checkBox4_flag = false; private void checkBox4_CheckedChanged(object sender, EventArgs e) { if (checkBox4_flag) { checkBox4_flag = false; } else { checkBox4_flag = true; } } bool checkBox6_flag = false; private void checkBox6_CheckedChanged(object sender, EventArgs e) { if (checkBox6_flag) { checkBox6_flag = false; } else { checkBox6_flag = true; } } bool checkBox1_flag = false; private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1_flag) { checkBox1_flag = false; } else { checkBox1_flag = true; } } private void button6_Click(object sender, EventArgs e) { label9.Text = "0"; label10.Text = "0"; TX = 0; RX = 0; } private void pictureBox1_Click(object sender, EventArgs e) { } bool button8_flag = true; private void button8_Click(object sender, EventArgs e) { if (button8_flag) { TopMost = true; button8_flag = false; button8.Text = "已置顶"; } else { TopMost = false; button8_flag = true; button8.Text = "置顶"; } } private void button7_Click(object sender, EventArgs e) { textBox2.Text = ""; } private void timer1_Tick(object sender, EventArgs e) { DateTime now = DateTime.Now; label11.Text = now.Year.ToString(); label11.Text = "/"; label11.Text = now.Month.ToString(); label11.Text = "/"; label11.Text = now.Day.ToString(); label11.Text = " "; label11.Text = now.Hour.ToString(); label11.Text = ":"; label11.Text = now.Minute.ToString(); label11.Text = ":"; label11.Text = now.Second.ToString(); } bool checkBox8_flag = false; private void checkBox8_CheckedChanged(object sender, EventArgs e) { if (checkBox8_flag) { checkBox8_flag = false; } else { checkBox8_flag = true; } } private void timer2_Tick(object sender, EventArgs e) { string str = ""; str = textBox2.Text; ser_mcs.Write(textBox2.Text); if (checkBox6_flag) { ser_mcs.Write("\r\n"); } } bool checkBox7_flag = false; private void checkBox7_CheckedChanged(object sender, EventArgs e) { if (checkBox7_flag) { checkBox7_flag = false; timer2.Enabled = false; } else { checkBox7_flag = true; timer2.Interval = System.Convert.ToInt32( textBox3.Text); timer2.Enabled = true; } } bool checkBox5_flag = false; private void checkBox5_CheckedChanged(object sender, EventArgs e) { if (checkBox5_flag) { checkBox5_flag = false; } else { checkBox5_flag = true; } } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论