主程序
using LedDemo.Common;
using System;
using System.Collections.Generic;
using System.IO.Ports;
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 LedDemo
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void window_loaded(object sender, RoutedEventArgs e)
{
//定义数组aryPort,遍历数组
//GetPortNames方法,返回当前计算机可以用的端口 ,以数组的方式返回
string[] aryPort = SerialPort.GetPortNames();
foreach(string item in aryPort)
{
//将数组的项添加到下拉框的列表中
xl.Items.Add(item);
}
//设置默认的选择项为第一个找的项(端口)
xl.SelectedIndex = 0;
}
/// <summary>
/// Send and Save
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void b_Click(object sender, RoutedEventArgs e)
{
int speed = -1; //定义速度的值为-1;
//定义一个布尔型,
bool isSuccess = int.TryParse(sd.Text,out speed);
//判断速度值得大小,是否在规定的范围内
if (isSuccess == false ||(speed <0 || speed>7))
{
//弹出的对话框,提示信息;
MessageBox.Show("请输入正确的值!(0--7)");
return;
}
LedCtrl led = new LedCtrl(xl.Text, speed);
string msg = led.SendSaveMsg(sr.Text);
MessageBox.Show(msg);
}
/// <summary>
/// Send No Save
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void nb_Click(object sender, RoutedEventArgs e)
{
int speed = -1;
bool isSuccess = int.TryParse(sd.Text, out speed);
if (isSuccess == false || (speed <0 || speed >7))
{
MessageBox.Show("请输入正确的值! (0--7) ");
return;
}
LedCtrl led = new LedCtrl(xl.Text,speed);
string msg = led.SendMsg(sr.Text);
MessageBox.Show(msg);
}
}
}
类包
using ICS.Acquisition;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LedDemo.Common
{
public class LedCtrl
{
ICS.Acquisition.LEDAnalysis led = null;
ICS.Acquisition.LedMessage ledMode = new ICS.Acquisition.LedMessage();
public LedCtrl(string serialNumber, int speed)
{
ICS.Models.Com.ComSettingModel com = new ICS.Models.Com.ComSettingModel();
com.LedCom = serialNumber;
led = ClassFactory.GetProvider(equipmentCategory.LEDAnalysis, com) as LEDAnalysis;
ledMode.HardwareAddress = "01";
ledMode.PlayerModel = "01";
ledMode.StopTime = "00";
ledMode.ColorType = "00";
//数据有效时间 范围0--99
ledMode.EffectiveTime = "00";
ledMode.Speed = speed.ToString();
}
/// <summary>
/// 发送并保存
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public string SendSaveMsg(string msg)
{
ledMode.Context = msg;
if (led.SetSaveLedMessage(ledMode))
{
return "发送成功";
}
else
{
return "发送失败";
}
}
public string SendMsg(string msg)
{
ledMode.Context = msg;
if (led.SetLedMessage(ledMode))
{
return "发送成功";
}
else
{
return "发送失败";
}
}
}
}