当前位置:   article > 正文

WPF串口通讯程序

WPF串口通讯程序

目录

一 设计原型

二 后台源码


一 设计原型

二 后台源码

  1. using HardwareCommunications;
  2. using System.IO.Ports;
  3. using System.Windows;
  4. namespace PortTest
  5. {
  6. /// <summary>
  7. /// Interaction logic for MainWindow.xaml
  8. /// </summary>
  9. public partial class MainWindow : Window
  10. {
  11. public MainWindow()
  12. {
  13. InitializeComponent();
  14. }
  15. SerialCommunication s = new SerialCommunication(new SerialPortParameter()
  16. {
  17. Port = "COM1",
  18. DataBits = 8,
  19. StopBits = StopBits.One,
  20. Baudrate = 115200,
  21. Parity = Parity.None,
  22. ConnectionTimeout = 3000,
  23. });
  24. SerialCommunication s2 = new SerialCommunication(new SerialPortParameter()
  25. {
  26. Port = "COM1",
  27. DataBits = 8,
  28. StopBits = StopBits.One,
  29. Baudrate = 115200,
  30. Parity = Parity.None,
  31. ConnectionTimeout = 3000,
  32. }, "\r\n");
  33. private void Button_Click(object sender, RoutedEventArgs e)
  34. {
  35. s.Open();
  36. s.Send("hello");
  37. s.Close();
  38. }
  39. private void Button_Click_1(object sender, RoutedEventArgs e)
  40. {
  41. //s.Open();
  42. //string data= s.Receive(1024);
  43. //string data= s.ReadExisting();
  44. //string data= s.ReadLine();
  45. s2.Open();
  46. string data = s2.Receive();
  47. s2.DataReadWriteTimeoutEvent += S2_DataReadWriteTimeoutEvent;
  48. s2.DataReceiveEvent += S2_DataReceiveEvent;
  49. log.Text = data;
  50. s.Close();
  51. }
  52. private void S2_DataReceiveEvent(object sender, string data)
  53. {
  54. count++;
  55. Dispatcher.Invoke(new Action(() =>
  56. {
  57. log3.Text = data + ":" + count;
  58. }));
  59. }
  60. int count = 0;
  61. private void S2_DataReadWriteTimeoutEvent(object sender, string errorInfo)
  62. {
  63. count++;
  64. Dispatcher.Invoke(new Action(() =>
  65. {
  66. log2.Text = errorInfo + count;
  67. }));
  68. }
  69. }
  70. }
  1. <Window x:Class="PortTest.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:PortTest"
  7. mc:Ignorable="d"
  8. Title="MainWindow" Height="450" Width="800">
  9. <Grid>
  10. <Grid.RowDefinitions>
  11. <RowDefinition/>
  12. <RowDefinition/>
  13. <RowDefinition/>
  14. <RowDefinition/>
  15. <RowDefinition/>
  16. </Grid.RowDefinitions>
  17. <Button Content="发送" FontSize="30" Click="Button_Click"/>
  18. <Button Content="接收" FontSize="30" Click="Button_Click_1" Grid.Row="1"/>
  19. <TextBox x:Name="log" Grid.Row="2"/>
  20. <TextBox x:Name="log2" Grid.Row="3"/>
  21. <TextBox x:Name="log3" Grid.Row="4"/>
  22. </Grid>
  23. </Window>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/869642
推荐阅读
相关标签
  

闽ICP备14008679号