增加连接时读取版本号功能,修改上位机搜索逻辑由固定192.68.1频段广播变为根据本地IP频段进行广播

This commit is contained in:
zhouyunhao
2024-08-16 17:06:38 +08:00
parent 535d80ffc1
commit 901d626629
6 changed files with 135 additions and 64 deletions

View File

@@ -57,7 +57,7 @@ namespace C_Windows_1
private UdpClient udpClient;
private const int Port = 9999; // 监听端口号
private const int ReceiveTimeout = 5000; // 超时时间(毫秒)
private const int ReceiveTimeout = 3000; // 超时时间(毫秒)
private bool isListening = false; // 监听标志位,控制是否继续监听
private Timer timeoutTimer; // 定时器用于控制超时
@@ -301,7 +301,7 @@ namespace C_Windows_1
int i = 0;
int progressvalue = 0;
public void ModbusRtuint(ModbusRtu X, byte station, string COM, int Bund, int numberbit, StopBits stopbit, Parity parity)
{
@@ -399,21 +399,21 @@ namespace C_Windows_1
case (int)yiqi.test://读取检测数据以及判断仪器是否结束
//operateResult21=Modbusread(170, 23, ReadData);
Booltomath(2, ReadData);
progressBar2.Value = i;
i++;
progressBar2.Value = progressvalue;
progressvalue += 2;
Modbusread(1013, 16, ReadData);//各项参数
Modbusread(1034, 26, ReadData);
//Modbusread(25, 24, ReadData);
//test(ReadData);
//int ba = progressBar2.Value + 2;
//if (ba >= progressBar2.Maximum)
// ba = progressBar2.Maximum;
//progressBar2.Value = ba;
if (progressvalue >= progressBar2.Maximum)
progressvalue = progressBar2.Maximum;
progressBar2.Value = progressvalue;
//if (!(grovalue > 99.8))
//{
// grovalue += grovalueone;
// progressBar2.Value = (int)grovalue;
//}
//else
@@ -421,7 +421,7 @@ namespace C_Windows_1
// progressBar2.Maximum = (int)grovalue;
//}
if (ReadData[2] == 0 && (ReadData[1041] == 1 || ReadData[1041] == 2))//当仪器状态位不为0且有结果时
{
workstation = (int)yiqi.finish;
@@ -467,13 +467,14 @@ namespace C_Windows_1
timerbar.Stop();
DataGridView1.Invoke(new System.Action(Display));
CreateFile();
Passzong.Text = ChanPass.ToString();
Sunzong.Text = ChanSum.ToString();
string path = System.Environment.CurrentDirectory + "\\Config\\Config.ini";
ConfigINI config = new ConfigINI(path);
config.IniWriteValue("Config", "Passzong", Passzong.Text);
config.IniWriteValue("Config", "Sunzong", Sunzong.Text);
//Passzong.Text = ChanPass.ToString();
//Sunzong.Text = ChanSum.ToString();
//string path = System.Environment.CurrentDirectory + "\\Config\\Config.ini";
//ConfigINI config = new ConfigINI(path);
//config.IniWriteValue("Config", "Passzong", Passzong.Text);
//config.IniWriteValue("Config", "Sunzong", Sunzong.Text);
Code.Clear();
progressvalue = 0;
workstation = (int)yiqi.standby;
@@ -1011,15 +1012,15 @@ namespace C_Windows_1
if (ReadData[1041] == 2)
{
ChanSum++;
//ChanSum++;
Tlight.Text = "NG";
Tlight.ForeColor = Color.Red;
}
if (ReadData[1041] == 1)
{
ChanPass++;
ChanSum++;
//ChanPass++;
//ChanSum++;
Tlight.Text = "OK";
Tlight.ForeColor = Color.Green;
}
@@ -1141,42 +1142,42 @@ namespace C_Windows_1
workstation = (int)yiqi.standby;
while (true)
{
//Thread.Sleep(500);
//if (connectflag==1)
//{
// connect = modbusClient.Read("1006", 1);//判断连接是否正常
// if (connect.IsSuccess)
// {
// TCPconnect.Enabled = false;
// PortNum.Text = modbusClient.IpAddress;
// label6.Text = modbusClient.Port.ToString();
// label6.ForeColor = Color.Green;
// //Code.Focus();
// }
// else
// {
// connectflag = 0;
// TCPconnect.Enabled = true;
// PortNum.Text = "";
// label6.Text = "未连接";
// label6.ForeColor = Color.Red;
// }
// machine2();//仪器读取线程
// MachineDisplay();//仪器显示线程
Thread.Sleep(500);
if (connectflag == 1)
{
// //if (MachineStatus.Text == "待机中" && connect.IsSuccess)
// //{
// // Thread.Sleep(3000);
// // workstation = (int)yiqi.start;
connect = modbusClient.Read("1006", 1);//判断连接是否正常
if (connect.IsSuccess)
{
TCPconnect.Enabled = false;
PortNum.Text = modbusClient.IpAddress;
label6.Text = modbusClient.Port.ToString();
label6.ForeColor = Color.Green;
//Code.Focus();
}
else
{
connectflag = 0;
TCPconnect.Enabled = true;
PortNum.Text = "";
label6.Text = "未连接";
label6.ForeColor = Color.Red;
}
machine2();//仪器读取线程
MachineDisplay();//仪器显示线程
// // MachineStatus.Text = "";
// //}
//}
// //if (MachineStatus.Text == "待机中" && connect.IsSuccess)
// //{
// // Thread.Sleep(3000);
// // workstation = (int)yiqi.start;
// // MachineStatus.Text = "";
// //}
}
}
}
}
);
@@ -1184,14 +1185,30 @@ namespace C_Windows_1
}
public string GetLocalIPAddress()
{
string localIP = "";
foreach (var addr in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (addr.AddressFamily == AddressFamily.InterNetwork) // 选择 IPv4 地址
{
localIP = addr.ToString();
break;
}
}
return localIP;
}
//点击“连接串口”按钮,连接串口
private void BtnCon1_Click(object sender, EventArgs e)
{
UDPResults.Items.Clear();
byte[] data = Encoding.ASCII.GetBytes("hello,udp server");
System.Net.IPAddress ip;
ip = System.Net.IPAddress.Parse("192.168.1.255");
// 广播到 192.168.1.255
string localip=GetLocalIPAddress();
localip = localip.Substring(0, 10) + "255";
ip = System.Net.IPAddress.Parse(localip);
// 获本机IP频段广播到 .255
IPEndPoint endPoint = new IPEndPoint(ip, Port);
try
@@ -3115,6 +3132,9 @@ namespace C_Windows_1
label6.ForeColor = Color.Green;
station2 = Convert.ToUInt16(stationIP);
station = station2.ToString("X2");
Modbusread(360, 4, ReadData);
labelruanjian.Text = "软件版本号:" + ReadData[360].ToString("X4") + ReadData[361].ToString("X4");
labelyinjian.Text = "硬件版本号:" + ReadData[362].ToString("X4") + ReadData[363].ToString("X4");
}