Example #1
0
        private void Run()
        {
            m_isThreadStop = false;

            ServiceLog.LogServiceMessage(string.Format("开始连接[{0}:{1}]", m_serverIP, m_serverPort));
            m_clientSocket = CreateClientSocket(m_serverIP, m_serverPort);
            if (m_clientSocket != null)
            {
                ServiceLog.LogServiceMessage(string.Format("连接[{0}:{1}]成功", m_serverIP, m_serverPort));
                try
                {
                    while (m_clientSocket != null && m_clientSocket.Connected)
                    {
                        Thread.Sleep(2047);
                        DisplayMessage(m_config.ModbusOperations.Count().ToString());
                        //SendAsync(m_clientSocket, DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss\r\n"));
                        foreach (ModbusTcpOperation operation in m_config.ModbusOperations)
                        {
                            DisplayMessage(operation.StartAddr.ToString());

                            byte[] plc_send   = new byte[12];
                            byte[] identifier = BitConverter.GetBytes(operation.Identifier);
                            byte[] protocol   = BitConverter.GetBytes(operation.Protocol);
                            byte[] length     = BitConverter.GetBytes(operation.Length);
                            byte[] startAddr  = BitConverter.GetBytes((ushort)operation.StartAddr);
                            byte[] regCount   = BitConverter.GetBytes((ushort)operation.RegCount);

                            plc_send[0]  = identifier[1];
                            plc_send[1]  = identifier[0];
                            plc_send[2]  = protocol[1];
                            plc_send[3]  = protocol[0];
                            plc_send[4]  = length[1];
                            plc_send[5]  = length[0];
                            plc_send[6]  = (byte)operation.DeviceAddr;
                            plc_send[7]  = (byte)operation.FunctionCode;
                            plc_send[8]  = startAddr[1];
                            plc_send[9]  = startAddr[0];
                            plc_send[10] = regCount[1];
                            plc_send[11] = regCount[0];

                            SendAsync(m_clientSocket, plc_send);

                            StateObject stateObj = new StateObject();
                            stateObj.ClientSocket = m_clientSocket;

                            DisplayMessage("");
                            DisplayMessage("开始接收信息...");
                            m_clientSocket.BeginReceive(stateObj.Buffer, 0, StateObject.BufferSize,
                                                        0, new AsyncCallback(ReceiveCallback), stateObj);
                        }
                    }
                }
                catch (Exception e)
                {
                    DisplayMessage(string.Format("连接出现异常:{0}", e.Message));
                }

                DisplayMessage("结束...");
            }
        }
