久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 9877|回復(fù): 17
打印 上一主題 下一主題
收起左側(cè)

C#制作串口助手源碼

  [復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
用C#做的一個(gè)簡易串口助手,可以作為練手使用,再也不用別人的串口助手了HHHH
基本功能都有,代碼也有很多注釋,歡迎下載。

程序運(yùn)行界面:


程序源碼如下:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO.Ports;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;

  11. namespace WinFormSeriport
  12. {
  13.     public partial class FORM1 : Form
  14.     {
  15.         SerialPort sp = new SerialPort();
  16.         
  17.         public FORM1()
  18.         {
  19.             Control.CheckForIllegalCrossThreadCalls = false;
  20.             InitializeComponent();
  21.             sp.Close();
  22.             lbl_TxRx.Text = "Tx:" +0+" " + "Rx:" + 0;
  23.         }
  24.         private void Form1_Load_1(object sender, EventArgs e)
  25.         {
  26.             Timer_SendDate.Interval = 1000;
  27.             Timer_SendDate.Stop();
  28.         }

  29.         
  30.         private void Form1_MouseMove(object sender, MouseEventArgs e)
  31.         {
  32.             lbl_Move.Text = e.X.ToString() + " " + e.Y.ToString();
  33.         }

  34.       

  35.         private void serialPort1_ErrorReceived(object sender, System.IO.Ports.SerialErrorReceivedEventArgs e)
  36.         {
  37.             MessageBox.Show("接收數(shù)據(jù)時(shí)發(fā)生未知錯(cuò)誤!", "錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Information);
  38.         }

  39.         private void btn_OpenSerial_Click(object sender, EventArgs e)
  40.         {
  41.             if (sp.IsOpen == false)
  42.             {
  43.                 btn_OpenSerial.Text = "開啟串口";
  44.                 try
  45.                 {
  46.                     sp.PortName = comboBox_Com.Text;
  47.                     sp.BaudRate = Convert.ToInt32(comboBox_Bot.Text);
  48.                     sp.Parity = (System.IO.Ports.Parity)Enum.Parse(typeof(System.IO.Ports.Parity), comboBox_Parity.Text);
  49.                     sp.DataBits = Convert.ToInt16(comboBox_DateBit.Text);
  50.                     sp.StopBits = (System.IO.Ports.StopBits)Enum.Parse(typeof(System.IO.Ports.StopBits), comboBox_StopBit.Text);
  51.                     sp.Encoding = Encoding.Default;    //設(shè)置串口編碼為default:獲取操作系統(tǒng)的當(dāng)前 ANSI 代碼頁的編碼。
  52.                     sp.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived);

  53.                     sp.Open();    //開啟串口
  54.                     if (checkBox_ClearShow.Checked)   //如果開啟了自動(dòng)清空功能
  55.                         Timer_SendDate.Start();

  56.                     btn_OpenSerial.Text = "關(guān)閉串口";
  57.                 }
  58.                 catch
  59.                 {
  60.                     MessageBox.Show("配置串口出錯(cuò),檢查各項(xiàng)參數(shù)是否設(shè)置!", "錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
  61.                 }
  62.             }
  63.             else
  64.             {
  65.                 btn_OpenSerial.Text = "關(guān)閉串口";
  66.                 try
  67.                 {
  68.                     sp.Close();

  69.                     btn_OpenSerial.Text = "開啟串口";
  70.                     MessageBox.Show("串口已關(guān)閉!","提示",MessageBoxButtons.OK, MessageBoxIcon.Information);
  71.                 }
  72.                 catch
  73.                 {
  74.                     MessageBox.Show("出現(xiàn)未知錯(cuò)誤,串口關(guān)閉失敗!", "錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);      
  75.                 }
  76.             }

  77.         }
  78.         string receiveData;
  79.         UInt32 receiveBytesCount;
  80.         string sendData;
  81.         UInt32 sendBytesCount;
  82.         private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
  83.         {

  84.             receiveData = sp.ReadExisting();
  85.             receiveBytesCount += (UInt32)receiveData.Length;


  86.             //字符串顯示
  87.             if (checkBox_ClearShow.Checked == false)
  88.             {
  89.                 if (checkBox_16Show.Checked == false)   //hex 方式顯示
  90.                 {
  91.                     richtxtbox_ShowGetNumber.AppendText(receiveData);
  92.                     richtxtbox_ShowGetNumber.Focus();   //讓光標(biāo)到這來
  93.                     richtxtbox_ShowGetNumber.Select(richtxtbox_ShowGetNumber.TextLength, 0);
  94.                     this.richtxtbox_ShowGetNumber.ScrollToCaret(); //設(shè)置光標(biāo)到最后
  95.                 }
  96.                 //16進(jìn)制顯示
  97.                 else
  98.                 {
  99.                     byte[] recData = System.Text.Encoding.Default.GetBytes(receiveData);// 將接受到的字符串據(jù)轉(zhuǎn)化成數(shù)組;  

  100.                     foreach (byte str in recData)
  101.                     {
  102.                         richtxtbox_ShowGetNumber.AppendText(string.Format("{0:X2} ", str));
  103.                     }
  104.                 }
  105.             }
  106.             lbl_TxRx.Text = "Tx:" + sendBytesCount.ToString()+" " + "Rx:" + receiveBytesCount.ToString();
  107.         }


  108.         private void btn_GetCom_Click(object sender, EventArgs e)  //自動(dòng)獲取串口號(hào)
  109.         {
  110.             string[] serialPortName = System.IO.Ports.SerialPort.GetPortNames();

  111.             foreach (string name in serialPortName)
  112.             {
  113.                 comboBox_Com.Text = name;
  114.             }
  115.         }

  116.         private void btn_Send_Click(object sender, EventArgs e)
  117.         {
  118.             if (sp.IsOpen == false)
  119.             {
  120.                 btn_OpenSerial.Text = "開啟串口";
  121.                 MessageBox.Show("請打開串口!", "錯(cuò)誤",MessageBoxButtons.OK, MessageBoxIcon.Information);
  122.             }
  123.             else
  124.             {
  125.                 btn_OpenSerial.Text = "關(guān)閉串口";
  126.                 if (checkBox_SendByItself.Checked)
  127.                 {
  128.                     Timer_SendDate.Interval = Convert.ToInt32(textBox_SendTime.Text);
  129.                     Timer_SendDate.Start();
  130.                 }
  131.                 sendData = richTextBox_Send.Text;
  132.                 if (checkBox_SendIn16.Checked == false)
  133.                 {
  134.                     sp.Write(sendData);
  135.                 }
  136.                 else
  137.                 {
  138.                     try
  139.                     {
  140.                         sendData.Replace("0x", "");   //去掉0x
  141.                         sendData.Replace("0X", "");   //去掉0X
  142.                         //  sendData.

  143.                         string[] strArray = sendData.Split(new char[] { ',', ',', '\r', '\n', ' ', '\t' });
  144.                         int decNum = 0;
  145.                         int i = 0;
  146.                         byte[] sendBuffer = new byte[strArray.Length];  //發(fā)送數(shù)據(jù)緩沖區(qū)

  147.                         foreach (string str in strArray)
  148.                         {
  149.                             try
  150.                             {
  151.                                 decNum = Convert.ToInt16(str, 16);
  152.                                 sendBuffer[i] = Convert.ToByte(decNum);
  153.                                 i++;
  154.                             }
  155.                             catch
  156.                             {

  157.                             }
  158.                         }
  159.                         sp.Write(sendBuffer, 0, sendBuffer.Length);

  160.                         //更新發(fā)送數(shù)據(jù)計(jì)數(shù)
  161.                         sendBytesCount += (UInt32)sendBuffer.Length;

  162.                         lbl_TxRx.Text = "Tx:" + sendBytesCount.ToString()+" " + "Rx:" + receiveBytesCount.ToString();

  163.                     }
  164.                     catch
  165.                     {

  166.                     }
  167.                 }
  168.             }
  169.         }

  170.         private void Timer_SendDate_Tick(object sender, EventArgs e)
  171.         {

  172.             if (checkBox_ClearShow.Checked)
  173.                 richtxtbox_ShowGetNumber.Text = null;

  174.             sendData = richTextBox_Send.Text;
  175.    
  176.             if (sp.IsOpen)
  177.             {
  178.                 btn_OpenSerial.Text = "關(guān)閉串口";
  179.                 if (checkBox_SendByItself.Checked)
  180.                 {
  181.                     if (checkBox_SendIn16.Checked == false)
  182.                     {
  183.                         sp.Write(sendData);
  184.                     }
  185.                     else
  186.                     {
  187.                         try
  188.                         {
  189.                             sendData.Replace("0x", "");   //去掉0x
  190.                             sendData.Replace("0X", "");   //去掉0X
  191.                             //  sendData.

  192.                             string[] strArray = sendData.Split(new char[] { ',', ',', '\r', '\n', ' ', '\t' });
  193.                             int decNum = 0;
  194.                             int i = 0;
  195.                             byte[] sendBuffer = new byte[strArray.Length];  //發(fā)送數(shù)據(jù)緩沖區(qū)

  196.       

  197.                             foreach (string str in strArray)
  198.                             {
  199.                                 try
  200.                                 {
  201.                                     decNum = Convert.ToInt16(str, 16);
  202.                                     sendBuffer[i] = Convert.ToByte(decNum);
  203.                                     i++;
  204.                                 }
  205.                                 catch
  206.                                 {

  207.                                 }
  208.                             }
  209.                             sp.Write(sendBuffer, 0, sendBuffer.Length);

  210.                         }
  211.                         catch
  212.                         {

  213.                         }
  214.                     }
  215.                     //更新發(fā)送數(shù)據(jù)計(jì)數(shù)
  216.                     sendBytesCount += (UInt32)sendData.Length;

  217.                     lbl_TxRx.Text = "Tx:" + sendBytesCount.ToString()+" " + "Rx:" + receiveBytesCount.ToString();
  218.                 }
  219.                 else
  220.                 {
  221.                  if (checkBox_ClearShow.Checked)
  222.                     checkBox_SendByItself.CheckState=CheckState .Unchecked ;
  223.                 else
  224.                     Timer_SendDate.Stop();
  225.                 }
  226.             }

  227.             else
  228.             {
  229.                     btn_OpenSerial.Text = "開啟串口";
  230.                     Timer_SendDate.Stop();
  231.                     MessageBox.Show("串口已經(jīng)關(guān)閉!", "錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Information);      
  232.             }
  233.         
  234.         }

  235.         private void btn_ClearAll_Click(object sender, EventArgs e)
  236.         {
  237.             richtxtbox_ShowGetNumber.Text = null;
  238.             richTextBox_Send.Text = null;
  239.         }
  240.     }
  241. }
復(fù)制代碼

所有資料51hei提供下載:
WinFormSeriport.zip (88.05 KB, 下載次數(shù): 311)

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏10 分享淘帖 頂1 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:18591 發(fā)表于 2019-3-3 19:25 | 只看該作者
看看~~~~~~~~~~~~~~
回復(fù)

使用道具 舉報(bào)

板凳
ID:337813 發(fā)表于 2019-8-6 08:46 | 只看該作者
非常感謝您的無私分享,謝謝!
回復(fù)

使用道具 舉報(bào)

地板
ID:99124 發(fā)表于 2019-12-7 21:21 | 只看該作者
學(xué)習(xí)一下,要是界面能全屏就好了。
回復(fù)

使用道具 舉報(bào)

5#
ID:67573 發(fā)表于 2019-12-13 12:23 | 只看該作者
非常感謝您的無私分享,謝謝!
回復(fù)

使用道具 舉報(bào)

6#
ID:491495 發(fā)表于 2020-1-17 20:53 | 只看該作者
非常好的資料,學(xué)習(xí)一下
回復(fù)

使用道具 舉報(bào)

7#
ID:687464 發(fā)表于 2020-1-19 23:30 | 只看該作者
挺好~學(xué)習(xí)一下
回復(fù)

使用道具 舉報(bào)

8#
ID:101466 發(fā)表于 2020-2-17 22:33 | 只看該作者
還是這里好,下載方便,謝謝
回復(fù)

使用道具 舉報(bào)

9#
ID:694712 發(fā)表于 2020-2-18 15:24 | 只看該作者
感謝分享。。。
回復(fù)

使用道具 舉報(bào)

10#
ID:59830 發(fā)表于 2020-2-20 18:11 | 只看該作者
看了一下,有些幫助。謝謝分享。
回復(fù)

使用道具 舉報(bào)

11#
ID:734421 發(fā)表于 2020-5-3 21:02 | 只看該作者
感謝,內(nèi)容可以
回復(fù)

使用道具 舉報(bào)

12#
ID:195703 發(fā)表于 2020-5-6 11:58 | 只看該作者
謝謝分享
回復(fù)

使用道具 舉報(bào)

13#
ID:749484 發(fā)表于 2020-5-13 09:58 | 只看該作者
支持一下原創(chuàng),我也有打算寫一個(gè)類似的,學(xué)習(xí)一下。
回復(fù)

使用道具 舉報(bào)

14#
ID:807693 發(fā)表于 2022-11-6 16:12 | 只看該作者
感謝,想?yún)⒖枷?/td>
回復(fù)

使用道具 舉報(bào)

15#
ID:121968 發(fā)表于 2022-11-23 14:22 | 只看該作者
非常好的資料,學(xué)習(xí)一下
回復(fù)

使用道具 舉報(bào)

16#
ID:1016815 發(fā)表于 2023-2-14 22:27 | 只看該作者
非常感謝您的無私分享,謝謝!
回復(fù)

使用道具 舉報(bào)

17#
ID:1051997 發(fā)表于 2023-3-15 15:46 | 只看該作者
感謝分享,對初學(xué)者來說是個(gè)很好的例子
回復(fù)

使用道具 舉報(bào)

18#
ID:288582 發(fā)表于 2024-7-2 08:19 | 只看該作者
學(xué)習(xí)一下,感謝分享!
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 国产高清在线精品一区二区三区 | www.成人.com| 精品毛片| 亚洲国产成人精品一区二区 | 操操日 | 精品av久久久久电影 | 欧美视频在线看 | 伊人青青久久 | 亚洲一区二区三区在线 | 成人性视频免费网站 | 国产精品一区二区无线 | 色在线免费视频 | 97超级碰碰| 亚洲成a人片 | 成人性生交大片免费看中文带字幕 | 亚洲精品乱码久久久久久蜜桃 | 电影91久久久 | 青青草国产在线观看 | 日韩不卡在线 | 91在线精品视频 | 超碰3| 日本欧美国产在线观看 | 国产一区电影 | 欧美福利视频一区 | 亚洲欧美日韩中文在线 | www九色 | 国产精品国产成人国产三级 | 亚洲综合小视频 | 亚洲一区二区三区国产 | 在线欧美激情 | 欧美亚洲成人网 | 亚洲一区二区成人 | 国产综合一区二区 | 91免费版在线 | 亚洲三级在线观看 | 国产福利视频导航 | 国产在线激情视频 | 中文字幕不卡在线观看 | 天天天天操| 亚洲精品一二三区 | 中文字幕在线欧美 |