標(biāo)題: 串口數(shù)據(jù)發(fā)送上位機(jī)源碼 [打印本頁(yè)]

作者: zd043    時(shí)間: 2018-6-22 16:43
標(biāo)題: 串口數(shù)據(jù)發(fā)送上位機(jī)源碼
簡(jiǎn)單的串口數(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ù)類(lèi)型的最小單位是字節(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)    //端口如果是關(guān)閉著的,請(qǐng)打開(kāi)
  44.             {
  45.                 try
  46.                 {
  47.                     serialPort1.PortName = comboBox1.Text;
  48.                     serialPort1.Open();
  49.                     button1.Text = "關(guān)閉窗口";
  50.                 }
  51.                 catch
  52.                 {
  53.                     MessageBox.Show("串口打開(kāi)失!");
  54.                 }
  55.             }
  56.             else
  57.             {
  58.                 try
  59.                 {
  60.                     serialPort1.Close();
  61.                     button1.Text = "打開(kāi)窗口";
  62.                 }
  63.                 catch
  64.                 {
  65.                     MessageBox.Show("串口關(guān)閉失!");
  66.                 }
  67.             }
  68.         }

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


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

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






歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1