串口通信學習

//前提須要數據庫

//須要一個 serialPort 工具 可在vs自帶的工具欄中得到數組

//源代碼加串口工具地址:app

//連接:https://pan.baidu.com/s/1YbfvdXEmfsJX87D-Jxljyg   提取碼:d32x工具

 

//記錄用戶打開的串口號 可改成泛型模式學習

string serialPortName;ui

//通常電腦是沒有串口號的,因此要用個軟件建立虛擬串口號this

//軟件名 - Configure Virtual Serial Port Driver編碼

 

波特率設置的值爲 1382400,921600,460800,256000,230400,128000,115200,76800,57600,spa

43000,38400,19200,14400,9600,4800,1200線程

中止位設置的值爲:1,1.5,2

數據位設置的值爲:8,7,6,5

校驗位設置的值爲:無,奇校驗,偶校驗

 

步驟:

1.設置串口的基本數據 (串口號,波特率,數據位,中止位,校驗位)

2.使用Open打開串口

3.使用串口工具的DataReceived()事件進行數據的接收

4.建立發送數據 Write() ,通常發送的是byte數組

5.使用Close關閉串口

 

界面:

 

 

 

//一

//在窗口Load事件中,設置串口基本數據的默認值

//1.拿到電腦上可使用的串口號,並賦值給窗口的串口下拉列表中,默認選項索引爲0

string[] prots = System.IO.Ports.SerialPort.GetPortNames();

this.cbPort.Items.AddRange(prots);

this.cbPort.SelectedIndex = this.cbPort.Items.Count > 0 ? 0 : -1;

//2.設置波特率,中止位,數據位,校驗位的默認值。

this.cbBaud.Text = "115200";

this.cbStopBit.Text = "1";

this.cbDataBit.Text = "8";

this.cbComparable.Text = "無";

 

 

//二

//設置打開串口和關閉串口按鈕的單擊事件

//爲串口拿到基本數據

serialPort .PortName = this.cbPort.Text;//串口號

serialPortName = this.cbPort.Text;//記錄用戶打開的串口號

 

serialPort.BaudRate = int.Parse(this.cbBaud.Text);//波特率

serialPort.DataBits = int.Parse(this.cbDataBit.Text);//數據位

//設置中止位

if (this.cbStopBit.Text == "1") { serialPort.StopBits = StopBits.One; }

else if (this.cbStopBit.Text == "1.5") { serialPort.StopBits = StopBits.OnePointFive; }

else if (this.cbStopBit.Text == "2") { serialPort.StopBits = StopBits.Two; }

//設置奇偶校驗

if (this.cbComparable.Text == "無") { serialPort.Parity = Parity.None; }

else if (this.cbComparable.Text == "奇校驗") { serialPort.Parity = Parity.Odd; }

else if (this.cbComparable.Text == "偶校驗") { serialPort.Parity = Parity.Even; }

 

//打開串口 只須要使用Open打開串口

serialPort1.Open();

 

this.btnOpenStat.Text = "關閉串口";

 

//若是按鈕的Text爲 關閉串口的話

serialPort.Close();//Close() - 關閉串口

 

 

//三 本次串口的簡單數據協議 1 - 字符串 bp - 醫療數據 2 - 文件

//經過串口工具自帶的DataReceived事件進行串口的數據接收

//此次的是本身定義的數據協議,通常都是用公司默認的協議 或者RS232和RS485

//獲取能夠讀取的字節數 默認最大值是4096 若是想要更改能夠直接改變串口的ReadBufferSize屬性

int len = serialPort.BytesToRead;

if(len == serialPort.ReadBufferSize){

MessageBox.Show("程序最多隻能接受"+serialPort1.ReadBufferSize+"字節的數據!","提示");

return;

}

//拿到數據,Read()

byte[] buff = new byte[len];

serialPort.Read(buff,0,len);

//若是選中了顯示時間就賦值,沒有就算

string DateNow = string.Empty;

//根據協議進行判斷

if(buff[0]==1){

//拿出原來的數據並去除協議部分 而後放入另外一個byte[]數組中

byte[] result = new byte[len];

Buffer.BlockCopy(buff,1,result,0,buff.Length-1);

//將byte數組保存可讀string 根據本身的編碼格式進行轉碼

string str = Encoding.Default.GetString(result);

//將數據分割爲string[] 判斷是否爲醫療數據

string[] strPle = str.Split(',');

if(strPle[0]=="bp"){

//放入方法中,從新拼裝成可用數據

str = RecHBpData(strPle);

}

//因爲咱們的接收數據的事件是在一個子線程裏面的,

//因此須要Invoke才能給咱們主線程建立的控件賦值

//固然你也可使用InvokeRequired()來判斷是否爲主線程建立的控件,我這裏就沒使用了

Invoke(new Action(() => {

//是否顯示時間

if(this.ckDateTime.Checked){

DateNow = "\r\n" + DateTime.Now.ToString();

}

//是否爲16進制顯示

if(this.checkBox1.Checked){

this.txtMsg.AppendText(DateNow + "\r\n" + byteToHexStr(buff));

}

//默認是使用字符串顯示

else{

this.txtMsg.AppendText(DateNow + "\r\n" + str);

}

 

})));

}

