由于嵌入式工程师无法调试光电分离腔的通信,因此将光电分离腔上位机改为服务器,待后续测试后再改为客户端

This commit is contained in:
春风过客 2023-08-04 11:18:55 +08:00
parent 7ea526d7e7
commit bc7ea19a6a
10 changed files with 389 additions and 389 deletions

View File

@ -168,7 +168,7 @@ namespace ZTTMS_Manage_yibayiyi_20230320
btnGetNewInfo.Enabled = true; btnGetNewInfo.Enabled = true;
Others.g_socketSendGDFL.Close(); Others.g_socketSendGDFL.Close();
//Others.g_dicSocketGDFL.Remove(Others.g_ipGDFL); Others.g_dicSocketGDFL.Remove(Others.g_ipGDFL);
PhotoelectricSeparationDataForm.strState = "连接断开"; PhotoelectricSeparationDataForm.strState = "连接断开";
PhotoelectricSeparationDataForm.change(); PhotoelectricSeparationDataForm.change();
@ -331,7 +331,7 @@ namespace ZTTMS_Manage_yibayiyi_20230320
btnGetNewInfo.Enabled = true; btnGetNewInfo.Enabled = true;
Others.g_socketSendGDFL.Close(); Others.g_socketSendGDFL.Close();
//Others.g_dicSocketGDFL.Remove(Others.g_ipGDFL); Others.g_dicSocketGDFL.Remove(Others.g_ipGDFL);
PhotoelectricSeparationDataForm.strState = "连接断开"; PhotoelectricSeparationDataForm.strState = "连接断开";
PhotoelectricSeparationDataForm.change(); PhotoelectricSeparationDataForm.change();

View File

@ -274,7 +274,7 @@ namespace ZTTMS_Manage_yibayiyi_20230320
EveryDayLog.WriteGDFL("光电分离腔请求设备重启失败,错误信息为:" + ex.ToString()); EveryDayLog.WriteGDFL("光电分离腔请求设备重启失败,错误信息为:" + ex.ToString());
EveryDayLog.WriteErrorGDFL("光电分离腔请求设备重启失败,错误信息为:" + ex.ToString()); EveryDayLog.WriteErrorGDFL("光电分离腔请求设备重启失败,错误信息为:" + ex.ToString());
//Others.g_dicSocketGDFL.Remove(Others.g_ipGDFL); Others.g_dicSocketGDFL.Remove(Others.g_ipGDFL);
Others.g_gdflReceive = false; Others.g_gdflReceive = false;
GDFLPortStateForm.strState = "连接断开"; GDFLPortStateForm.strState = "连接断开";

View File

