实例介绍
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Globalization; using NativeWifi; /* string sIn = "FF"; textBox5.Text = ""; foreach (char c in sIn) //用字符编码 { textBox5.Text = ((int)c).ToString(); } * //textBox5.Text = Convert.ToDecimal("2.9853").ToString("0.00"); //保留小数 变量.ToString() 字符型转换 转为字符串 12345.ToString("n"); //生成 12,345.00 12345.ToString("C"); //生成 ¥12,345.00 12345.ToString("e"); //生成 1.234500e 004 12345.ToString("f4"); //生成 12345.0000 12345.ToString("x"); //生成 3039 (16进制) 12345.ToString("p"); //生成 1,234,500.00% * 字串变量.Replace("子字串","替换为") 字串替换 如: string str="中国"; str=str.Replace("国","央"); //将国字换为央字 Response.Write(str); //输出结果为“中央” * 一、 1、取字符串的前i个字符 (1)string str1=str.Substring(0,i); (2)string str1=str.Remove(i,str.Length-i); 2、去掉字符串的前i个字符 string str1=str.Remove(0,i); string str1=str.SubString(i); 3.提取字符串中的第i个字符开始的长度为j的字符串; string str = "GTAZB_JiangjBen_123"; int start=3,length=8; Console.WriteLine(str.Substring(start-1, length)); 4、从右边开始去掉i个字符: string str1=str.Substring(0,str.Length-i); string str1=str.Remove(str.Length-i,i); 5、 6 、如果字符串中有"abc"则替换成"ABC" str=str.Replace("abc","ABC"); 7、c#截取字符串最后一个字符的问题!!!!!!!!!!!!!!!!!!!!! str1.Substring(str1.LastIndexOf(",") 1); 8、C# 截取字符串最后一个字符 k = k.Substring(k.Length-1, 1); */ using System.IO.Ports; namespace SerialCommunicate { public partial class Form1 : Form { byte[] bufferID = new byte[8]; bool Button1Statue; int count,idcount,timecnt, num32; string RecordID1, RecordID2, RecordID3; public delegate void DeleUpdateTextbox(string dateRe); public int iTextbox1 = 0; public Form1() { InitializeComponent(); System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; } private void SearchAndAddSerialToComboBox(SerialPort MyPort, ComboBox MyBox) { //将可用端口号添加到ComboBox string[] MyString = new string[20]; //最多容纳20个,太多会影响调试效率 string Buffer; //缓存 MyBox.Items.Clear(); //清空ComboBox内容 for (int i = 1; i < 20; i ) //循环 { try //核心原理是依靠try和catch完成遍历 { Buffer = "COM" i.ToString(); MyPort.PortName = Buffer; MyPort.Open(); //如果失败,后面的代码不会执行 MyString[i - 1] = Buffer; MyBox.Items.Add(Buffer); //打开成功,添加至下俩列表 MyPort.Close(); //关闭 } catch { } } MyBox.Text = MyString[0]; //初始化 } private void button1_Click(object sender, EventArgs e) { if (serialPort1.IsOpen) //串口打开就关闭 { try { serialPort1.Close(); } catch { } //确保万无一失 //button1.Text = "打开串口"; button1.BackgroundImage = Properties.Resources.light_off; //灭 Button1Statue = false; } else { try { serialPort1.PortName = comboBox1.Text; //端口号 serialPort1.BaudRate = 19200; serialPort1.Open(); //打开端口 //button1.Text = "关闭串口"; button1.BackgroundImage = Properties.Resources.on;//亮 Button1Statue = true; } catch { MessageBox.Show("串口打开失败", "错误"); } } } 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);//必须手动添加事件处理程序 SearchAndAddSerialToComboBox(serialPort1, comboBox1); count = 0; idcount = 0; timecnt = 0; label8.Text = Convert.ToString(System.DateTime.Now); } //使用Control.Invoke private void button4_Click(object sender, EventArgs e) { SearchAndAddSerialToComboBox(serialPort1, comboBox1); } private void UpdateTextbox(string dataRe) { if (iTextbox1 == 0) { this.textBox1.Text = dataRe; iTextbox1 ; } else { textBox1.AppendText(dataRe); } } private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)//串口数据接收事件 { /* int readSize = serialPort1.ReadBufferSize;//获取串口接收到的字节数 byte[] buffer = new byte[readSize];// 重新定义缓存区大小 serialPort1.Read(buffer,0,readSize); //string str = System.Text.Encoding.Default.GetString(buffer);//byte[]转成string: //byte[] byteArray = System.Text.Encoding.Default.GetBytes(str);//string类型转成byte[] //frm1.changed = new Form1.Form1Handle(button_changed);//改变值的事件 public delegate void Form2Handle(string s); string str2 = Convert.ToString(buffer[0], 16).ToUpper();//转换为大写十六进制字符串 */ string dataRe; byte[] byteRead = new byte[serialPort1.BytesToRead];// 重新定义 DeleUpdateTextbox deleupdatetextbox = new DeleUpdateTextbox(UpdateTextbox); serialPort1.Read(byteRead, 0, byteRead.Length);//获取串口接收到的字 /* if (!radioButton3.Checked)//如果接收模式为字符模式 { dataRe = Encoding.Default.GetString(byteRead);//接收的byte[]转成string: textBox2.Invoke(deleupdatetextbox, dataRe); } else {*/ for (int i = 0; i < byteRead.Length; i ) { byte temp = byteRead[i]; dataRe = temp.ToString("X2");// " ";//字符串格式控制符 X为十六进制 2为每次都是两位数 textBox1.Invoke(deleupdatetextbox, dataRe); } /* if (dataRe == "55") { for (int j = 0; j < byteRead.Length; j ) { bufferID[j] = byteRead[i]; } }*/ //textBox1.AppendText(dataRe); // } } private void button5_Click(object sender, EventArgs e) { for (int i = 0; i <3; i ) { textBox2.Text = bufferID[0].ToString(); } } private void textBox1_TextChanged(object sender, EventArgs e) { string str, str2; int len; // str2 = "55"; len = textBox1.Text.Length; if (len >= 16) { str = textBox1.Text.Substring(0, 3); if (str=="55A") { idcount = 1; RecordID1 = textBox1.Text.Substring(6, 6); textBox4.Text = textBox1.Text.Substring(4, 2); if (textBox3.Text != RecordID1) idcount = 0; textBox3.Text = RecordID1; label1.Text = idcount.ToString(); // j = textBox1.Text.Substring(0, 2); count = 1; switch (count) { case 1: RecordID1 = textBox1.Text.Substring(6, 6); break; case 2: RecordID2 = textBox1.Text.Substring(6, 6); break; case 3: RecordID3 = textBox1.Text.Substring(6, 6); break; } if (count >= 3) { count = 0; if ((String.Equals(RecordID1, RecordID2))&&(String.Equals(RecordID3, RecordID2))) { idcount = 1; if (idcount > 99) idcount = 0; label1.Text = idcount.ToString(); textBox3.Text = RecordID1; str2 = RecordID1.Substring(1, 5);//get 1-5 int dectemp = Convert.ToInt32(str2, 16); //转成10进制 textBox5.Text = Convert.ToString(dectemp);//change to string from dec if (textBox5.TextLength < 5) textBox5.Text = "000" Convert.ToString(dectemp); else if (textBox5.TextLength <6) textBox5.Text = "00" Convert.ToString(dectemp); else if (textBox5.TextLength < 7) textBox5.Text = "0" Convert.ToString(dectemp); else textBox5.Text = Convert.ToString(dectemp); str2 = textBox5.Text; str2 = str2.Insert(3, "-"); textBox5.Text = str2; /* if (!radioButton5.Checked)//2gig { str = RecordID1.Substring(0, 1);//get 1-5 int dec2 = Convert.ToInt32(str, 16); //转成10进制 str=Convert.ToString(dec2); textBox5.Text = str "-" textBox5.Text; }*/ } else { //textBox1.Text = ""; //textBox3.Text = ""; idcount = 0; } RecordID1 = ""; RecordID2 = ""; RecordID3 = ""; } } textBox1.Text = ""; } if (len < 16) { timer1.Enabled = true; } } private void textBox3_TextChanged(object sender, EventArgs e) { /* string str; int len; len = textBox3.Text.Length; str = textBox3.Text; if (len >= 16) { if (str!= RecordID1) textBox3.Text = ""; }*/ } private void timer1_Tick(object sender, EventArgs e) { string j; int len; bool result; // str = textBox1.Text; len = textBox1.Text.Length; timecnt = 1; if (timecnt > 6) { timer1.Enabled = false; textBox1.Text = ""; timecnt = 0; } // for (int i = 0; i < 3; i ) // { textBox5.Text = bufferID[i].ToString(); } /* string str2 = Convert.ToString(bufferID[0], 16).ToUpper();//转换为大写十六进制字符串 textBox3.AppendText(str2); str2 = Convert.ToString(bufferID[1], 16).ToUpper();//转换为大写十六进制字符串 textBox3.AppendText(str2); str2 = Convert.ToString(bufferID[2], 16).ToUpper();//转换为大写十六进制字符串 textBox3.AppendText(str2);*/ /*if (len >= 16) { textBox3.Text = ""; for (int i = 0; i < 8; i ) { string str2 = Convert.ToString(bufferID[i], 16).ToUpper();//转换为大写十六进制字符串 textBox3.AppendText(str2); } // j = textBox1.Text.Substring(0, 2); count = 1; switch (count) { case 1: RecordID1 = textBox1.Text.Substring(6, 6); break; case 2: RecordID2 = textBox1.Text.Substring(6, 6); break; case 3: RecordID3 = textBox1.Text.Substring(6, 6); break; } if (count >= 3) { count = 0; if ((String.Equals(RecordID1, RecordID2))&&(String.Equals(RecordID3, RecordID2))) { idcount = 1; if (idcount > 99) idcount = 0; label1.Text = idcount.ToString(); textBox3.Text = RecordID1; } else { textBox1.Text = ""; textBox3.Text = ""; idcount = 0; } RecordID1 = ""; RecordID2 = ""; RecordID3 = ""; } }*/ } private void radioButton2_CheckedChanged(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { try { serialPort1.Close();//关闭串口 // button1.Enabled = true;//打开串口按钮可用 // button2.Enabled = true;//关闭串口按钮不可用 this.Close(); } 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 )//取余3运算作用是防止用户输入的字符为奇数个 { Data[0] = Convert.ToByte(textBox2.Text.Substring(i * 2, 2), 16); serialPort1.Write(Data, 0, 1);//循环发送(如果输入字符为0A0BB,则只发送0A,0B) } if (textBox2.Text.Length % 2 != 0)//剩下一位单独处理 { Data[0] = Convert.ToByte(textBox2.Text.Substring(textBox2.Text.Length-1, 1), 16);//单独发送B(0B) serialPort1.Write(Data, 0, 1);//发送 } } } } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { } private void groupBox1_Enter(object sender, EventArgs e) { } private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) { } } }
标签: 解码
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论