WPF与51单片机之间的串口通信

电子元件 4年前 (2022) aysz01
0

WPF部分:

(1)建立WPF工程,步骤略

下面是MainWindow.xaml.cs的内容

1 using System;

2 using System.Collections.Generic;

3 using System.Text;

4 using System.Windows;

5 using System.Windows.Controls;

6 using System.Windows.Data;

7 using System.Windows.Documents;

8 using System.Windows.Input;

9 using System.Windows.Media;

10 using System.Windows.Media.Imaging;

11 using System.Windows.Shapes;

12 using System.IO.Ports; //跟串口相关,不能只是引用system.IO

13 using System.Threading; //跟串口相关,线程的引入

14

15 namespace WpfApplication3

16 {

17 public partial class MainWindow : Window

18 {

19 delegate void HandleInterfaceUpdateDelagate(string text);//委托;此为重点

20 HandleInterfaceUpdateDelagate interfaceUpdateHandle;

21 public MainWindow()

22 {

23 this.InitializeComponent();

24 }

25 ///

26 /// 发送按钮的单击事件

27 ///

28 private void btnSend_Click(object sender, RoutedEventArgs e)

29 {

30 //实例化串口对象(默认:COMM1,9600,e,8,1)

31 SerialPort serialPort1 = new SerialPort();

32 //更改参数

33 serialPort1.PortName = "COM1"; //串口号(参考串口调试助手)

34 serialPort1.BaudRate = 9600; //波特率

35 serialPort1.Parity = Parity.None; //校验位

36 serialPort1.DataBits = 8; //数据位

37 serialPort1.StopBits = StopBits.One; //停止位

38

39 //上述步骤可以用在实例化时调用SerialPort类的重载构造函数

40 //SerialPort serialPort = new SerialPort("COM1", 9600, Parity.None, StopBits.one);

41

42 //打开串口(打开串口后不能修改端口名,波特率等参数,修改参数要在串口关闭后修改)

43 if (!serialPort1.IsOpen)

44 {

45 serialPort1.Open();

46 }

47 else

48 MessageBox.Show("Port is open!");

49

50 //用字节的形式发送数据

51 SendBytesData(serialPort1);

52

53 //开启接收数据线程

54 ReceiveData(serialPort1);

55 }

56 ///

57 /// 开启接收数据线程

58 ///

59 private void ReceiveData(SerialPort serialPort)

60 {

61 //同步阻塞接收数据线程

62 Thread threadReceive = new Thread(new ParameterizedThreadStart(SynReceiveData));

63 threadReceive.Start(serialPort);

64 }

65

66 //发送二进制数据

67 private void SendBytesData(SerialPort serialPort)

68 {

69 byte[] bytesSend = System.Text.Encoding.Default.GetBytes(txtSend.Text);

70 serialPort.Write(bytesSend, 0, bytesSend.Length);

71 }

72

73 //同步阻塞读取

74 private void SynReceiveData(object serialPortobj)

75 {

76

77 SerialPort serialPort = (SerialPort)serialPortobj;

78 System.Threading.Thread.Sleep(0);

79 serialPort.ReadTimeout = 1000;

80 try

81 {

82 //先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致

83 int n = serialPort.BytesToRead;

84 byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据

85 //received_count += n;//增加接收计数

86 serialPort.Read(buf, 0, n);//读取缓冲数据

87 //因为要访问ui资源,所以需要使用invoke方式同步ui

88 interfaceUpdateHandle = new HandleInterfaceUpdateDelagate(UpdateTextBox);//实例化委托对象

89 Dispatcher.Invoke(interfaceUpdateHandle,

new string[]{Encoding.ASCII.GetString(buf)});

90 }

91 catch (Exception e)

92 {

93 MessageBox.Show(e.Message);

94 //处理超时错误

95 }

96 serialPort.Close();

97 }

98

99 private void UpdateTextBox(string text)

100 {

101 txtReceive.Text = text;

102 }

103 }

104 }

(2)下面是MainWindow.xaml的内容

1

2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

4 x:Class="WpfApplication3.MainWindow"

5 x:Name="Window"

6 Title="MainWindow"

7 Width="300" Height="300">

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

51单片机部分:

我选用的是STC89C52RC型号的单片机,带有串口通信的51单片机应该都可以,但可能设置上有些不同。

下面是C语言代码(可参考郭天祥著的51单片机C语言编程)。

1 #include

2 #define uchar unsigned char

3 #define uint unsigned int

4

5 uchar flag = 0;

6 uchar a = 0; //定义两个变量

7

8 void main()

9 {

10

11 TMOD=0x20;//设置定时器1为工作方式2

12 TH1=0xfd; //设置波特率为9600

13 TL1=0xfd; //设置波特率为9600

14 TR1=1; //定时器T1运行控制位,当GATE为0而TR1为1时,允许T1计数

15 REN=1;

16

17 SM0=0; //方式1,是十位异步收发器(8位数据)

18 SM1=1; // 波特率可变

19

20 EA=1; //开总中断

21 ES=1; //串行中断允许标志位,ES=1,允许串行中断

22 while(1)

23 {

24

版权声明:aysz01 发表于 2022-08-06 10:32:09。
转载请注明:WPF与51单片机之间的串口通信 | 鳌游电工

暂无评论

暂无评论...