DeviceSocket.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace IMCS.CCS.DeviceProtocol
  8. {
  9. public class DeviceSocket
  10. {
  11. private Socket ClientSocket;
  12. private string ip;
  13. int port;
  14. public DeviceSocket(string _ip, int _port)
  15. {
  16. ip = _ip;
  17. port = _port;
  18. }
  19. private bool _ison = false;
  20. public bool IsOn
  21. {
  22. get { return _ison; }
  23. set
  24. {
  25. }
  26. }
  27. public void DeviceInit()
  28. {
  29. try
  30. {
  31. ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  32. ClientSocket.Connect(ip, port);
  33. }
  34. catch (Exception ex)
  35. {
  36. IMCS.CCS.Log.Instance.WriteLogAdd($"53-->{ex.Message}");
  37. //IsOn = true;
  38. IsOn = false;
  39. }
  40. // byte[] bt = Encoding.UTF8.GetBytes("Hello");
  41. // ClientSocket.Send(bt);
  42. }
  43. public void DeviceState()
  44. {
  45. System.Timers.Timer timer = new System.Timers.Timer();
  46. timer.Elapsed += Timer_Elapsed;
  47. timer.Interval = 3000;
  48. timer.Start();
  49. }
  50. private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  51. {
  52. try
  53. {
  54. // if (ClientSocket != null && ClientSocket.Connected)
  55. {
  56. // ClientSocket.Send(Encoding.UTF8.GetBytes("\r"));
  57. IsOn = true;
  58. }
  59. }
  60. catch (Exception ex)
  61. {
  62. IMCS.CCS.Log.Instance.WriteLogAdd($"85-->{ex.Message}");
  63. IsOn = false;
  64. //ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  65. //ClientSocket.Connect("192.168.0.11", 1100);
  66. //if (!ClientSocket.Connected)
  67. {
  68. ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  69. try
  70. {
  71. ClientSocket.Connect(ip, port);
  72. }
  73. catch
  74. {
  75. Log.Instance.WriteLogAdd($"{ip}-重新连接失败");
  76. }
  77. }
  78. }
  79. finally
  80. { }
  81. }
  82. public void ReceiveSocket()
  83. {
  84. byte[] by = new byte[1024];
  85. while (true)
  86. {
  87. if (ClientSocket != null) /*&& IsOn*/
  88. {
  89. int leng = 0;
  90. try
  91. {
  92. leng = ClientSocket.Receive(by);
  93. }
  94. catch (Exception ex)
  95. {
  96. IMCS.CCS.Log.Instance.WriteLogAdd($"123-->{ex.Message}");
  97. }
  98. }
  99. }
  100. }
  101. public void DeviceStart()
  102. {
  103. Task.Factory.StartNew(() =>
  104. {
  105. while (true)
  106. {
  107. byte[] by = new byte[1024];
  108. if (ClientSocket != null) /*&& IsOn*/
  109. {
  110. int leng = 0;
  111. try
  112. {
  113. leng = ClientSocket.Receive(by);
  114. }
  115. catch (Exception ex)
  116. {
  117. IMCS.CCS.Log.Instance.WriteLogAdd($"154-->{ex.Message}");
  118. }
  119. }
  120. }
  121. });
  122. }
  123. public void DeviceSend(object ob)
  124. {
  125. string SendValue;
  126. try
  127. {
  128. SendValue = ob.ToString();
  129. if (ClientSocket.Connected)
  130. {
  131. ClientSocket.Close();
  132. this.DeviceInit();
  133. }
  134. ClientSocket.Send(Encoding.ASCII.GetBytes(ob.ToString()));
  135. }
  136. catch (Exception ex)
  137. {
  138. this.DeviceInit();
  139. System.Threading.Thread.Sleep(1000);
  140. SendValue = ob.ToString();
  141. ClientSocket.Send(Encoding.ASCII.GetBytes(ob.ToString()));
  142. IMCS.CCS.Log.Instance.WriteLogAdd($"179-->{ex.Message}");
  143. }
  144. }
  145. }
  146. }