標籤:.text 格式 smart big 字元 iso reset 代碼 需要
C#-WinForm 串口通訊
//C# 的串口通訊,是採用serialPort控制項,下面是對serialPort控制項(也是串口通訊必備資訊)的配置如下代碼:
serialPort1.PortName = commcomboBox1.Text;
serialPort1.BaudRate = int.Parse(baudcomboBox2.Text);
serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity),efficacycomboBox3.Text);
serialPort1.DataBits = int.Parse(databitcomboBox4.Text);
serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits),stopbitcomboBox5.Text);
//PortName:是所用串口的名稱,一般當首次連入串口通訊裝置時,都會提示採用了哪個COM。
//BaudRate:傳輸速率,一般採用值有300,600,1200,2400,4800,9600,14400,28800,36000,115000等。
//Parity:效驗位,一般採用值有None,Even,Odd。
//DataBits:資料位元,一般採用值有5,6,7,8。
//StopBits:停止位,一般採用值有1,2,3。
//開啟串口的代碼如下:
/*前面為串口基礎資訊的配置,這裡為開啟串口*/
if (!serialPort1.IsOpen)
{
try
{
serialPort1.Open();
}
catch (Exception)
{
MessageBox.Show("PortAccess is failure,Reset The Serial BasicInformation", "Prompting");
}
}
//發送串口內容的代碼如下:
/*發送字串str的格式就需要根據具體的串口裝置協議來定*/
string str = "";
serialPort1.Write(str);
byte[] sendByte = Encoding.BigEndianUnicode.GetBytes(str.ToCharArray());
serialPort1.Write(sendByte, 0, sendByte.Length);
From <http://www.cnblogs.com/smartsmile/p/6234214.html>
C#-WinForm 串口通訊