C#之windows桌面软件第九课:汉字串口助手
生活随笔
收集整理的這篇文章主要介紹了
C#之windows桌面软件第九课:汉字串口助手
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
? ? ? ? C#之windows桌面軟件第九課:漢字串口助手
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.IO.Ports; namespace SerialCommunicate {public partial class Form1 : Form{public Form1(){InitializeComponent();serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); //串口數(shù)據(jù)接收事件serialPort1.Encoding = Encoding.GetEncoding("GB2312"); //設(shè)置串口接收和發(fā)送數(shù)據(jù)格式的編碼方式為GB2312System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; }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 Form1_Load(object sender, EventArgs e){for (int i = 1; i < 20; i++){comboBox1.Items.Add("COM" + i.ToString()); //添加串口}comboBox1.Text = "COM1"; //默認選項comboBox2.Text = "9600";}/*注意:操作系統(tǒng)的串口是非實時性的,所以一次串口中斷可能發(fā)送多個字節(jié),而單片機是一個實時系統(tǒng),且每接受一個字節(jié)就中斷一次。 *///串口接收事件private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) {if (!radioButton3.Checked)//如果是字符串{textBox1.AppendText(serialPort1.ReadExisting()); //串口類會自動處理漢字,所以不需要特別轉(zhuǎn)換}else{//serialPort1.BytesToRead得到中斷讀取的字節(jié)長度(即得到緩沖區(qū)區(qū)中數(shù)據(jù)的長度)byte[] data = new byte[serialPort1.BytesToRead]; //定義緩沖區(qū),因為串口事件觸發(fā)時有可能收到不止一個字節(jié)serialPort1.Read(data, 0, data.Length);//data.Length等同data.Length。從serialPort1輸入緩沖區(qū)讀取一些字節(jié)并將那些字節(jié)寫入字節(jié)數(shù)組中指定的偏移量處。foreach (byte Member in data) {string str = Convert.ToString(Member, 16).ToUpper();textBox1.AppendText("0x" + (str.Length == 1 ? "0" + str : str) + " ");}}}private void button2_Click(object sender, EventArgs e){try{serialPort1.Close(); //關(guān)閉串口 button1.Enabled = true;button2.Enabled = false;}catch{}}private void button3_Click(object sender, EventArgs e){byte[] Data = new byte[1]; //單字節(jié)發(fā)數(shù)據(jù) if (serialPort1.IsOpen){if (textBox2.Text != ""){if (!radioButton1.Checked){try{serialPort1.Write(textBox2.Text);//serialPort1.WriteLine(); //字符串寫入}catch{MessageBox.Show("串口數(shù)據(jù)寫入錯誤", "錯誤");}}else //數(shù)據(jù)模式{try //如果此時用戶輸入字符串中含有非法字符(字母,漢字,符號等等,try,catch塊可以捕捉并提示){for (int i = 0; i < (textBox2.Text.Length - textBox2.Text.Length % 2) / 2; i++)//轉(zhuǎn)換偶數(shù)個{Data[0] = Convert.ToByte(textBox2.Text.Substring(i * 2, 2), 16); //轉(zhuǎn)換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); //寫入}//Data = Convert.ToByte(textBox2.Text.Substring(textBox2.Text.Length - 1, 1), 16);// }}catch{MessageBox.Show("數(shù)據(jù)轉(zhuǎn)換錯誤,請輸入數(shù)字。", "錯誤");}}}}}} }?
www.DoYoung.net(部分代碼來至杜洋工作室)
《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的C#之windows桌面软件第九课:汉字串口助手的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#之windows桌面软件第八课:汉字
- 下一篇: C#之windows桌面软件第十课:电脑