Esempio n. 1
0
        private void ComComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            var comboBox = (ComboBox)sender;

            // 需要关闭端口后设置
            serialPort = SerialPortHelper.GetSerialPort(comboBox.Text);

            // 数据接收回调
            SerialPortHelper.ParseReData = (byte[] data) =>
            {
                Console.WriteLine("解析数据!");
                controllerToRead.ParseDataToControllerItem(data);

                // 解析完成后,读取下一个控制器数据
                // ReadControllerData(serialPort, ++controllerToReadIndex);
            };

            // 同步时间
            byte[] txData = { 0x00,                                       0x00,                         0x10,                    0x01,                      0x07, (byte)(DateTime.Now.Year - 2000),
                              (byte)DateTime.Now.Month, (byte)DateTime.Now.Day, (byte)DateTime.Now.DayOfWeek, (byte)DateTime.Now.Hour, (byte)DateTime.Now.Minute,
                              (byte)DateTime.Now.Second };

            SerialPortHelper.SendData(serialPort, txData);

            Thread.Sleep(100);

            // 读取第一个控制器信息
            ReadControllerData(serialPort, 0);
        }
Esempio n. 2
0
        /// <summary>
        /// 开关
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PowerButton_Click(object sender, EventArgs e)
        {
            Button powerBtn  = (Button)sender;
            int    groupId   = 0;
            int    addressId = 0;

            HelperTool.GetControllerAddressId(powerBtn.Tag.ToString(), out groupId, out addressId);

            currentControllerItem = GetCurrentController(groupId, addressId);
            if (currentControllerItem == null)
            {
                return;
            }

            byte powerByte = 0x01;

            if (currentControllerItem.PowerState)
            {
                powerByte = 0x00;
                powerBtn.BackgroundImage = Properties.Resources.powerOff;
            }
            else
            {
                powerBtn.BackgroundImage = Properties.Resources.powerOn;
            }

            currentControllerItem.PowerState = !currentControllerItem.PowerState;

            byte[] txData = { (byte)currentControllerItem.GroupId, (byte)currentControllerItem.AddressId, 0x10, 0x10, 0x01, powerByte };

            SerialPortHelper.SendData(serialPort, txData);
        }
Esempio n. 3
0
        private void RefreshButton_Click(object sender, EventArgs e)
        {
            // 同步时间
            byte[] txData = { 0x00,                                       0x00,                         0x10,                    0x01,                      0x07, (byte)(DateTime.Now.Year - 2000),
                              (byte)DateTime.Now.Month, (byte)DateTime.Now.Day, (byte)DateTime.Now.DayOfWeek, (byte)DateTime.Now.Hour, (byte)DateTime.Now.Minute,
                              (byte)DateTime.Now.Second };

            SerialPortHelper.SendData(serialPort, txData);

            // RefreshSettingDisplay();

            ReadSelectedComData(currentControllerItem.GroupId, currentControllerItem.AddressId);
        }
Esempio n. 4
0
        /// <summary>
        /// 发送命令到设备
        /// </summary>
        /// <param name="controllerItem"></param>
        private void SendSettingToDevice(ControllerItem controllerItem)
        {
            controllerItem.SortTimers();

            // 起始地址
            int         startAddress = 0x20;
            List <byte> txData       = new List <byte>()
            {
                (byte)controllerItem.GroupId, (byte)controllerItem.AddressId, 0x10, (byte)startAddress, 0x50
            };

            // 添加关断定时器数据
            for (int i = 0; i < maxTimePointCount; i++)
            {
                if (i >= controllerItem.OffTimerCount)
                {
                    txData.AddRange(new byte[] { 0xff, 0xff });

                    continue;
                }

                byte[] minutesBytes = BitConverter.GetBytes(controllerItem.OffTimersMinutes[i]);

                txData.AddRange(new byte[] { minutesBytes[0], minutesBytes[1] });
            }

            // 添加打开定时器
            for (int i = 0; i < maxTimePointCount; i++)
            {
                if (i >= controllerItem.OnTimerCount)
                {
                    txData.AddRange(new byte[] { 0xff, 0xff, 0, 0, 0, 0, 0, 0 });

                    continue;
                }

                byte[] minutesBytes = BitConverter.GetBytes(controllerItem.OnTimersMinutes[i]);

                txData.AddRange(new byte[] { minutesBytes[0], minutesBytes[1] });

                foreach (var item in controllerItem.OnTimersColorValues[i])
                {
                    txData.Add((byte)item);
                }
            }

            SerialPortHelper.SendData(serialPort, txData.ToArray());
        }
