C#-WinForm 串口通訊

來源:互聯網
上載者:User

標籤:.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 串口通訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.