当前位置:网站首页>The host computer develops C# language: simulates the STC serial port assistant to receive the data sent by the microcontroller
The host computer develops C# language: simulates the STC serial port assistant to receive the data sent by the microcontroller
2022-08-05 10:18:00 【first good morning...】
题目概述:
design a simulationSTC串口助手,Receive or send data through the serial port.
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];//存储一个字节 Send the next byte after the update
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");
}
}
}
上机实践:
边栏推荐
- Still looking for a network backup resources?Hurry up to collect the following network backup resource search artifact it is worth collecting!
- DFINITY 基金会创始人谈熊市沉浮,DeFi 项目该何去何从
- js劫持数组push方法
- 【翻译】混沌网+SkyWalking:为混沌工程提供更好的可观察性
- 2022 Hangzhou Electric Power Multi-School Session 6 1008.Shinobu Loves Segment Tree Regular Questions
- SMB + SMB2: Accessing shares return an error after prolonged idle period
- STM32+ULN2003 drives 28BYJ4 stepper motor (forward and reverse according to the number of turns)
- The difference between find, matches, lookingAt matching strings in matcher
- 仿SBUS与串口数据固定转换
- 语音社交软件开发——充分发挥其价值
猜你喜欢
FPGA:开发环境Vivado的使用
three.js debugging tool dat.gui use
蚁剑webshell动态加密连接分析与实践
创建一个 Dapp,为什么要选择波卡?
Which big guy has the 11G GI and ojvm patches in April or January 2020, please help?
高质量 DeFi 应用构建指南,助力开发者玩转 DeFi Summer
FPGA: Use of the development environment Vivado
leetcode: 529. Minesweeper Game
dotnet OpenXML parsing PPT charts Getting started with area charts
深入理解 Istio 流量管理的超时时间设置
随机推荐
[Strong Net Cup 2022] WP-UM
linux下oracle常见操作以及日常积累知识点(函数、定时任务)
three物体围绕一周呈球形排列
Jenkins使用手册(2) —— 软件配置
egg框架使用(一)
[Android]如何使用RecycleView in Kotlin project
牛刀小试基本语法,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang基本语法和变量的使用EP02
three objects are arranged in a spherical shape around the circumference
Analysis and practice of antjian webshell dynamic encrypted connection
基于MindSpore高效完成图像分割,实现Dice!
Bias lock/light lock/heavy lock lock is healthier. How is locking and unlocking accomplished?
The JVM collection that Alibaba's top architects have summarized for many years, where can't I check it!
How does the official account operate and maintain?Public account operation and maintenance professional team
Introduction to SD NAND Flash!
2022杭电多校 第6场 1008.Shinobu Loves Segment Tree 规律题
一文道清什么是SPL
Confessing in the era of digital transformation: Mai Cong Software allows enterprises to use data in the easiest way
入门 Polkadot 平行链开发,看这一篇就够了
电气工程的标准是什么
【Unity】【UGUI】【在屏幕上显示文本】