|
簡單的串口數(shù)據(jù)發(fā)送,供大家參考!
0.png (5.24 KB, 下載次數(shù): 48)
下載附件
2018-6-22 17:54 上傳
0.png (4.82 KB, 下載次數(shù): 46)
下載附件
2018-6-22 17:54 上傳
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace 串口_數(shù)據(jù)發(fā)送
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void Form1_Load(object sender, EventArgs e)
- {
- // comboBox1.Items.Add("COM1");
- for (int i = 1; i < 16; i++)
- {
- comboBox1.Items.Add("COM"+i.ToString());
- }
- comboBox1.SelectedIndex = 1; //初始化COM口
-
- //串口發(fā)送的數(shù)據(jù)類型的最小單位是字節(jié)
- for (int i = 1; i < 20; i++)
- {
- string str = Convert.ToString(i, 16);
- if (str.Length == 1)
- str = "0" + str;
- str = "0x" + str;
- comboBox2.Items.Add(str);
- }
- }
- private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- }
- private void button1_Click(object sender, EventArgs e)
- {
- //serialPort1.PortName = comboBox1.Text;
- if (!serialPort1.IsOpen) //端口如果是關閉著的,請打開
- {
- try
- {
- serialPort1.PortName = comboBox1.Text;
- serialPort1.Open();
- button1.Text = "關閉窗口";
- }
- catch
- {
- MessageBox.Show("串口打開失!");
- }
- }
- else
- {
- try
- {
- serialPort1.Close();
- button1.Text = "打開窗口";
- }
- catch
- {
- MessageBox.Show("串口關閉失!");
- }
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- //發(fā)送數(shù)據(jù)
- serialPort1.Write()
- }
- }
- }
復制代碼
|
|