Example #1
0
            private bool tryGetPackage(byte[] buff, int len, int offset, LinkPackage package)
            {
                bool flag = false;
                int  t;

                while (offset < len && (!flag))
                {
                    PackageParseResult res = package.ReadFromBuffer(buff, len, offset, out t);
                    switch (res)
                    {
                    case PackageParseResult.NoSTX:
                        offset++;
                        break;

                    case PackageParseResult.NoEnoughData:
                        flag = true;
                        break;

                    case PackageParseResult.BadCheckSum:
                        Debug.WriteLine("[port scanner]bad checksum");
                        offset++;
                        break;

                    case PackageParseResult.Yes:
                        return(true);
                    }
                }
                return(false);
            }
 public override bool SendPackage(LinkPackage package, bool wait)
 {
     if (isSending)
     {
         if (wait)
         {
             while (isSending)
             {
                 ;
             }
         }
         else
         {
             return(false);
         }
     }
     isSending = true;
     try
     {
         port.Write(package.Buffer, 0, package.PackageSize);
         dataSent += package.PackageSize;
         OnSendPackageEvent(this, new LinkEventArgs(package));
     }
     catch
     {
     }
     isSending = false;
     return(true);
 }
Example #3
0
        public virtual LinkPackage Clone()
        {
            LinkPackage p = new LinkPackage(buffer.Length);

            buffer.CopyTo(p.buffer, 0);
            p.dataSize     = dataSize;
            p.reverseBytes = this.reverseBytes;
            p.timeStamp    = timeStamp;
            return(p);
        }
Example #4
0
 /// <summary>
 /// 直接发送一个数据包
 /// </summary>
 /// <param name="package">要发送的数据包</param>
 /// <param name="wait">是否允许等待发送</param>
 /// <returns></returns>
 public virtual bool SendPackage(LinkPackage package, bool wait)
 {
     if (isSending)
     {
         if (wait)
         {
             while (isSending)
             {
                 ;
             }
         }
         else
         {
             return(false);
         }
     }
     return(true);
 }
Example #5
0
        public CommLink(LinkProtocol p)
        {
            protocol             = p;
            dataReceived         = 0;
            dataSent             = 0;
            TxRate               = 0;
            RxRate               = 0;
            state                = LinkState.Disconnected;
            isSending            = false;
            receivedPackageQueue = new ConcurrentQueue <LinkPackage>();
            sendPackageQueue     = new Queue <LinkPackage>();
            switch (p)
            {
            case LinkProtocol.SBLink:
                receivePackage = new SBLinkPackage();
                break;

            default:
                receivePackage = new LinkPackage(2048);
                break;
            }
        }
Example #6
0
 public LinkEventArgs(LinkPackage p)
 {
     Package = new List <LinkPackage>();
     Package.Add(p);
 }
Example #7
0
 /// <summary>
 /// 直接发送一个数据包
 /// </summary>
 /// <param name="package">要发送的数据包</param>
 /// <returns></returns>
 public virtual bool SendPackage(LinkPackage package)
 {
     return(SendPackage(package, true));
 }