用c#写新建的窗体程序在vs里面可以打开运行 点击debug里面的.exe文件就不能运行 只有这次不行以前都可以
后来试了一下吧 下面这行注释了 双击.exe文件就可以运行 这什么情况呀private ModbusTcpNet busTcpClient = new ModbusTcpNet("192.168.1.200");
我特意又写了个新的 只写这一行代码 双击.exe文件都无法运行 也不报错。
完整测试代码如下
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HslCommunication;
using HslCommunication.ModBus;
using System.Threading;
namespace 可能是modbustcp
{
public partial class Form1 : Form
{
private ModbusTcpNet busTcpClient = new ModbusTcpNet("192.168.1.200", 502, 1);
public Form1()
{
InitializeComponent();
this.WindowState = FormWindowState.Maximized;//最大化窗体
quanping1(); //控件随窗体全屏显示
}
private void Form1_Load(object sender, EventArgs e)
{
busTcpClient.ConnectServer();//切换为长链接模式
//后台线程一直读
Thread thread = new Thread(jiankong);
thread.IsBackground = true;
thread.Start();
}
private void jiankong()
{
while (true)//一直读
{
bool coil100 = busTcpClient.ReadCoil("2048").Content; //2048=2049(m0的modbus地址)-1
if (coil100)
{
button1.BackColor = Color.Red;
}
else
{
button1.BackColor = Color.Black;
}
}
}
//记录所以控件信息
public void quanping1()
{
int count = this.Controls.Count * 2 + 2;
float[] factor = new float[count];
int i = 0;
factor[i++] = Size.Width;
factor[i++] = Size.Height;
foreach (Control ctrl in this.Controls)
{
factor[i++] = ctrl.Location.X / (float)Size.Width;
factor[i++] = ctrl.Location.Y / (float)Size.Height;
ctrl.Tag = ctrl.Size;//!!!
}
Tag = factor;
}
//同步改变控件信息
public void quanping2()
{
if (Tag != null)
{
float[] scale = (float[])Tag;
int i = 2;
foreach (Control ctrl in this.Controls)
{
ctrl.Left = (int)(Size.Width * scale[i++]);
ctrl.Top = (int)(Size.Height * scale[i++]);
ctrl.Width = (int)(Size.Width / (float)scale[0] * ((Size)ctrl.Tag).Width);
ctrl.Height = (int)(Size.Height / (float)scale[1] * ((Size)ctrl.Tag).Height);
}
}
}
private void Form1_Resize(object sender, EventArgs e)
{
//控件随窗体全屏显示
quanping2();
}
}
}
[此贴子已经被作者于2022-4-10 22:26编辑过]