public ModbusMasterTcpConnection(TcpClient client, ModbusTcpSlave slave) : base(new ModbusIpTransport(new TcpClientAdapter(client))) { if (client == null) throw new ArgumentNullException("client"); if (slave == null) throw new ArgumentException("slave"); _client = client; _slave = slave; _log.DebugFormat("Creating new Master connection at IP:{0}", EndPoint); _log.Debug("Begin reading header."); Stream.BeginRead(_mbapHeader, 0, 6, ReadHeaderCompleted, null); }
public ModbusMasterTcpConnection(TcpClient client, ModbusTcpSlave slave) : base(new ModbusIpTransport(new TcpClientAdapter(client))) { if (client == null) { throw new ArgumentNullException("client"); } if (slave == null) { throw new ArgumentException("slave"); } _client = client; _slave = slave; _log.DebugFormat("Creating new Master connection at IP:{0}", EndPoint); _log.Debug("Begin reading header."); Stream.BeginRead(_mbapHeader, 0, 6, ReadHeaderCompleted, null); }
public ModbusMasterTcpConnection(TcpClient client, ModbusTcpSlave slave) : base(new ModbusIpTransport(new TcpClientAdapter(client))) { if (client == null) { throw new ArgumentNullException(nameof(client)); } if (slave == null) { throw new ArgumentNullException(nameof(slave)); } _client = client; _endPoint = client.Client.RemoteEndPoint.ToString(); _stream = client.GetStream(); _slave = slave; _requestHandlerTask = Task.Run((Func <Task>)HandleRequestAsync); }
public MasterConnection(string endPoint, NetworkStream stream, ModbusTcpSlave slave) { if (stream == null) { throw new ArgumentException("stream"); } if (slave == null) { throw new ArgumentException("slave"); } _log.DebugFormat("Creating new Master connection at IP:{0}", endPoint); _slave = slave; _endPoint = endPoint; _stream = stream; _log.Debug("Begin reading header."); _stream.BeginRead(_mbapHeader, 0, 6, ReadHeaderCompleted, null); }
public ModbusMasterTcpConnection(TcpClient client, ModbusTcpSlave slave) : base(new ModbusIpTransport(new TcpClientAdapter(client))) { if (client == null) { throw new ArgumentNullException("client"); } if (slave == null) { throw new ArgumentException("slave"); } _client = client; _endPoint = client.Client.RemoteEndPoint.ToString(); _stream = client.GetStream(); _slave = slave; Debug.WriteLine("Creating new Master connection at IP:{0}", EndPoint); Debug.WriteLine("Begin reading header."); Stream.BeginRead(_mbapHeader, 0, 6, ReadHeaderCompleted, null); }
public ModbusMasterTcpConnection(TcpClient client, ModbusTcpSlave slave) : base(new ModbusIpTransport(new TcpClientAdapter(client))) { if (client == null) throw new ArgumentNullException("client"); if (slave == null) throw new ArgumentNullException("slave"); _client = client; _endPoint = client.Client.RemoteEndPoint.ToString(); _stream = client.GetStream(); _slave = slave; _readHeaderCompletedCallback = ReadHeaderCompleted; _readFrameCompletedCallback = ReadFrameCompleted; _writeCompletedCallback = WriteCompleted; Debug.WriteLine("Creating new Master connection at IP:{0}", EndPoint); Debug.WriteLine("Begin reading header."); Stream.BeginRead(_mbapHeader, 0, 6, _readHeaderCompletedCallback, null); }
internal void AcceptCompleted(IAsyncResult ar) { ModbusTcpSlave slave = (ModbusTcpSlave)ar.AsyncState; try { // use Socket async API for compact framework compat Socket socket = null; lock (_serverLock) { if (_server == null) // Checks for disposal to an otherwise unnecessary exception (which is slow and hinders debugging). { return; } socket = Server.Server.EndAccept(ar); } TcpClient client = new TcpClient { Client = socket }; var masterConnection = new ModbusMasterTcpConnection(client, slave); masterConnection.ModbusMasterTcpConnectionClosed += (sender, eventArgs) => RemoveMaster(eventArgs.EndPoint); lock (_mastersLock) _masters.Add(client.Client.RemoteEndPoint.ToString(), masterConnection); Debug.WriteLine("Accept completed."); // Accept another client // use Socket async API for compact framework compat lock (_serverLock) Server.Server.BeginAccept(AcceptCompleted, slave); } catch (ObjectDisposedException) { // this happens when the server stops } }
internal void AcceptCompleted(IAsyncResult ar) { ModbusTcpSlave slave = (ModbusTcpSlave)ar.AsyncState; try { TcpClient client = _server.EndAcceptTcpClient(ar); lock (_mastersLock) _masters.Add(client.Client.RemoteEndPoint.ToString(), client); new MasterConnection(client.Client.RemoteEndPoint.ToString(), client.GetStream(), slave); _log.Debug("Accept completed."); // Accept another client _server.BeginAcceptTcpClient(AcceptCompleted, slave); } catch (ObjectDisposedException) { // this happens when the server stops } }
internal void AcceptCompleted(IAsyncResult ar) { ModbusTcpSlave slave = (ModbusTcpSlave)ar.AsyncState; try { TcpClient client = _server.EndAcceptTcpClient(ar); var masterConnection = new ModbusMasterTcpConnection(client, slave); masterConnection.ModbusMasterTcpConnectionClosed += (sender, eventArgs) => RemoveMaster(eventArgs.EndPoint); lock (_mastersLock) _masters.Add(client.Client.RemoteEndPoint.ToString(), masterConnection); _logger.Debug("Accept completed."); // Accept another client _server.BeginAcceptTcpClient(AcceptCompleted, slave); } catch (ObjectDisposedException) { // this happens when the server stops } }
internal void AcceptCompleted(IAsyncResult ar) { ModbusTcpSlave slave = (ModbusTcpSlave)ar.AsyncState; try { // use Socket async API for compact framework compat Socket socket = null; lock (_serverLock) socket = Server.Server.EndAccept(ar); TcpClient client = new TcpClient { Client = socket }; var masterConnection = new ModbusMasterTcpConnection(client, slave); masterConnection.ModbusMasterTcpConnectionClosed += (sender, eventArgs) => RemoveMaster(eventArgs.EndPoint); lock (_mastersLock) _masters.Add(client.Client.RemoteEndPoint.ToString(), masterConnection); _logger.Debug("Accept completed."); // Accept another client // use Socket async API for compact framework compat lock (_serverLock) Server.Server.BeginAccept(AcceptCompleted, slave); } catch (ArgumentException) { // TODO: this happens when } catch (ObjectDisposedException) { // this happens when the server stops } }
public MasterConnection(string endPoint, NetworkStream stream, ModbusTcpSlave slave) { if (stream == null) throw new ArgumentException("stream"); if (slave == null) throw new ArgumentException("slave"); _log.DebugFormat("Creating new Master connection at IP:{0}", endPoint); _slave = slave; _endPoint = endPoint; _stream = stream; _log.Debug("Begin reading header."); _stream.BeginRead(_mbapHeader, 0, 6, ReadHeaderCompleted, null); }
private void StartButton_Click(object sender, EventArgs e) { if(StartButton.Text == "Start") { try{ IpAdressBox.BackColor = UnitIdBox.BackColor; address = (IPAddress)IpAdressBox.SelectedItem; log.Info("IP Adress to bound to: " + address.ToString()); tcpListener = new TcpListener(address, (int)PortBox.Value); tcpListener.Start(); slave = ModbusTcpSlave.CreateTcp((byte)UnitIdBox.Value, tcpListener); slave.DataStore = Modbus.Data.DataStoreFactory.CreateDefaultDataStore(); slave.DataStore.DataStoreWrittenTo += DataStore_DataStoreWrittenTo; slave.Listen(); foreach(RegValue r in regValues) slave.DataStore.HoldingRegisters[r.Register] = (ushort)r.Value; StartButton.Text = "Stop"; } catch(Exception ex) { log.Error("Error listening to modbus", ex); MessageBox.Show("Error starting Modbus!"); } } else { slave.Dispose(); tcpListener.Stop(); StartButton.Text = "Start"; } }