Exemplo n.º 1
0
        public PLCModBusRtuDriver(PLCDescriptor descriptor)
        {
            sendCommandDateTime = DateTime.Now;

            this.descriptor = descriptor;
            parseLock       = new object();
            sendLock        = new object();

            receBytesCache = new List <byte>();

            fastQueue = new Queue <RtuRegistPackage>();
            sendQueue = new Queue <RtuRegistPackage>();

            parseTd = new Thread(new ThreadStart(ParseModBusPackage));
            parseTd.IsBackground = true;
            parseTd.Start();

            sendTd = new Thread(new ThreadStart(SendModBusPackage));
            sendTd.IsBackground = true;
            sendTd.Start();

            tcpPort = new TcpClientPort(descriptor.IpAddress, descriptor.Port, descriptor.AutoReConnInterval);
            tcpPort.OnConnNotify        += TcpPort_OnConnNotify;
            tcpPort.OnExceptionHappened += TcpPort_OnExceptionHappened;
            tcpPort.OnPortDataReceived  += TcpPort_OnPortDataReceived;

            heartTimer          = new System.Timers.Timer();
            heartTimer.Interval = descriptor.SendInterval * 1000;
            heartTimer.Elapsed += SendTimer_Elapsed;
            heartTimer.Enabled  = true;
        }
Exemplo n.º 2
0
        private PLCModBusRtuDriver GetDriverByXmlNode(XmlElement element)
        {
            byte   salve     = byte.Parse(element.Attributes["salve"].Value, NumberStyles.HexNumber);
            string ipAddress = element.Attributes["ipAddress"].Value;
            int    port      = 502;

            int.TryParse(element.Attributes["port"]?.Value, out port);
            string sendInterval = element.Attributes["sendInterval"].Value;
            int    interval     = 10;

            int.TryParse(sendInterval, out interval);
            //关于寄存器 这里暂时不做配置
            PLCDescriptor      descriptor = new PLCDescriptor(salve, ipAddress, port, interval);
            PLCModBusRtuDriver driver     = new PLCModBusRtuDriver(descriptor);

            return(driver);
        }