学习编写串口通信过程中,发现主要难题在于写出的代码能够正常发送数据,但是无法接收数据,而且网上案例较少,研究多日终于能够正常完成收发的串口通信代码,特分享源码供大家讨论。新手上路,写法拙劣,望多多包涵。
是用C#在Vilual Studio2022的WPF框架内编写的。
(1)UI界面设计代码
<Window x:Class="SerialExample2.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:SerialExample2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBox x:Name="Sended_Work" HorizontalAlignment="Left" Margin="550,48,0,0" TextWrapping="Wrap" Text="发送消息" VerticalAlignment="Top" Width="200" Height="82" FontSize="16"/>
<TextBox x:Name="tbxReceivedData" HorizontalAlignment="Left" Margin="555,175,0,0" TextWrapping="Wrap" Text="接收消息" VerticalAlignment="Top" Width="200" Height="130" FontSize="16"/>
<Label Content="接收区" HorizontalAlignment="Left" Margin="550,140,0,0" VerticalAlignment="Top" FontSize="16"/>
<Label Content="发送区" HorizontalAlignment="Left" Margin="550,10,0,0" VerticalAlignment="Top" FontSize="16"/>
<ComboBox x:Name="cbxPortName" HorizontalAlignment="Left" Margin="110,14,0,0" VerticalAlignment="Top" Width="120"/>
<Button x:Name="btnOpenPort" Content="打开" HorizontalAlignment="Left" Margin="55,190,0,0" VerticalAlignment="Top" Height="40" Width="50" Click="btnOpenPort_Click_1"/>
<Button x:Name="btnSendData" Content="发送" HorizontalAlignment="Left" Margin="155,195,0,0" VerticalAlignment="Top" Height="40" Width="60" Click="btnSendData_Click_1"/>
<TextBox x:Name="tbxDataToSend" HorizontalAlignment="Left" Margin="60,105,0,0" TextWrapping="Wrap" Text="编辑输入内容" VerticalAlignment="Top" Width="170" FontSize="16" Height="60"/>
</Grid>
</Window>
(2)后台代码
using System.IO.Ports;
using System.Text;
using System.Windows;
namespace SerialExample2
{
public partial class MainWindow : Window
{
private readonly SerialPort _serialPort;
public MainWindow()
{
InitializeComponent();
// Get available serial ports
string[] portNames = SerialPort.GetPortNames();
// Add port names to combobox
cbxPortName.ItemsSource = portNames;
cbxPortName.SelectedIndex = 0;
// Create serial port object
_serialPort = new SerialPort();
// Register event handler for data received event
_serialPort.DataReceived += SerialPortOnDataReceived;
}
private void SerialPortOnDataReceived(object sender, SerialDataReceivedEventArgs e)
{
// Read available bytes from the serial port
byte[] buffer = new byte[_serialPort.BytesToRead];
_serialPort.Read(buffer, 0, buffer.Length);
// Convert the bytes to string using the default encoding
string data = Encoding.Default.GetString(buffer);
// Update the UI with received data
Dispatcher.Invoke(() =>
{
tbxReceivedData.Text += data;
});
}
private void btnOpenPort_Click_1(object sender, RoutedEventArgs e)
{
if (_serialPort.IsOpen)
{
// Close serial port
_serialPort.Close();
// Update UI
btnOpenPort.Content = "Open";
cbxPortName.IsEnabled = true;
tbxDataToSend.IsEnabled = false;
btnSendData.IsEnabled = false;
}
else
{
// Create serial port object
_serialPort.PortName = cbxPortName.SelectedValue.ToString();
_serialPort.BaudRate = 9600;
// Open serial port
_serialPort.Open();
// Update UI
btnOpenPort.Content = "Close";
cbxPortName.IsEnabled = false;
tbxDataToSend.IsEnabled = true;
btnSendData.IsEnabled = true;
}
}
private void btnSendData_Click_1(object sender, RoutedEventArgs e)
{
if (_serialPort.IsOpen)
{
// Send data
_serialPort.Write(tbxDataToSend.Text);
Sended_Work.Text += tbxDataToSend.Text;
}
}
}
}