简单的串口功能
using Microsoft.Win32;
using System;
using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Windows;
using System.Windows.Input;
namespace serial_port
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
//声明串口!!!!!!!!!!!!
SerialPort ComPort = new SerialPort();
//串口集
public string[] portNames { get; set; }
//打开标志
bool openFlag = false;
//得到当前可用串口
private void GetPort()
{
portNames = SerialPort.GetPortNames(); //得到可用串口
if (portNames.Length > 0)
{
txtTip.Text = “检测到“ + portNames.Length + “个可用串口“;
}
else
{
txtTip.Text = “未检测到串口“;
cbbPort.ItemsSource = null;
return;
}
cbbPort.ItemsSource = portNames; //添加可用串口
cbbPort.SelectedValue = portNames[0]; //默认选中
}
//打开事件
private void btnPort_Click(object sender, RoutedEventArgs e)
{
OpenPort();
}
//鼠标进入事件
private void cbbPort_MouseEnter(object sender, MouseEventArgs e)
{
if (openFlag == false)
{
GetPort();
}
}
//打开串口方法
private void OpenPort()
{
if (cbbPort.SelectedValue == null)
{
GetPort();
}
if (openFlag == false)
{
try
{
//设置
ComPort.PortName = cbbPort.SelectedValue.ToString();
ComPort.BaudRate = 115200;
ComPort.Parity = Parity.None;
ComPort.StopBits = StopBits.One;
ComPort.DataBits = 8;
ComPort.Handshake = Handshake.None;
// ComPort.RtsEnable = true;
ComPort.Open();
txtTip.Text = “打开成功“;
openFlag = true;
btnPort.Content = “关闭串口“;
}
catch
{
txtTip.Text = “打开失败“;
}
}
else
{
try
{
ComPort.DiscardInBuffer();
ComPort.DiscardOutBuffer();
ComPort.Close();
openFlag = false;
btnPort.Content = “打开串口“;
txtTip.Text = “关闭成功“;
}
catch
{
txtTip.Text = “关闭失败“;
}
}
}
//发送方法
private bool sendData(byte[] sendBuffer)
{
if (openFlag == false)
{
txtTip.Text = “请先打开串口“;
return false;
}
try
{
ComPort.Write(sendBuffer, 0, sendBuffer.Length);
ComPort.DiscardOutBuffer();
return true;
}
catch
{
txtTip.Text = “发送数据失败“;
return false;
}
}
//接收方法
private void receiveData(byte[] receiveBuffer)
{
receiveBuffer = new byte[ComPort.BytesToRead];
try
{
ComPort.Read(receiveBuffer, 0, receiveBuffer.Length);
ComPort.DiscardInBuffer();
}
catch
{
txtTip.Text = (“读取失败“);
}
}
//按钮例子,调用发送
private void btn_test_Click(object sender, RoutedEventArgs e)
{
byte[] send_data = new byte[5];
send_data[0] = 0xff;
send_data[1] = 0xff;
send_data[2] = 0xff;
send_data[3] = 0xff;
send_data[4] = 0xff;
sendData(send_data);
}
}
}
<Window x:Class=“serial_port.MainWindow”
xmlns=“http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=“http://schemas.microsoft.com/winfx/2006/xaml”
xmlns:d=“http://schemas.microsoft.com/expression/blend/2008”
xmlns:mc=“http://schemas.openxmlformats.org/markup-compatibility/2006”
xmlns:local=“clr-namespace:serial_port”
mc:Ignorable=“d”
Title=“MainWindow” Height=“350” Width=“525”>
<Grid>
<TextBox x:Name=“txtTip” HorizontalAlignment=“Left” Height=“24” Margin=“10,163,0,0” TextWrapping=“Wrap” Text=“提示“ VerticalAlignment=“Top” Width=“172”/>
<ComboBox x:Name=“cbbPort” RenderTransformOrigin=“1.436,2.058” BorderBrush=“Black” Foreground=“Black” FontFamily=“Segoe Script” Background=“{x:Null}” FontWeight=“Bold” Height=“32” VerticalAlignment=“Top” HorizontalAlignment=“Left” Width=“74” MouseEnter=“cbbPort_MouseEnter” Margin=“10,10,0,0”/>
<Button x:Name=“btnPort” Content=“打开串口“ Margin=“121,10,0,0” RenderTransformOrigin=“-0.833,1.211” Click=“btnPort_Click” Background=“#FFFAFAFA” BorderBrush=“Black” FontFamily=“STXingkai” FontSize=“16” HorizontalAlignment=“Left” Width=“85” Height=“32” VerticalAlignment=“Top”/>
<Button x:Name=“btn_test” Content=“例子“ Margin=“241,109,0,0” RenderTransformOrigin=“-0.833,1.211” Background=“#FFFAFAFA” BorderBrush=“Black” FontFamily=“STXingkai” FontSize=“16” HorizontalAlignment=“Left” Width=“85” Height=“32” VerticalAlignment=“Top” Click=“btn_test_Click”/>
</Grid>
</Window>