Esempio n. 1
0
 private void btn_Find_Click(object sender, EventArgs e)
 {
     try
     {
         ///自定义表达式 查找这个任务
         if (task != null)
         {
             TestContext t = task.GetTask(s => { return(s.TaskName.Equals(tb_taskName.Text)); });
             if (t.TaskName != null)
             {
                 //  MessageBox.Show(ModbusHelper.BytesToHexString(t.RX));
                 short[] a = ModbusHelper.DataUnPackingToShort(modbusType, t.RX);
                 if (a == null)
                 {
                     return;
                 }
                 string s = "";
                 foreach (var item in a)
                 {
                     s += ", " + item;
                 }
                 label6.Text = s;
             }
         }
     }
     catch
     {
     }
 }
Esempio n. 2
0
        private void button3_Click(object sender, EventArgs e)
        {
            byte[] a = new byte[] { 0x0A, 0x03, 0x04, 0x00, 0xA6, 0x00, 0xAC, 0xA0, 0xAD };
            byte[] b = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x0A, 0x03, 0x04, 0x00, 0xA6, 0x00, 0xAC, 0xA0, 0xAD };
            ModbusHelper.DataUnPackingToDouble(ModbusHelper.ModbusType.RTU, a, 3);
            ModbusHelper.DataUnPackingToDouble(ModbusHelper.ModbusType.Tcp, b, 3);


            ModbusHelper.DataUnPackingToShort(ModbusHelper.ModbusType.RTU, a);
            ModbusHelper.DataUnPackingToShort(ModbusHelper.ModbusType.Tcp, b);
        }
Esempio n. 3
0
        private void btn_addtask_Click(object sender, EventArgs e)
        {
            ModbusHelper.OperationCode c = (ModbusHelper.OperationCode)Convert.ToByte(tb_Code.Text);

            byte[] a = ModbusHelper.DataPacking(modbusType, c, ModbusHelper.HexStringToBytes(tb_Station.Text)[0], ModbusHelper.HexStringToBytes(tb_data.Text));

            addr++;
            string s = addr.ToString().PadLeft(4, '0') + "0006";

            tb_data.Text     = s;
            tb_taskName.Text = "t" + addr.ToString();;
            AddTask(tb_taskName.Text, a);
        }
Esempio n. 4
0
        private void button2_Click(object sender, EventArgs e)
        {
            ModbusHelper.OperationCode c = (ModbusHelper.OperationCode)Convert.ToByte(tb_Code.Text);//获取操作码

            //将给定的  操作码、站号 、数据  按照modbusType 打包为标准报文格式
            byte[] a = ModbusHelper.DataPacking(modbusType, c, ModbusHelper.HexStringToBytes(tb_Station.Text)[0], ModbusHelper.HexStringToBytes(tb_data.Text));



            //按钮点动输出 都应该这样设置特别是t.ExecuteOnce = true;  如果不写这一句、会一直发送指令给仪表。
            //执行完你创建的任务应该把他删除、否则他会一直存在用任务列表、影响迭代效率
            #region 创建一个控制输出的任务 Demo
            TestContext t = new TestContext();
            t.TaskName    = tb_taskName.Text;
            t.TX          = a;
            t.ExecuteOnce = true;              //只发送一次  !:如果不为ture 这个任务会被循环执行
            t.Priority    = TaskPriority.High; //即刻发送  !:设置优先级使这个任务立即被执行一次,然后不管会不会成功 这个任务都会被降级为普通任务
            t.IsTempTask  = true;              //临时任务 :在调用查找该任务的同时会把该任务删除  !:在查找方法返回结果值后、这个被查找的任务就被删除掉了
            if (task != null)
            {
                task.AddOrUpdateTaskToQueue(t);
            }
            #endregion
        }
Esempio n. 5
0
        /// <summary>
        /// 如果返回的是错误码、抛出异常 ;其他情况返回空。
        /// </summary>
        /// <param name="t"></param>
        /// <param name="channel"></param>
        /// <returns></returns>
        public static byte[] Receive(Collector.ITaskContext t, Collector.Channel.BaseChannel channel)
        {
            sendByte = t.GetTX();

            buf.Clear();
            #region

            switch (sendByte[1])
            {
            case 0x03:
                recLength = (BitConverter.ToInt16(new byte[] { sendByte[sendByte.Length - 3], sendByte[sendByte.Length - 4] }, 0) * 2) + 5;
                break;

            case 0x06:
                recLength = sendByte.Length;
                break;

            case 0x10:
                recLength = 8;
                break;

            default:
                throw new Exception("发送指令中存在不支持的指令码:" + sendByte[1].ToString());
            }


            #endregion


            sw.Restart();
            while (sw.ElapsedMilliseconds < recLength + TimeOut)
            {
                buf.AddRange(channel.Read(recLength));

                if (buf.Count > 2) //判断返回的是否有错误
                {
                    if (buf[1] != sendByte[1])
                    {
                        buf.Clear();
                        return(buf.ToArray());
                    }
                }
                if (buf.Count >= recLength)//接收完指定长度后,判断crc是否通过
                {
                    sw.Stop();
                    if (!ModbusHelper.CheckDataCrc16(buf.ToArray()) || buf.Count != recLength)
                    {
                    }
                    break;
                }
            }
            sw.Stop();
            if (buf.Count > 0)
            {
                ErrorCount = 0;
            }
            else
            {
                ErrorCount++;
            }
            if (ErrorCount > 3)
            {
                throw new Exception("串口多次未读取到数据、请检查通讯是否有问题");
            }
            return(buf.ToArray());
        }