C#控制LED灯总算发送是失败

图片说明
主程序
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 "发送失败";
        }
    }
}

}

net

2个回答

问问厂家

ICS.Acquisition
这个怎么实现的,是用什么方式连接的电脑,端口有没有配置好,驱动有没有装好。这种问题在这里问是没意义的。别人没有你的设备。

你花了钱买了设备,厂家就应该做好售后服务。

你的com2口是否存在,波特率、校验方式等等怎么设置的。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问