三通道示波器
【说明】
【语言】:C#
【平台】:Win10 .NETFrameWork
【硬件】:采用CH340作TTL转串口电平转换器
【信道复用方式】:分时复用
【下位机软件要求】:见代码注释
【代码使用方式】:使用vs建立C#窗体应用后直接将代码作为主文件即可
【注意事项】:目前的设定是PC连上串口设备后才能运行
下位机软件要求截取:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15/*FuncTip * * @Name:YlocsOfChannlesInload * @Arg:None * @Func:将ylocs的数据加载到各通道缓冲区ylocsofChannelX * 下位机发送数据格式: * ————————————————————— * 标志数据|CH1数据|CH2数据|CH3数据|标志数据... * ————————————————————— * 数据长度:8bits * 建议二进制形式发送 * 如使用标准库printf * 建议采用%c格式发送数据 * */
完整代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Timers; using System.IO.Ports; using System.Threading; namespace RubbishDrawer { public partial class Form1 : Form { //窗体初始化 public Form1() { InitializeComponent(); /* 绘制作图区域边框 */ DrawTable(); /* 添加按钮 */ AddbtnStop(); ylocsInit(); /* 初始化时钟 */ TimerConfig(100);//时钟TIC间隔搞长点----<帧数>搞低点 /* 初始化串口 */ PortConfig(115200);//波特率设置为9600 串口在连接后才可被检测到 } /* ----------A-作图---------- */ /* 作图区相关常量 */ private const short TableLength = 63;//图板的长度 private const short TableHeight = 70;//图版的高度 private const int TableLeft = 50;//图板左边沿 private const int TableTop = 20;//图板上边沿 int[] ylocs = new int[TableLength * 4];//缓存发来的数据,用作points的y坐标元数据 int[] ylocsOfCh1 = new int[TableLength]; int[] ylocsOfCh2 = new int[TableLength]; int[] ylocsOfCh3 = new int[TableLength]; /* 计时器相关常量 */ private const int TicMax = TableLength + 2;//TIMENOW的上限(周期TIC数) /*FuncTip * * @Name:ylocsInit * @Arg:None * @Func:将各通道缓冲区ylocsofchX初始化位对应Y轴0刻度位置 * */ public void ylocsInit() { for(int ptr=0;ptr<TableLength;ptr++){ ylocsOfCh1[ptr] = TableTop; ylocsOfCh2[ptr] = TableTop + TableHeight / 3; ylocsOfCh3[ptr] = TableTop + 2 * TableHeight / 3; } } /*FuncTip * * @Name:DrawTable * @Arg:None * @Func:绘制作图区域边界 * */ public void DrawTable() { Point[] Edges = { new Point(TableLeft-1, TableTop), new Point(TableLeft+TableLength-1, TableTop), new Point(TableLeft+TableLength-1, TableTop+TableHeight-1), new Point(TableLeft-1, TableTop+TableHeight-1), new Point(TableLeft-1, TableTop), }; Graphics Table = CreateGraphics(); Table.PageUnit = GraphicsUnit.Millimeter; Pen TablePen = new Pen(Color.Black, 0.1F); Table.DrawLines(TablePen, Edges); Point[] CrossLines = { new Point(TableLeft-1, TableTop+TableHeight/3), new Point(TableLeft+TableLength-1, TableTop+TableHeight/3), new Point(TableLeft+TableLength-1, TableTop+2*TableHeight/3), new Point(TableLeft-1, TableTop+2*TableHeight/3), }; //TablePen.Width = 0.5F; Table.DrawLines(TablePen, CrossLines); } /* 添加控制按钮(绘图允许控制) */ /*FuncTip * * @Name:AddbtnStop * @Arg:None * @Func:添加按钮btnStop到窗体 * */ public void AddbtnStop() { Button btnStop = new Button(); btnStop.Text = "Stop"; btnStop.Top = TableTop + TableHeight + 2; btnStop.Left = TableLeft; btnStop.Click += new System.EventHandler(btnStopClk); this.Controls.Add(btnStop); } /*VarTip * * @Name:ShowOrNot * Function:绘图允许标志 * */ bool ShowOrNot = true; /*FuncTip * * @Name:btnStopClk * @Arg:sender触发事件的控件 * e事件(通用写法) * @Func:按键btnStop的单击事件响应函数 * 改变Var:ShowOrNot的值 * */ public void btnStopClk(object sender, EventArgs e) { ShowOrNot = ShowOrNot == true ? false : true; } /*FuncTip * * @Name:TimerConfig * @Arg:None * @Func:时钟(计时器)配置 * */ public void TimerConfig(int TimerTic) { /* 获取timer对象 */ System.Timers.Timer ATimer = new System.Timers.Timer(TimerTic); ATimer.AutoReset = true;//自动回填 ATimer.Elapsed += new System.Timers.ElapsedEventHandler(TimerEvent); ATimer.Enabled = true;//使能时钟 } /*FuncTip * * @Name:TimerEvent * @Arg:通用写法 * @Func:计时器事件响应函数 * 清空绘图区图像 * 读取串口数据 * 绘制新数据图像 * */ public void TimerEvent(object sender, ElapsedEventArgs e) { if (!ShowOrNot) return; /* 更新ylocs */ //清空图板 { Graphics ForNext = CreateGraphics(); PointF[] points = new PointF[TableLength]; PointF[] pointsForCh1 = new PointF[TableLength]; PointF[] pointsForCh2 = new PointF[TableLength]; PointF[] pointsForCh3 = new PointF[TableLength]; for (int jj = 0; jj < TableLength; jj++)//填充本次绘制的部分 { points[jj] = new PointF((float)jj + TableLeft, ylocs[jj]); pointsForCh1[jj] = new PointF((float)jj + TableLeft, ylocsOfCh1[jj]); pointsForCh2[jj] = new PointF((float)jj + TableLeft, ylocsOfCh2[jj]); pointsForCh3[jj] = new PointF((float)jj + TableLeft, ylocsOfCh3[jj]); } ForNext.PageUnit = GraphicsUnit.Millimeter; //ForNext.DrawCurve(new Pen(this.BackColor, 5F), points); ForNext.DrawCurve(new Pen(this.BackColor, 5F), pointsForCh1); ForNext.DrawCurve(new Pen(this.BackColor, 5F), pointsForCh2); ForNext.DrawCurve(new Pen(this.BackColor, 5F), pointsForCh3); /* 获取y坐标数据到ylocs[] -- GetData() */ //RandYlocs();//-测试阶段用randylocs ReadData(); } /* 按TIMENOW特征绘图 */ /* * 绘图方式为从低到高刷新图样 * TIMENOW作为刷新尺度的标定 */ /* 按参数绘制数据对应图像 */ //if(TIMENOW<TableLength-3)//3是前后两种颜色曲线的隔离区-否则“清空”操作不好做 MkPntAndDraw(); } /*FuncTip * * @Name:MkPntAndDraw * @Arg:None * @Func:在TimerEvent中被调用 * 将串口数据转换成绘图区坐标点 * 加载到ylocsofChannelX中 * */ public void MkPntAndDraw() { DrawTable(); PointF[] points = new PointF[TableLength]; PointF[] pointsForCh1 = new PointF[TableLength]; PointF[] pointsForCh2 = new PointF[TableLength]; PointF[] pointsForCh3 = new PointF[TableLength]; for (int jj = 0; jj < TableLength; jj++)//填充本次绘制的部分 { points[jj] = new PointF((float)jj + TableLeft, ylocs[jj]); pointsForCh1[jj] = new PointF((float)jj+TableLeft,ylocsOfCh1[jj]); pointsForCh2[jj] = new PointF((float)jj + TableLeft, ylocsOfCh2[jj]); pointsForCh3[jj] = new PointF((float)jj + TableLeft, ylocsOfCh3[jj]); } /* 两种色笔 */ Pen RedPen = new Pen(Color.Red, 0.5F); Pen GreenPen = new Pen(Color.Green, 0.5F); /* 获取 graphis实列对象 -- 用于调用drawCurve() */ Graphics Drawer = CreateGraphics(); Drawer.PageUnit = GraphicsUnit.Millimeter;//以毫米为单位绘制图形 //Drawer.DrawCurve(GreenPen, points); Drawer.DrawCurve(GreenPen, pointsForCh1); Drawer.DrawCurve(GreenPen, pointsForCh2); Drawer.DrawCurve(GreenPen, pointsForCh3); Drawer.Dispose(); } /* ---------B-串口通信---------- */ /*VarcTip * * @Name:_SerialPort * @Func:串口通信封装类 对象实例 * 用于读取串口数据 * */ System.IO.Ports.SerialPort _serialPort = new SerialPort(); /*FuncTip * * @Name:PortConfig * @Arg:BaudRate 串口的波特率 * @Func:配置串口 * */ public void PortConfig(int BaudRate)///需要读取数据--使用thread类 多线程 - 再看看哦 { //配置COM口参数 /* 端口名 串口在连接后才可被检测到 */ try { string[] portnames = SerialPort.GetPortNames(); _serialPort.PortName = portnames[0]; } catch (Exception e) { MessageBox.Show("未在COM口检测到设备,请连接串口通信设备后重新启动应用程序n" + e.ToString()); } /* 波特率 */ _serialPort.BaudRate = BaudRate; /* 数据位 */ _serialPort.DataBits = 8; /* 校验位 */ _serialPort.Parity = Parity.None; /* 停止位 */ _serialPort.StopBits = StopBits.One; /* 配置COM口超时参数 */ _serialPort.ReadTimeout = SerialPort.InfiniteTimeout; _serialPort.WriteTimeout = SerialPort.InfiniteTimeout; /* 打开COM口 */ _serialPort.Open(); } /*FuncTip * * @Name:ReadData * @Arg:None * @Func:读取数据到ylocs * */ public void ReadData() { byte[] buffer = new byte[TableLength * 4];/* (通道数3+起始标志1)四个 */ _serialPort.Read(buffer, 0, TableLength * 4); int ptrForYlocs = 0; /* 将byte数据转换成int16(short)类型 */ //for(int ptr = 0; ptr+2 < buffer.Length; ptr +=2){ for (int ptr = 0; ptr < ylocs.Length; ptr++) { ylocs[ptrForYlocs] = buffer[ptr];//BitConverter.ToInt16(buffer,ptr); ptrForYlocs++; } //MessageBox.Show(ylocs[0].ToString()); /* 将ylocs中的数数据分配到各个通道的缓冲区 */ /**使用readInt方法读取数据到ylocs,已经废弃 **/ /**ylocs[ptr] = ReadInt() % (TableHeight - 5) + TableTop;**/ /* 装填ylocs到各个通道的缓冲区(ptrforchX) */ YlocsOfChannlesInload(); /* 清空_serialport数据缓冲区 */ _serialPort.DiscardInBuffer(); _serialPort.DiscardOutBuffer(); } /*FuncTip * * @Name:YlocsOfChannlesInload * @Arg:None * @Func:将ylocs的数据加载到各通道缓冲区ylocsofChannelX * 下位机发送数据格式: * ————————————————————— * 标志数据|CH1数据|CH2数据|CH3数据|标志数据... * ————————————————————— * 数据长度:8bits * 建议二进制形式发送 * 如使用标准库printf * 建议采用%c格式 * */ private void YlocsOfChannlesInload() { short ptrofylocsstart = 0;/* 数据开始位置标志,找到第一个255的位置 */ /* 检索第一个数据包&找到刷新后第一个完整数据包的起始位置 */ while ((ptrofylocsstart < 5) && (ylocs[ptrofylocsstart] != 88)) { ptrofylocsstart++; } int ptrforinload = 0;/* 装填数据时的迭代器,每个channel都re0 */ for (int ptrforch1 = ptrofylocsstart + 1; ptrforch1 + 4 < ylocs.Length; ptrforch1 += 4) { ylocsOfCh1[ptrforinload] = ylocs[ptrforch1] % (TableHeight / 3) + TableTop; ptrforinload++; } ptrforinload = 0; for (int ptrforch2 = ptrofylocsstart + 2; ptrforch2 + 4 < ylocs.Length; ptrforch2 += 4) { ylocsOfCh2[ptrforinload] = ylocs[ptrforch2] % (TableHeight / 3) + TableTop + TableHeight / 3; ptrforinload++; } ptrforinload = 0; for (int ptrforch3 = ptrofylocsstart + 3; ptrforch3 + 4 < ylocs.Length; ptrforch3 += 4) { ylocsOfCh3[ptrforinload] = ylocs[ptrforch3] % (TableHeight / 3) + TableTop + 2 * TableHeight / 3; ptrforinload++; } for (int ptrforfinalPoints = TableLength - 1; ptrforfinalPoints >= TableLength - 2; ptrforfinalPoints--) { ylocsOfCh1[ptrforfinalPoints] = TableTop; ylocsOfCh2[ptrforfinalPoints] = TableTop + TableHeight / 3; ylocsOfCh3[ptrforfinalPoints] = TableTop + 2 * TableHeight / 3; } } } }
最后
以上就是可靠金鱼最近收集整理的关于C#三通道示波器-串口通信的全部内容,更多相关C#三通道示波器-串口通信内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复