資源簡介
c#串口通訊log存儲及實時波形繪制代碼,工具比較實用,這里只有源代碼,設計窗口的設計還需要大家自行根據我博客中的圖片進行修改
代碼片段和文件信息
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.Threading;
using?System.IO.Ports;
using?System.IO;
using?System.Collections;
using?System.Windows.Forms.DataVisualization.Charting;
using?System.Text.Regularexpressions;
namespace?WindowsFormsApp1
{
????public?partial?class?IO_Setting_Form?:?Form
????{
????????private?SerialPort?ComDevice?=?new?SerialPort();
????????private?Queue?dataQueue?=?new?Queue(100);
????????private?Queue?stringdataQueue?=?new?Queue(100);
????????private?int?num?=?5;
????????public?IO_Setting_Form()
????????{
????????????InitializeComponent();
????????????InitralConfig();
????????}
????????private?void?Form2_Load(object?sender?EventArgs?e)
????????{
????????????textBoxUpSpec.Text?=?“10“;
????????????textBoxBottomSpec.Text?=?“0“;
????????????System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls?=?false;
????????????AddParameters();
????????????InitChart();
????????}
????????private?void?InitralConfig()
????????{
????????????//查詢主機上存在的串口
????????????comboBox_Port.Items.AddRange(SerialPort.GetPortNames());
????????????if?(comboBox_Port.Items.Count?>?0)
????????????{
????????????????comboBox_Port.SelectedIndex?=?0;
????????????}
????????????else
????????????{
????????????????comboBox_Port.Text?=?“未檢測到串口“;
????????????}
????????????comboBox_BaudRate.SelectedIndex?=?-1;
????????????comboBox_DataBits.SelectedIndex?=?-1;
????????????comboBox_StopBits.SelectedIndex?=?-1;
????????????comboBox_CheckBits.SelectedIndex?=?-1;
????????????//pictureBox_Status.BackgroundImage?=?Properties.Resources.red;
????????????//this.pictureBox_Status.Image?=?Image.FromFile(Application.StartupPath?“\\red.PNG“);
????????????this.pictureBox_Status.Image?=?Image.FromFile(“D:/VS_Files/WindowsFormsApplication1/WindowsFormsApplication1/Icon/red.png“);
????????????//向ComDevice.DataReceived(是一個事件)注冊一個方法Com_DataReceived,當端口類接收到信息時時會自動調用Com_DataReceived方法
????????????ComDevice.DataReceived?+=?new?SerialDataReceivedEventHandler(Com_DataReceived);
????????}
????????///?
????????///?一旦ComDevice.DataReceived事件發生,就將從串口接收到的數據顯示到接收端對話框
????????///?
????????///?
????????///?
????????///?
????????char[]?ch1={};
????????private?void?Com_DataReceived(object?sender?SerialDataReceivedEventArgs?e)
????????{
????????????//開辟接收緩沖區
????????????byte[]?Buffer?=?new?byte[ComDevice.BytesToRead];
????????????//從串口讀取數據
????????????ComDevice.Read(Buffer?0?Buffer.Length);
????????????//實現數據的解碼與顯示
????????????AddData(Buffer);
????????????/*
????????????string?str1?=?new?ASCIIEncoding().GetString(Buffer);
????????????for?(int?i?=?0;?i?????????????{
????????????????if?(stringdataQueue.Count?>?100)
????????????????{
?
- 上一篇:Halcon生成的計算圓心C#代碼
- 下一篇:C#客戶管理源代碼
評論
共有 條評論