FrmCommunication.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace HRT_Measure
  11. {
  12. public partial class FrmCommunication : Form
  13. {
  14. public FrmCommunication()
  15. {
  16. InitializeComponent();
  17. }
  18. private void FrmCommunication_Load(object sender, EventArgs e)
  19. {
  20. try
  21. {
  22. foreach (var item in ProgramHelp.Instance.dictCommPLC)
  23. {
  24. cobPLC.Items.Add(item.Key);
  25. }
  26. if(cobPLC.Items.Count > 0) cobPLC.SelectedIndex = 0;
  27. rabPLCDataType.SelectedIndex = 0;
  28. foreach (var item in ProgramHelp.Instance.dictClient)
  29. {
  30. cobTcpClient.Items.Add(item.Key);
  31. }
  32. if(cobTcpClient.Items.Count > 0) cobTcpClient.SelectedIndex = 0;
  33. foreach (var item in ProgramHelp.Instance.dictServer)
  34. {
  35. cobTcpServer.Items.Add(item.Key);
  36. }
  37. if (cobTcpServer.Items.Count > 0) cobTcpServer.SelectedIndex = 0;
  38. timer1.Enabled = true;
  39. }
  40. catch (Exception)
  41. {
  42. }
  43. }
  44. private void CobPLC_SelectedIndexChanged(object sender, EventArgs e)
  45. {
  46. }
  47. private void BtnPLCConnect_Click(object sender, EventArgs e)
  48. {
  49. ProgramHelp.Instance.dictCommPLC[cobPLC.Text].Connect();
  50. }
  51. private void Timer1_Tick(object sender, EventArgs e)
  52. {
  53. try
  54. {
  55. if(ProgramHelp.Instance.dictCommPLC.Count > 0)
  56. {
  57. btnPLCConnect.FillColor = ProgramHelp.Instance.dictCommPLC[cobPLC.Text].isPLCConnected ? Color.Green : Color.Gray;
  58. }
  59. if(ProgramHelp.Instance.dictClient.Count > 0)
  60. {
  61. btnClientConnect.FillColor = ProgramHelp.Instance.dictClient[cobTcpClient.Text].isClientConnected ? Color.Green : Color.Gray;
  62. txtClientRecv.Text = ProgramHelp.Instance.dictClient[cobTcpClient.Text].strClientRecv;
  63. }
  64. if(ProgramHelp.Instance.dictServer.Count > 0)
  65. {
  66. btnTcpServerStart.FillColor = ProgramHelp.Instance.dictServer[cobTcpServer.Text].isServerConnected ? Color.Green : Color.Gray;
  67. txtTCPServerRecv.Text = ProgramHelp.Instance.dictServer[cobTcpServer.Text].strRecv;
  68. }
  69. }
  70. catch (Exception)
  71. {
  72. }
  73. }
  74. #region PLC
  75. private void BtnPLCRead_Click(object sender, EventArgs e)
  76. {
  77. if(txtPLCAdd.Text.Trim() == "")
  78. {
  79. MessageBox.Show("请写入地址!");
  80. }
  81. switch (rabPLCDataType.SelectedIndex)
  82. {
  83. case 0:
  84. if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].ReadInt(txtPLCAdd.Text, out int idata, out string strContent))
  85. {
  86. txtPLCReturn.Text = $"读取到{idata}";
  87. }
  88. else
  89. {
  90. txtPLCReturn.Text = $"读取失败:{strContent}";
  91. }
  92. break;
  93. case 1:
  94. if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].ReadFloat(txtPLCAdd.Text, out float fdata, out string strfContent))
  95. {
  96. txtPLCReturn.Text = $"读取到{fdata}";
  97. }
  98. else
  99. {
  100. txtPLCReturn.Text = $"读取失败:{strfContent}";
  101. }
  102. break;
  103. case 2:
  104. if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].ReadDouble(txtPLCAdd.Text, out double ddata, out string strdContent))
  105. {
  106. txtPLCReturn.Text = $"读取到{ddata}";
  107. }
  108. else
  109. {
  110. txtPLCReturn.Text = $"读取失败:{strdContent}";
  111. }
  112. break;
  113. case 3:
  114. if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].ReadShort(txtPLCAdd.Text, out short sdata, out string strsContent))
  115. {
  116. txtPLCReturn.Text = $"读取到{sdata}";
  117. }
  118. else
  119. {
  120. txtPLCReturn.Text = $"读取失败:{strsContent}";
  121. }
  122. break;
  123. case 4:
  124. if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].ReadCoin(txtPLCAdd.Text, out bool bdata, out string strbContent))
  125. {
  126. txtPLCReturn.Text = $"读取到{bdata}";
  127. }
  128. else
  129. {
  130. txtPLCReturn.Text = $"读取失败:{strbContent}";
  131. }
  132. break;
  133. case 5:
  134. if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].ReadUshort(txtPLCAdd.Text, out ushort ushortdata, out string strushortContent))
  135. {
  136. txtPLCReturn.Text = $"读取到{ushortdata}";
  137. }
  138. else
  139. {
  140. txtPLCReturn.Text = $"读取失败:{strushortContent}";
  141. }
  142. break;
  143. case 6:
  144. if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].ReadString(txtPLCAdd.Text, out string strdata, out string strdContent1))
  145. {
  146. txtPLCReturn.Text = $"读取到{strdata}";
  147. }
  148. else
  149. {
  150. txtPLCReturn.Text = $"读取失败:{strdContent1}";
  151. }
  152. break;
  153. }
  154. }
  155. private void BtnPLCWrite_Click(object sender, EventArgs e)
  156. {
  157. if (txtPLCAdd.Text.Trim() == "")
  158. {
  159. MessageBox.Show("请写入地址!");
  160. }
  161. if(txtPLCValue.Text.Trim() == "")
  162. {
  163. MessageBox.Show("请写入要写入的值!");
  164. }
  165. switch (rabPLCDataType.SelectedIndex)
  166. {
  167. case 0:
  168. if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].WriteInt(txtPLCAdd.Text, int.Parse(txtPLCValue.Text), out string striContent))
  169. {
  170. txtPLCReturn.Text = $"写入{txtPLCValue.Text}到{txtPLCAdd.Text}";
  171. }
  172. else
  173. {
  174. txtPLCReturn.Text = $"写入失败:{striContent}";
  175. }
  176. break;
  177. case 1:
  178. if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].WriteFloat(txtPLCAdd.Text, float.Parse(txtPLCValue.Text), out string strfContent))
  179. {
  180. txtPLCReturn.Text = $"写入{txtPLCValue.Text}到{txtPLCAdd.Text}";
  181. }
  182. else
  183. {
  184. txtPLCReturn.Text = $"写入失败:{strfContent}";
  185. }
  186. break;
  187. case 2:
  188. if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].WriteDouble(txtPLCAdd.Text, double.Parse(txtPLCValue.Text), out string strdContent))
  189. {
  190. txtPLCReturn.Text = $"写入{txtPLCValue.Text}到{txtPLCAdd.Text}";
  191. }
  192. else
  193. {
  194. txtPLCReturn.Text = $"写入失败:{strdContent}";
  195. }
  196. break;
  197. case 3:
  198. if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].WriteShort(txtPLCAdd.Text, short.Parse(txtPLCValue.Text), out string striContent1))
  199. {
  200. txtPLCReturn.Text = $"写入{txtPLCValue.Text}到{txtPLCAdd.Text}";
  201. }
  202. else
  203. {
  204. txtPLCReturn.Text = $"写入失败:{striContent1}";
  205. }
  206. break;
  207. case 4:
  208. if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].WriteCoin(txtPLCAdd.Text, Convert.ToBoolean(txtPLCValue.Text), out string striContent2))
  209. {
  210. txtPLCReturn.Text = $"写入{txtPLCValue.Text}到{txtPLCAdd.Text}";
  211. }
  212. else
  213. {
  214. txtPLCReturn.Text = $"写入失败:{striContent2}";
  215. }
  216. break;
  217. case 5:
  218. if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].WriteUShort(txtPLCAdd.Text, Convert.ToUInt16(txtPLCValue.Text), out string striContent12))
  219. {
  220. txtPLCReturn.Text = $"写入{txtPLCValue.Text}到{txtPLCAdd.Text}";
  221. }
  222. else
  223. {
  224. txtPLCReturn.Text = $"写入失败:{striContent12}";
  225. }
  226. break;
  227. case 6:
  228. if (ProgramHelp.Instance.dictCommPLC[cobPLC.Text].WriteString(txtPLCAdd.Text, txtPLCValue.Text, out string strdContent1))
  229. {
  230. txtPLCReturn.Text = $"写入{txtPLCValue.Text}到{txtPLCAdd.Text}";
  231. }
  232. else
  233. {
  234. txtPLCReturn.Text = $"写入失败:{strdContent1}";
  235. }
  236. break;
  237. }
  238. }
  239. #endregion
  240. #region Client
  241. private void BtnClientConnect_Click(object sender, EventArgs e)
  242. {
  243. ProgramHelp.Instance.dictClient[cobTcpClient.Text].TcpClientStart();
  244. }
  245. private void BtnClientSend_Click(object sender, EventArgs e)
  246. {
  247. ProgramHelp.Instance.dictClient[cobTcpClient.Text].TcpClientSend(txtClientSend.Text.Trim());
  248. }
  249. #endregion
  250. #region Server
  251. private void BtnTcpServerStart_Click(object sender, EventArgs e)
  252. {
  253. ProgramHelp.Instance.dictServer[cobTcpServer.Text].server.Start();
  254. }
  255. private void BtnTCPServerSend_Click(object sender, EventArgs e)
  256. {
  257. ProgramHelp.Instance.dictServer[cobTcpServer.Text].Send(txtTCPServerSend.Text.Trim());
  258. }
  259. #endregion
  260. }
  261. }