Example #2
0
        private void Run()
        {
            m_isThreadStop = false;

            ServiceLog.LogServiceMessage(string.Format("开始连接[{0}:{1}]", m_serverIP, m_serverPort));
            m_clientSocket = CreateClientSocket(m_serverIP, m_serverPort);
            if (m_clientSocket != null)
            {
                ServiceLog.LogServiceMessage(string.Format("连接[{0}:{1}]成功", m_serverIP, m_serverPort));
                try
                {
                    while (m_clientSocket != null && m_clientSocket.Connected)
                    {
                        Thread.Sleep(47);


                        SendAsync(m_clientSocket, DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss\r\n"));

                        StateObject stateObj = new StateObject();
                        stateObj.ClientSocket = m_clientSocket;

                        DisplayMessage("");
                        DisplayMessage("开始接收信息...");
                        m_clientSocket.BeginReceive(stateObj.Buffer, 0, StateObject.BufferSize,
                                                    0, new AsyncCallback(ReceiveCallback), stateObj);
                    }
                }
                catch (Exception e)
                {
                    DisplayMessage(string.Format("连接出现异常:{0}", e.Message));
                }

                DisplayMessage("结束...");
            }
        }
Example #3
0
        public void OnStart(string dataProtocol, string serverIP, string serverPort,
                            GetDataServiceDAL dal)
        {
            if (!IPAddress.TryParse(serverIP, out m_serverIP))
            {
                ServiceLog.LogServiceMessage(string.Format("IP地址[{0}]无效。", serverIP));
                return;
            }

            if (!ushort.TryParse(serverPort, out m_serverPort))
            {
                ServiceLog.LogServiceMessage(string.Format("端口[{0}]无效。", serverPort));
                return;
            }


            try
            {
                m_clientThread = new Thread(new ThreadStart(Run));
                m_clientThread.IsBackground = true;
                m_clientThread.Start();
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #4
0
        //protected override void OnStart(string[] args)
        public void OnStart()
        {
            ServiceLog.LogServiceMessage("启动服务");

            //读取配置,获取服务器列表,每个服务器创建一个对应的客户端连接
            m_svrCfg = new ServersConfig();
            List <DataServerConfigDesc> dataServers = m_svrCfg.GetServerConfigDescList();

            foreach (DataServerConfigDesc dataServer in dataServers)
            {
                ServiceLog.LogServiceMessage(dataServer.ServerCommType);
                switch (dataServer.ServerCommType.ToUpper())
                {
                case "MODBUSTCP":
                    ModbusTcpConfig mbTcpCfg = new ModbusTcpConfig(dataServer.ServerName,
                                                                   dataServer.ServerCommType, dataServer.ServerConfigNode);
                    if (mbTcpCfg != null)
                    {
                        ModbusTCPHandler mbTcpHandler = new ModbusTCPHandler(mbTcpCfg);
                        m_commHandlers.Add(mbTcpHandler);
                        mbTcpHandler.OnStart();
                    }
                    break;

                default:
                    break;
                }
                //通讯太频繁会出问题
                Thread.Sleep(10);
            }
        }
Example #5
0
 public ServersConfig()
 {
     m_xmlDoc = new XmlDocument();
     try
     {
         m_xmlDoc.Load(string.Format("{0}\\ServersConfig.xml",
                                     AppDomain.CurrentDomain.BaseDirectory));
     }
     catch (Exception ex)
     {
         m_xmlDoc = null;
         ServiceLog.LogServiceMessage(ex.Message);
     }
 }
Example #6
0
        public override void OnStart()
        {
            if (!IPAddress.TryParse(m_config.ServerIP, out m_serverIP))
            {
                IPAddress[] ips = Dns.GetHostAddresses(m_config.ServerIP);
                if (ips.Length == 0)
                {
                    ServiceLog.LogServiceMessage(string.Format("IP地址[{0}]无效。", m_config.ServerIP));
                    return;
                }
                else
                {
                    foreach (IPAddress ip in ips)
                    {
                        if (!ip.IsIPv6LinkLocal && !ip.IsIPv6Multicast && !ip.IsIPv6SiteLocal)
                        {
                            m_serverIP = ip;
                            break;
                        }
                    }
                    if (m_serverIP == null)
                    {
                        ServiceLog.LogServiceMessage(string.Format("IP地址[{0}]无效。", m_config.ServerIP));
                        return;
                    }
                }
            }

            m_serverPort = m_config.ServerPort;


            try
            {
                m_clientThread = new Thread(new ThreadStart(Run));
                m_clientThread.IsBackground = true;
                m_clientThread.Start();
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #7
0
        private Socket CreateClientSocket(IPAddress serverIP, ushort serverPort)
        {
            Socket clientSocket = null;

            try
            {
                IPEndPoint endPoint = new IPEndPoint(serverIP, serverPort);
                clientSocket = new Socket(AddressFamily.InterNetwork,
                                          SocketType.Stream, ProtocolType.Tcp);
                clientSocket.Connect(endPoint);

                return(clientSocket);
            }
            catch (Exception e)
            {
                ServiceLog.LogServiceMessage(string.Format("连接[{0}:{1}]失败:{2}",
                                                           serverIP, serverPort, e.Message));
                return(null);
            }
        }
Example #8
0
        private List <ModbusTcpOperation> GetModbusTcpOperations()
        {
            List <ModbusTcpOperation> ops = new List <ModbusTcpOperation>();

            XmlNode opsNode = GetModbusTcpOperationsNode();

            if (opsNode != null)
            {
                XmlNodeList opNodeList = opsNode.SelectNodes("operation");
                foreach (XmlNode opNode in opNodeList)
                {
                    ModbusTcpOperation op = new ModbusTcpOperation();

                    if (opNode.Attributes["device_addr"] != null)
                    {
                        string sDeviceAddr = opNode.Attributes["device_addr"].Value;
                        byte   tempDeviceAddr;
                        if (byte.TryParse(sDeviceAddr, out tempDeviceAddr))
                        {
                            op.DeviceAddr = tempDeviceAddr;
                        }
                    }
                    if (op.DeviceAddr == null)
                    {
                        continue;
                    }

                    if (opNode.Attributes["func_code"] != null)
                    {
                        string sFuncCode = opNode.Attributes["func_code"].Value;
                        byte   tempFuncCode;
                        if (byte.TryParse(sFuncCode, out tempFuncCode))
                        {
                            op.FunctionCode = tempFuncCode;
                        }
                    }
                    if (op.FunctionCode == null)
                    {
                        continue;
                    }

                    if (opNode.Attributes["start_addr"] != null)
                    {
                        string sStartAddr = opNode.Attributes["start_addr"].Value;
                        ushort tempStartAddr;
                        if (ushort.TryParse(sStartAddr, out tempStartAddr))
                        {
                            op.StartAddr = tempStartAddr;
                        }
                    }
                    if (op.StartAddr == null)
                    {
                        continue;
                    }

                    if (opNode.Attributes["reg_count"] != null)
                    {
                        string sRegCount = opNode.Attributes["reg_count"].Value;
                        ushort tempRegCount;
                        if (ushort.TryParse(sRegCount, out tempRegCount))
                        {
                            op.RegCount = tempRegCount;
                        }
                    }
                    if (op.RegCount == null)
                    {
                        continue;
                    }

                    op.Length     = 6;
                    op.Protocol   = 0;
                    op.Identifier = m_identifier++;
                    ServiceLog.LogServiceMessage(op.ToString());
                    ops.Add(op);
                }
            }

            return(ops);
        }
Example #9
0
 protected override void OnStop()
 {
     ServiceLog.LogServiceMessage("停止服务");
 }