当前位置:首页 > 技术 > C/C++ > 正文内容

C# 串口调式工具简单开发

watrt6年前 (2018-03-27)C/C++14580

        在玩单片机和嵌入式开发中。经常会用到一个工具。那就是串口。这个是做开发经常会用到的工具。而且在现实中很多的设备在使用中也会使用到串口来输出调试数据或者使用串口来升级软件。

        而在使用中使用上位机的开发中也经常要使用到串口。现在我就来使用C# 对串口的数据通信做一个简单的开发。

        首先上效果图

搜狗截图18年03月27日1547_2.png搜狗截图18年03月27日1547_2.png

        没有什么看头非常的简单。以后有空在进一步进行功能的添加和补充。

        使用的是控件方式,没有使用多线程来操作。

        代码也非常简单几十行代码搞定。

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;
using System.IO.Ports;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Dictionary <string, StopBits> stoplist;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void toolStripDropDownButton1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string [] comseting={"1200","2400","4800","9600","19200","38400","57600","115200","460800","921600"};
            toolStripComboBox1.Items.AddRange(comseting);
            stoplist = new Dictionary<string, StopBits>();
            stoplist.Add("1", StopBits.One);
            stoplist.Add("1.5", StopBits.OnePointFive);
            stoplist.Add("2", StopBits.Two);

            for (int i = 0; i < 20; i++) {
                toolStripComboBox2.Items.Add("COM"+i.ToString());
            }
        }

        private void toolStripComboBox2_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = this.toolStripComboBox2.Text;
        }

        private void toolStripComboBox1_Click(object sender, EventArgs e)
        {
            serialPort1.BaudRate = int.Parse(this.toolStripComboBox1.Text);
        }

        private void toolStripComboBox3_Click(object sender, EventArgs e)
        {
            serialPort1.DataBits = int.Parse(this.toolStripComboBox3.Text);
        }

        private void toolStripComboBox4_Click(object sender, EventArgs e)
        {
            serialPort1.StopBits = stoplist[this.toolStripComboBox4.Text];
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = this.toolStripComboBox2.Text;
            serialPort1.BaudRate = int.Parse(this.toolStripComboBox1.Text);
            serialPort1.DataBits = int.Parse(this.toolStripComboBox3.Text);
            serialPort1.StopBits = stoplist[this.toolStripComboBox4.Text];
            try {
                serialPort1.Open();
                this.toolStripButton1.Enabled = false;


            }catch(Exception err){
                MessageBox.Show(err.Message);
                this.toolStripButton1.Enabled = true;
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            serialPort1.Close();
            this.toolStripButton1.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try {
                byte[] sendbytes = Encoding.Default.GetBytes(textBox2.Text);
                serialPort1.Write(sendbytes, 0, sendbytes.Length);
                textBox1.Text +="\r\n[发送:]" + textBox2.Text;
                textBox2.Text = "";
            }
            catch (Exception err) {
                MessageBox.Show(err.Message);
            }
            

        }

        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int len = serialPort1.BytesToRead;
            if (len == 0) return;
            byte[] receiveMsg = new byte[len];
            serialPort1.Read(receiveMsg, 0, len);
            textBox1.Text += "\r\n[收到:]" + Encoding.Default.GetString(receiveMsg); ;
        }
    }
}

        源码:WindowsFormsApplication1.rar


分享给朋友:

相关文章

变量指向函数。

变量指向函数。

把一个数组指向函数。#include <stdio.h> int max(int a,int b); int min(int a,int b); int main(int argc, char *argv[]) { int a1=0; int b1=0; int(*p[1])(int,int); //定义一个指针类型的数组 注...

linux和windows下 C/C 的sleep函数

linux和windows下 C/C 的sleep函数

简介:函数名: sleep功 能: 执行挂起一段时间用 法: unsigned sleep(unsigned seconds);在VC中使用带上头文件#include <windows.h>在gcc编译器中,使用的头文件因gcc版本的不同而不同linux系统需要添加的头文件  #include <unistd.h>注意:在VC中Sleep中的第一个英文字符为大写的"S"在标准C中是sleep, 不要大写.. 下面使用大写的来说明,, 具体用什么看你用什...

arduino esp32开发板安装困难解决方法

arduino esp32开发板安装困难解决方法

最近想使用arduino来编辑一下esp32固件,结果出师不利,IDE迟迟安装不好。下面提供一下解决方法供大家参考。我使用的是arduino是1.8.19版,在新版本上没有尝试过。但是思路应该差不多吧。首先在在arduino IDE中设置乐鑫的附加开发板的地址:https://dl.espressif.com/dl/package_esp32_index.json stm32:http://dan.drown.org/stm32duino/package_STM32duino_index.jso...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。