|
簡單的串口數據發送,供大家參考!
0.png (5.24 KB, 下載次數: 48)
下載附件
2018-6-22 17:54 上傳
0.png (4.82 KB, 下載次數: 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 串口_數據發送
- {
- 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口
-
- //串口發送的數據類型的最小單位是字節
- 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)
- {
- //發送數據
- serialPort1.Write()
- }
- }
- }
復制代碼
|
-
-
串口-數據發送.rar
2018-6-22 16:42 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
44.35 KB, 下載次數: 32, 下載積分: 黑幣 -5
|