修改站号和IP
This commit is contained in:
@@ -23,6 +23,7 @@ using System.Net.Sockets;
|
||||
using Timer = System.Windows.Forms.Timer;
|
||||
using System.Net;
|
||||
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
||||
using Org.BouncyCastle.Utilities.Net;
|
||||
|
||||
namespace C_Windows_1
|
||||
{
|
||||
@@ -47,7 +48,7 @@ namespace C_Windows_1
|
||||
//public ModbusTcpNet modbusClient;
|
||||
public ModbusRtuOverTcp modbusClient /*= new ModbusRtuOverTcp("192.168.1.174", 9999)*/;
|
||||
//产测计数
|
||||
|
||||
// public static Form1 f1;
|
||||
public int ChanSum;
|
||||
public int ChanPass;
|
||||
|
||||
@@ -60,7 +61,7 @@ namespace C_Windows_1
|
||||
private bool isListening = false; // 监听标志位,控制是否继续监听
|
||||
private Timer timeoutTimer; // 定时器用于控制超时
|
||||
|
||||
int connectflag = 0;
|
||||
public int connectflag = 0;
|
||||
public Form1()
|
||||
{
|
||||
f1 = this;
|
||||
@@ -1083,7 +1084,7 @@ namespace C_Windows_1
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
OperateResult<byte[]> connect;
|
||||
|
||||
@@ -1123,12 +1124,11 @@ namespace C_Windows_1
|
||||
udpClient = new UdpClient();
|
||||
udpClient.EnableBroadcast = true;
|
||||
udpClient.Client.ReceiveTimeout = ReceiveTimeout;
|
||||
|
||||
//modbusClient.ReceiveTimeOut = 1000;
|
||||
|
||||
|
||||
|
||||
//modbusClient.ConnectTimeOut = 100;
|
||||
//modbusClient.ReceiveTimeOut = 100;
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1140,38 +1140,38 @@ namespace C_Windows_1
|
||||
workstation = (int)yiqi.standby;
|
||||
while (true)
|
||||
{
|
||||
Thread.Sleep(500);
|
||||
if (connectflag==1)
|
||||
{
|
||||
//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();//仪器显示线程
|
||||
// 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();//仪器显示线程
|
||||
|
||||
//if (MachineStatus.Text == "待机中" && connect.IsSuccess)
|
||||
//{
|
||||
// Thread.Sleep(3000);
|
||||
// workstation = (int)yiqi.start;
|
||||
// //if (MachineStatus.Text == "待机中" && connect.IsSuccess)
|
||||
// //{
|
||||
// // Thread.Sleep(3000);
|
||||
// // workstation = (int)yiqi.start;
|
||||
|
||||
// MachineStatus.Text = "";
|
||||
//}
|
||||
}
|
||||
// // MachineStatus.Text = "";
|
||||
// //}
|
||||
//}
|
||||
|
||||
|
||||
|
||||
@@ -1188,8 +1188,8 @@ namespace C_Windows_1
|
||||
{
|
||||
UDPResults.Items.Clear();
|
||||
byte[] data = Encoding.ASCII.GetBytes("hello,udp server");
|
||||
IPAddress ip;
|
||||
ip = IPAddress.Parse("192.168.1.255");
|
||||
System.Net.IPAddress ip;
|
||||
ip = System.Net.IPAddress.Parse("192.168.1.255");
|
||||
// 广播到 192.168.1.255
|
||||
IPEndPoint endPoint = new IPEndPoint(ip, Port);
|
||||
|
||||
@@ -1214,7 +1214,7 @@ namespace C_Windows_1
|
||||
{
|
||||
if (!isListening) return;
|
||||
|
||||
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, Port);
|
||||
IPEndPoint remoteEndPoint = new IPEndPoint(System.Net.IPAddress.Any, Port);
|
||||
byte[] receivedData = udpClient.EndReceive(ar, ref remoteEndPoint);
|
||||
string receivedMessage = Encoding.ASCII.GetString(receivedData);
|
||||
|
||||
@@ -1242,7 +1242,7 @@ namespace C_Windows_1
|
||||
try
|
||||
{
|
||||
connectflag = 0;
|
||||
if (connect.IsSuccess)
|
||||
//if (connect.IsSuccess)
|
||||
{
|
||||
connectflag = 0;
|
||||
modbusClient.ConnectClose();
|
||||
@@ -1911,13 +1911,13 @@ namespace C_Windows_1
|
||||
//{
|
||||
// MessageBox.Show("条码长度设置错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return;
|
||||
//}
|
||||
//modbusClient.Write("0", true);
|
||||
workstation = (int)yiqi.start;
|
||||
Code.Focus();
|
||||
//workstation = (int)yiqi.start;
|
||||
|
||||
|
||||
modbusClient.Write("0", true);
|
||||
|
||||
//workstation = (int)yiqi.start;
|
||||
|
||||
|
||||
//workstation = (int)yiqi.start;
|
||||
//Code.Focus();
|
||||
|
||||
|
||||
}
|
||||
@@ -2035,9 +2035,10 @@ namespace C_Windows_1
|
||||
{
|
||||
try
|
||||
{
|
||||
workstation = (int)yiqi.rst;
|
||||
//Code.Clear();
|
||||
Code.Focus();
|
||||
//workstation = (int)yiqi.rst;
|
||||
|
||||
//Code.Focus();
|
||||
modbusClient.Write("1", true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -3081,32 +3082,41 @@ namespace C_Windows_1
|
||||
{
|
||||
|
||||
}
|
||||
string stationIP;
|
||||
string ipAddress;
|
||||
|
||||
private void TCPconnect_Click(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
|
||||
//根据:符号分割站号和密码
|
||||
//根据:符号分割站号和IP
|
||||
string[] parts = UDPResults.Text.Split(':');
|
||||
|
||||
// 确保分割结果符合预期
|
||||
if (parts.Length == 2)
|
||||
{
|
||||
// 站号
|
||||
string station = parts[0];
|
||||
stationIP = parts[0];
|
||||
|
||||
// IP 地址
|
||||
string ipAddress = parts[1];
|
||||
ipAddress = parts[1];
|
||||
|
||||
|
||||
modbusClient = new ModbusRtuOverTcp(ipAddress, 9999, Convert.ToByte(station));
|
||||
if(modbusClient.ConnectServer().IsSuccess)
|
||||
modbusClient = new ModbusRtuOverTcp(ipAddress, 9999, Convert.ToByte(stationIP));
|
||||
//modbusClient.ConnectTimeOut = 1000;
|
||||
//modbusClient.ReceiveTimeOut = 1000;
|
||||
if (modbusClient.ConnectServer().IsSuccess)
|
||||
{
|
||||
connectflag = 1;
|
||||
TCPconnect.Enabled = false;
|
||||
PortNum.Text = modbusClient.IpAddress;
|
||||
label6.Text = modbusClient.Port.ToString();
|
||||
label6.ForeColor = Color.Green;
|
||||
station2 = Convert.ToUInt16(stationIP);
|
||||
station = station2.ToString("X2");
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -3123,10 +3133,37 @@ namespace C_Windows_1
|
||||
|
||||
}
|
||||
|
||||
OperateResult changestation=new OperateResult();
|
||||
public static string station;
|
||||
|
||||
public static ushort station2;
|
||||
|
||||
private void button2_Click(object sender, EventArgs e)
|
||||
{
|
||||
ushort station = Convert.ToUInt16(zhanhao.Text);
|
||||
modbusClient.Write("1076", station);
|
||||
station2 = Convert.ToUInt16(zhanhao.Text);
|
||||
modbusClient.Read("0", (ushort)1);
|
||||
changestation =modbusClient.Write("1076", station2);
|
||||
if (changestation.IsSuccess)
|
||||
{
|
||||
|
||||
station = station2.ToString("X2");
|
||||
connectflag = 0;
|
||||
modbusClient.ConnectClose();
|
||||
Thread.Sleep(200);
|
||||
modbusClient = new ModbusRtuOverTcp(ipAddress, 9999, Convert.ToByte(station2));
|
||||
Thread.Sleep(200);
|
||||
if (modbusClient.ConnectServer().IsSuccess)
|
||||
{
|
||||
connectflag = 1;
|
||||
TCPconnect.Enabled = false;
|
||||
PortNum.Text = modbusClient.IpAddress;
|
||||
label6.Text = modbusClient.Port.ToString();
|
||||
UDPResults.Text = station2.ToString() + ":" + modbusClient.IpAddress;
|
||||
label6.ForeColor = Color.Green;
|
||||
|
||||
MessageBox.Show("仪器站号修改为" + station2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void label28_Click(object sender, EventArgs e)
|
||||
|
||||
Reference in New Issue
Block a user