日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

c# 串口 开发

發布時間:2025/3/20 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c# 串口 开发 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在網上查了很多關于串口的知識。后來才發現原來c#里含有對串口開發的控件,害的我白忙活了半天。看來學習方法很重要....首先在窗體添加一個serialPort1控件。沒有的去com組建里找出來。

??????????????? serialPort1.PortName;// 設置串口名稱如“COM1”
??????????????? serialPort1.BaudRate;//設置串口波特率
??????????????? serialPort1.Parity;//奇偶校驗檢查協議
??????????????? serialPort1.ReadTimeout;//讀取操作超時
??????????????? serialPort1.WriteTimeout;//寫入操作超時

serialPort1.Open();//打開串口

serialPort1.Close();//關閉串口

???????????? byte[] data = Encoding.Unicode.GetBytes("9600");
??????????? string str = Convert.ToBase64String(data);
??????????? serialPort1.WriteLine(str); //寫入數據

//串口接收數據,本函數默認串口已經打開
??????? public void serialReceive(SerialPort serialPort1)
??????? {
??????????? string ss = "";
??????????? byte firstByte = Convert.ToByte(serialPort1.ReadByte());
??????????? int bytesRead = serialPort1.BytesToRead;
??????????? byte[] bytesData = new byte[bytesRead + 1];
??????????? bytesData[0] = firstByte;
??????????? for (int i = 1; i <= bytesRead; i++)
??????????? {
??????????????? bytesData[i] = Convert.ToByte(serialPort1.ReadByte());
??????????????? ss = ss + bytesData[i].ToString();
??????????? }
??????????? textBox2.Text = ss;

??????????????? //textBox2.Text = serialPort1.ReadLine();
??????? }


??????? //串口發送數據,本函數默認串口已經打開
??????? public void serialSend(SerialPort serialPort1,string sendString)
??????? {
??????????? //byte[] sendBytes = Encoding.Unicode.GetBytes(sendString);
??????????? //string send64String = Convert.ToBase64String(sendBytes);
??????????? if (serialPort1.IsOpen)
??????????? {
??????????????? byte[] data = Encoding.Unicode.GetBytes("9600");
??????????????? serialPort1.Write(data, 0, data.Length);
??????????????? //serialPort1.WriteLine(sendString);
??????????? }
??????????? else
??????????? {
??????????????? MessageBox.Show("請先打開串口!");
??????????? }
??????? }

轉載于:https://www.cnblogs.com/gllgsoft/archive/2010/07/05/1771557.html

總結

以上是生活随笔為你收集整理的c# 串口 开发的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。