仅作自己学习使用
1 准备部份
需要两个调试软件commix和Virtual Serial Port Driver,分别用于监视串口和创造虚拟串口。
第一个软件是这样的:
资源在这里:免费下载:Commix
也可以前往官网下载:Bwsensing— Attitude is everything
点击Download即可
第二个软件是这样的:
官方下载链接:Virtual Serial Port Driver
2 Xaml代码
界面做得很丑,能用就行,关键是原理:
<Window x:Class="WPF_ZhaoXi_0205.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:WPF_ZhaoXi_0205"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBox x:Name="textBox_receive" Grid.Row="0" Grid.Column="0" FontSize="15" Margin="10" Text="接收窗口"/>
<TextBox x:Name="textBox_send" Grid.Row="0" Grid.Column="1" FontSize="15" Margin="10" Text="发送窗口"/>
<Button x:Name="button_open" Grid.Row="1" Grid.Column="0" Content="打开串口" Margin="10" Click="button_open_Click"/>
<Button x:Name="button_recisive" Grid.Row="1" Grid.Column="1" Content="接收数据" Margin="10" Click="button_recisive_Click"/>
<Button x:Name="button_send" Grid.Row="2" Grid.Column="0" Content="发送数据" Margin="10" Click="button_send_Click"/>
</Grid>
</Window>
3 cs代码
using System.IO.Ports;
using System.Text;
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;
namespace WPF_ZhaoXi_0205
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
// 声明一个串口对象
SerialPort sp = null;
public MainWindow()
{
InitializeComponent();
// 实例化串口对象
//sp = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
sp = new SerialPort();
// 设置通讯的属性
sp.PortName = "COM2"; // 串口名称
sp.BaudRate = 9600; // 波特率
sp.Parity = Parity.None; // 校验位
sp.DataBits = 8; // 数据位
sp.StopBits = StopBits.One; // 停止位
// 第二种接收数据的方式,被动接收,如称重,扫码枪等
sp.DataReceived += Sp_DataReceived;
}
/// <summary>
/// 第二种数据接收的方式
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] bt1 = new byte[sp.BytesToRead];
sp.Read(bt1, 0, bt1.Length);
// 这里在异步线程处理了UI控件,而UI控件必须在主线程处理,因此要报错
//textBox_recsive.Text = Encoding.ASCII.GetString(bt1);
// 因此把这个语句放在UI线程(主线程进行)
this.Dispatcher.Invoke(() =>
{
textBox_receive.Text = Encoding.ASCII.GetString(bt1);
});
}
/// <summary>
/// 打开串口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_open_Click(object sender, RoutedEventArgs e)
{
// 打开动作
try
{
// 串口的一端只能同时被一个用户打开,否则报错,所以看是否串口已经被占用
sp.Open();
MessageBox.Show(sp.PortName+"串口已打开", "提示");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"提示");
}
}
/// <summary>
/// 发送数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_send_Click(object sender, RoutedEventArgs e)
{
// 发送动作(与打开动作操作同一个串口对象)
// sp.Write();
string str_send = textBox_send.Text;
byte[] bt1 = Encoding.ASCII.GetBytes(str_send);
byte[] bt2 = new byte[] { 0x01, 0x02, 0x03 };
sp.Write(bt1, 0, bt1.Length); // 在bytes中从位置0开始发送bytes.Length个字节
}
/// <summary>
/// 接收数据(第一种接收方式,主动请求接收方式)
/// 我要你再给
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_recisive_Click(object sender, RoutedEventArgs e)
{
// 长度是串口能够读到的最大的字节数量
byte[] bt1 = new byte[sp.BytesToRead];
sp.Read(bt1, 0, bt1.Length); // 从当前串口中的位置0处开始读取bt1.Length个字节到bt1中
textBox_receive.Text = Encoding.ASCII.GetString(bt1);
}
}
}