private void button2_Click(object sender, EventArgs e) { // 断开连接 busTcpClient.ConnectClose( ); button2.Enabled = false; button1.Enabled = true; panel2.Enabled = false; }
private void button1_Click(object sender, EventArgs e) { // 连接 if (!System.Net.IPAddress.TryParse(textBox1.Text, out System.Net.IPAddress address)) { MessageBox.Show(DemoUtils.IpAddressInputWrong); return; } if (!int.TryParse(textBox2.Text, out int port)) { MessageBox.Show(DemoUtils.PortInputWrong); return; } if (!byte.TryParse(textBox15.Text, out byte station)) { MessageBox.Show("Station input is wrong!"); return; } busTcpClient?.ConnectClose( ); busTcpClient = new ModbusRtuOverTcp(textBox1.Text, port, station); busTcpClient.AddressStartWithZero = checkBox1.Checked; ComboBox1_SelectedIndexChanged(null, new EventArgs( )); // 设置数据服务 busTcpClient.IsStringReverse = checkBox3.Checked; try { OperateResult connect = busTcpClient.ConnectServer( ); if (connect.IsSuccess) { MessageBox.Show(HslCommunication.StringResources.Language.ConnectedSuccess); button2.Enabled = true; button1.Enabled = false; panel2.Enabled = true; userControlCurve1.ReadWriteNet = busTcpClient; } else { MessageBox.Show(HslCommunication.StringResources.Language.ConnectedFailed + connect.Message); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public static void ConnectServer() { //初始化 Tools.WriteStartLog(); ClientInfo.RequestTime = DateTime.Now; ClientInfo.LinkId = -1; ClientInfo.cache = MemoryCache.Default; var policy = new CacheItemPolicy { AbsoluteExpiration = DateTime.Now.AddMinutes(10) }; ClientInfo.cache.Set("one", "", policy); ClientInfo.cache.Set("many", "", policy); ServiceStackHelper.Patch(); #region Hsl注册 if (!HslCommunication.Authorization.SetAuthorizationCode("92db0877-b41e-4445-a594-7be68e32a5ee")) { Console.WriteLine("Authorization failed! The current program can only be used for 8 hours!"); return; } #endregion //Console.CancelKeyPress += Console_CancelKeyPress; //一个大坑,没有这个前面的appSettings出不来 var assembliesWithServices = new Assembly[1]; assembliesWithServices[0] = typeof(AppHost).Assembly; var appHost = new AppHost("AppHost", assembliesWithServices); IAppSettings appSettings = new AppSettings(); var localIp = appSettings.Get <string>("Modbus.LocalIP"); var tcpPortMany = appSettings.Get <int>("Modbus.TcpPort.Many"); var tcpPortOne = appSettings.Get <int>("Modbus.TcpPort.One"); var modbusTimeout = appSettings.Get <int>("Modbus.Timeout", 3000); var groupName = appSettings.Get <string>("Modbus.GroupName"); var jobListRedisConfig = appSettings.Get <RedisEndpoint>("Redis.ModbusJobList"); var jobListTimeOut = appSettings.Get <int>("JobList.Timeout"); useLinkId = appSettings.Get <bool>("Modbus.UserLinkId"); var isDebug = appSettings.Get <bool>("Modbus.IsDebug"); var app = appSettings.Get <string>("AppCMD"); var isUseStrictCheckMode = appSettings.Get <string>("Modbus.IsUseStrictCheckMode"); var appRunTime = appSettings.Get <string>("AppRunTime"); jobListRedisConfig = Tools.GetRedisEndpoint(jobListRedisConfig); JsConfig.DateHandler = DateHandler.ISO8601DateTime; JsConfig.IncludeNullValues = true; Console.WriteLine("GroupName : " + groupName); Console.WriteLine("Modbus.LocalIP : " + localIp); Console.WriteLine("Modbus.TcpPort : " + $"{tcpPortOne}<->{tcpPortMany}"); Console.WriteLine("Modbus.Timeout : " + modbusTimeout + " 毫秒"); Console.WriteLine("Redis.Host : " + jobListRedisConfig.Host); Console.WriteLine("JobList.Timeout : " + jobListTimeOut + " 秒"); Console.WriteLine("Modbus.UserLinkId : " + useLinkId); Console.WriteLine("Modbus.IsDebug : " + isDebug); Console.WriteLine("Modbus.IsUseStrictCheckMode: " + isUseStrictCheckMode); "程序开始执行".Info(); redisClient = new RedisClient(jobListRedisConfig); var bridge = new Bridge.TcpBridge(localIp, tcpPortOne, tcpPortMany); var time = appRunTime.Split(','); var hour = int.Parse(time[0]); var minute = int.Parse(time[1]); var second = int.Parse(time[2]); long endTime = DateTime.Now.Add(new TimeSpan(hour, minute, second)).Ticks; try { client = new ModbusRtuOverTcp(localIp, tcpPortOne) { ReceiveTimeOut = modbusTimeout }; //断开长连接,转为短连接 client.ConnectClose(); //client.ConnectServer(); } catch (Exception ex) { Console.WriteLine("Task.Run[0-0]:" + ex.Message); Console.WriteLine("Task.Run[0-0]:" + ex.StackTrace); } }