找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3872|回復: 0
打印 上一主題 下一主題
收起左側

串口數(shù)據(jù)發(fā)送上位機源碼

[復制鏈接]
跳轉到指定樓層
樓主
ID:356736 發(fā)表于 2018-6-22 16:43 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
簡單的串口數(shù)據(jù)發(fā)送,供大家參考!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;

  9. namespace 串口_數(shù)據(jù)發(fā)送
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }

  17.       
  18.         private void Form1_Load(object sender, EventArgs e)
  19.         {
  20.            // comboBox1.Items.Add("COM1");
  21.             for (int i = 1; i < 16; i++)
  22.             {
  23.                 comboBox1.Items.Add("COM"+i.ToString());
  24.             }
  25.             comboBox1.SelectedIndex = 1;  //初始化COM口
  26.            
  27.             //串口發(fā)送的數(shù)據(jù)類型的最小單位是字節(jié)
  28.             for (int i = 1; i < 20; i++)
  29.             {
  30.                 string str = Convert.ToString(i, 16);
  31.                 if (str.Length == 1)
  32.                     str = "0" + str;
  33.                 str = "0x" + str;
  34.                 comboBox2.Items.Add(str);
  35.             }

  36.         }

  37.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  38.         {

  39.         }

  40.         private void button1_Click(object sender, EventArgs e)
  41.         {
  42.             //serialPort1.PortName = comboBox1.Text;
  43.             if (!serialPort1.IsOpen)    //端口如果是關閉著的,請打開
  44.             {
  45.                 try
  46.                 {
  47.                     serialPort1.PortName = comboBox1.Text;
  48.                     serialPort1.Open();
  49.                     button1.Text = "關閉窗口";
  50.                 }
  51.                 catch
  52.                 {
  53.                     MessageBox.Show("串口打開失!");
  54.                 }
  55.             }
  56.             else
  57.             {
  58.                 try
  59.                 {
  60.                     serialPort1.Close();
  61.                     button1.Text = "打開窗口";
  62.                 }
  63.                 catch
  64.                 {
  65.                     MessageBox.Show("串口關閉失!");
  66.                 }
  67.             }
  68.         }

  69.         private void button2_Click(object sender, EventArgs e)
  70.         {
  71.             //發(fā)送數(shù)據(jù)
  72.             serialPort1.Write()
  73.         }
  74.     }
  75. }
復制代碼


串口-數(shù)據(jù)發(fā)送.rar

44.35 KB, 下載次數(shù): 32, 下載積分: 黑幣 -5

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

使用道具 舉報

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

本版積分規(guī)則

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

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

快速回復 返回頂部 返回列表