WPF定时器串口发送WPF定时器串口发送
using System;
using System.Collections.Generic; using System.Linq;
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.M...
WPF定时器串口发送
using System;
using System.Collections.Generic; using System.Linq;
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;
using System.Timers; //定时器命名 using System.IO.Ports;
namespace WpfApplication1
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
public int i;
SerialPort port = new SerialPort();//定义全局
public MainWindow()
{
InitializeComponent(); Timer t = new Timer(); //定义
t.Interval = 1000; //间隔1S t.Enabled = true; //开始 t.Elapsed+=new ElapsedEventHandler(t_Elapsed); //时间到 处理事件
port.Open(); //打开串口
this.i = 0;
}
void t_Elapsed(object source, ElapsedEventArgs e)
{
byte[] SeriaData = new byte[1];
this.label1.Dispatcher.Invoke(new Action(() => { this.label1.Content =
i.ToString(); }));//委托 标签控件显示数据
this.i++;
SeriaData[0] = (byte)i;
port.Write(SeriaData, 0, 1);//串口发送
}
}
}
本文档为【WPF定时器串口发送】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。