C#---串口调试助手

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using System.Windows;
7 using System.Windows.Controls;
8 using System.Windows.Data;
9 using System.Windows.Documents;
10 using System.Windows.Input;
11 using System.Windows.Media;
12 using System.Windows.Media.Imaging;
13 using System.Windows.Navigation;
14 using System.Windows.Shapes;
15 using System.IO.Ports;
16 using System.Text;
17
18
19 namespace SerialDebuggingWPF
20 {
21 /// <summary>
22 /// MainWindow.xaml 的交互逻辑
23 /// </summary>
24 public partial class MainWindow : Window
25 {
26 private System.IO.Ports.SerialPort serialPort = new SerialPort();//串口对象
27 private StringBuilder stringBuilder = new StringBuilder();//作为缓冲
28 public MainWindow()
29 {
30 InitializeComponent();
31 }
32
33 /// <summary>
34 /// 加载窗口
35 /// </summary>
36 /// <param name="sender"></param>
37 /// <param name="e"></param>
38
39 private void Window_Loaded(object sender, RoutedEventArgs e)
40 {
41 portname.ItemsSource = SerialPort.GetPortNames();
42 if (portname.Items.Count > 0)
43 {
44 this.portname.SelectedIndex = 0;
45 }
46 else
47 {
48 MessageBox.Show("未找到有效串口!!", "提示");
49 }
50 //Console.WriteLine(this.baud.Items.IndexOf("9600")+"");
51 this.baud.SelectedIndex = 1;
52 this.jy.SelectedIndex = 0;
53 this.jy_Copy.SelectedIndex = 0;
54 this.SJ.SelectedIndex = 6;
55
56 }
57 /// <summary>
58 /// 串口数据通信
59 /// </summary>
60 /// <param name="sender"></param>
61 /// <param name="e"></param>
62 private void DataReceived(object sender, SerialDataReceivedEventArgs e)
63 {
64 Byte[] receivedData = new Byte[serialPort.BytesToRead];//获取读取字节个数
65 try
66 {
67 serialPort.Read(receivedData, 0, receivedData.Length);
68 String info = Encoding.GetEncoding("GB2312").GetString(receivedData);
69 updateReceiveText("接收数据: " + info);
70 }
71 catch
72 {
73 MessageBox.Show("串口失效", "提示");
74 serialPort.Close();
75 if (Connect.Dispatcher.CheckAccess())
76 {
77 Connect.Content = "打开串口";
78 }
79 else
80 {
81 Action act = () => { Connect.Content = "打开串口"; };
82 Connect.Dispatcher.Invoke(act);
83 }
84 }
85 }
86
87
88
89
90
91
92
93 /// <summary>
94 /// 串口连接按钮
95 /// </summary>
96 /// <param name="sender"></param>
97 /// <param name="e"></param>
98 private void Connect_Click(object sender, RoutedEventArgs e)
99 {
100 if (serialPort.IsOpen)
101 {
102 serialPort.Close();
103 if (Connect.Dispatcher.CheckAccess())
104 {
105 Connect.Content = "打开串口";
106 }
107 else
108 {
109 Action act = () => { Connect.Content = "打开串口"; };
110 Connect.Dispatcher.Invoke(act);
111 }
112 Console.WriteLine("正在关闭串口");
113 }
114 else
115 {
116 if (portname.Text != null && !portname.Text.Equals(""))
117 {
118 serialPort.PortName = portname.SelectedItem.ToString();
119 serialPort.BaudRate = Convert.ToInt32(baud.Text);
120 try
121 {
122 serialPort.Open();//打开串口
123 if (Connect.Dispatcher.CheckAccess())
124 {
125 Connect.Content = "关闭串口";
126 }
127 else
128 {
129 Action act = () => { Connect.Content = "关闭串口"; };
130 Connect.Dispatcher.Invoke(act);
131 }
132 Console.WriteLine("正在打开串口");
133 serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceived);//添加事件监听程序
134 }
135 catch
136 {
137 MessageBox.Show("该串口不存在或被占用,请确认", "提示");
138 }
139 }
140 else
141 {
142 MessageBox.Show("请输入正确的串口号", "提示");
143 }
144 }
145 }
146
147
148 /// <summary>
149 /// 数据发送按钮
150 /// </summary>
151 /// <param name="sender"></param>
152 /// <param name="e"></param>
153 private void Send_Click(object sender, RoutedEventArgs e)
154 {
155 String s = this.SendText.Text.Trim();
156 if (serialPort.IsOpen)
157 {
158 if (s != null && !"".Equals(s))
159 {
160 try
161 {
162 byte[] Data = Encoding.UTF8.GetBytes(s);
163 serialPort.Write(Data, 0, Data.Length);
164 updateReceiveText("发送数据: " + s);
165 }
166 catch (Exception ex)
167 {
168 Console.WriteLine(ex.Message);
169 MessageBox.Show("串口失效", "提示");
170 }
171 }
172 else
173 {
174 Console.WriteLine("文本为空");
175 }
176 }
177 else
178 {
179 MessageBox.Show("请确认串口状态", "提示");
180 }
181 }
182
183 private void updateReceiveText(string str)
184 {
185 if (ReceiveText.Dispatcher.CheckAccess())
186 {
187 ReceiveText.AppendText(str + "\r\n");
188 }
189 else
190 {
191 Action act = () => { ReceiveText.AppendText(str + "\r\n"); };
192 ReceiveText.Dispatcher.Invoke(act);
193 }
194 }
195
196 private void Connect_Copy_Click(object sender, RoutedEventArgs e)
197 {
198 this.ReceiveText.Clear();
199 this.SendText.Clear();
200 }
201
202
203 }
204 }
205
206
207
1 using System;
2 using System.Collections.Generic;
3 using System.IO.Ports;
4 using System.Linq;
5 using System.Text;
6 using System.Threading.Tasks;
7
8 namespace SerialDebuggingWPF
9 {
10 class MyConvert
11 {
12
13
14
15 public static byte[] strToToHexByte(string hexString)//16进制字符转字节数组
16 {
17 hexString = hexString.Replace(" ", "");
18 if ((hexString.Length % 2) != 0)
19 hexString += " ";
20 byte[] returnBytes = new byte[hexString.Length / 2];
21 for (int i = 0; i < returnBytes.Length; i++)
22 returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
23 return returnBytes;
24 }
25
26 public static String Byteto16String(byte b)//字节转16进制字符
27 {
28 String str = Convert.ToString(b, 16);
29 if (str.Length == 1)
30 {
31 str = "0" + str;
32 }
33 return str;
34 }
35
36 public static String Byteto16String(byte[] b)//字节数组转16进制字符
37 {
38 String s = "";
39 foreach (byte date in b)
40 {
41 String str = Convert.ToString(date, 16);
42 if (str.Length == 1)
43 {
44 str = "0" + str;
45 }
46 s = s + str;
47 }
48
49 return s;
50 }
51
52 public static String Byteto16String(byte[] b, int a)//具体长度字节数组转16进制字符
53 {
54 String s = "";
55 for (int i = 0; i < a; i++)
56 {
57 String str = Convert.ToString(b[i], 16);
58 if (str.Length == 1)
59 {
60 str = "0" + str;
61 }
62 s = s + str;
63 }
64
65 return s;
66 }
67
68 public static String Byteto16String(byte[] B, int a, int b)//指定长度字节数组转16进制字符
69 {
70 String s = "";
71 for (int i = 0; i < b; i++, a++)
72 {
73 String str = Convert.ToString(B[a], 16);
74 if (str.Length == 1)
75 {
76 str = "0" + str;
77 }
78 s = s + str;
79 }
80
81 return s;
82 }
83
84 //int i = Convert.ToInt32("10", 16);//将十六进制“10”转换为十进制i
85
86 //string s = string.Format("{0:X}", i);//将十进制i转换为十六进制s
87
88 public static String[] SubString(String Data)//切割字符串成字符数组
89 {//14
90 Data = Data.Replace(" ", "");
91 String[] data = new String[Data.Length / 2];//7
92 for (int x = 0, y = 0; x < data.Length; x++, y += 2)//0~6
93 {
94 data[x] = Data.Substring(y, 2);
95 }
96 return data;
97 }
98
99 public static String SubString2(String Data)//修改字符串成两个一组加空格
100 {
101 String[] data = SubString(Data);
102 String s = "";
103 for (int i = 0; i < data.Length; i++)
104 {
105 if (i < data.Length - 1)
106 {
107 s = s + data[i] + " ";
108 }
109 else
110 {
111 s = s + data[i];
112 }
113 }
114 return s.ToUpper();
115 }
116
117 public static String String_10toString_16(String s)//十进制字符转16进制字符
118 {
119 int a = Convert.ToInt32(s);
120 String ss = String.Format("{0:X}", a);
121 if (ss.Length == 1)
122 {
123 ss = "0" + ss;
124 }
125 ss = ss.ToUpper();
126 return ss;
127 }
128
129 }
130 }

