基本信息
源码名称:串口通讯库雏形(入门级示例)
源码大小:0.16M
文件格式:.zip
开发语言:C#
更新时间:2019-11-02
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 1 元 
   源码介绍


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WYZCommunication;
using System.Text.RegularExpressions;

namespace WYZCommSample01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = System.IO.Ports.SerialPort.GetPortNames();
            Array.Sort(ports);
            comboPort.Items.AddRange(ports);
            comboPort.SelectedIndex = comboPort.Items.Count > 0 ? 0 : -1;
            comboBaudrate.SelectedIndex = 0;
            device.OnRaw  = device_OnRaw;

            //注册具体每个事件
            MyDataCollection results = device.Results as MyDataCollection;
            results.Data1.GetNew  = Data1_GetNew;
            results.Data2.GetNew  = Data2_GetNew;
        }

        void Data1_GetNew(AnalyzeResult<int> m)
        {
            ListViewItem item = new ListViewItem("Data1");
            item.SubItems.Add(m.ToString());
            item.SubItems.Add(m.Valid ? "Get new" : "Data timeout");
            item.SubItems.Add(DateTime.Now.ToString("HH:mm:ss"));
            listViewData.Invoke((EventHandler)delegate 
            {
                listViewData.Items.Add(item);
                listViewData.EnsureVisible(listViewData.Items.Count - 1);
            });
        }

        void Data2_GetNew(AnalyzeResult<SampleData> m)
        {
            ListViewItem item = new ListViewItem("Data2");
            item.SubItems.Add(m.ToString());
            item.SubItems.Add(m.Valid ? "Get new" : "Data timeout");
            item.SubItems.Add(DateTime.Now.ToString("HH:mm:ss"));
            listViewData.Invoke((EventHandler)delegate
            {
                listViewData.Items.Add(item);
                listViewData.EnsureVisible(listViewData.Items.Count - 1);
            });
        }

        void device_OnRaw(byte[] bytes)
        {
            StringBuilder builder = new StringBuilder();
            textBoxRaw.Invoke((EventHandler)delegate 
            {
                if (checkHex.Checked)
                {
                    foreach (byte b in bytes)
                    {
                        builder.Append(b.ToString("X2")   " ");
                    }
                }
                else
                {
                    builder.Append(device.Comm.Encoding.GetString(bytes));
                }
                textBoxRaw.AppendText(builder.ToString()); 
            });
        }

        WyzComm device = new WyzComm(new SerialPort(), new MyDataCollection());

        private void buttonConnect_Click(object sender, EventArgs e)
        {
            if (string.Compare(buttonConnect.Tag.ToString(), "OffLine", true) == 0)
            {
                SerialPortSetting sps = new SerialPortSetting();
                sps.Baudrate = int.Parse(comboBaudrate.Text);
                sps.Port = int.Parse(Regex.Match(comboPort.Text, @"\d ").Value);
                if (device.Comm.Open(sps))
                {
                    buttonConnect.Text = "Disconnect";
                    buttonConnect.Tag = "OnLine";
                }
            }
            else
            {
                device.Comm.Close();
                buttonConnect.Tag = "OffLine";
                buttonConnect.Text = "Connect";
            }
        }

        private void checkWordWrap_CheckedChanged(object sender, EventArgs e)
        {
            textBoxRaw.WordWrap = checkWordWrap.Checked;
        }
    }
}