通用工业视觉软件设计之通讯模块-添加TCP服务端-2
一 添加服务器:ShowFrmEComInfo();
/// <summary>
/// 添加服务器
/// </summary>
/// <param name="obj"></param>
private void AddTcpServer(object obj)
{
try
{
//创建tcp服务端
m_ComunCation = EComManager.CreateECom(CommunicationModel.TcpServer);
ECommunication ECom = EComManager.GetECommunication(m_ComunCation);
ECom.ReceiveString += ECom_ReceiveString;
//设置通讯参数
//不需要设置监听的ip 默认是0.0.0.0 就可以监听所有ip段
//设置端口
ECom.LocalPort = 8000;
// 显示UI
ShowFrmEComInfo();
}
catch (Exception ex)
{
Log.Error(ex.ToString());
}
}
二
/// <summary>
/// 显示通讯列表数据
/// </summary>
private void ShowFrmEComInfo()
{
CommunicationLst.Clear();
foreach (var item in EComManager.s_ECommunicationDic)
{
CommunicationLst.Add(new ComunInfo { Name = item.Value.Key, isSelect = item.Value.IsConnected });
}
//更新通讯界面
// ModuleSystemHelper.DataEventChange.DeviceFrmStatus = true;
}
设备信息
/// <summary>
/// 设备信息
/// </summary>
public class DeviceInfo
{
//驱动名称
public string DeviceName { get; set; }
//驱动类型
public string DeviceType { get; set; }
//显示图标
public object IconImage { get; set; }
//背景色
public string ImageColor { get; set; }
//是否链接
public bool IsConnected { get; set; }
}
三被选中,显示配置界面
3.1 创建CommunicationPart文件夹
3.2 创建用户控件-CommunTcpServer
3.3 创建用户控件-UcAaSTxt
带有增减按钮的数值控制器
<UserControl x:Class="ModuleView.UcAaSTxt"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ModuleView"
mc:Ignorable="d">
<UserControl.Resources>
<ResourceDictionary Source="pack://application:,,,/ModuleView;component/SummaryStyle.xaml"/>
</UserControl.Resources>
<Border Background="White">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<!--控件显示名称-->
<TextBlock
Grid.Row="0"
Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=numHeadName,Mode=TwoWay}"
Foreground="Black" VerticalAlignment="Center"
HorizontalAlignment="Left" Margin="5"
FontSize="13"/>
<!--显示内容-->
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBox
ContextMenu="{x:Null}"
Grid.Row="1"
BorderThickness="0"
TextChanged="TextBox_TextChanged"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl},
Path=ControlValue,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
<Border BorderBrush="Black" BorderThickness="0,0,0,1" VerticalAlignment="Center" Grid.Row="2"/>
</Grid>
<!--按钮-->
<Grid Grid.Column="2">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button
Style="{StaticResource DataLinkBtnStyle}"
Content=""
Width="26"
Height="26"
Foreground="Black"
Grid.Column="0"
FontSize="16"
Name="btn_Add"
MouseLeftButtonDown="btn_Add_MouseLeftButtonDown"
MouseLeftButtonUp="btn_Add_MouseLeftButtonUp"/>
<Button
Style="{StaticResource DataLinkBtnStyle}"
Content=""
Width="26"
Height="26"
Foreground="Black"
Grid.Column="1"
FontSize="16"
Margin="0 0 5 0"
Name="btn_Reduce"
MouseLeftButtonDown="btn_Reduce_MouseLeftButtonDown"
MouseLeftButtonUp="btn_Reduce_MouseLeftButtonUp"/>
</Grid>
</Grid>
</Border>
</UserControl>
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;
namespace ModuleView
{
/// <summary>
/// UcAaSTxt.xaml 的交互逻辑
/// </summary>
public partial class UcAaSTxt : UserControl
{
public UcAaSTxt()
{
InitializeComponent();
// 为四个定时器注册 Tick 事件处理程序
Timer1.Tick += Timer1_Tick;
Timer2.Tick += Timer2_Tick;
Timer3.Tick += Timer3_Tick;
Timer4.Tick += Timer4_Tick;
btn_Add.AddHandler(Button.MouseLeftButtonDownEvent, new MouseButtonEventHandler(this.btn_Add_MouseLeftButtonDown), true);
btn_Add.AddHandler(Button.MouseLeftButtonUpEvent, new MouseButtonEventHandler(this.btn_Add_MouseLeftButtonUp), true);
btn_Reduce.AddHandler(Button.MouseLeftButtonDownEvent, new MouseButtonEventHandler(this.btn_Reduce_MouseLeftButtonDown), true);
btn_Reduce.AddHandler(Button.MouseLeftButtonUpEvent, new MouseButtonEventHandler(this.btn_Reduce_MouseLeftButtonUp), true);
}
System.Windows.Forms.Timer Timer1 = new System.Windows.Forms.Timer() { Interval = 1000, Enabled = false };
System.Windows.Forms.Timer Timer2 = new System.Windows.Forms.Timer() { Interval = 1000, Enabled = false };
System.Windows.Forms.Timer Timer3 = new System.Windows.Forms.Timer() { Interval = 1000, Enabled = false };
System.Windows.Forms.Timer Timer4 = new System.Windows.Forms.Timer() { Interval = 1000, Enabled = false };
#region 列表名称
//列表名称
public string numHeadName
{
get { return (string)this.GetValue(numNameProperty); }
set { this.SetValue(numNameProperty, value); }
}
public static readonly DependencyProperty numNameProperty =
DependencyProperty.Register("numHeadName", typeof(string), typeof(UcAaSTxt), new PropertyMetadata(default(string)));
#endregion
#region 最小值
public int minValue
{
get { return (int)this.GetValue(minValueProperty); }
set { this.SetValue(minValueProperty, value); }
}
public static readonly DependencyProperty minValueProperty =
DependencyProperty.Register("minValue", typeof(int), typeof(UcAaSTxt), new PropertyMetadata(default(int)));
#endregion
#region 最大值
public int maxValue
{
get { return (int)this.GetValue(maxValueProperty); }
set { this.SetValue(maxValueProperty, value); }
}
public static readonly DependencyProperty maxValueProperty =
DependencyProperty.Register("maxValue", typeof(int), typeof(UcAaSTxt), new PropertyMetadata(default(int)));
#endregion
#region 区间值
public int midValue
{
get { return (int)this.GetValue(midValueProperty); }
set { this.SetValue(midValueProperty, value); }
}
public static readonly DependencyProperty midValueProperty =
DependencyProperty.Register("midValue", typeof(int), typeof(UcAaSTxt), new PropertyMetadata(default(int)));
#endregion
#region 控件值
/// <summary>
/// 控件值
/// </summary>
public int ControlValue
{
get { return (int)GetValue(ControlValueContent); }
set { SetValue(ControlValueContent, value); }
}
public static readonly DependencyProperty ControlValueContent =
DependencyProperty.Register("ControlValue", typeof(int), typeof(UcAaSTxt), new PropertyMetadata(default(int)));
#endregion
#region 路由事件
public static readonly RoutedEvent ValueEvent = EventManager.RegisterRoutedEvent("TextValueEvent",
RoutingStrategy.Bubble, typeof(EventHandler<RoutedEventArgs>), typeof(UcAaSTxt));
public event RoutedEventHandler TextValueEvent
{
add { AddHandler(ValueEvent, value); }
remove { RemoveHandler(ValueEvent, value); }
}
#endregion
private void btn_Add_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (ControlValue >= maxValue)
{
ControlValue = maxValue;
}
else
{
ControlValue = ControlValue + midValue;
}
Timer1.Interval = 2000;
Timer1.Enabled = true;
}
private void btn_Add_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Timer1.Enabled = false;
Timer2.Enabled = false;
}
private void btn_Reduce_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (ControlValue <= minValue)
{
ControlValue = minValue;
}
else
{
ControlValue = ControlValue - midValue;
}
Timer3.Interval = 2000;
Timer3.Enabled = true;
}
private void btn_Reduce_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Timer3.Enabled = false;
Timer4.Enabled = false;
}
private void Timer1_Tick(object sender, EventArgs e)
{
Timer2.Interval = 50;
Timer2.Enabled = true;
Timer1.Enabled = false;
}
private void Timer2_Tick(object sender, EventArgs e)
{
if (ControlValue >= maxValue)
{
ControlValue = maxValue;
}
else
{
ControlValue = ControlValue + midValue;
}
}
private void Timer3_Tick(object sender, EventArgs e)
{
Timer4.Interval = 50;
Timer4.Enabled = true;
Timer3.Enabled = false;
}
private void Timer4_Tick(object sender, EventArgs e)
{
if (ControlValue <= minValue)
{
ControlValue = minValue;
}
else
{
ControlValue = ControlValue - midValue;
}
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
RoutedEventArgs args = new RoutedEventArgs(ValueEvent, ControlValue);
this.RaiseEvent(args);
}
}
}
四个定时器(Timer1
、Timer2
、Timer3
、Timer4
)主要用于实现按钮长按快速增减数值的功能。以
(1) Timer1
和 Timer2
(用于“增加”按钮 btn_Add
)
-
Timer1
:- 在按钮按下时(
MouseLeftButtonDown
)启动,初始间隔为 2000ms(2秒)。 - 作用是延迟触发快速增加模式,避免误触时立即加速。
- 当
Timer1
触发Tick
事件后,会关闭自身(Timer1.Enabled = false
),并启动Timer2
。
- 在按钮按下时(
-
Timer2
:- 间隔设置为 50ms,启动后开始高频执行
Tick
事件。 - 每次触发时,将
ControlValue
增加midValue
(直到达到maxValue
)。 - 实现按钮长按时的连续快速增加效果。
- 间隔设置为 50ms,启动后开始高频执行
(2) Timer3
和 Timer4
(用于“减少”按钮 btn_Reduce
)
-
Timer3
:- 逻辑与
Timer1
对称,在按钮按下时启动,初始间隔为 2000ms。 - 触发后关闭自身,启动
Timer4
。
- 逻辑与
-
Timer4
:- 间隔为 50ms,高频触发
Tick
事件。 - 每次触发时,将
ControlValue
减少midValue
(直到达到minValue
)。 - 实现按钮长按时的连续快速减少效果。
- 间隔为 50ms,高频触发
2. 关键交互流程
-
按下按钮(如
btn_Add
):- 立即执行一次数值增减(
ControlValue += midValue
)。 - 启动
Timer1
(或Timer3
),延迟 2 秒后切换到高频定时器(Timer2
/Timer4
)。
- 立即执行一次数值增减(
-
长按按钮:
- 2 秒后,
Timer1
触发,切换到Timer2
,开始以 50ms 间隔快速增加数值。 - 松开按钮时(
MouseLeftButtonUp
),关闭所有定时器,停止自动增减。
- 2 秒后,
-
松开按钮:
- 立即停止所有定时器(
Timer1
/Timer3
和Timer2
/Timer4
),防止继续修改值。
- 立即停止所有定时器(