Form1.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. using PLCS7;
  2. using S7.Net;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace PLCConnector
  13. {
  14. public partial class Form1 : Form
  15. {
  16. PLCLinker pl;
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. pl = new PLCLinker(CpuType.S71500, "192.168.0.1", 0, 1, 18, 41, 20, 6, 200);
  21. }
  22. public static void parkingSpaceDBTest(AbstractPLCLinker pl, Random rnd)
  23. {
  24. if (!pl.isConnected) { pl.PLCConnect(); }
  25. else
  26. {
  27. //读车位表信息
  28. List<object> psList = pl.ReadFromPLC(PLCDataType.parkingSpace, 1);
  29. psList.AddRange(pl.ReadFromPLC(PLCDataType.parkingSpace, 2));
  30. foreach (var ps in psList)
  31. {
  32. Console.WriteLine(((ParkingSpaceStru)ps).ToString());
  33. }
  34. //写入车位状态信息
  35. ParkingSpaceStru pss = new ParkingSpaceStru
  36. {
  37. parkingSpace = (short)rnd.Next(1, 3),
  38. spaceStatus = (short)rnd.Next(0, 3)
  39. };
  40. pl.WriteToPLC(pss, PLCDataType.central);
  41. Console.WriteLine("写入状态值");
  42. }
  43. }
  44. public static void terminalDBTest(AbstractPLCLinker pl, Random rnd)
  45. {
  46. if (!pl.isConnected) { pl.PLCConnect(); }
  47. else
  48. {
  49. //读终端块数据
  50. List<object> tList = pl.ReadFromPLC(PLCDataType.terminal, 1);
  51. foreach (var term in tList)
  52. {
  53. Console.WriteLine(((TerminalStru)term).ToString());
  54. }
  55. //模拟中控写数据
  56. TerminalStru tsCentral = new TerminalStru
  57. {
  58. terminalID = 1,
  59. paymentStatus = (short)rnd.Next(100, 999),
  60. licVerification = -1,
  61. parkingFee = (short)rnd.Next(100, 999),
  62. userType = (short)rnd.Next(100, 999)
  63. };
  64. //模拟终端写数据
  65. TerminalStru tsTerminal = new TerminalStru
  66. {
  67. terminalID = 1,
  68. terminalStatus = (short)rnd.Next(100, 999),
  69. btnStatus = (short)rnd.Next(100, 999),
  70. licenseCodeA = (short)rnd.Next(100, 999),
  71. licenseCodeB = -1,
  72. licenseCodeC = (short)rnd.Next(100, 999),
  73. receiptNum = (short)rnd.Next(100, 999)
  74. };
  75. //pl.WriteToPLC(tsCentral, PLCDataType.central);
  76. pl.WriteToPLC(tsTerminal, PLCDataType.terminal);
  77. }
  78. }
  79. private void button1_Click(object sender, EventArgs e)
  80. {
  81. PLCDataType type = PLCDataType.parkingSpace;
  82. int offset = 0;
  83. int value = 0;
  84. try
  85. {
  86. offset = Int32.Parse(textBox2.Text);
  87. value = Int32.Parse(textBox3.Text);
  88. }
  89. catch { MessageBox.Show("异常"); return; }
  90. switch (textBox4.Text)
  91. {
  92. case "中控":
  93. type = PLCDataType.central;
  94. break;
  95. case "终端1":
  96. type = PLCDataType.terminal;
  97. break;
  98. case "终端2":
  99. type = PLCDataType.terminal;
  100. offset += 42;
  101. break;
  102. case "终端3":
  103. type = PLCDataType.terminal;
  104. offset += 42*2;
  105. break;
  106. case "车位1":
  107. type = PLCDataType.parkingSpace;
  108. break;
  109. }
  110. if (value < 32768)
  111. {
  112. pl.WriteAccordingToOffset(type, offset, (short)value);
  113. }
  114. else
  115. {
  116. pl.WriteAccordingToOffset(type, offset, value);
  117. }
  118. }
  119. private void button2_Click(object sender, EventArgs e)
  120. {
  121. PLCDataType type = PLCDataType.parkingSpace;
  122. int id = -1;
  123. switch ((string)textBox5.Text)
  124. {
  125. case "中控":
  126. type = PLCDataType.central;id = 0;
  127. break;
  128. case "终端1":
  129. type = PLCDataType.terminal; id = 1;
  130. break;
  131. case "终端2":
  132. type = PLCDataType.terminal; id = 2;
  133. break;
  134. case "终端3":
  135. type = PLCDataType.terminal; id = 3;
  136. break;
  137. case "车位1":
  138. type = PLCDataType.parkingSpace; id = 4;
  139. break;
  140. }
  141. string result = "";
  142. try
  143. {
  144. if (id == 4)
  145. {
  146. List<object> list = pl.ReadFromPLC(type, 1);
  147. result = ((ParkingSpaceStru)list[0]).ToString();
  148. }
  149. else if (id > 0)
  150. {
  151. List<object> list = pl.ReadFromPLC(type, id);
  152. result = ((TerminalStru)list[0]).ToString();
  153. }
  154. else if (id == 0)
  155. {
  156. List<object> list = pl.ReadFromPLC(type, id);
  157. result = ((MainBlockStru)list[0]).ToString();
  158. }
  159. }
  160. catch { MessageBox.Show("异常"); return; }
  161. textBox1.Text = result.Replace(",","\r\n");
  162. }
  163. private void button3_Click(object sender, EventArgs e)
  164. {
  165. PLCDataType type = PLCDataType.parkingSpace;
  166. int id = -1;
  167. switch ((string)textBox6.Text)
  168. {
  169. case "中控":
  170. type = PLCDataType.central; id = 0;
  171. break;
  172. case "终端1":
  173. type = PLCDataType.terminal; id = 1;
  174. break;
  175. case "终端2":
  176. type = PLCDataType.terminal; id = 2;
  177. break;
  178. case "终端3":
  179. type = PLCDataType.terminal; id = 3;
  180. break;
  181. case "车位1":
  182. type = PLCDataType.parkingSpace; id = 4;
  183. break;
  184. }
  185. string result = "";
  186. try
  187. {
  188. if (id == 4)
  189. {
  190. List<object> list = pl.ReadFromPLC(type, 1);
  191. result = ((ParkingSpaceStru)list[0]).ToString();
  192. }
  193. else if (id > 0)
  194. {
  195. List<object> list = pl.ReadFromPLC(type, id);
  196. result = ((TerminalStru)list[0]).ToString();
  197. }
  198. else if (id == 0)
  199. {
  200. List<object> list = pl.ReadFromPLC(type, id);
  201. result = ((MainBlockStru)list[0]).ToString();
  202. }
  203. }
  204. catch { MessageBox.Show("异常"); return; }
  205. textBox7.Text = result.Replace(",", "\r\n");
  206. }
  207. private void button4_Click(object sender, EventArgs e)
  208. {
  209. PLCDataType type = PLCDataType.parkingSpace;
  210. int id = -1;
  211. switch ((string)textBox8.Text)
  212. {
  213. case "中控":
  214. type = PLCDataType.central; id = 0;
  215. break;
  216. case "终端1":
  217. type = PLCDataType.terminal; id = 1;
  218. break;
  219. case "终端2":
  220. type = PLCDataType.terminal; id = 2;
  221. break;
  222. case "终端3":
  223. type = PLCDataType.terminal; id = 3;
  224. break;
  225. case "车位1":
  226. type = PLCDataType.parkingSpace; id = 4;
  227. break;
  228. }
  229. string result = "";
  230. try
  231. {
  232. if (id == 4)
  233. {
  234. List<object> list = pl.ReadFromPLC(type, 1);
  235. result = ((ParkingSpaceStru)list[0]).ToString();
  236. }
  237. else if (id > 0)
  238. {
  239. List<object> list = pl.ReadFromPLC(type, id);
  240. result = ((TerminalStru)list[0]).ToString();
  241. }
  242. else if (id == 0)
  243. {
  244. List<object> list = pl.ReadFromPLC(type, id);
  245. result = ((MainBlockStru)list[0]).ToString();
  246. }
  247. }
  248. catch { MessageBox.Show("异常"); return; }
  249. textBox9.Text = result.Replace(",", "\r\n");
  250. }
  251. private void btn_refresh_Click(object sender, EventArgs e)
  252. {
  253. Timer timer1 = new Timer();
  254. timer1.Interval = 1000;
  255. timer1.Tick += new EventHandler(button2_Click);
  256. timer1.Start();
  257. Timer timer2 = new Timer();
  258. timer2.Interval = 1000;
  259. timer2.Tick += new EventHandler(button3_Click);
  260. timer2.Start();
  261. Timer timer3 = new Timer();
  262. timer3.Interval = 1000;
  263. timer3.Tick += new EventHandler(button4_Click);
  264. timer3.Start();
  265. }
  266. private void btn_clear_Click(object sender, EventArgs e)
  267. {
  268. TerminalStru tsFromCentral = new TerminalStru
  269. {
  270. terminalID = 1,
  271. parkingFee = (short)32767,
  272. paymentStatus = (short)0,
  273. licVerification =(short)0,
  274. userType = (short)0,
  275. };
  276. TerminalStru tsFromTerminal = new TerminalStru
  277. {
  278. terminalID = 1,
  279. btnStatus = -1,
  280. cmd = (short)0,
  281. receiptNum = (short)0,
  282. };
  283. pl.WriteToPLC(tsFromCentral, PLCDataType.central);
  284. pl.WriteToPLC(tsFromTerminal, PLCDataType.terminal);
  285. }
  286. /// <summary>
  287. /// 凭证号写入车位
  288. /// </summary>
  289. /// <param name="sender"></param>
  290. /// <param name="e"></param>
  291. private void button5_Click(object sender, EventArgs e)
  292. {
  293. List<object> list = pl.ReadFromPLC(PLCDataType.terminal, 1);
  294. int receipt = ((TerminalStru)list[0]).receiptNum;
  295. pl.WriteAccordingToOffset(PLCDataType.parkingSpace, 14, receipt);
  296. }
  297. /// <summary>
  298. /// 启动地感
  299. /// </summary>
  300. /// <param name="sender"></param>
  301. /// <param name="e"></param>
  302. private void button7_Click(object sender, EventArgs e)
  303. {
  304. List<object> list = pl.ReadFromPLC(PLCDataType.terminal, 1);
  305. short ground = ((TerminalStru)list[0]).groundStatus;
  306. if (ground == (short)1)
  307. {
  308. pl.WriteAccordingToOffset(PLCDataType.terminal, 36, (short)0);
  309. }
  310. else
  311. {
  312. pl.WriteAccordingToOffset(PLCDataType.terminal, 36, (short)1);
  313. }
  314. }
  315. /// <summary>
  316. /// 启动号牌机
  317. /// </summary>
  318. /// <param name="sender"></param>
  319. /// <param name="e"></param>
  320. private void button8_Click(object sender, EventArgs e)
  321. {
  322. pl.WriteAccordingToOffset(PLCDataType.central, 2, (short)1);
  323. }
  324. /// <summary>
  325. /// 清空中控db
  326. /// </summary>
  327. /// <param name="sender"></param>
  328. /// <param name="e"></param>
  329. private void button9_Click(object sender, EventArgs e)
  330. {
  331. List<object> list = pl.ReadFromPLC(PLCDataType.central, 0);
  332. short complete = ((MainBlockStru)list[0]).processCompleted;
  333. if (complete == (short)0)
  334. {
  335. pl.WriteAccordingToOffset(PLCDataType.central, 2, (short)0);
  336. pl.WriteAccordingToOffset(PLCDataType.central, 16, (short)1);
  337. pl.WriteAccordingToOffset(PLCDataType.central, 52, (short)0);
  338. }
  339. else
  340. {
  341. pl.WriteAccordingToOffset(PLCDataType.central, 2, (short)0);
  342. pl.WriteAccordingToOffset(PLCDataType.central, 16, (short)0);
  343. pl.WriteAccordingToOffset(PLCDataType.central, 52, (short)0);
  344. }
  345. }
  346. }
  347. }