Esempio n. 5
0
 public ActionResult WenDu(int type)
 {
     if (type == 1)
     {
         //增加温度
         SerialPortHelper.SendData(true, new byte[] { 0xFC, 0xB2, 0x11 });
         return(Content("温度提升中..."));
     }
     else if (type == 0)
     {
         //降低温度
         SerialPortHelper.SendData(true, new byte[] { 0xFC, 0xB2, 0x12 });
         return(Content("停止增温中..."));
     }
     return(Content("请稍候..."));
 }
Esempio n. 6
0
 public ActionResult YangQi(int type)
 {
     if (type == 1)
     {
         //开始增氧
         SerialPortHelper.SendData(true, new byte[] { 0xFC, 0xB1, 0x21 });
         return(Content("增加氧气中..."));
     }
     else if (type == 0)
     {
         //停止增氧
         SerialPortHelper.SendData(true, new byte[] { 0xFC, 0xB1, 0x22 });
         return(Content("停止增氧中..."));
     }
     return(Content("请稍候..."));
 }
Esempio n. 7
0
        public ActionResult Jinshui(int type)
        {
            if (type == 1)
            {
                //开始进水

                SerialPortHelper.SendData(true, new byte[] { 0xFC, 0xB2, 0x21 });
                return(Content("正在进水..."));
            }
            else if (type == 0)
            {
                //停止进水
                SerialPortHelper.SendData(true, new byte[] { 0xFC, 0xB2, 0x22 });
                return(Content("进水停止中..."));
            }
            return(Content("请稍候..."));
        }
Esempio n. 8
0
        public ActionResult PaiShui(int type)
        {
            if (type == 1)
            {
                //开始排水

                SerialPortHelper.SendData(true, new byte[] { 0xFC, 0xB1, 0x11 });
                return(Content("正在排水..."));
            }
            else if (type == 0)
            {
                //停止排水
                SerialPortHelper.SendData(true, new byte[] { 0xFC, 0xB1, 0x12 });
                return(Content("排水停止中..."));
            }
            return(Content("请稍候..."));
        }
Esempio n. 9
0
        private void PowerButton_Click(object sender, EventArgs e)
        {
            byte powerByte = 0x01;

            if (currentControllerItem.PowerState)
            {
                powerByte = 0x0;
                powerButton.BackgroundImage = Properties.Resources.powerOff;
            }
            else
            {
                powerButton.BackgroundImage = Properties.Resources.powerOn;
            }

            currentControllerItem.PowerState = !currentControllerItem.PowerState;
            byte[] txData = { (byte)currentControllerItem.GroupId, (byte)currentControllerItem.AddressId, 0x10, 0x10, 0x01, powerByte };

            SerialPortHelper.SendData(serialPort, txData);
        }
Esempio n. 10
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSend_Click(object sender, EventArgs e)
        {
            var data = txtSender.Text.Trim();

            if (data.Length == 0)
            {
                MessageBox.Show("请先填写发送内容");
                txtSender.Focus();
                return;
            }

            //发送
            try
            {
                SerialPortHelper.SendData(data, ckb16Send.Checked ? SendFormat.Hex : SendFormat.String);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发送失败,{ex.Message}");
            }
        }
Esempio n. 11
0
        private void TrackBar1_MouseUp(object sender, MouseEventArgs e)
        {
            // 如果设置定时器,直接返回
            if (enableCheckBox.Checked)
            {
                return;
            }

            // 直接发送命令
            TrackBar trackBar = (TrackBar)sender;

            // 滑动条索引
            int trackBarIndex = (int.Parse(trackBar.Tag.ToString()) - 10000);
            // 寄存器地址
            int registerAddr = 0x10 + trackBarIndex * 2 - 1;

            Int16 colorValue = (Int16)trackBar.Value;

            byte[] colorValueBytes = BitConverter.GetBytes(colorValue);
            // 发送数据
            byte[] txData = { (byte)currentControllerItem.GroupId, (byte)currentControllerItem.AddressId, 0x10, (byte)registerAddr, 0x02, colorValueBytes[0], colorValueBytes[1] };

            SerialPortHelper.SendData(serialPort, txData);
        }
Esempio n. 12
0
 public void SendData(string str)
 {
     dataServer.SendData(str);
 }