-
大小: 205KB文件類型: .7z金幣: 1下載: 1 次發(fā)布日期: 2021-05-10
- 語(yǔ)言: C#
- 標(biāo)簽:
資源簡(jiǎn)介
C#串口通訊上位機(jī)源碼(打包為7z格式),源碼為Visual Studio的C#工程,非常適合想通過C#編寫上位機(jī)的初學(xué)者參考學(xué)習(xí)。代碼包含串口上位機(jī)的基本功能,如可用串口檢測(cè)、收發(fā)字符和Hex數(shù)據(jù)、保存上次使用的串口號(hào)、收發(fā)數(shù)據(jù)量記錄等。此代碼為本人學(xué)習(xí)C#時(shí)編寫的,參考了他人的教程,并在其基礎(chǔ)上加以改進(jìn)。此工程源碼可二次開發(fā),添加代碼擴(kuò)展為你需要的串口上位機(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;
using?System.Runtime.InteropServices;
namespace?Serial_Communicate
{
????public?partial?class?Form1?:?Form
????{
????????int?Received_Data_Cnt?=?0?Transmit_Data_Cnt=0;
????????string?CurrentPortName;
????????[DllImport(“kernel32“)]
????????private?static?extern?long?WritePrivateProfileString(string?section?string?key?string?val?string?filePath);//系統(tǒng)dll導(dǎo)入ini寫函數(shù)
????????[DllImport(“kernel32“)]
????????private?static?extern?int?GetPrivateProfileString(string?section?string?key?string?def?StringBuilder?retVal?int?size?string?filePath);//系統(tǒng)dll導(dǎo)入ini讀函數(shù)
????????string?FileName?=?System.AppDomain.CurrentDomain.baseDirectory?+?“data.ini“;//ini文件名
????????StringBuilder?temp?=?new?StringBuilder(255);//存儲(chǔ)讀出ini內(nèi)容變量
????????public?Form1()
????????{
????????????InitializeComponent();
????????????System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls?=?false;
????????????serialPort1.Encoding?=?Encoding.GetEncoding(“GB2312“);//加入漢字支持
????????}
????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
????????????SearchAndAddSerialToComboBox(serialPort1?comboBox1);//查找可用串口
????????????comboBox2.Text?=?“115200“;
????????????serialPort1.DataReceived?+=?new?SerialDataReceivedEventHandler(port_DataReceived);//串口事件處理程序聲明
????????????GetPrivateProfileString(“PortData“?“PortName“?“COM1“?temp?256?FileName);//讀取ini值,默認(rèn)是COM1
????????????comboBox1.Text?=?temp.ToString();//初始化
????????}
????????private?void?port_DataReceived(object?sender?SerialDataReceivedEventArgs?e)//串口數(shù)據(jù)接收事件
????????{
????????????Received_Data_Cnt?+=?serialPort1.BytesToRead;//記錄接收字節(jié)數(shù)
????????????Received_Data_Cnt?=?Received_Data_Cnt?>?9999???9999?:?Received_Data_Cnt;//限幅
????????????label5.Text?=?“RX:“+Received_Data_Cnt.ToString();//顯示到lable中
????????????if?(!radioButton3.Checked)//接收模式為接收字符
????????????{
????????????????textBox1.AppendText(serialPort1.ReadExisting()+“?“);?//將讀取到的字符串添加到窗口顯示
????????????}
????????????else//接收模式為接收數(shù)值
????????????{
????????????????byte[]?data?=?new?byte[serialPort1.BytesToRead];//定義緩沖區(qū),串口事件觸發(fā)時(shí)有可能收到不止一個(gè)字節(jié)
????????????????serialPort1.Read(data?0?data.Length);
????????????????foreach?(byte?Member?in?data)//遍歷用法
????????????????{
????????????????????string?str?=?Convert.ToString(Member16).ToUpper();//轉(zhuǎn)換為大寫16進(jìn)制字符串
????????????????????textBox1.AppendText(“0x“?+?(str.Length?==?1???“0“?+?str?:?str)?+?“?“);
????????????????}
????????????}
????????}
????????private?void?button1_Click(object?sender?EventArgs?e)//打開串口
????????{
????????????if?(!serialPort1.IsOpen)//串口未開
????????????{
????????????????try
????????????????{
????????????????????serialPort1.BaudRate?=?Convert.ToInt32(comboBox2.Text?10);//串口波特率設(shè)置
????????????????????serialPort1.PortName?=?comboBox1.Text;/
評(píng)論
共有 條評(píng)論