• C#编程之串口(二)


    这一章继续讲解一下串口部分设计。

    我们知道串口通常情况下有两个重要的参数需要设定,分别是串口号和波特率。

    串口号,用来进行数据流传送,波特率用来设定数据流传送速度。这里我们选用comboBox控件来设计可以配置这两个参数的方法。

    添加这两个控件的同时,我们添加发送编辑框:

    1        <ComboBox Name="myCOM" Margin="0,40,10,0" VerticalAlignment="Top" HorizontalAlignment="Right" Width="90" Height="22"/>
    2         <Label Content="COM" HorizontalAlignment="Left" Margin="417,10,0,0" VerticalAlignment="Top" Width="62"/>
    3         <ComboBox Name="myBaudRate" Margin="0,102,10,0" VerticalAlignment="Top" HorizontalAlignment="Right" Width="90" Height="22"/>
    4         <Label Content="BaudRate" HorizontalAlignment="Left" Margin="417,71,0,0" VerticalAlignment="Top"/>
    5         <TextBox HorizontalAlignment="Left" Height="80" Margin="10,240,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="482"/>
    6        <Label Content="Transmit Data" HorizontalAlignment="Left" Margin="15,214,0,0" VerticalAlignment="Top" Width="97"/>

    那么这里我们要读取comboBox控件内容,并将该内容用来配置串口参数:

    内容读取: comboBoxName.Text ,默认值的设定为: comboBoxName.SelectedIndex = comboBoxName.Items.IndexOf("Context"); 例如,将参数打印到edit编辑框中:

    1 myCOM.SelectedIndex = myCOM.Items.IndexOf("COM1");
    2 m_textBox1.Text = myCOM.Text;

    将其配置为串口参数: myPort.PortName = myCOM.Text;   myPort.BaudRate = int.Parse(myBaudRate.Text); 

    整体代码:

    CS:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.IO.Ports;
    
    namespace SearilPort
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            SerialPort myPort = new SerialPort();
            bool serialState;
            public MainWindow()
            {
                InitializeComponent();
                myComboxs();
            }
            private void myComboxs()
            {
                myCOM.Items.Add("COM0");
                myCOM.Items.Add("COM1");
                myCOM.Items.Add("COM2");
                myCOM.Items.Add("COM3");
                myCOM.Items.Add("COM4");
                myCOM.SelectedIndex = myCOM.Items.IndexOf("COM1");
                myBaudRate.Items.Add("2400");
                myBaudRate.Items.Add("4800");
                myBaudRate.Items.Add("9600");
                myBaudRate.Items.Add("38400");
                myBaudRate.Items.Add("115200");
                myBaudRate.SelectedIndex = myBaudRate.Items.IndexOf("9600");
                serialState = false;
                myPort.DataReceived += DataReceived;
            }
            private void Btn1_Click(object sender, RoutedEventArgs e)
            {
                try
                {
                    if (serialState == false)
                    {
                        if(myPort.IsOpen)
                        {
                            myPort.Close();
                        }
                        myPort.BaudRate = int.Parse(myBaudRate.Text);
                        myPort.DataBits = 8;
                        myPort.PortName = myCOM.Text;
                        myPort.Open();
                        serialState = true;
                        m_textBox1.Text = "Serial Port is Opened...
    ";
                        myBtn.Content = "Closed";
                    }
                    else
                    {
                        if (myPort.IsOpen)
                        {
                            myPort.Close();
                        }
                        serialState = false;
                        m_textBox1.Text = "Serial Port is Closed...
    ";
                        myBtn.Content = "Open";
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            private void DataReceived(object sender,SerialDataReceivedEventArgs e)
            {
                try
                {
                    byte[] inbuf = new byte[8];
                    myPort.Read(inbuf, 0, inbuf.Length);
                    string str = System.Text.Encoding.Default.GetString(inbuf);
                    this.Dispatcher.Invoke(new Action(() =>
                        {
                            foreach (byte i in inbuf)
                            {
                                m_textBox1.Text += i.ToString("X2");
                            }
                            m_textBox1.Text += "
    ";
                        }));
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
    View Code

    AXML:

     1 <Window x:Class="SearilPort.MainWindow"
     2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     4         Title="MainWindow" Height="350" Width="525">
     5     <Grid>
     6         <Button Name="myBtn" Content="Open" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="75" Click="Btn1_Click" Margin="0,0,25,147"/>
     7         <TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Width="393" Height="160" Name="m_textBox1" Margin="10,40,0,0"/>
     8         <ComboBox Name="myCOM" Margin="0,40,10,0" VerticalAlignment="Top" HorizontalAlignment="Right" Width="90" Height="22"/>
     9         <Label Content="COM" HorizontalAlignment="Left" Margin="417,10,0,0" VerticalAlignment="Top" Width="62"/>
    10         <ComboBox Name="myBaudRate" Margin="0,102,10,0" VerticalAlignment="Top" HorizontalAlignment="Right" Width="90" Height="22"/>
    11         <Label Content="BaudRate" HorizontalAlignment="Left" Margin="417,71,0,0" VerticalAlignment="Top"/>
    12         <TextBox HorizontalAlignment="Left" Height="80" Margin="10,240,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="482"/>
    13         <Label Content="Received Data&#xD;&#xA;" HorizontalAlignment="Left" Margin="15,10,0,0" VerticalAlignment="Top" Width="121" Height="30"/>
    14         <Label Content="Transmit Data" HorizontalAlignment="Left" Margin="15,214,0,0" VerticalAlignment="Top" Width="97"/>
    15 
    16     </Grid>
    17 </Window>
    View Code

    注:我们将接收事件初始化放在comboBox控件初始化函数内,是为了避免重复申明自定义接收事件函数 myPort.DataReceived += DataReceived; 

    至此,串口参数option功能基本设计完成,在接下来的章节中,我们将对接收到数据进行加密,加密后发送回主机,完成创建加密序列号功能。

    End.

    谢谢.

  • 相关阅读:
    AJAX 跨域请求与 JSONP详解
    深入理解PHP的mvc框架
    读文文件md5值
    快速排序
    编写简单GUI程序
    简单的加减法
    rallway.py
    用列表构建栈结构
    模拟用户登陆注册
    密码生成
  • 原文地址:https://www.cnblogs.com/lumao1122-Milolu/p/12010583.html
Copyright © 2020-2023  润新知