C#---串口调试助手的更多相关文章
- 串口调试助手vc源程序及其详细编写过程
串口调试助手vc源程序及其详细编写过程 目次: 1.建立项目 2.在项目中插入MSComm控件 3.利用ClassWizard定义CMSComm类控制变量 4.在对话框中添加控件 5.添加串口事件 ...
- 基于.Net C# 通信开发-串口调试助手
基于.Net C# 通信开发-串口调试助手 1.概述 串口调试助手,广泛应用于工控领域的数据监控.数据采集.数据分析等工作,可以帮助串口应用设计.开发.测试人员检查所开发的串口应用软硬件的数据收发状况 ...
- Qt 编写串口调试助手
一.成品图展示 成品图如下所示: 二.串口通讯步骤 1.在工程文件(.pro)中添加串口通信相关运行库:QT += serialport 2.在头文件中添加: #include <QSerial ...
- OSDA - 一个以MIT协议开源的串口调试助手
市场其实有很多开源的串行端口调试助手(Open Serial Port debug assistant),但其中很大一部分没有明确的开源协议,还有一部分只限个人使用,所以编写了一个并以MIT协议授权开 ...
- ubuntu安装ch34x驱动,并安装串口调试助手
1.查看系统自带的ch34x驱动 kangxubo@kangxubo-HKNS:/lib/modules/5.19.0-38-generic/kernel/drivers/usb/serial$ ls ...
- [转]web串口调试助手,浏览器控制串口设备
本文转自:https://blog.csdn.net/ldevs/article/details/39664697 打开串口时查找可用串口供选择 通过javascript调用activex控制串口收发 ...
- Delphi - 采用第三方控件TMS、SPComm开发串口调试助手
第三方控件TMS.SPComm的下载与安装 盒子上可搜索关键字进行下载,TMS是.dpk文件,SPComm.pas文件: 安装方法自行百度,不做赘述. 通过TMS控件进行界面布局 界面预览: Delp ...
- C#基于wpf编写的串口调试助手
支持数据保存,自定义协议解码等功能 链接:https://pan.baidu.com/s/1zvhcES4QIjpDDJGzth1qOA 提取码:lp2x
- 纪念下自学QT 第十天 终于写成了串口调试助手
- Modbus通讯协议学习 - 串口调试
概述 我们在做任何事情之前都需要获取很多 调试步骤: 1:485转换器连接硬件 2:485转换器上的USB接口连接电脑. 3:打开设备管理器 ->查看端口 4:打开串口调试工具,在串口配置的地方 ...
随机推荐
- 数据挖掘机器学习[二]---汽车交易价格预测详细版本{EDA-数据探索性分析}
题目出自阿里天池赛题链接:零基础入门数据挖掘 - 二手车交易价格预测-天池大赛-阿里云天池 相关文章: 特征工程详解及实战项目[参考] 数据挖掘---汽车车交易价格预测[一](测评指标:EDA) 数据 ...
- 【6】VScode 无法在终端输入问题,提示:无法在只读编辑器中编辑
相关文章: [1]VScode中文界面方法-------超简单教程 [2]VScode搭建python和tensorflow环境 [3]VSCode 主题设置推荐,自定义配色方案,修改注释高亮颜色 [ ...
- 21.14 Python 实现Web指纹识别
在当今的Web安全行业中,识别目标网站的指纹是渗透测试的常见第一步.指纹识别的目的是了解目标网站所使用的技术栈和框架,从而进一步根据目标框架进行针对性的安全测试,指纹识别的原理其实很简单,目前主流的识 ...
- NFS实现部署Linux文件共享
NFS 即网络文件系统,是一种使用于分布式文件系统的协议,由Sun公司开发,于1984年向外公布,功能是通过网络让不同的机器,不同的操作系统能够彼此分享各自的数据,让应用程序在客户端通过网络访问位于服 ...
- P5047 [Ynoi2019 模拟赛] Yuno loves sqrt technology II 题解
题目链接:Yuno loves sqrt technology II 很早以前觉得还挺难的一题.本质就是莫队二次离线,可以参考我这篇文章的讲述莫队二次离线 P5501 [LnOI2019] 来者不拒, ...
- SpringMVC的执行流程及初始化流程
今天大致来看一下Spring MVC的执行流程和初始化流程是什么样的 1,执行流程:也就是一个请求是怎么到我们Controller的 2,初始化流程:也就是那些 HandlerMapping.Hand ...
- Java并发(七)----线程sleep、yield、线程优先级
1.sleep 与 yield sleep 调用 sleep 会让当前线程从 Running 进入 Timed Waiting 状态(阻塞) 其它线程可以使用 interrupt 方法打断正在睡眠的线 ...
- RabbitMQ基础学习Full版
RabbitMQ 消息队列在软件中的应用场景 异步处理上(优于原先的方式) 为什么优于呢? 首先,通常情况下,如上图我们其实不用消息队列的情况下,其实也可以不用100ms,不用allof即可 那么优势 ...
- Jackson objectMapper.readValue 方法 详解
直接说结论方便一目了然: 1. 简单的直接Bean.class 2. 复杂的用 TypeReference 这样就完事了. public class TestMain2 { public static ...
- 蔚来杯2022牛客暑期多校训练营2 GJK
比赛链接 G 题解 知识点:思维. \(lds(p)\) 表示最小上升子序列分划数 (Dilworth 定理) \(lis(p)\cdot lds(p) \geq n \Rightarrow max ...