//若是傳送的文件的話

else if(buff[0]==2){

ProcessRecieveFile(buff);

}

 

//通常呢,這些方法是放在一個方法類庫裏面,可是我懶得放就在一個類裏面了

#region 封裝的方法

 

/// <summary>

/// 傳輸的是文件就進行文件的操做

/// </summary>

/// <param name="data">數據流</param>

public void ProcessRecieveFile(byte[] data)

{

using (SaveFileDialog dialog = new SaveFileDialog())

{

dialog.DefaultExt = "txt";

dialog.Filter = "文本文件(*.txt)|*.txt|全部文件(*.*)|*.*";

//因爲大部分可能不是主線程,因此咱們要加this,否則不會彈出保存框

if (dialog.ShowDialog(this) != DialogResult.OK)

{

return;

}

byte[] result = new byte[data.Length - 1];

Buffer.BlockCopy(data, 1, result, 0, data.Length - 1);

File.WriteAllBytes(dialog.FileName, result);

}

}

 

#region 封裝 數據定義的協議

 

public static string RecHBpData(string[] strPle)

{

string str = string.Empty;

 

//判斷儀器

str += $"{StaticConstant.DicMedicalnoun[strPle[1]]} ";

 

//增長時間

str += " " + strPle[2] + " " + strPle[3] + "\r\n";

//增長具體數據

str += $"{StaticConstant.DicMedicalnoun[strPle[4]]}: {strPle[5].TrimStart('0')} \r\n{StaticConstant.DicMedicalnoun[strPle[6]]}: {strPle[7].TrimStart('0')}";

 

 

 

 

 

return str;

}

 

 

#endregion

 

 

/// <summary>

/// 字節數組轉16進制字符串

/// </summary>

/// <param name="bytes">byte數組</param>

/// <returns>16進制顯示形式</returns>

public static string byteToHexStr(byte[] bytes)

{

string retuenStr = "";

try

{

if (bytes != null)

{

for (int i = 0; i < bytes.Length; i++)

{

retuenStr += bytes[i].ToString("X2") + " ";//變成16進制,兩個中間用空格隔開

}

}

return retuenStr;

}

catch (Exception ex)

{

return retuenStr;

}

}

 

/// <summary>

/// 字符串轉16進制格式,不夠自動前面補0

/// </summary>

/// <param name="hexString"></param>

/// <returns></returns>

public static byte[] strToHexByte(string hexString)

{

int i;

if ((hexString.Length % 2) != 0)

{//奇數個

byte[] returnBytes = new byte[(hexString.Length + 1) / 2];

try

{

for (i = 0; i < hexString.Length - 1; i++)

{

returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);

i = i * 2;

}

returnBytes[returnBytes.Length - 1] = Convert.ToByte(hexString.Substring(hexString.Length - 1, 1).PadLeft(2, '0'), 16);

}

catch (Exception)

{

MessageBox.Show("含有非16進制字符", "提示");

return null;

}

return returnBytes;

}

else

{

byte[] returnBytes = new byte[(hexString.Length) / 2];

try

{

for (i = 0; i < hexString.Length; i++)

{

returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);

}

}

catch (Exception)

{

MessageBox.Show("含有非16進制字符", "提示");

return null;

}

return returnBytes;

}

}

 

#endregion

 

 

//個人醫療數據的封裝是放在配置文件中,通常咱們都是放在數據庫中,不過我就作個小學習工具就 //沒作到數據庫裏面了

 

//四 發送數據

//發送數據,我這邊分爲發送文件和發送字符串

//1.發送字符串

string str = this.txtSendMsg.Text.Trim().Tostring();

try{

if(str.Length > 0){

//16進制發送

if(this.ckSend.Checked){

byte[] byt = strToHexBytes(str);

byte[] result = new byte[byt.Length+1];

//加上數據協議

result[0] = 1;

//數據轉移

Buffer.BlockCopy(byt,0,result,1,byt.Lengt);

//發送數據,byte形式發送

serialPort.Write(result,0,result.Length);

}

//默認字符串發送

else{

//根據本身的編碼格式編碼

byte[] dataByte = Encoding.Default.GetBytes(str);

byte[] result = new byte[dataByte.Length + 1];

//加上數據協議

result[0] = 1;

Buffer.BlockCopy(dataByte ,0,result,1,dataByte .Lengt);

//發送數據,byte形式發送

serialPort.Write(result,0,result.Length);

}

}

}catch(Exception ex){}

 

//2.發送文件

try{

using(OpenFileDialog dialog = new OpenFileDialog()){

if(dialog.ShowDialog(this) != DialogResult.OK){

return;

}

byte[] data = File.ReadAllBytes(dialog.FileName);

byte[] result = new byte[data.Length + 1];

result[0] = 2;

Buffer.BlockCopy(data,0,result,1,data.Length);

serialPort.Write(result,0,result.Lenght);

}

}catch(Exception ex){

MessageBox.Show(ex.Message);

}

相關文章
相關標籤/搜索