WPF與51單片機(jī)之間的串口通信
WPF部分:
(1)建立WPF工程,步驟略
下面是MainWindow.xaml.cs的內(nèi)容
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; //跟串口相關(guān),不能只是引用system.IO
13 using System.Threading; //跟串口相關(guān),線程的引入
14
15 namespace WpfApplication3
16 {
17 public partial class MainWindow : Window
18 {
19 delegate void HandleInterfaceUpdateDelagate(string text);//委托;此為重點(diǎn)
20 HandleInterfaceUpdateDelagate interfaceUpdateHandle;
21 public MainWindow()
22 {
23 this.InitializeComponent();
24 }
25 ///
26 /// 發(fā)送按鈕的單擊事件
27 ///
28 private void btnSend_Click(object sender, RoutedEventArgs e)
29 {
30 //實(shí)例化串口對(duì)象(默認(rèn):COMM1,9600,e,8,1)
31 SerialPort serialPort1 = new SerialPort();
32 //更改參數(shù)
33 serialPort1.PortName = "COM1"; //串口號(hào)(參考串口調(diào)試助手)
34 serialPort1.BaudRate = 9600; //波特率
35 serialPort1.Parity = Parity.None; //校驗(yàn)位
36 serialPort1.DataBits = 8; //數(shù)據(jù)位
37 serialPort1.StopBits = StopBits.One; //停止位
38
39 //上述步驟可以用在實(shí)例化時(shí)調(diào)用SerialPort類的重載構(gòu)造函數(shù)
40 //SerialPort serialPort = new SerialPort("COM1", 9600, Parity.None, StopBits.one);
41
42 //打開串口(打開串口后不能修改端口名,波特率等參數(shù),修改參數(shù)要在串口關(guān)閉后修改)
43 if (!serialPort1.IsOpen)
44 {
45 serialPort1.Open();
46 }
47 else
48 MessageBox.Show("Port is open!");
49
50 //用字節(jié)的形式發(fā)送數(shù)據(jù)
51 SendBytesData(serialPort1);
52
53 //開啟接收數(shù)據(jù)線程
54 ReceiveData(serialPort1);
55 }
56 ///
57 /// 開啟接收數(shù)據(jù)線程
58 ///
59 private void ReceiveData(SerialPort serialPort)
60 {
61 //同步阻塞接收數(shù)據(jù)線程
62 Thread threadReceive = new Thread(new ParameterizedThreadStart(SynReceiveData));
63 threadReceive.Start(serialPort);
64 }
65
66 //發(fā)送二進(jìn)制數(shù)據(jù)
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 //先記錄下來,避免某種原因,人為的原因,操作幾次之間時(shí)間長(zhǎng),緩存不一致
83 int n = serialPort.BytesToRead;
84 byte[] buf = new byte[n];//聲明一個(gè)臨時(shí)數(shù)組存儲(chǔ)當(dāng)前來的串口數(shù)據(jù)
85 //received_count += n;//增加接收計(jì)數(shù)
86 serialPort.Read(buf, 0, n);//讀取緩沖數(shù)據(jù)
87 //因?yàn)橐L問ui資源,所以需要使用invoke方式同步ui
88 interfaceUpdateHandle = new HandleInterfaceUpdateDelagate(UpdateTextBox);//實(shí)例化委托對(duì)象
89 Dispatcher.Invoke(interfaceUpdateHandle,
new string[]{Encoding.ASCII.GetString(buf)});
90 }
91 catch (Exception e)
92 {
93 MessageBox.Show(e.Message);
94 //處理超時(shí)錯(cuò)誤
95 }
96 serialPort.Close();
97 }
98
99 private void UpdateTextBox(string text)
100 {
101 txtReceive.Text = text;
102 }
103 }
104 }
(2)下面是MainWindow.xaml的內(nèi)容
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單片機(jī)部分:
我選用的是STC89C52RC型號(hào)的單片機(jī),帶有串口通信的51單片機(jī)應(yīng)該都可以,但可能設(shè)置上有些不同。
下面是C語言代碼(可參考郭天祥著的51單片機(jī)C語言編程)。
1 #include
2 #define uchar unsigned char
3 #define uint unsigned int
4
5 uchar flag = 0;
6 uchar a = 0; //定義兩個(gè)變量
7
8 void main()
9 {
10
11 TMOD=0x20;//設(shè)置定時(shí)器1為工作方式2
12 TH1=0xfd; //設(shè)置波特率為9600
13 TL1=0xfd; //設(shè)置波特率為9600
14 TR1=1; //定時(shí)器T1運(yùn)行控制位,當(dāng)GATE為0而TR1為1時(shí),允許T1計(jì)數(shù)
15 REN=1;
16
17 SM0=0; //方式1,是十位異步收發(fā)器(8位數(shù)據(jù))
18 SM1=1; // 波特率可變
19
20 EA=1; //開總中斷
21 ES=1; //串行中斷允許標(biāo)志位,ES=1,允許串行中斷
22 while(1)
23 {
24