C#串口通信程序?qū)嵗斀?/h1>
更新時(shí)間:2013年12月02日 10:43:20 作者:
在.NET平臺(tái)下創(chuàng)建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空間是System.IO.Ports,創(chuàng)建C#串口通信程序的具體實(shí)現(xiàn)是如何的呢?讓我們開(kāi)始吧
創(chuàng)建C#串口通信程序之命名空間
System.IO.Ports命名空間中最重用的是SerialPort 類。
創(chuàng)建C#串口通信程序之創(chuàng)建SerialPort 對(duì)象
通過(guò)創(chuàng)建SerialPort 對(duì)象,我們可以在程序中控制串口通信的全過(guò)程。
我們將要用到的SerialPort 類的方法:
ReadLine():從輸入緩沖區(qū)讀一新行的值,如果沒(méi)有,會(huì)返回NULL
WriteLine(string):寫(xiě)入輸出緩沖
Open():打開(kāi)一個(gè)新的串口連接
Close():關(guān)閉
復(fù)制代碼 代碼如下:
SerialPort sp = new SerialPort ();
默認(rèn)情況下,DataBits 值是8,StopBits 是1,通信端口是COM1。這些都可以在下面的屬性中重新設(shè)置:
BaudRate:串口的波特率
StopBits:每個(gè)字節(jié)的停止位數(shù)量
ReadTimeout:當(dāng)讀操作沒(méi)有完成時(shí)的停止時(shí)間。單位,毫秒
還有不少其它公共屬性,自己查閱MSDN。
創(chuàng)建C#串口通信程序之串口的硬件知識(shí)
在數(shù)據(jù)傳輸?shù)臅r(shí)候,每個(gè)字節(jié)的數(shù)據(jù)通過(guò)單個(gè)的電纜線傳輸。包包括開(kāi)始位,數(shù)據(jù),結(jié)束為。一旦開(kāi)始位傳出,后面就會(huì)傳數(shù)據(jù),可能是5,6,7或8位,就看你的設(shè)定了。發(fā)送和接收必須設(shè)定同樣的波特率和數(shù)據(jù)位數(shù)。
創(chuàng)建C#串口通信程序之無(wú)貓模式
沒(méi)有Modem模式的電纜只是簡(jiǎn)單地交叉?zhèn)魉秃徒邮站€。同樣DTR & DSR, 和 RTS & CTS也需要交叉。這里,我們?nèi)龡l線。互連2和3(一段的2pin連接3pin),連接兩端的5pin。
創(chuàng)建C#串口通信程序示例程序
如果想使用默認(rèn)屬性,按“Save Status”按鈕,如果想改變屬性按“Property”。設(shè)定好之后,可以通信了。
主窗口的代碼
復(fù)制代碼 代碼如下:
#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;
#endregion
namespace Serialexpample
{
partial class Form1 : Form
{
//create instance of property page
//property page is used to set values for stop bits and
//baud rate
PropertyPage pp = new PropertyPage();
//create an Serial Port object
SerialPort sp = new SerialPort();
public Form1()
{
InitializeComponent();
}
private void propertyButton_Click(object sender, EventArgs e)
{
//show property dialog
pp.ShowDialog();
propertyButton.Hide();
}
private void sendButton_Click(object sender, EventArgs e)
{
try
{
//write line to serial port
sp.WriteLine(textBox.Text);
//clear the text box
textBox.Text = "";
}
catch (System.Exception ex)
{
baudRatelLabel.Text = ex.Message;
}
}
private void ReadButton_Click(object sender, EventArgs e)
{
try
{
//clear the text box
textBox.Text = "";
//read serial port and displayed the data in text box
textBox.Text = sp.ReadLine();
}
catch (System.Exception ex)
{
baudRatelLabel.Text = ex.Message;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show("Do u want to Close the App");
sp.Close();
}
private void startCommButton_Click(object sender, EventArgs e)
{
startCommButton.Hide();
sendButton.Show();
readButton.Show();
textBox.Show();
}
//when we want to save the status(value)
private void saveStatusButton_Click_1(object sender, EventArgs e)
{
//display values
//if no property is set the default values
if (pp.bRate == "" && pp.sBits == "")
{
dataBitLabel.Text = "BaudRate = " +
sp.BaudRate.ToString();
readTimeOutLabel.Text = "StopBits = " +
sp.StopBits.ToString();
}
else
{
dataBitLabel.Text = "BaudRate = " +
pp.bRate;
readTimeOutLabel.Text = "StopBits = " + pp.sBits;
} //創(chuàng)建C#串口通信程序
parityLabel.Text = "DataBits = " +
sp.DataBits.ToString();
stopBitLabel.Text = "Parity = " +
sp.Parity.ToString();
readTimeOutLabel.Text = "ReadTimeout = " +
sp.ReadTimeout.ToString();
if (propertyButton.Visible == true)
propertyButton.Hide();
saveStatusButton.Hide();
startCommButton.Show();
try
{
//open serial port
sp.Open();
//set read time out to 500 ms
sp.ReadTimeout = 500;
}
catch (System.Exception ex)
{
baudRatelLabel.Text = ex.Message;
}
}
}
}
創(chuàng)建C#串口通信程序之屬性設(shè)置對(duì)話框代碼:
復(fù)制代碼 代碼如下:
#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
#endregion
namespace Serialexpample
{
partial class PropertyPage : Form
{
//variables for storing values of baud rate and stop bits
private string baudR = "";
private string stopB = "";
//property for setting and getting baud rate and stop bits
public string bRate
{
get
{
return baudR;
}
set
{
baudR = value;
}
}
public string sBits
{
get
{
return stopB;
}
set
{
stopB = value;
}
}
public PropertyPage()
{
InitializeComponent();
}
private void cancelButton_Click(object sender, EventArgs e)
{
this.bRate = "";
this.sBits = "";
//close form
this.Close();
}
private void okButton_Click_1(object sender, EventArgs e)
{
//here we set the value for stop bits and baud rate.
this.bRate = BaudRateComboBox.Text;
this.sBits = stopBitComboBox.Text;
//
this.Close();
}
}
}
C#串口通信程序創(chuàng)建的相關(guān)內(nèi)容就向你介紹到這里,希望對(duì)你了解創(chuàng)建C#串口通信程序的步驟和需要注意的事宜。
相關(guān)文章
-
C#中多維數(shù)組[,]和交錯(cuò)數(shù)組[][]的區(qū)別
這篇文章介紹了C#中多維數(shù)組[,]和交錯(cuò)數(shù)組[][]的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下 2022-01-01
-
C# 循環(huán)判斷會(huì)進(jìn)來(lái)幾次的實(shí)現(xiàn)代碼
這篇文章主要介紹了C# 循環(huán)判斷會(huì)進(jìn)來(lái)幾次的實(shí)現(xiàn)代碼,代碼中就一個(gè)循環(huán),循環(huán)的判斷是從一個(gè)函數(shù)獲取值,需要的朋友可以參考下 2018-06-06
-
C#中使用IrisSkin2.dll美化WinForm程序界面的方法
這篇文章主要介紹了c#中使用IrisSkin2.dll美化WinForm程序界面的實(shí)現(xiàn)方法,需要的朋友可以參考下 2013-05-05
-
C# 如何在WINForm程序中創(chuàng)建XML文件
這篇文章主要介紹了C# 如何在WINForm程序中創(chuàng)建XML文件,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下 2021-02-02
-
VSCode配置C#運(yùn)行環(huán)境的完整步驟
這篇文章主要給大家介紹了關(guān)于VSCode配置C#運(yùn)行環(huán)境的完整步驟,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧 2020-09-09
-
C#中析構(gòu)函數(shù)、Dispose、Close方法的區(qū)別
本文詳細(xì)對(duì)比了C#中析構(gòu)函數(shù)、Dispose和Close方法的區(qū)別,三者都是釋放資源,本文介紹了他們各自的使用方法和使用場(chǎng)景,希望對(duì)大家有所幫助。 2016-04-04
-
C#實(shí)現(xiàn)求一組數(shù)據(jù)眾數(shù)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)求一組數(shù)據(jù)眾數(shù)的方法,這里以浮點(diǎn)型數(shù)組為例分析了C#求眾數(shù)的算法原理與實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下 2015-08-08
最新評(píng)論
創(chuàng)建C#串口通信程序之命名空間
System.IO.Ports命名空間中最重用的是SerialPort 類。
創(chuàng)建C#串口通信程序之創(chuàng)建SerialPort 對(duì)象
通過(guò)創(chuàng)建SerialPort 對(duì)象,我們可以在程序中控制串口通信的全過(guò)程。
我們將要用到的SerialPort 類的方法:
ReadLine():從輸入緩沖區(qū)讀一新行的值,如果沒(méi)有,會(huì)返回NULL
WriteLine(string):寫(xiě)入輸出緩沖
Open():打開(kāi)一個(gè)新的串口連接
Close():關(guān)閉
SerialPort sp = new SerialPort ();
默認(rèn)情況下,DataBits 值是8,StopBits 是1,通信端口是COM1。這些都可以在下面的屬性中重新設(shè)置:
BaudRate:串口的波特率
StopBits:每個(gè)字節(jié)的停止位數(shù)量
ReadTimeout:當(dāng)讀操作沒(méi)有完成時(shí)的停止時(shí)間。單位,毫秒
還有不少其它公共屬性,自己查閱MSDN。
創(chuàng)建C#串口通信程序之串口的硬件知識(shí)
在數(shù)據(jù)傳輸?shù)臅r(shí)候,每個(gè)字節(jié)的數(shù)據(jù)通過(guò)單個(gè)的電纜線傳輸。包包括開(kāi)始位,數(shù)據(jù),結(jié)束為。一旦開(kāi)始位傳出,后面就會(huì)傳數(shù)據(jù),可能是5,6,7或8位,就看你的設(shè)定了。發(fā)送和接收必須設(shè)定同樣的波特率和數(shù)據(jù)位數(shù)。
創(chuàng)建C#串口通信程序之無(wú)貓模式
沒(méi)有Modem模式的電纜只是簡(jiǎn)單地交叉?zhèn)魉秃徒邮站€。同樣DTR & DSR, 和 RTS & CTS也需要交叉。這里,我們?nèi)龡l線。互連2和3(一段的2pin連接3pin),連接兩端的5pin。
創(chuàng)建C#串口通信程序示例程序
如果想使用默認(rèn)屬性,按“Save Status”按鈕,如果想改變屬性按“Property”。設(shè)定好之后,可以通信了。
主窗口的代碼
#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;
#endregion
namespace Serialexpample
{
partial class Form1 : Form
{
//create instance of property page
//property page is used to set values for stop bits and
//baud rate
PropertyPage pp = new PropertyPage();
//create an Serial Port object
SerialPort sp = new SerialPort();
public Form1()
{
InitializeComponent();
}
private void propertyButton_Click(object sender, EventArgs e)
{
//show property dialog
pp.ShowDialog();
propertyButton.Hide();
}
private void sendButton_Click(object sender, EventArgs e)
{
try
{
//write line to serial port
sp.WriteLine(textBox.Text);
//clear the text box
textBox.Text = "";
}
catch (System.Exception ex)
{
baudRatelLabel.Text = ex.Message;
}
}
private void ReadButton_Click(object sender, EventArgs e)
{
try
{
//clear the text box
textBox.Text = "";
//read serial port and displayed the data in text box
textBox.Text = sp.ReadLine();
}
catch (System.Exception ex)
{
baudRatelLabel.Text = ex.Message;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show("Do u want to Close the App");
sp.Close();
}
private void startCommButton_Click(object sender, EventArgs e)
{
startCommButton.Hide();
sendButton.Show();
readButton.Show();
textBox.Show();
}
//when we want to save the status(value)
private void saveStatusButton_Click_1(object sender, EventArgs e)
{
//display values
//if no property is set the default values
if (pp.bRate == "" && pp.sBits == "")
{
dataBitLabel.Text = "BaudRate = " +
sp.BaudRate.ToString();
readTimeOutLabel.Text = "StopBits = " +
sp.StopBits.ToString();
}
else
{
dataBitLabel.Text = "BaudRate = " +
pp.bRate;
readTimeOutLabel.Text = "StopBits = " + pp.sBits;
} //創(chuàng)建C#串口通信程序
parityLabel.Text = "DataBits = " +
sp.DataBits.ToString();
stopBitLabel.Text = "Parity = " +
sp.Parity.ToString();
readTimeOutLabel.Text = "ReadTimeout = " +
sp.ReadTimeout.ToString();
if (propertyButton.Visible == true)
propertyButton.Hide();
saveStatusButton.Hide();
startCommButton.Show();
try
{
//open serial port
sp.Open();
//set read time out to 500 ms
sp.ReadTimeout = 500;
}
catch (System.Exception ex)
{
baudRatelLabel.Text = ex.Message;
}
}
}
}
創(chuàng)建C#串口通信程序之屬性設(shè)置對(duì)話框代碼:
#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
#endregion
namespace Serialexpample
{
partial class PropertyPage : Form
{
//variables for storing values of baud rate and stop bits
private string baudR = "";
private string stopB = "";
//property for setting and getting baud rate and stop bits
public string bRate
{
get
{
return baudR;
}
set
{
baudR = value;
}
}
public string sBits
{
get
{
return stopB;
}
set
{
stopB = value;
}
}
public PropertyPage()
{
InitializeComponent();
}
private void cancelButton_Click(object sender, EventArgs e)
{
this.bRate = "";
this.sBits = "";
//close form
this.Close();
}
private void okButton_Click_1(object sender, EventArgs e)
{
//here we set the value for stop bits and baud rate.
this.bRate = BaudRateComboBox.Text;
this.sBits = stopBitComboBox.Text;
//
this.Close();
}
}
}
C#串口通信程序創(chuàng)建的相關(guān)內(nèi)容就向你介紹到這里,希望對(duì)你了解創(chuàng)建C#串口通信程序的步驟和需要注意的事宜。
相關(guān)文章
C#中多維數(shù)組[,]和交錯(cuò)數(shù)組[][]的區(qū)別
這篇文章介紹了C#中多維數(shù)組[,]和交錯(cuò)數(shù)組[][]的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01C# 循環(huán)判斷會(huì)進(jìn)來(lái)幾次的實(shí)現(xiàn)代碼
這篇文章主要介紹了C# 循環(huán)判斷會(huì)進(jìn)來(lái)幾次的實(shí)現(xiàn)代碼,代碼中就一個(gè)循環(huán),循環(huán)的判斷是從一個(gè)函數(shù)獲取值,需要的朋友可以參考下2018-06-06C#中使用IrisSkin2.dll美化WinForm程序界面的方法
這篇文章主要介紹了c#中使用IrisSkin2.dll美化WinForm程序界面的實(shí)現(xiàn)方法,需要的朋友可以參考下2013-05-05C# 如何在WINForm程序中創(chuàng)建XML文件
這篇文章主要介紹了C# 如何在WINForm程序中創(chuàng)建XML文件,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-02-02VSCode配置C#運(yùn)行環(huán)境的完整步驟
這篇文章主要給大家介紹了關(guān)于VSCode配置C#運(yùn)行環(huán)境的完整步驟,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09C#中析構(gòu)函數(shù)、Dispose、Close方法的區(qū)別
本文詳細(xì)對(duì)比了C#中析構(gòu)函數(shù)、Dispose和Close方法的區(qū)別,三者都是釋放資源,本文介紹了他們各自的使用方法和使用場(chǎng)景,希望對(duì)大家有所幫助。2016-04-04C#實(shí)現(xiàn)求一組數(shù)據(jù)眾數(shù)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)求一組數(shù)據(jù)眾數(shù)的方法,這里以浮點(diǎn)型數(shù)組為例分析了C#求眾數(shù)的算法原理與實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08