Example #1
0
            private void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                byte[] buff = new byte[buffersize];
                int    len  = port.BytesToRead;
                int    offset;

                if (len > buffersize)
                {
                    len = buffersize;
                }
                port.Read(buff, 0, len);
                port.Close();

                offset = 0;
                SBLinkPackage sbp = new SBLinkPackage();

                if (tryGetPackage(buff, len, offset, sbp))
                {
                    offset = sbp.PackageSize;
                    if (tryGetPackage(buff, len, offset, sbp))
                    {
                        state    = PortScannerState.Available;
                        protocol = LinkProtocol.SBLink;
                        return;
                    }
                }

                Debug.WriteLine("[port scanner]unavilable port(no link):" + name);
                state = PortScannerState.Unavailable;
            }
Example #2
0
 public PortData(string pn, int br, int md)
 {
     name          = pn;
     port          = new SerialPort(pn);
     port.BaudRate = br;
     port.DataBits = 8;
     port.StopBits = System.IO.Ports.StopBits.One;
     port.ReceivedBytesThreshold = md;
     port.DataReceived          += Port_DataReceived;
     buffersize = md + 64;
     protocol   = LinkProtocol.NoLink;
     state      = PortScannerState.NewPort;
 }
Example #3
0
//CONSTRUCTOR
    public Link(XmlNode configuration)
        : base(configuration)
    {
        //create two link sides
        XmlAttribute from = XmlParser.GetAttribute(configuration, fromTag);
        XmlAttribute to   = XmlParser.GetAttribute(configuration, toTag);

        linkSides[0]            = new LinkSide(from.Value, this);
        linkSides[1]            = new LinkSide(to.Value, this);
        linkSides[0].SecondSide = linkSides[1];
        linkSides[1].SecondSide = linkSides[0];

        LinkProtocol[] protocols = new LinkProtocol[2];
        protocols[0] = LinkProtocol.Create(this, configuration, linkSides[0], linkSides[1]);
        protocols[1] = LinkProtocol.Create(this, configuration, linkSides[1], linkSides[0]);

        //connect elements
        linkSides[0].LinkProtocol = protocols[0];
        linkSides[1].LinkProtocol = protocols[1];

        //parse attributes
        XmlAttribute ber = configuration.Attributes[berTag];

        if (ber != null) // if ber == null leave default value of ber ==0
        {
            this.ber = double.Parse(ber.Value);
            if (this.ber < 0)
            {
                throw new ArgumentException("BER cannot be less than 0.");
            }
        }
        XmlAttribute speed = XmlParser.GetAttribute(configuration, speedTag);

        this.speed = double.Parse(speed.Value);
        if (this.speed < 0)
        {
            throw new ArgumentException("Link speed cannot be less than 0.");
        }
        //create delay generator
        XmlNode delayGenerator = XmlParser.GetChildNode(configuration, delayGeneratorTag);

        this.delayGenerator = RandomGenerator.Create(delayGenerator);
        //subscribe to state change events (break etc.)
        OnBreak   += onBreak;
        OnRepair  += onRepair;
        OnTurnOff += onTurnOff;
        OnTurnOn  += onTurnOn;
    }
 public SerialLink(string portName, LinkProtocol p, int br) : base(p)
 {
     port          = new SerialPort(portName);
     port.BaudRate = br;
     port.DataBits = 8;
     port.StopBits = StopBits.One;
     port.ReceivedBytesThreshold = 16;
     port.DataReceived          += Port_DataReceived;
     buffer             = new byte[1048576];//1M
     isUpdatingBuffer   = false;
     isParsingBuffer    = false;
     lastPackageTime    = DateTime.Now;
     receiveTimeOut     = 5000;
     backgroundListener = new Thread(backgroundWorker);
     backgroundListener.IsBackground = true;
     backgroundListener.Start();
     connectTime    = DateTime.Now;
     sw             = new Stopwatch();
     enqueueHandler = new EnqueuePackage(receivedPackageQueue.Enqueue);
 }
Example #5
0
        /// <summary>
        /// Converts the enum value for Link Protocol to the string value used for CKE config.
        /// </summary>
        /// <param name="protocol">The protocol enum value.</param>
        /// <returns>The protocol string value.</returns>
        public static string ToSettingValue(this LinkProtocol protocol)
        {
            switch (protocol)
            {
            case LinkProtocol.Http:
                return("http://");

            case LinkProtocol.Ftp:
                return("ftp://");

            case LinkProtocol.News:
                return("news://");

            case LinkProtocol.Other:
                return(string.Empty);

            case LinkProtocol.Https:
            default:
                return("https://");
            }
        }
Example #6
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;
            }
        }
 public void setProtocol(LinkProtocol protocol)
 {
     this.protocol = protocol;
 }