
1)在wpf程序中,不能使用原来winform下提供的serialport控件了。这是因为wcf中的控件与winform中控件的工作原理完全不一样。
2)在wpf中,虽然不能再用serialport控件,但是,却仍然可以使用在xmal后台cs中使用serialport对象。例如:
using System.IO.Portsnamespace WpfCommand
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
SerialPort port
public MainWindow()
{
InitializeComponent()
//初始化串口
port = new SerialPort("COM1", 9600, Parity.None, 8)
//事件处理
port.DataReceived += port_DataReceived
}
void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//
}
}
}
3)如果要在wpf中使用定时器,计算超时,可以使用using System.Timers.Timer
using System.IO.Portsnamespace WpfCommand
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
SerialPort port
System.Timers.Timer timer
public MainWindow()
{
InitializeComponent()
port = new SerialPort("COM1", 9600, Parity.None, 8)
port.DataReceived += port_DataReceived
//定时器
timer = new System.Timers.Timer()
timer.Elapsed += timer_Elapsed
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//
}
void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//
}
亲,简单来讲winform能做的,wpf都能做,而且界面做的更好,更炫,wpf编辑程序界面可以向编辑html界面一样,wpf可以使用XAML语言编辑wpf程序的界面,winform则不能使用XAML。至于使用串口,wpf和winform的使用是一样的,因为他们的后台语言用的都是C#欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)