|
@@ -42,47 +42,55 @@ namespace OmronFinsTCP.Net
|
|
|
/// <returns></returns>
|
|
|
public short Link(string rIP, short rPort, short timeOut = 3000)
|
|
|
{
|
|
|
- if(BasicClass.PingCheck(rIP,timeOut))
|
|
|
+ try
|
|
|
{
|
|
|
- if(BasicClass.Client.Client==null)
|
|
|
+ if (BasicClass.PingCheck(rIP, timeOut))
|
|
|
{
|
|
|
- BasicClass.Client = new TcpClient();
|
|
|
- }
|
|
|
- BasicClass.Client.Connect(rIP, (int)rPort);
|
|
|
- BasicClass.Stream = BasicClass.Client.GetStream();
|
|
|
- Thread.Sleep(10);
|
|
|
+ if (BasicClass.Client.Client == null)
|
|
|
+ {
|
|
|
+ BasicClass.Client = new TcpClient();
|
|
|
+ }
|
|
|
+ BasicClass.Client.Connect(rIP, (int)rPort);
|
|
|
+ BasicClass.Stream = BasicClass.Client.GetStream();
|
|
|
+ Thread.Sleep(10);
|
|
|
|
|
|
- if (BasicClass.SendData(FinsClass.HandShake()) != 0)
|
|
|
- {
|
|
|
- return -1;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- //开始读取返回信号
|
|
|
- byte[] buffer = new byte[24];
|
|
|
- if (BasicClass.ReceiveData(buffer) != 0)
|
|
|
+ if (BasicClass.SendData(FinsClass.HandShake()) != 0)
|
|
|
{
|
|
|
return -1;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- if (buffer[15] != 0)//TODO:这里的15号是不是ERR信息暂时不能完全肯定
|
|
|
+ //开始读取返回信号
|
|
|
+ byte[] buffer = new byte[24];
|
|
|
+ if (BasicClass.ReceiveData(buffer) != 0)
|
|
|
+ {
|
|
|
return -1;
|
|
|
+ }
|
|
|
else
|
|
|
{
|
|
|
- BasicClass.pcNode = buffer[19];
|
|
|
- BasicClass.plcNode = buffer[23];
|
|
|
- return 0;
|
|
|
+ if (buffer[15] != 0)//TODO:这里的15号是不是ERR信息暂时不能完全肯定
|
|
|
+ return -1;
|
|
|
+ else
|
|
|
+ {
|
|
|
+ BasicClass.pcNode = buffer[19];
|
|
|
+ BasicClass.plcNode = buffer[23];
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ else
|
|
|
+ {
|
|
|
+ BasicClass.Client?.Close();
|
|
|
+ //连接超时
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
}
|
|
|
- else
|
|
|
+ catch(Exception ex)
|
|
|
{
|
|
|
- BasicClass.Client?.Close();
|
|
|
- //连接超时
|
|
|
return -1;
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/// <summary>
|