private void OpenSession(string channel, ushort mb_addr) { // See if we are supposed to connect to an existing session if (this.SessionId != null) { this.MbSlaveSession = (SMBSession)TMWSession.LookupSession(Convert.ToUInt32(this.SessionId)); if (this.MbSlaveSession != null) { this.Channel = (MBChannel)this.MbSlaveSession.Channel; this.Channel.SessionCloseEvent += new TMWChannel.SessionCloseEventDelegate(SessionCloseEvent); } else { throw new Exception("Unable to load Modbus Slave Simulator. Unable to find session."); } } else { try { MbSlaveSession = new SMBSession(Channel); MbSlaveSession.SlaveAddress = mb_addr; MbSlaveSession.Name = "sMBSim_" + mb_addr; MbSlaveSession.OpenSession(); } catch (Exception ex) { MessageBox.Show("Error Opening: " + ex.Message); Channel = null; MbSlaveSession = null; Close(); return; } } }
/// <summary> /// Closes the channel /// </summary> private void CloseChannel() { if (this.Channel != null) this.Channel.CloseChannel(); this.Channel = null; }
/// <summary> /// Open Channel /// </summary> /// <param name="port"></param> /// <param name="total_ports"></param> private string OpenChannel(ushort port) { // if we are already connected disconnect if (this.Channel != null) { if (this.Channel.Name == "sMBSim_" + port) { CloseSession(); CloseChannel(); } } try { Channel = new MBChannel(TMW_CHANNEL_OR_SESSION_TYPE.SLAVE); Channel.Type = WINIO_TYPE.TCP; Channel.WinTCPipPort = Convert.ToUInt16(port); Channel.WinTCPipAddress = IP; Channel.WinTCPmode = TCP_MODE.SERVER; Channel.Name = "sMBSim_" + port; Channel.Protocol = TMW_PROTOCOL.MB; Channel.OpenChannel(); return Channel.Name; } catch (Exception ex) { MessageBox.Show("Error Opening: " + ex.Message); Channel = null; Close(); return null; } }