Example #1
0
        private void _writeSocket(byte[] buffer, int length)
        {
            if (_socket != null && _socket.Connected && _dtuconnected)
            {
                try
                {
                    byte[] sn = BitConverter.GetBytes(ulong.Parse(_connectionInfo.DtuSn, NumberStyles.AllowHexSpecifier));
                    Array.Reverse(sn);

                    byte[] bytes = buffer.Take(length).ToArray();

                    if (GetType() == typeof(NetTransService))
                    {
                        MonitorDataManager.instance.Add(_connectionInfo.DtuSn, bytes, MonitorDataType.MDT_LOC);

                        PassThrough passthrough = new PassThrough(sn, bytes);
                        Package     pkg         = new Package(passthrough);
                        Logger.Trace("[{0}] ===> {1}", _connectionInfo.DtuSn, pkg.ToString());
                        _socket.Send(pkg.toBytes());
                    }
                    else if (GetType() == typeof(ComTransService))
                    {
                        if (_connector.GetType() == typeof(ComConnector))
                        {
                            ComConnector cc = _connector as ComConnector;
                            MonitorDataManager.instance.Add(_connectionInfo.DtuSn, bytes, MonitorDataType.MDT_COM);
                            byte           syncflag       = 1; //同步波特率
                            ComPassThrough compassthrough = new ComPassThrough(sn, syncflag, cc.VCom.DCBlock.BaudRate, cc.VCom.DCBlock.ByteSize, cc.VCom.DCBlock.Parity, cc.VCom.DCBlock.StopBits, bytes);
                            Package        pkg            = new Package(compassthrough);
                            Logger.Trace("[{0}] ===> {1}", _connectionInfo.DtuSn, pkg.ToString());
                            _socket.Send(pkg.toBytes());
                        }
                    }
                }
                catch (Exception e)
                {
                    Logger.Error(e.ToString());
                }
            }
        }
Example #2
0
        public void _doLocalSend(byte[] buffer, int length)
        {
            try
            {
                byte[]  bytes   = buffer.Take(length).ToArray();
                Package package = new Package(bytes);

                Logger.Trace("[{0}] <=== {1}", _connectionInfo.DtuSn, package.ToString());

                if (package.Id == Protocol.ID_HEARTBEAT)
                {
                    HeartBeat heartbeat   = new HeartBeat(package.getPayload());
                    HeartBeat myheartbeat = new HeartBeat();
                    Package   pkg         = new Package(myheartbeat);

                    Logger.Trace("[{0}] ===> {1}", _connectionInfo.DtuSn, pkg.ToString());
                    _socket.Send(pkg.toBytes());
                }
                else if (package.Id == Protocol.ID_CONNECTRESPONSE && !_dtuconnected)
                {
                    ConnectResponse response = new ConnectResponse(package.getPayload());
                    if (response.Result == 0)
                    {
                        //连接成功
                        _dtuconnected = true;
                    }
                    else if (response.Result == 1)
                    {
                        //连接失败
                        if (response.Reason == 1)
                        {
                            //DTU不存在
                            _connectionInfo.Status = (int)RunningStatus.RS_DTUNOTFOUND;
                            Logger.Info("[{0}] dtu:[{1}] not found!", _connectionInfo.DtuSn, _connectionInfo.DtuSn);
                        }
                        else if (response.Reason == 2)
                        {
                            //DTU未在线
                            _connectionInfo.Status = (int)RunningStatus.RS_DTUNOTCONNECTED;
                            Logger.Info("[{0}] dtu:[{1}] not online!", _connectionInfo.DtuSn, _connectionInfo.DtuSn);
                        }
                        else if (response.Reason == 3)
                        {
                            //DTU调试中
                            _connectionInfo.Status = (int)RunningStatus.RS_DTUALREADYDEBUGING;
                            Logger.Info("[{0}] dtu:[{1}] is debugging!", _connectionInfo.DtuSn, _connectionInfo.DtuSn);
                        }
                        else if (response.Reason == 4)
                        {
                            //DTU监控中
                            _connectionInfo.Status = (int)RunningStatus.RS_DTUNOTDEBUGMODE;
                            Logger.Info("[{0}] dtu:[{1}] is monitoring!", _connectionInfo.DtuSn, _connectionInfo.DtuSn);
                        }
                    }
                    _waitdtuconnect = false;
                }
                else if (_dtuconnected)
                {
                    if (package.Id == Protocol.ID_PLCCONNECTRESPONSE && _waitplcconnect)
                    {
                        PlcConnectResponse response = new PlcConnectResponse(package.getPayload());
                        if (response.Result == 0)
                        {
                            //连接成功
                            _plcconnected = true;
                        }
                        else if (response.Result == 1)
                        {
                        }
                        _waitplcconnect = false;
                    }
                    else if (_plcconnected)
                    {
                        if (package.Id == Protocol.ID_PASSTHROUGH)
                        {
                            PassThrough passthrough = new PassThrough(package.getPayload());

                            MonitorDataManager.instance.Add(_connectionInfo.DtuSn, passthrough.Data, MonitorDataType.MDT_NET);

                            _connector.Send(passthrough.Data, passthrough.Data.Length);

                            Logger.Debug("passthrough: {0}", passthrough.ToString());
                        }
                        if (package.Id == Protocol.ID_COMPASSTHROUGH)
                        {
                            ComPassThrough compassthrough = new ComPassThrough(package.getPayload());

                            MonitorDataManager.instance.Add(_connectionInfo.DtuSn, compassthrough.Data, MonitorDataType.MDT_NET);

                            _connector.Send(compassthrough.Data, compassthrough.Data.Length);

                            Logger.Debug("passthrough: {0}", compassthrough.ToString());
                        }
                        else if (package.Id == Protocol.ID_DEBUGEXCEPTION)
                        {
                            DebugException exception = new DebugException(package.getPayload());
                            if (exception.Reason == 1)
                            {
                                _dtuconnected = false;
                                //DTU监控中
                                _connectionInfo.Status = (int)RunningStatus.RS_CONNECTBREAK;
                                Logger.Info("[{0}] connection break!", _connectionInfo.DtuSn);
                            }


                            Logger.Debug("DebugException: {0}", exception.ToString());
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Error(e.ToString());
            }
        }