当前位置: 首页 > news >正文

C# CS_Prj01 串口通信控制台程序

一直以来,玩8088单板机,上位机都是使用的绿色现成的串口软件。

今天,感觉8088单板机的各部分测试都基本完成了。

本着玩的精神,自己写一个上位机的简单串口程序,与自己的8088单板机通讯。

功能:一个完整的C#命令行程序,使用串口8以9600波特率每秒发送字符'A',并实时显示接收到的所有字

1.测试结果

2.完整程序

using System;
using System.IO.Ports;
using System.Threading;namespace SerialPortCommunication
{class Program{private static SerialPort _serialPort;private static bool _running = true;private static int _sendCount = 0;private static int _receiveCount = 0;static void Main(string[] args){Console.Title = "串口通信监控 (COM8, 9600 bps)";Console.ForegroundColor = ConsoleColor.Cyan;Console.WriteLine("串口通信命令行程序");Console.WriteLine("====================");Console.ResetColor();Console.WriteLine("配置: COM8, 9600 bps, 8N1");Console.WriteLine("功能: 每秒发送字符 'A',实时显示接收数据");Console.WriteLine("按 Q 键退出程序");Console.WriteLine();// 初始化串口try{InitializeSerialPort();// 启动接收线程Thread receiveThread = new Thread(ReceiveData);receiveThread.IsBackground = true;receiveThread.Start();// 启动发送线程Thread sendThread = new Thread(SendData);sendThread.IsBackground = true;sendThread.Start();// 监控退出键while (_running){if (Console.KeyAvailable){var key = Console.ReadKey(true).Key;if (key == ConsoleKey.Q){_running = false;}}Thread.Sleep(100);}// 关闭串口_serialPort.Close();Console.ForegroundColor = ConsoleColor.Yellow;Console.WriteLine("\n程序已终止");Console.ResetColor();Console.WriteLine($"发送统计: {_sendCount} 条消息");Console.WriteLine($"接收统计: {_receiveCount} 个字符");}catch (Exception ex){Console.ForegroundColor = ConsoleColor.Red;Console.WriteLine($"错误: {ex.Message}");Console.ResetColor();}Console.WriteLine("按任意键退出...");Console.ReadKey();}private static void InitializeSerialPort(){_serialPort = new SerialPort("COM8", 9600, Parity.None, 8, StopBits.One){Handshake = Handshake.None,ReadTimeout = 500,WriteTimeout = 500,Encoding = System.Text.Encoding.ASCII};_serialPort.Open();Console.ForegroundColor = ConsoleColor.Green;Console.WriteLine("串口已成功打开");Console.ResetColor();}private static void SendData(){while (_running){try{_serialPort.Write("A");_sendCount++;// 在控制台显示发送状态Console.ForegroundColor = ConsoleColor.Blue;Console.WriteLine($"[发送] A ({DateTime.Now:HH:mm:ss.fff})");Console.ResetColor();}catch (Exception ex){Console.ForegroundColor = ConsoleColor.Red;Console.WriteLine($"[发送错误] {ex.Message}");Console.ResetColor();}// 每秒发送一次Thread.Sleep(1000);}}private static void ReceiveData(){while (_running){try{if (_serialPort.BytesToRead > 0){string data = _serialPort.ReadExisting();_receiveCount += data.Length;// 在控制台显示接收数据Console.ForegroundColor = ConsoleColor.Magenta;Console.Write($"[接收] ");Console.ResetColor();// 特殊字符处理foreach (char c in data){if (c == '\n'){Console.WriteLine();}else if (c == '\r'){// 忽略回车符}else if (char.IsControl(c)){Console.Write($"[0x{((int)c):X2}]");}else{Console.Write(c);}}}}catch (Exception ex){Console.ForegroundColor = ConsoleColor.Red;Console.WriteLine($"[接收错误] {ex.Message}");Console.ResetColor();}Thread.Sleep(10); // 短暂休眠避免CPU占用过高}}}
}

3.技术实现

多线程结构

 

http://www.dtcms.com/a/252092.html

相关文章:

  • sparseDrive(1): 论文解读
  • Faithful Logical Reasoning via Symbolic Chain-of-Thought
  • 第十六届蓝桥杯国赛(2025)C/C++B组 蓝桥星数字 独家解析
  • 超标量处理器设计9-执行
  • 647. 回文子串
  • 进程间通信之进程间传递文件描述符
  • 【JS-1】JavaScript的三种书写位置详解:内联、内部与外部
  • firewalld防火墙(一):基础概念、配置详解与实战应用
  • 数据库核心技术深度剖析:事务、索引、锁与SQL优化实战指南(第六节)-----InnoDB引擎
  • YOLOv4 训练与推理流程详解
  • Java并发编程实战 Day 23:并发系统性能调优
  • Android跨进程通信深度优化:从Binder机制原理到企业级实战指南
  • sparseDrive(2):环境搭建及效果演示
  • 【Linux服务器】-安装zabbix-负载环境(故障自动切换场景)
  • MySQL面试题(完整版一百道经典高频面试题)
  • LDPC码的译码算法
  • Node.js验证码:从生成到验证的趣味之旅
  • 语音转文本ASR、文本转语音TTS
  • 如何在 Elementary OS 上安装 Google Chrome 浏览器
  • Qdrant向量数据库的增删改查
  • 华为OD-2024年E卷-增强的strstr[100分] -- python
  • 通过 BLE 和 Wi-Fi 交换优化基于 ID 的远程无人机通信的延迟
  • ubuntu 22.04 安装部署logstash 7.10.0详细教程
  • VSCODE + EIDE 下 STM32 编程使用部分外设无法通过编译:undefined reference to ‘xxx‘
  • 基于 Transformer RoBERTa的情感分类任务实践总结之五——剪枝
  • 山东大学项目实训-创新实训-法律文书专家系统-项目报告(八)
  • vue中的h渲染函数
  • AI医生24小时在线:你的健康新‘算法监护人
  • 《江西南昌棒垒球》一级运动员 vs 二级运动员·棒球1号位
  • 【C/C++】内核开发之进程调度大纲