コード例 #1
0
        public int Send(Collector.ITaskContext t, Collector.Channel.BaseChannel channel)
        {
            int i = channel.Write(t.GetTX());


            return(i);
        }
コード例 #2
0
 public int Send(Collector.ITaskContext t, Collector.Channel.BaseChannel channel)
 {
     random.NextBytes(affair);
     byte[] a = t.GetTX();
     a[0] = affair[0];
     a[1] = affair[1];
     return(channel.Write(a));
 }
コード例 #3
0
 /// <summary>
 /// 如果返回的是错误码、抛出异常 ;其他情况返回空。
 /// </summary>
 /// <param name="t"></param>
 /// <param name="channel"></param>
 /// <returns></returns>
 public byte[] Receive(Collector.ITaskContext t, Collector.Channel.BaseChannel channel)
 {
     sw.Restart();
     byte[] a = new byte[] { };
     while (sw.ElapsedMilliseconds < TimeOut)
     {
         a = channel.Read(256);
         if (a.Length > 2)
         {
             if (a[0] == affair[0] && a[1] == affair[1])
             {
                 break;
             }
         }
     }
     sw.Stop();
     return(a);
 }
コード例 #4
0
        /// <summary>
        /// 如果返回的是错误码、抛出异常 ;其他情况返回空。
        /// </summary>
        /// <param name="t"></param>
        /// <param name="channel"></param>
        /// <returns></returns>
        public 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();
            int timeSpan = recLength + TimeOut;
            while (sw.ElapsedMilliseconds < timeSpan)
            {
                buf.AddRange(channel.Read(64));

                if (buf.Count > 2) //判断返回的是否有错误
                {
                    if (buf[1] != sendByte[1])
                    {
                        buf.Clear();
                        break;
                    }
                }
                if (buf.Count >= recLength)//接收完指定长度后,判断crc是否通过
                {
                    sw.Stop();
                    if (buf.Count != recLength)
                    {
                        buf.Clear();//////////////////////
                    }
                    else if (!ModbusCvt.CheckDataCrc16(buf.ToArray()) || buf.Count != recLength)
                    {
                        buf.Clear();//////////////////////
                    }
                    break;
                }
            }
            sw.Stop();
            if (buf.Count > 0)
            {
                ErrorCount = 0;
            }
            else
            {
                ErrorCount++;
            }
            if (ErrorCount > 3)
            {
                throw new Exception("串口多次未读取到数据、请检查通讯是否有问题");
            }
            return(buf.ToArray());
        }
コード例 #5
0
 public static int Send(Collector.ITaskContext t, Collector.Channel.BaseChannel channel)
 {
     return(channel.Write(t.GetTX()));
 }