@ -13,11 +13,11 @@ namespace ZTTMS_Manage_yibayiyi_20230320
{ {
class Others class Others
{ {
//public static string g_ipGDFL; public static string g_ipGDFL;
//键值对集合键为IP地址值为Socket //键值对集合键为IP地址值为Socket
//将远程连接的客户端的IP地址和端口号存入集合中 //将远程连接的客户端的IP地址和端口号存入集合中
//public static Dictionary<string, Socket> g_dicSocketGDFL = new Dictionary<string, Socket>(); public static Dictionary<string, Socket> g_dicSocketGDFL = new Dictionary<string, Socket>();
public static Socket g_socketSend; public static Socket g_socketSend;
//public static TcpClient g_socketSend; //public static TcpClient g_socketSend;

View File

@ -58,7 +58,7 @@ namespace ZTTMS_Manage_yibayiyi_20230320
Others.g_gdflReceive = false; Others.g_gdflReceive = false;
g_ip = "41.244.68.61"; //g_ip = "41.244.68.61";
//g_ip = "127.0.0.1"; //g_ip = "127.0.0.1";
g_port = 55000; g_port = 55000;
@ -70,27 +70,27 @@ namespace ZTTMS_Manage_yibayiyi_20230320
try try
{ {
////在服务器端创建一个负责IP地址和端口号的Socket //在服务器端创建一个负责IP地址和端口号的Socket
//g_socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); g_socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
////g_socketWatch.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); //g_socketWatch.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
////string ip3 = "192.168.1.240"; //string ip3 = "192.168.1.240";
//IPAddress ip2 = IPAddress.Any; IPAddress ip2 = IPAddress.Any;
////创建端口号对象 //创建端口号对象
//Console.WriteLine("创建端口号对象"); Console.WriteLine("创建端口号对象");
////IPEndPoint point = new IPEndPoint(IPAddress.Parse(ip3), g_port); //IPEndPoint point = new IPEndPoint(IPAddress.Parse(ip3), g_port);
//IPEndPoint point = new IPEndPoint(ip2, g_port); IPEndPoint point = new IPEndPoint(ip2, g_port);
////监听 //监听
//g_socketWatch.Bind(point); g_socketWatch.Bind(point);
//g_socketWatch.Listen(10); g_socketWatch.Listen(10);
//trdConnect = new Thread(ListenConnect); trdConnect = new Thread(ListenConnect);
//trdConnect.IsBackground = true;
//trdConnect.Start(g_socketWatch);
trdConnect = new Thread(ListenConnectClient);
trdConnect.IsBackground = true; trdConnect.IsBackground = true;
trdConnect.Start(); trdConnect.Start(g_socketWatch);
//trdConnect = new Thread(ListenConnectClient);
//trdConnect.IsBackground = true;
//trdConnect.Start();
Thread trdDataSplit = new Thread(DataSplit); Thread trdDataSplit = new Thread(DataSplit);
trdDataSplit.IsBackground = true; trdDataSplit.IsBackground = true;
@ -497,7 +497,7 @@ namespace ZTTMS_Manage_yibayiyi_20230320
Others.g_gdflReceive = false; Others.g_gdflReceive = false;
Others.g_socketSendGDFL = null; Others.g_socketSendGDFL = null;
Others.g_socketSendGDFL.Close(); Others.g_socketSendGDFL.Close();
//Others.g_dicSocketGDFL.Remove(Others.g_ipGDFL); Others.g_dicSocketGDFL.Remove(Others.g_ipGDFL);
CallInvokeUIUpdate("UpdateState", "连接断开"); CallInvokeUIUpdate("UpdateState", "连接断开");
GDFLPortStateForm.strState = "连接断开"; GDFLPortStateForm.strState = "连接断开";
@ -644,230 +644,230 @@ namespace ZTTMS_Manage_yibayiyi_20230320
#region Connnect #region Connnect
//private void ListenConnect(object o) private void ListenConnect(object o)
//{
// //等待客户端的连接并且创建与之通信用的Socket
// Socket socketWatch = o as Socket;
// while (true)
// {
// Thread.Sleep(1);
// try
// {
// //负责和客户端通信的Socket
// Others.g_socketSendGDFL = socketWatch.Accept();
// Others.g_socketSendGDFL.IOControl(IOControlCode.KeepAliveValues, GetKeepAliveData(), null);
// Others.g_socketSendGDFL.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
// Others.g_ipGDFL = Others.g_socketSendGDFL.RemoteEndPoint.ToString();
// Console.WriteLine(Others.g_ipGDFL);
// if (!Others.g_dicSocketGDFL.ContainsKey(Others.g_ipGDFL))
// {
// //将远程连接的客户端的IP地址和端口号存入集合中
// Others.g_dicSocketGDFL.Add(Others.g_ipGDFL, Others.g_socketSendGDFL);
// Others.g_gdflReceive = true;
// Console.WriteLine("新增远程连接:" + Others.g_ipGDFL + "\r" + DateTime.Now);
// EveryDayLog.WriteGDFL("光电分离腔新增远程连接:" + Others.g_ipGDFL);
// CallInvokeUIUpdate("UpdatelblIP", "IP" + Others.g_ipGDFL);
// CallInvokeUIUpdate("UpdatelblState", "连接成功");
// GDFLPortStateForm.strState = "连接成功";
// GDFLPortStateForm.change();
// List<object> list = new List<object>
// {
// Others.g_ipGDFL,
// Others.g_socketSendGDFL
// };
// //开启一个新线程不停接收客户端的数据
// Thread trdReceive = new Thread(DataReceive);
// trdReceive.IsBackground = true;
// trdReceive.Start(list);
// }
// else
// {
// if (!Others.g_dicSocketGDFL[Others.g_ipGDFL].Connected)
// {
// Console.WriteLine(Others.g_ipGDFL + "连接已断开!");
// try
// {
// Others.g_dicSocketGDFL[Others.g_ipGDFL].Shutdown(SocketShutdown.Both);
// }
// catch
// {
// }
// finally
// {
// Others.g_dicSocketGDFL[Others.g_ipGDFL].Close(100);
// Others.g_dicSocketGDFL[Others.g_ipGDFL].Dispose();
// }
// Others.g_dicSocketGDFL.Remove(Others.g_ipGDFL);
// //将远程连接的客户端的IP地址和端口号存入集合中
// Others.g_dicSocketGDFL.Add(Others.g_ipGDFL, Others.g_socketSendGDFL);
// Others.g_gdflReceive = true;
// Console.WriteLine("新增远程连接:" + Others.g_ipGDFL + "\r" + DateTime.Now);
// CallInvokeUIUpdate("UpdatelblIP", "IP" + Others.g_ipGDFL);
// CallInvokeUIUpdate("UpdatelblState", "连接成功");
// GDFLPortStateForm.strState = "连接成功";
// GDFLPortStateForm.change();
// ////根据1min内接收到的心跳包数量判断连接是否正常
// //timerCheckConnectState.Interval = 60 * 1000;
// //timerCheckConnectState.Enabled = true;
// //timerCheckConnectState.Start();
// //EveryDayLog.WriteGDFL("定时器(判断接收到的心跳包数量是否正常)已开启!");
// ////开启一个新线程不停接收客户端的数据
// //Thread trdReceive = new Thread(DataReceive);
// //trdReceive.IsBackground = true;
// //trdReceive.Start(Others.g_socketSendGDFL);
// List<object> list = new List<object>
// {
// Others.g_ipGDFL,
// Others.g_socketSendGDFL
// };
// //开启一个新线程不停接收客户端的数据
// Thread trdReceive = new Thread(DataReceive);
// trdReceive.IsBackground = true;
// trdReceive.Start(list);
// continue;
// }
// else if (Others.g_dicSocketGDFL[Others.g_ipGDFL].Poll(1000, SelectMode.SelectRead))
// {
// try
// {
// Others.g_dicSocketGDFL[Others.g_ipGDFL].Shutdown(SocketShutdown.Both);
// }
// catch
// {
// }
// finally
// {
// Others.g_dicSocketGDFL[Others.g_ipGDFL].Close(100);
// Others.g_dicSocketGDFL[Others.g_ipGDFL].Dispose();
// }
// Others.g_dicSocketGDFL.Remove(Others.g_ipGDFL);
// Others.g_gdflReceive = false;
// Console.WriteLine("ListenConnectError移除远程连接" + Others.g_ipGDFL + "\r" + DateTime.Now);
// CallInvokeUIUpdate("UpdatelblState", "连接断开");
// GDFLPortStateForm.strState = "连接断开";
// GDFLPortStateForm.change();
// //将远程连接的客户端的IP地址和端口号存入集合中
// Others.g_dicSocketGDFL.Add(Others.g_ipGDFL, Others.g_socketSendGDFL);
// Others.g_gdflReceive = true;
// Console.WriteLine("新增远程连接:" + Others.g_ipGDFL + "\r" + DateTime.Now);
// CallInvokeUIUpdate("UpdatelblIP", "IP" + Others.g_ipGDFL);
// CallInvokeUIUpdate("UpdatelblState", "连接成功");
// GDFLPortStateForm.strState = "连接成功";
// GDFLPortStateForm.change();
// ////根据1min内接收到的心跳包数量判断连接是否正常
// //timerCheckConnectState.Interval = 60 * 1000;
// //timerCheckConnectState.Enabled = true;
// //timerCheckConnectState.Start();
// //EveryDayLog.WriteGDFL("定时器(判断接收到的心跳包数量是否正常)已开启!");
// ////开启一个新线程不停接收客户端的数据
// //Thread trdReceive = new Thread(DataReceive);
// //trdReceive.IsBackground = true;
// //trdReceive.Start(Others.g_socketSendGDFL);
// List<object> list = new List<object>
// {
// Others.g_ipGDFL,
// Others.g_socketSendGDFL
// };
// //开启一个新线程不停接收客户端的数据
// Thread trdReceive = new Thread(DataReceive);
// trdReceive.IsBackground = true;
// trdReceive.Start(list);
// continue;
// }
// }
// }
// catch (Exception ex)
// {
// Console.WriteLine("GDFLListenConnectError" + ex.Message);
// EveryDayLog.WriteErrorGDFL("GDFLListenConnectError" + ex.Message);
// Console.WriteLine(Others.g_ipGDFL + "连接已断开!");
// try
// {
// Others.g_dicSocketGDFL[Others.g_ipGDFL].Shutdown(SocketShutdown.Both);
// }
// catch
// {
// }
// finally
// {
// Others.g_dicSocketGDFL[Others.g_ipGDFL].Close(100);
// Others.g_dicSocketGDFL[Others.g_ipGDFL].Dispose();
// }
// Others.g_dicSocketGDFL.Remove(Others.g_ipGDFL);
// }
// }
//}
private bool Connect(string ip, int port)
{ {
if (Others.g_gdflReceive == true) //等待客户端的连接并且创建与之通信用的Socket
Socket socketWatch = o as Socket;
while (true)
{ {
Console.WriteLine("光电分离腔连接中,无需再连接!"); Thread.Sleep(1);
return true; try
{
//负责和客户端通信的Socket
Others.g_socketSendGDFL = socketWatch.Accept();
Others.g_socketSendGDFL.IOControl(IOControlCode.KeepAliveValues, GetKeepAliveData(), null);
Others.g_socketSendGDFL.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
Others.g_ipGDFL = Others.g_socketSendGDFL.RemoteEndPoint.ToString();
Console.WriteLine(Others.g_ipGDFL);
if (!Others.g_dicSocketGDFL.ContainsKey(Others.g_ipGDFL))
{
//将远程连接的客户端的IP地址和端口号存入集合中
Others.g_dicSocketGDFL.Add(Others.g_ipGDFL, Others.g_socketSendGDFL);
Others.g_gdflReceive = true;
Console.WriteLine("新增远程连接:" + Others.g_ipGDFL + "\r" + DateTime.Now);
EveryDayLog.WriteGDFL("光电分离腔新增远程连接:" + Others.g_ipGDFL);
CallInvokeUIUpdate("UpdatelblIP", "IP" + Others.g_ipGDFL);
CallInvokeUIUpdate("UpdatelblState", "连接成功");
GDFLPortStateForm.strState = "连接成功";
GDFLPortStateForm.change();
List<object> list = new List<object>
{
Others.g_ipGDFL,
Others.g_socketSendGDFL
};
//开启一个新线程不停接收客户端的数据
Thread trdReceive = new Thread(DataReceive);
trdReceive.IsBackground = true;
trdReceive.Start(list);
}
else
{
if (!Others.g_dicSocketGDFL[Others.g_ipGDFL].Connected)
{
Console.WriteLine(Others.g_ipGDFL + "连接已断开!");
try
{
Others.g_dicSocketGDFL[Others.g_ipGDFL].Shutdown(SocketShutdown.Both);
}
catch
{
}
finally
{
Others.g_dicSocketGDFL[Others.g_ipGDFL].Close(100);
Others.g_dicSocketGDFL[Others.g_ipGDFL].Dispose();
}
Others.g_dicSocketGDFL.Remove(Others.g_ipGDFL);
//将远程连接的客户端的IP地址和端口号存入集合中
Others.g_dicSocketGDFL.Add(Others.g_ipGDFL, Others.g_socketSendGDFL);
Others.g_gdflReceive = true;
Console.WriteLine("新增远程连接:" + Others.g_ipGDFL + "\r" + DateTime.Now);
CallInvokeUIUpdate("UpdatelblIP", "IP" + Others.g_ipGDFL);
CallInvokeUIUpdate("UpdatelblState", "连接成功");
GDFLPortStateForm.strState = "连接成功";
GDFLPortStateForm.change();
////根据1min内接收到的心跳包数量判断连接是否正常
//timerCheckConnectState.Interval = 60 * 1000;
//timerCheckConnectState.Enabled = true;
//timerCheckConnectState.Start();
//EveryDayLog.WriteGDFL("定时器(判断接收到的心跳包数量是否正常)已开启!");
////开启一个新线程不停接收客户端的数据
//Thread trdReceive = new Thread(DataReceive);
//trdReceive.IsBackground = true;
//trdReceive.Start(Others.g_socketSendGDFL);
List<object> list = new List<object>
{
Others.g_ipGDFL,
Others.g_socketSendGDFL
};
//开启一个新线程不停接收客户端的数据
Thread trdReceive = new Thread(DataReceive);
trdReceive.IsBackground = true;
trdReceive.Start(list);
continue;
}
else if (Others.g_dicSocketGDFL[Others.g_ipGDFL].Poll(1000, SelectMode.SelectRead))
{
try
{
Others.g_dicSocketGDFL[Others.g_ipGDFL].Shutdown(SocketShutdown.Both);
}
catch
{
}
finally
{
Others.g_dicSocketGDFL[Others.g_ipGDFL].Close(100);
Others.g_dicSocketGDFL[Others.g_ipGDFL].Dispose();
}
Others.g_dicSocketGDFL.Remove(Others.g_ipGDFL);
Others.g_gdflReceive = false;
Console.WriteLine("ListenConnectError移除远程连接" + Others.g_ipGDFL + "\r" + DateTime.Now);
CallInvokeUIUpdate("UpdatelblState", "连接断开");
GDFLPortStateForm.strState = "连接断开";
GDFLPortStateForm.change();
//将远程连接的客户端的IP地址和端口号存入集合中
Others.g_dicSocketGDFL.Add(Others.g_ipGDFL, Others.g_socketSendGDFL);
Others.g_gdflReceive = true;
Console.WriteLine("新增远程连接:" + Others.g_ipGDFL + "\r" + DateTime.Now);
CallInvokeUIUpdate("UpdatelblIP", "IP" + Others.g_ipGDFL);
CallInvokeUIUpdate("UpdatelblState", "连接成功");
GDFLPortStateForm.strState = "连接成功";
GDFLPortStateForm.change();
////根据1min内接收到的心跳包数量判断连接是否正常
//timerCheckConnectState.Interval = 60 * 1000;
//timerCheckConnectState.Enabled = true;
//timerCheckConnectState.Start();
//EveryDayLog.WriteGDFL("定时器(判断接收到的心跳包数量是否正常)已开启!");
////开启一个新线程不停接收客户端的数据
//Thread trdReceive = new Thread(DataReceive);
//trdReceive.IsBackground = true;
//trdReceive.Start(Others.g_socketSendGDFL);
List<object> list = new List<object>
{
Others.g_ipGDFL,
Others.g_socketSendGDFL
};
//开启一个新线程不停接收客户端的数据
Thread trdReceive = new Thread(DataReceive);
trdReceive.IsBackground = true;
trdReceive.Start(list);
continue;
}
}
}
catch (Exception ex)
{
Console.WriteLine("GDFLListenConnectError" + ex.Message);
EveryDayLog.WriteErrorGDFL("GDFLListenConnectError" + ex.Message);
Console.WriteLine(Others.g_ipGDFL + "连接已断开!");
try
{
Others.g_dicSocketGDFL[Others.g_ipGDFL].Shutdown(SocketShutdown.Both);
}
catch
{
}
finally
{
Others.g_dicSocketGDFL[Others.g_ipGDFL].Close(100);
Others.g_dicSocketGDFL[Others.g_ipGDFL].Dispose();
}
Others.g_dicSocketGDFL.Remove(Others.g_ipGDFL);
}
} }
Others.g_socketSendGDFL = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
Others.g_socketSendGDFL.IOControl(IOControlCode.KeepAliveValues, GetKeepAliveData(), null);
Others.g_socketSendGDFL.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
IPEndPoint point = new IPEndPoint(IPAddress.Parse(ip), port);
Others.g_socketSendGDFL.Connect(point);
Others.g_gdflReceive = true;
}
catch (Exception ex)
{
Console.WriteLine("光电分离腔服务器未开启:" + ex.Message);
EveryDayLog.WriteGDFL("光电分离腔服务器未开启:" + ex.Message);
waitReConnect = false;
lblConnectState.Text = "服务器未开启";
Others.g_gdflReceive = false;
return false;
}
Console.WriteLine("光电分离腔已连接服务器!");
EveryDayLog.WriteGDFL("光电分离腔已连接服务器!");
waitReConnect = true;
Others.g_gdflReceive = true;
return true;
} }
//private bool Connect(string ip, int port)
//{
// if (Others.g_gdflReceive == true)
// {
// Console.WriteLine("光电分离腔连接中,无需再连接!");
// return true;
// }
// Others.g_socketSendGDFL = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// try
// {
// Others.g_socketSendGDFL.IOControl(IOControlCode.KeepAliveValues, GetKeepAliveData(), null);
// Others.g_socketSendGDFL.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
// IPEndPoint point = new IPEndPoint(IPAddress.Parse(ip), port);
// Others.g_socketSendGDFL.Connect(point);
// Others.g_gdflReceive = true;
// }
// catch (Exception ex)
// {
// Console.WriteLine("光电分离腔服务器未开启:" + ex.Message);
// EveryDayLog.WriteGDFL("光电分离腔服务器未开启:" + ex.Message);
// waitReConnect = false;
// lblConnectState.Text = "服务器未开启";
// Others.g_gdflReceive = false;
// return false;
// }
// Console.WriteLine("光电分离腔已连接服务器!");
// EveryDayLog.WriteGDFL("光电分离腔已连接服务器!");
// waitReConnect = true;
// Others.g_gdflReceive = true;
// return true;
//}
private byte[] GetKeepAliveData() private byte[] GetKeepAliveData()
{ {
uint dummy = 0; uint dummy = 0;
@ -878,166 +878,166 @@ namespace ZTTMS_Manage_yibayiyi_20230320
return inOptionValues; return inOptionValues;
} }
private bool WaitReConnect(string ip, int port) //private bool WaitReConnect(string ip, int port)
{ //{
waitReConnect = false; // waitReConnect = false;
Console.WriteLine("光电分离腔开始进行三次握手!"); // Console.WriteLine("光电分离腔开始进行三次握手!");
try // try
{ // {
//进行三次握手 // //进行三次握手
for (int i = 0; i < 3; i++) // for (int i = 0; i < 3; i++)
{ // {
waitReConnect = Connect(ip, port); // waitReConnect = Connect(ip, port);
DateTime time = DateTime.Now; // DateTime time = DateTime.Now;
while ((DateTime.Now - time).TotalSeconds < 5 && !waitReConnect) // while ((DateTime.Now - time).TotalSeconds < 5 && !waitReConnect)
{ // {
Thread.Sleep(1); // Thread.Sleep(1);
} // }
if (waitReConnect == true) // if (waitReConnect == true)
{ // {
Console.WriteLine("连接成功!"); // Console.WriteLine("连接成功!");
Others.g_gdflReceive = true; // Others.g_gdflReceive = true;
lblConnectState.Text = "连接成功"; // lblConnectState.Text = "连接成功";
return true; // return true;
} // }
if (Others.g_gdflReceive == true) // if (Others.g_gdflReceive == true)
{ // {
Console.WriteLine("连接中,无需再连接!"); // Console.WriteLine("连接中,无需再连接!");
return true; // return true;
} // }
Console.WriteLine(i + 1 + "超时5s重连"); // Console.WriteLine(i + 1 + "超时5s重连");
lblConnectState.Text = "超时重连"; // lblConnectState.Text = "超时重连";
Others.g_gdflReceive = false; // Others.g_gdflReceive = false;
Others.g_socketSendGDFL = null; // Others.g_socketSendGDFL = null;
} // }
Console.WriteLine("光电分离腔三次都没有超时重连成功!"); // Console.WriteLine("光电分离腔三次都没有超时重连成功!");
lblConnectState.Text = "超时重连3次失败"; // lblConnectState.Text = "超时重连3次失败";
Others.g_gdflReceive = false; // Others.g_gdflReceive = false;
string message = "无法与光电分离腔建立连接"; // string message = "无法与光电分离腔建立连接";
reConnectTime++; // reConnectTime++;
if (reConnectTime >= 5) // if (reConnectTime >= 5)
{ // {
reConnectTime = 0; // reConnectTime = 0;
g_sqlRecordTime = DateTime.Now; // g_sqlRecordTime = DateTime.Now;
int cycleTime = 0; // int cycleTime = 0;
while (cycleTime < 3) // while (cycleTime < 3)
{ // {
string sqlId = IdHelper.GetId(); // string sqlId = IdHelper.GetId();
int num = 0; // int num = 0;
string sql = $"insert into tb_gdflstateinfo_data(id,record_time,equipment_type,equipment_id,version_number,error_code) " + // string sql = $"insert into tb_gdflstateinfo_data(id,record_time,equipment_type,equipment_id,version_number,error_code) " +
$"values('{sqlId}','{g_sqlRecordTime}','{g_sqlTerminalEquipmentType}','{g_sqlTerminalEquipmentId}','{g_sqlVersionNumber}','{message}');"; // $"values('{sqlId}','{g_sqlRecordTime}','{g_sqlTerminalEquipmentType}','{g_sqlTerminalEquipmentId}','{g_sqlVersionNumber}','{message}');";
try // try
{ // {
num = MySQL.ExecuteSql(sql); // num = MySQL.ExecuteSql(sql);
} // }
catch (Exception ex) // catch (Exception ex)
{ // {
Console.WriteLine("LinkStateError:" + ex.ToString()); // Console.WriteLine("LinkStateError:" + ex.ToString());
EveryDayLog.WriteErrorGDFL("光电分离腔存储连接状态错误:" + ex.Message); // EveryDayLog.WriteErrorGDFL("光电分离腔存储连接状态错误:" + ex.Message);
} // }
if (num > 0) // if (num > 0)
{ // {
Console.WriteLine("光电分离腔连接状态插入成功!" + DateTime.Now); // Console.WriteLine("光电分离腔连接状态插入成功!" + DateTime.Now);
break; // break;
} // }
else // else
{ // {
Console.WriteLine("光电分离腔连接状态插入失败,重新进行插入!" + DateTime.Now); // Console.WriteLine("光电分离腔连接状态插入失败,重新进行插入!" + DateTime.Now);
EveryDayLog.WriteErrorGDFL("光电分离腔连接状态插入失败,重新进行插入!"); // EveryDayLog.WriteErrorGDFL("光电分离腔连接状态插入失败,重新进行插入!");
} // }
cycleTime++; // cycleTime++;
Thread.Sleep(1); // Thread.Sleep(1);
} // }
} // }
} // }
catch (Exception ex) // catch (Exception ex)
{ // {
Console.WriteLine(ex.Message); // Console.WriteLine(ex.Message);
EveryDayLog.WriteErrorGDFL("WaitReConnectError:" + ex.Message); // EveryDayLog.WriteErrorGDFL("WaitReConnectError:" + ex.Message);
} // }
return false; // return false;
} //}
public void ConnectDispose() //public void ConnectDispose()
{ //{
if (Others.g_socketSendGDFL == null) // if (Others.g_socketSendGDFL == null)
{ // {
EveryDayLog.WriteGDFL("光电分离腔的通信连接已断开!"); // EveryDayLog.WriteGDFL("光电分离腔的通信连接已断开!");
return; // return;
} // }
if (Others.g_socketSendGDFL.Connected) // if (Others.g_socketSendGDFL.Connected)
{ // {
Others.g_socketSendGDFL.Close(); // Others.g_socketSendGDFL.Close();
Others.g_socketSendGDFL.Dispose(); // Others.g_socketSendGDFL.Dispose();
Others.g_gdflReceive = false; // Others.g_gdflReceive = false;
Others.g_socketSendGDFL = null; // Others.g_socketSendGDFL = null;
} // }
EveryDayLog.WriteGDFL("光电分离腔的通信连接已断开!"); // EveryDayLog.WriteGDFL("光电分离腔的通信连接已断开!");
} //}
private void ListenConnectClient() //private void ListenConnectClient()
{ //{
while (true) // while (true)
{ // {
if (Others.g_gdflReceive == false || Others.g_socketSendGDFL == null || !Others.g_socketSendGDFL.Connected) // if (Others.g_gdflReceive == false || Others.g_socketSendGDFL == null || !Others.g_socketSendGDFL.Connected)
{ // {
Others.g_gdflReceive = false; // Others.g_gdflReceive = false;
bool result = WaitReConnect(g_ip, g_port); // bool result = WaitReConnect(g_ip, g_port);
Console.WriteLine("光电分离腔连接情况:" + result); // Console.WriteLine("光电分离腔连接情况:" + result);
if (result) // if (result)
{ // {
Others.g_gdflReceive = true; // Others.g_gdflReceive = true;
CallInvokeUIUpdate("UpdatelblIP", "IP" + g_ip); // CallInvokeUIUpdate("UpdatelblIP", "IP" + g_ip);
CallInvokeUIUpdate("UpdatelblState", "连接成功"); // CallInvokeUIUpdate("UpdatelblState", "连接成功");
//timerCheckConnectState.Enabled = true; // //timerCheckConnectState.Enabled = true;
//timerCheckConnectState.Interval = 60 * 1000; // //timerCheckConnectState.Interval = 60 * 1000;
//timerCheckConnectState.Stop(); // //timerCheckConnectState.Stop();
//开启一个新线程不停接收客户端的数据 // //开启一个新线程不停接收客户端的数据
Thread trdReceive = new Thread(DataReceive); // Thread trdReceive = new Thread(DataReceive);
trdReceive.IsBackground = true; // trdReceive.IsBackground = true;
trdReceive.Start(Others.g_socketSendGDFL); // trdReceive.Start(Others.g_socketSendGDFL);
} // }
else // else
{ // {
CallInvokeUIUpdate("UpdatelblState", "连接失败"); // CallInvokeUIUpdate("UpdatelblState", "连接失败");
Others.g_gdflReceive = false; // Others.g_gdflReceive = false;
} // }
EveryDayLog.WriteGDFL("光电分离腔连接,连接状态:" + lblConnectState.Text); // EveryDayLog.WriteGDFL("光电分离腔连接,连接状态:" + lblConnectState.Text);
} // }
else // else
{ // {
Thread.Sleep(1); // Thread.Sleep(1);
} // }
} // }
} //}
#endregion #endregion
@ -1140,22 +1140,22 @@ namespace ZTTMS_Manage_yibayiyi_20230320
Console.WriteLine("GDFLReceiveError" + ex.Message); Console.WriteLine("GDFLReceiveError" + ex.Message);
EveryDayLog.WriteErrorGDFL("GDFLReceiveError" + ex.Message); EveryDayLog.WriteErrorGDFL("GDFLReceiveError" + ex.Message);
//try try
//{ {
// Others.g_dicSocketGDFL[Others.g_ipGDFL].Shutdown(SocketShutdown.Both); Others.g_dicSocketGDFL[Others.g_ipGDFL].Shutdown(SocketShutdown.Both);
//} }
//catch catch
//{ {
//} }
//finally finally
//{ {
// Others.g_dicSocketGDFL[Others.g_ipGDFL].Close(100); Others.g_dicSocketGDFL[Others.g_ipGDFL].Close(100);
// Others.g_dicSocketGDFL[Others.g_ipGDFL].Dispose(); Others.g_dicSocketGDFL[Others.g_ipGDFL].Dispose();
//} }
//Console.WriteLine(Others.g_ipGDFL + "连接已断开!"); Console.WriteLine(Others.g_ipGDFL + "连接已断开!");
//Others.g_dicSocketGDFL.Remove(Others.g_ipGDFL); Others.g_dicSocketGDFL.Remove(Others.g_ipGDFL);
Others.g_gdflReceive = false; Others.g_gdflReceive = false;
Others.g_socketSendGDFL = null; Others.g_socketSendGDFL = null;
@ -1165,7 +1165,7 @@ namespace ZTTMS_Manage_yibayiyi_20230320
GDFLPortStateForm.strState = "连接断开"; GDFLPortStateForm.strState = "连接断开";
GDFLPortStateForm.change(); GDFLPortStateForm.change();
ConnectDispose(); //ConnectDispose();
} }
} }
} }