/// <summary> /// Running thread handler /// </summary> protected void Worker() { var server = new ModbusServer(new ModbusTcpCodec()) { Address = SlaveId }; server.IncommingData += DriverIncommingData; server.OutgoingData += DriverOutgoingData; try { while (_thread.ThreadState == ThreadState.Running) { //wait for an incoming connection _listener = _socket.GetTcpListener(server); _listener.ServeCommand += listener_ServeCommand; _listener.Start(); AppendLog(String.Format("Accepted connection.")); Thread.Sleep(1); } } catch (Exception ex) { String msg = ex.Message; } }
private void DoDisconnect() { if (InvokeRequired) { BeginInvoke(new Action(DoDisconnect)); return; } if (_listener != null) { _listener.Abort(); _listener = null; } if (_uart != null) { _uart.Close(); _uart.Dispose(); _uart = null; } if (_thread != null && _thread.IsAlive) { if (_thread.Join(2000) == false) { _thread.Abort(); _thread = null; } } if (_socket != null) { _socket.Dispose(); _socket = null; } }
private void BtnConnectClick(object sender, EventArgs e) { try { switch (CommunicationMode) { case CommunicationMode.RTU: _uart = new SerialPort(PortName, Baud, Parity, DataBits, StopBits); _uart.Open(); var rtuServer = new ModbusServer(new ModbusRtuCodec()) { Address = SlaveId }; rtuServer.OutgoingData += DriverOutgoingData; rtuServer.IncommingData += DriverIncommingData; _listener = _uart.GetListener(rtuServer); _listener.ServeCommand += listener_ServeCommand; _listener.Start(); AppendLog(String.Format("Connected using RTU to {0}", PortName)); break; case CommunicationMode.UDP: _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); _socket.Bind(new IPEndPoint(IPAddress.Any, TCPPort)); //create a server driver var udpServer = new ModbusServer(new ModbusTcpCodec()) { Address = SlaveId }; udpServer.OutgoingData += DriverOutgoingData; udpServer.IncommingData += DriverIncommingData; //listen for an incoming request _listener = _socket.GetUdpListener(udpServer); _listener.ServeCommand += listener_ServeCommand; _listener.Start(); AppendLog(String.Format("Listening to UDP port {0}", TCPPort)); break; case CommunicationMode.TCP: _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _socket.Bind(new IPEndPoint(IPAddress.Any, TCPPort)); _socket.Listen(10); //create a server driver _thread = new Thread(Worker); _thread.Start(); AppendLog(String.Format("Listening to TCP port {0}", TCPPort)); // simulate classic broadcasrter = new UDPBroadcaster(); broadcasrter.SendDatagrams(TCPPort); break; } } catch (Exception ex) { AppendLog(ex.Message); return; } btnConnect.Enabled = false; buttonDisconnect.Enabled = true; grpExchange.Enabled = true; groupBoxFunctions.Enabled = false; groupBoxTCP.Enabled = false; groupBoxRTU.Enabled = false; groupBoxMode.Enabled = false; }
private void CallRemotingGestionServer(StatusFrameDecoded message) { TcpChannel tcpChannel = null; try { tcpChannel = new TcpChannel(); ChannelServices.RegisterChannel(tcpChannel, false); Type requiredType = typeof(ICommServer); ICommServer remoteObject = (ICommServer)Activator.GetObject(requiredType, "tcp://localhost:9998/CommServer"); remoteObject.SetDeviceStatus(message.DeviceId, message.StatusOnOff, message.UpTime); } catch (Exception e) { log.Error("Se cagó", e); } finally { if (tcpChannel != null) { ChannelServices.UnregisterChannel(tcpChannel); } } }
private void BtnConnectClick(object sender, EventArgs e) { try { switch (CommunicationMode) { case CommunicationMode.RTU: _uart = new SerialPort(PortName, Baud, Parity, DataBits, StopBits); _uart.Open(); var rtuServer = new ModbusServer(new ModbusRtuCodec()); rtuServer.OutgoingData += DriverOutgoingData; rtuServer.IncommingData += DriverIncommingData; _listener = _uart.GetListener(rtuServer); _listener.ServeCommand += listener_ServeCommand; _listener.Start(); AppendLog(String.Format("Connected using RTU to {0}", PortName)); break; case CommunicationMode.UDP: _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); _socket.Bind(new IPEndPoint(IPAddress.Any, TCPPort)); //create a server driver var udpServer = new ModbusServer(new ModbusTcpCodec()); udpServer.OutgoingData += DriverOutgoingData; udpServer.IncommingData += DriverIncommingData; //listen for an incoming request _listener = _socket.GetUdpListener(udpServer); _listener.ServeCommand += listener_ServeCommand; _listener.Start(); AppendLog(String.Format("Listening to UDP port {0}", TCPPort)); break; case CommunicationMode.TCP: _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _socket.Bind(new IPEndPoint(IPAddress.Any, TCPPort)); _socket.Listen(10); //create a server driver _thread = new Thread(Worker); _thread.Start(); AppendLog(String.Format("Listening to TCP port {0}", TCPPort)); break; } } catch (Exception ex) { AppendLog(ex.Message); return; } btnConnect.Enabled = false; buttonDisconnect.Enabled = true; grpExchange.Enabled = true; groupBoxTCP.Enabled = false; groupBoxRTU.Enabled = false; groupBoxMode.Enabled = false; }
public List <DeviceInfo> GetDevices() { TcpChannel tcpChannel = new TcpChannel(); try { ICommServer iCommServer = ConnectToCommServer(tcpChannel); List <DeviceInfo> devices = iCommServer.GetDevices(); return(devices); } catch (Exception) { throw new Exception("Ocurrio un error al obtener los Dispositivos"); } finally { DisconnectFromRemotingServer(tcpChannel); } }
protected void Init() { try { //获取应用程序运行路径 string path = AppDomain.CurrentDomain.BaseDirectory; DataSet DBds = new DataSet(); //读入数据库连接参数 DBds = MD5Encrypt.DES.instance().DecryptXML2DS(path + "conf.xml", 1); m_SysPara.Clear(); foreach (DataRow r in DBds.Tables["Parameters"].Rows) { m_SysPara.Add(r["name"].ToString(), r["value"].ToString()); } m_CommClient = CommManager.instance().CreateCommClient <CommandMsgV2, TKMessageV2Extractor, TKMessageV2Encoder>( m_SysPara["Adapter Name"].ToString(), m_SysPara["Server IP"].ToString(), Convert.ToInt32(m_SysPara["Server Port"]), Convert.ToInt32(m_SysPara["Comm Timeout"]), true, false); m_CommClient.onLog += new TK_AlarmManagement.LogHandler(LogReceiver); #region Declear Adapter m_Adapter = new T(); m_Adapter.ControllerPort = Convert.ToInt32(m_SysPara["Controller Port"]); m_Adapter.Name = m_SysPara["Adapter Name"].ToString(); m_Adapter.Interval = Convert.ToInt32(m_SysPara["Retrieve Interval"]); m_Adapter.SvrID = Convert.ToInt32(m_SysPara["SvrID"]); m_Adapter.EncodingStr = m_SysPara["Encoding"]; m_Adapter.Init(m_CommClient); #endregion DefLib.Util.Logger.Instance().SubscibeLog("", new DefLib.Util.Logger.LogFunction(LogReceiver)); //m_Adapter.LogReceived += new TK_AlarmManagement.LogHandler(LogReceiver); m_Adapter.StateChanged += new StateChangeHandler(m_Adapter_StateChanged); m_Name = m_Adapter.Name; // 配置监控终端服务器 List <Constants.TK_CommandType> acceptedCommands = new List <Constants.TK_CommandType>(); acceptedCommands.Add(Constants.TK_CommandType.RESPONSE); List <Constants.TK_CommandType> superCommands = new List <Constants.TK_CommandType>(); superCommands.Add(Constants.TK_CommandType.RESPONSE); m_ControllerServer = CommManager.instance().CreateCommServer <DefaultInterpreter, CommandMsgV2, TKMessageV2Extractor, TKMessageV2Encoder>("监控服务器", acceptedCommands, superCommands, Convert.ToInt32(m_SysPara["Controller Port"]), Convert.ToInt32(m_SysPara["MaxController"]), 30, true, false); m_ControllerServer.onLog += new LogHandler(LogReceiver); acceptedCommands.Clear(); superCommands.Clear(); Dictionary <Constants.TK_CommandType, byte> empty = new Dictionary <Constants.TK_CommandType, byte>(); CommandProcessor.instance().registerReportHandler(Constants.TK_CommandType.ADAPTER_START, this, empty); CommandProcessor.instance().registerReportHandler(Constants.TK_CommandType.ADAPTER_STOP, this, empty); CommandProcessor.instance().registerReportHandler(Constants.TK_CommandType.ADAPTER_GETRUNTIMEINFO, this, empty); CommandProcessor.instance().registerReportHandler(Constants.TK_CommandType.ADAPTER_GETOMCLIST, this, empty); CommandProcessor.instance().registerReportHandler(Constants.TK_CommandType.ADAPTER_GETCURLOG, this, empty); CommandProcessor.instance().registerReportHandler(Constants.TK_CommandType.ADAPTER_GETLOGFILES, this, empty); CommandProcessor.instance().registerReportHandler(Constants.TK_CommandType.ADAPTER_SHUTDOWN, this, empty); } catch (Exception ex) { Logger.Instance().SendLog("AdapterController", ex.ToString()); throw ex; } }
public AdapterControlHelper(ICommServer server) { m_AdapterServer = server; }