|
分享學(xué)習(xí)一下
51hei.png (6.05 KB, 下載次數(shù): 79)
下載附件
2019-11-3 16:31 上傳
單片機(jī)源程序如下:
- 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();
- System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- serialPort1.PortName = comboBox1.Text;
- serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text,10);//十進(jìn)制數(shù)據(jù)轉(zhuǎn)換
- serialPort1.Open();
- button1.Enabled = false;//打開串口按鈕不可用
- button2.Enabled = true;//關(guān)閉串口
- }
- catch {
- MessageBox.Show("端口錯(cuò)誤,請(qǐng)檢查串口", "錯(cuò)誤");
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- for (int i = 1; i < 20; i++)
- {
- comboBox1.Items.Add("COM" + i.ToString());
- }
- comboBox1.Text = "COM1";//串口號(hào)多額默認(rèn)值
- comboBox2.Text = "9600";//波特率默認(rèn)值
-
- /*****************非常重要************************/
-
- serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);//必須手動(dòng)添加事件處理程序
- }
- private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)//串口數(shù)據(jù)接收事件
- {
- if (!radioButton3.Checked)//如果接收模式為字符模式
- {
- string str = serialPort1.ReadExisting();//字符串方式讀
- textBox1.AppendText(str);//添加內(nèi)容
- }
- else { //如果接收模式為數(shù)值接收
- byte data;
- data = (byte)serialPort1.ReadByte();//此處需要強(qiáng)制類型轉(zhuǎn)換,將(int)類型數(shù)據(jù)轉(zhuǎn)換為(byte類型數(shù)據(jù),不必考慮是否會(huì)丟失數(shù)據(jù)
- string str = Convert.ToString(data, 16).ToUpper();//轉(zhuǎn)換為大寫十六進(jìn)制字符串
- textBox1.AppendText("0x" + (str.Length == 1 ? "0" + str : str) + " ");//空位補(bǔ)“0”
- //上一句等同為:if(str.Length == 1)
- // str = "0" + str;
- // else
- // str = str;
- // textBox1.AppendText("0x" + str);
- }
- }
- private void radioButton2_CheckedChanged(object sender, EventArgs e)
- {
- }
- private void button2_Click(object sender, EventArgs e)
- {
- try
- {
- serialPort1.Close();//關(guān)閉串口
- button1.Enabled = true;//打開串口按鈕可用
- button2.Enabled = false;//關(guān)閉串口按鈕不可用
- }
- catch (Exception err)//一般情況下關(guān)閉串口不會(huì)出錯(cuò),所以不需要加處理程序
- {
- }
- }
- private void button3_Click(object sender, EventArgs e)
- {
- byte[] Data = new byte[1];//作用同上集
- if (serialPort1.IsOpen)//判斷串口是否打開,如果打開執(zhí)行下一步操作
- {
- if (textBox2.Text != "")
- {
- if (!radioButton1.Checked)//如果發(fā)送模式是字符模式
- {
- try
- {
- serialPort1.WriteLine(textBox2.Text);//寫數(shù)據(jù)
- }
- catch (Exception err)
- {
- MessageBox.Show("串口數(shù)據(jù)寫入錯(cuò)誤", "錯(cuò)誤");//出錯(cuò)提示
- serialPort1.Close();
- button1.Enabled = true;//打開串口按鈕可用
- button2.Enabled = false;//關(guān)閉串口按鈕不可用
- }
- }
- else
- {
- for (int i = 0; i < (textBox2.Text.Length - textBox2.Text.Length % 2) / 2; i++)//取余3運(yùn)算作用是防止用戶輸入的字符為奇數(shù)個(gè)
- {
- Data[0] = Convert.ToByte(textBox2.Text.Substring(i * 2, 2), 16);
- serialPort1.Write(Data, 0, 1);//循環(huán)發(fā)送(如果輸入字符為0A0BB,則只發(fā)送0A,0B)
- }
- if (textBox2.Text.Length % 2 != 0)//剩下一位單獨(dú)處理
- {
- Data[0] = Convert.ToByte(textBox2.Text.Substring(textBox2.Text.Length-1, 1), 16);//單獨(dú)發(fā)送B(0B)
- serialPort1.Write(Data, 0, 1);//發(fā)送
- }
- }
- }
- }
- }
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
SerialCommunicate.zip
(238.24 KB, 下載次數(shù): 49)
2019-11-3 16:28 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|