Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 描述为WPF的COM端口_C#_Wpf - Fatal编程技术网

C# 描述为WPF的COM端口

C# 描述为WPF的COM端口,c#,wpf,C#,Wpf,首先,我对这一点非常陌生,已经在几天几小时内搜索了多个帖子。我试图在WPF中制作一个程序,在GUI上有一个输入文本框,当我点击一个按钮时,它会向一个arduino发送一个6位数的字母和数字组合,这个arduino是根据描述内容找到的。此时,我可以成功地输入要发送的字符串,并将COM端口手动输入到具有连接和断开按钮的文本框中。显然,这不是最人性化的。我试图实现我在许多搜索中找到的代码,但没有用,老实说,我不太确定我在做什么。再次感谢您的帮助。这是我目前用c#和XAML编写的代码 C# 使用系统

首先,我对这一点非常陌生,已经在几天几小时内搜索了多个帖子。我试图在WPF中制作一个程序,在GUI上有一个输入文本框,当我点击一个按钮时,它会向一个arduino发送一个6位数的字母和数字组合,这个arduino是根据描述内容找到的。此时,我可以成功地输入要发送的字符串,并将COM端口手动输入到具有连接和断开按钮的文本框中。显然,这不是最人性化的。我试图实现我在许多搜索中找到的代码,但没有用,老实说,我不太确定我在做什么。再次感谢您的帮助。这是我目前用c#和XAML编写的代码


C#

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用System.IO.Ports;
使用制度管理;
使用系统诊断;
名称空间控制器
{
///     
///MainWindow.xaml的交互逻辑
///     
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
只读SerialPort sp=新的SerialPort();
私有void文本框\u LostFocus(对象发送方,RoutedEventArgs e)
{
if(((文本框)发送方).Text.Length<6)
{
MessageBox.Show(“请输入6个字符的ED编号”);
}
}
私有无效对\u单击(对象发送者,路由目标e)
{
尝试
{
sp.Write(ednumber.Text);
}
捕获(例外)
{
显示(“请连接到COM端口”);
}
}
专用void Connect_单击(对象发送方,路由目标)
{
尝试
{
字符串portName=comportno.Text;
sp.PortName=PortName;
sp.BaudRate=9600;
sp.Open();
status.Text=“已连接”;
}
捕获(例外)
{
Show(“请提供有效的端口号或检查您的连接”);
}
}
私有无效断开连接\u单击(对象发送方,路由目标e)
{
尝试
{
sp.Close();
status.Text=“断开”;
}
捕获(例外)
{
MessageBox.Show(“先连接,然后断开”);
}
}
}
}
<Window    
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
        xmlns:av="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="av" x:Class="controller_Arduino.MainWindow"    
        Title="RP Telemtry HUD" Height="450" Width="1020">
    <Grid>
        <Button x:Name="pair" IsEnabled="{Binding Text.Length, ElementName=ednumber}" Content="Pair ED" HorizontalAlignment="Center" Margin="0,252,0,0" VerticalAlignment="Top" Width="88" Height="52" Click="pair_Click"/>
        <TextBlock x:Name="status" HorizontalAlignment="Left" Margin="742,168,0,0" TextWrapping="Wrap" Text="Disconnected" VerticalAlignment="Top" Height="29" Width="94"/>
        <TextBox x:Name="comportno" HorizontalAlignment="Left" Height="23" Margin="742,79,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="158"/>
        <Button x:Name="Connect" Content="Connect" HorizontalAlignment="Left" Margin="742,114,0,0" VerticalAlignment="Top" Width="70" Height="24" Click="Connect_Click"/>
        <Button x:Name="Disconnect" Content="Disconnect" HorizontalAlignment="Left" Margin="830,114,0,0" VerticalAlignment="Top" Width="70" Height="24" Click="Disconnect_Click"/>
        <TextBox x:Name="ednumber" HorizontalAlignment="Center" Height="52" Margin="0,145,0,0" Text="" TextWrapping="Wrap" VerticalAlignment="Top" Width="196" MaxLength="6" LostFocus="TextBox_LostFocus"/>
    </Grid>
</Window>   
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.IO.Ports;
using System.Management;
using System.Diagnostics;


namespace controller_Arduino
{

    /// <summary>    
    /// Interaction logic for MainWindow.xaml    
    /// </summary>    
    public partial class MainWindow : Window
    {




        public MainWindow()
        {
            InitializeComponent();
        }

        readonly SerialPort sp = new SerialPort();




        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (((TextBox)sender).Text.Length < 6)
            {
                MessageBox.Show("Please enter 6 character ED number");
            }
        }
        private void pair_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                sp.Write(ednumber.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("Plese Connect To COM Port");
            }
        }
        private void Connect_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                String portName = comportno.Text;
                sp.PortName = portName;
                sp.BaudRate = 9600;
                sp.Open();
                status.Text = "Connected";
            }
            catch (Exception)
            {

                MessageBox.Show("Please give a valid port number or check your connection");
            }
        }

        private void Disconnect_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                sp.Close();
                status.Text = "Disconnected";
            }
            catch (Exception)
            {

                MessageBox.Show("First Connect and then disconnect");
            }
        }




    }

}