当前位置:网站首页>上位机开发C#语言:模拟STC串口助手接收单片机发送数据
上位机开发C#语言:模拟STC串口助手接收单片机发送数据
2022-08-05 10:10:00 【最早的早安...】
题目概述:
设计一个模拟STC串口助手,通过串口进行接收或发送数据。
STC串口助手:
编程:
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 SerialCommunicate
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
serialPort1.PortName = comboBox1.Text;
serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text,10);//十进制数据转换
serialPort1.Open();
button1.Enabled = false;//打开串口按钮不可用
button2.Enabled = true;//关闭串口
}
catch {
MessageBox.Show("端口错误,请检查串口", "错误");
}
}
private void Form1_Load(object sender, EventArgs e)
{
textBox3.Text = DateTime.Now.ToString("h:mm:ss");
timer1.Enabled = true;
timer1.Interval = 1000;
for (int i = 1; i < 20; i++)
{
comboBox1.Items.Add("COM" + i.ToString());
}
comboBox1.Text = "COM4";//串口号多额默认值
comboBox2.Text = "9600";//波特率默认值
/*****************非常重要************************/
serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);//必须手动添加事件处理程序
}
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)//串口数据接收事件
{
if (!radioButton3.Checked)//如果接收模式为字符模式
{
string str = serialPort1.ReadExisting();//字符串方式读
textBox1.AppendText(str);//添加内容
}
else { //如果接收模式为数值接收
byte data;
data = (byte)serialPort1.ReadByte();//此处需要强制类型转换,将(int)类型数据转换为(byte类型数据,不必考虑是否会丢失数据
string str = Convert.ToString(data, 16).ToUpper();//转换为大写十六进制字符串
textBox1.AppendText("0x" + (str.Length == 1 ? "0" + str : str) + " ");//空位补“0”
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
serialPort1.Close();//关闭串口
button1.Enabled = true;//打开串口按钮可用
button2.Enabled = false;//关闭串口按钮不可用
}
catch //一般情况下关闭串口不会出错,所以不需要加处理程序
{
}
}
private void button3_Click(object sender, EventArgs e)
{
byte[] Data = new byte[1];//存储一个字节 更新后发送下一个字节
if (serialPort1.IsOpen)//判断串口是否打开,如果打开执行下一步操作
{
if (textBox2.Text != "")
{
if (!radioButton1.Checked)//如果发送模式是字符模式
{
try
{
serialPort1.WriteLine(textBox2.Text);//写数据
}
catch (Exception err)
{
MessageBox.Show("串口数据写入错误", "错误");//出错提示
serialPort1.Close();
button1.Enabled = true;//打开串口按钮可用
button2.Enabled = false;//关闭串口按钮不可用
}
}
else
{
for (int i = 0; i < (textBox2.Text.Length - textBox2.Text.Length % 2) / 2; i++)//取余3运算作用是防止用户输入的字符为奇数个
{
Data[0] = Convert.ToByte(textBox2.Text.Substring(i * 2, 2), 16);
serialPort1.Write(Data, 0, 1);//循环发送(如果输入字符为0A0BB,则只发送0A,0B)
}
if (textBox2.Text.Length % 2 != 0)//剩下一位单独处理
{
Data[0] = Convert.ToByte(textBox2.Text.Substring(textBox2.Text.Length-1, 1), 16);//单独发送B(0B)
serialPort1.Write(Data, 0, 1);//发送
}
}
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
textBox3.Text = DateTime.Now.ToString("h:mm:ss");
}
}
}
上机实践:
边栏推荐
- After Keil upgrades to AC6, what changes?
- 开源一夏|OpenHarmony如何查询设备类型(eTS)
- uniapp connect ibeacon
- 【温度预警程序de开发】事件驱动模型实例运用
- E-sports, convenience, efficiency, security, key words for OriginOS functions
- Why are RELTABLESPACE values 0 for many tables displayed in sys_class?
- 基于MindSpore高效完成图像分割,实现Dice!
- 第四章:redis 数组结构的set和一些通用命令「建议收藏」
- leetcode: 529. Minesweeper Game
- mysql进阶(二十七)数据库索引原理
猜你喜欢

2022 Huashu Cup Mathematical Modeling Question A Optimization Design Ideas for Ring Oscillators Code Sharing

茄子科技CEO仇俊:以用户为中心,做用户真正需要的产品

SD NAND Flash简介!

【MindSpore Easy-Diantong Robot-01】You may have seen many knowledge quiz robots, but this one is a bit different

我们的Web3创业项目,黄了
![[Strong Net Cup 2022] WP-UM](/img/3d/caeab05ddca278af274dbf6e2f8ba1.png)
[Strong Net Cup 2022] WP-UM

产品太多了,如何实现一次登录多产品互通?

技术干货 | 基于 MindSpore 实现图像分割之豪斯多夫距离
![[强网杯2022]WP-UM](/img/3d/caeab05ddca278af274dbf6e2f8ba1.png)
[强网杯2022]WP-UM

创建一个 Dapp,为什么要选择波卡?
随机推荐
技术干货 | 基于 MindSpore 实现图像分割之豪斯多夫距离
蚁剑webshell动态加密连接分析与实践
three.js调试工具dat.gui使用
After Keil upgrades to AC6, what changes?
uniapp 连接ibeacon
气象数据数据处理实例——matlab字符串切割匹配与R语言日期匹配(数据拼接)
SMB + SMB2: Accessing shares return an error after prolonged idle period
MySQL之数据视图
PHP 操作mangoDb
Tanabata romantic date without overtime, RPA robot helps you get the job done
three.js debugging tool dat.gui use
第五章:多线程通信—wait和notify
Still looking for a network backup resources?Hurry up to collect the following network backup resource search artifact it is worth collecting!
2022 Huashu Cup Mathematical Modeling Ideas Analysis and Exchange
电竞、便捷、高效、安全,盘点OriginOS功能的关键词
微服务 技术栈
Introduction to SD NAND Flash!
2022 Huashu Cup Mathematical Modeling Question A Optimization Design Ideas for Ring Oscillators Code Sharing
企业的数字化转型到底是否可以买来?
百年北欧奢华家电品牌ASKO智能三温区酒柜臻献七夕,共品珍馐爱意