|
@@ -11,6 +11,8 @@ namespace UAS_PLCDataReader.PublicMethod
|
|
|
{
|
|
{
|
|
|
class ModeBusTCPServer
|
|
class ModeBusTCPServer
|
|
|
{
|
|
{
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
private bool isOpen = false;
|
|
private bool isOpen = false;
|
|
|
|
|
|
|
|
private bool receiveData;
|
|
private bool receiveData;
|
|
@@ -106,8 +108,8 @@ namespace UAS_PLCDataReader.PublicMethod
|
|
|
IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
|
|
IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
|
|
|
for (int i = 0; i < IpEntry.AddressList.Length; i++)
|
|
for (int i = 0; i < IpEntry.AddressList.Length; i++)
|
|
|
{
|
|
{
|
|
|
- if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork) {
|
|
|
|
|
- if(IpEntry.AddressList[i].ToString().Contains("192.168.127.20"))
|
|
|
|
|
|
|
+ if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
|
|
|
|
|
+ {
|
|
|
iP = IpEntry.AddressList[i].ToString();
|
|
iP = IpEntry.AddressList[i].ToString();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -121,7 +123,7 @@ namespace UAS_PLCDataReader.PublicMethod
|
|
|
socketWatch.Bind(endpoint);
|
|
socketWatch.Bind(endpoint);
|
|
|
//将套接字的监听队列长度限制为20
|
|
//将套接字的监听队列长度限制为20
|
|
|
socketWatch.Listen(20);
|
|
socketWatch.Listen(20);
|
|
|
- Task.Factory.StartNew(WatchConnecting,TaskCreationOptions.LongRunning);
|
|
|
|
|
|
|
+ Task.Factory.StartNew(WatchConnecting, TaskCreationOptions.LongRunning);
|
|
|
////创建一个监听线程
|
|
////创建一个监听线程
|
|
|
//threadWatch = new Thread(WatchConnecting);
|
|
//threadWatch = new Thread(WatchConnecting);
|
|
|
////将窗体线程设置为与后台同步
|
|
////将窗体线程设置为与后台同步
|
|
@@ -142,6 +144,9 @@ namespace UAS_PLCDataReader.PublicMethod
|
|
|
{
|
|
{
|
|
|
if (!ReceiveCoding.ContainsKey(IPAddress))
|
|
if (!ReceiveCoding.ContainsKey(IPAddress))
|
|
|
ReceiveCoding.Add(IPAddress, receiveCoding);
|
|
ReceiveCoding.Add(IPAddress, receiveCoding);
|
|
|
|
|
+ //去除指令空格
|
|
|
|
|
+ Command = Command.Replace(" ", "");
|
|
|
|
|
+ byte[] arr = new byte[Command.Length / 2];
|
|
|
foreach (Socket item in list)
|
|
foreach (Socket item in list)
|
|
|
{
|
|
{
|
|
|
if (item != null)
|
|
if (item != null)
|
|
@@ -166,17 +171,11 @@ namespace UAS_PLCDataReader.PublicMethod
|
|
|
item.Send(Encoding.ASCII.GetBytes(str));
|
|
item.Send(Encoding.ASCII.GetBytes(str));
|
|
|
break;
|
|
break;
|
|
|
case "Hexadecimal":
|
|
case "Hexadecimal":
|
|
|
- char[] values = Command.ToCharArray();
|
|
|
|
|
- string strH = null;
|
|
|
|
|
- foreach (char letter in values)
|
|
|
|
|
|
|
+ for (int i = 0; i < Command.Length / 2; i++)
|
|
|
{
|
|
{
|
|
|
- // Get the integral value of the character.
|
|
|
|
|
- int value = Convert.ToInt32(letter);
|
|
|
|
|
- // Convert the decimal value to a hexadecimal value in string form.
|
|
|
|
|
- string hexOutput = String.Format("{0:X}", value);
|
|
|
|
|
- strH += hexOutput + " ";
|
|
|
|
|
|
|
+ arr[i] = (byte)Convert.ToInt32(Command.Substring(i * 2, 2), 16);
|
|
|
}
|
|
}
|
|
|
- item.Send(Encoding.UTF8.GetBytes(strH));
|
|
|
|
|
|
|
+ item.Send(arr);
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|