資源簡介
C#串口通訊上位機源碼(打包為7z格式),源碼為Visual Studio的C#工程,非常適合想通過C#編寫上位機的初學者參考學習。代碼包含串口上位機的基本功能,如可用串口檢測、收發字符和Hex數據、保存上次使用的串口號、收發數據量記錄等。此代碼為本人學習C#時編寫的,參考了他人的教程,并在其基礎上加以改進。此工程源碼可二次開發,添加代碼擴展為你需要的串口上位機。
代碼片段和文件信息
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);//系統dll導入ini寫函數
????????[DllImport(“kernel32“)]
????????private?static?extern?int?GetPrivateProfileString(string?section?string?key?string?def?StringBuilder?retVal?int?size?string?filePath);//系統dll導入ini讀函數
????????string?FileName?=?System.AppDomain.CurrentDomain.baseDirectory?+?“data.ini“;//ini文件名
????????StringBuilder?temp?=?new?StringBuilder(255);//存儲讀出ini內容變量
????????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值,默認是COM1
????????????comboBox1.Text?=?temp.ToString();//初始化
????????}
????????private?void?port_DataReceived(object?sender?SerialDataReceivedEventArgs?e)//串口數據接收事件
????????{
????????????Received_Data_Cnt?+=?serialPort1.BytesToRead;//記錄接收字節數
????????????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//接收模式為接收數值
????????????{
????????????????byte[]?data?=?new?byte[serialPort1.BytesToRead];//定義緩沖區,串口事件觸發時有可能收到不止一個字節
????????????????serialPort1.Read(data?0?data.Length);
????????????????foreach?(byte?Member?in?data)//遍歷用法
????????????????{
????????????????????string?str?=?Convert.ToString(Member16).ToUpper();//轉換為大寫16進制字符串
????????????????????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);//串口波特率設置
????????????????????serialPort1.PortName?=?comboBox1.Text;/
評論
共有 條評論