简单的串口通信

实现简单的串口通信
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;
using System.IO;
using System.Threading;
using System.Windows.Threading;


namespace 串口通信
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        SerialPort sp = new SerialPort();
        public MainWindow()
        {
            InitializeComponent();
            //加载所有的端口
            cbxPorts.ItemsSource = SerialPort.GetPortNames();
            //设置botelv
            cbxBat.ItemsSource = new int[] { 38400, 9600, 115200, 57600 };
        }
        //打开端口
        private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            //判断端口是否打开
            if (!sp.IsOpen)
            {
                //端口
                sp.PortName = cbxPorts.Text;
                //波特率
                sp.BaudRate = int.Parse(cbxBat.Text);
                //打开端口
                sp.Open();
                tbxShow.Text = "串口打开成功";
            }
            else
            {
                tbxShow.Text = "打开串口失败";
            }
        }
        //发送信息
        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            //向端口发送信息
            byte[] buff = Encoding.Default.GetBytes(tbxSend.Text);


            sp.Write(buff,0,buff.Length);
        }
        //接收事件
        private void btnRecieve_Click(object sender, RoutedEventArgs e)
        {
            sp.DataReceived += sp_DataReceived;
        }


        void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            //获取缓存中的字节数
            int count = sp.BytesToRead;
            //字符转数组
            byte[] buff = new byte[count];
            //读取到端口发来信息
            sp.Read(buff,0,count);
            //把读取的信息放到控件中
            string str = Encoding.Default.GetString(buff);
            //委托显示
            Dispatcher.Invoke(new Action(()=>
                {
                    lbxRecieve.Items.Add(str);
                }));
        }
    }
}
<Window x:Class="串口通信.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="411.036" Width="532.3">
    <Grid>
        <ComboBox x:Name="cbxPorts" HorizontalAlignment="Left" Margin="95,27,0,0" VerticalAlignment="Top" Width="75" FontSize="16"/>
        <Button x:Name="btnOpen" Content="打开" HorizontalAlignment="Left" Margin="95,138,0,0" VerticalAlignment="Top" Width="75" FontSize="16" Click="btnOpen_Click"/>
        <Label Content="端口:" HorizontalAlignment="Left" Margin="35,29,0,0" VerticalAlignment="Top"/>
        <ComboBox x:Name="cbxBat" HorizontalAlignment="Left" Margin="95,78,0,0" VerticalAlignment="Top" Width="75" FontSize="16"/>
        <Label Content="波特率:" HorizontalAlignment="Left" Margin="35,80,0,0" VerticalAlignment="Top"/>
        <Label Content="状态:" HorizontalAlignment="Left" Margin="41,196,0,0" VerticalAlignment="Top" Width="129"/>
        <TextBox x:Name="tbxShow" HorizontalAlignment="Left" Height="26" Margin="41,244,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="129"/>
        <TextBox x:Name="tbxSend" HorizontalAlignment="Left" Height="26" Margin="226,78,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="252"/>
        <Label Content="发送数据框:" HorizontalAlignment="Left" Margin="226,27,0,0" VerticalAlignment="Top" Width="102"/>
        <Button x:Name="btnSend" Content="发送" HorizontalAlignment="Left" Margin="403,138,0,0" VerticalAlignment="Top" Width="75" Click="btnSend_Click"/>
        <Label Content="发送数据框:" HorizontalAlignment="Left" Margin="226,196,0,0" VerticalAlignment="Top" Width="102"/>
        <Button x:Name="btnRecieve" Content="接收" HorizontalAlignment="Left" Margin="403,316,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.548,3.486" Click="btnRecieve_Click"/>
        <ListBox x:Name="lbxRecieve" HorizontalAlignment="Left" Height="69" Margin="226,227,0,0" VerticalAlignment="Top" Width="252"/>

    </Grid>
</Window>
上面是界面代码

 

猜你喜欢

转载自blog.csdn.net/qq_41634598/article/details/80432076
今日推荐