FormPLCConf.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. using PLCS7;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Configuration;
  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 FormPLCConf : Form
  15. {
  16. PLCLinker pl;
  17. public FormPLCConf()
  18. {
  19. InitializeComponent();
  20. pl = (PLCLinker)Monitor.Monitor.PLC;
  21. Control.ControlCollection cc = this.Controls;
  22. foreach (Control c in cc)
  23. {
  24. c.Enabled = false;
  25. }
  26. btn_PLCLink.Enabled = true;
  27. }
  28. public static void parkingSpaceDBTest(AbstractPLCLinker pl, Random rnd)
  29. {
  30. if (!pl.isConnected) { pl.PLCConnect(); }
  31. else
  32. {
  33. //读车位表信息
  34. List<object> psList = pl.ReadFromPLC(PLCDataType.parkingSpace, 1);
  35. psList.AddRange(pl.ReadFromPLC(PLCDataType.parkingSpace, 2));
  36. foreach (var ps in psList)
  37. {
  38. Console.WriteLine(((ParkingSpaceStru)ps).ToString());
  39. }
  40. //写入车位状态信息
  41. ParkingSpaceStru pss = new ParkingSpaceStru
  42. {
  43. parkingSpace = (short)rnd.Next(1, 3),
  44. spaceStatus = (short)rnd.Next(0, 3)
  45. };
  46. pl.WriteToPLC(pss, PLCDataType.central);
  47. Console.WriteLine("写入状态值");
  48. }
  49. }
  50. public static void terminalDBTest(AbstractPLCLinker pl, Random rnd)
  51. {
  52. if (!pl.isConnected) { pl.PLCConnect(); }
  53. else
  54. {
  55. //读终端块数据
  56. List<object> tList = pl.ReadFromPLC(PLCDataType.terminal, 1);
  57. foreach (var term in tList)
  58. {
  59. Console.WriteLine(((TerminalStru)term).ToString());
  60. }
  61. //模拟中控写数据
  62. TerminalStru tsCentral = new TerminalStru
  63. {
  64. terminalID = 1,
  65. paymentStatus = (short)rnd.Next(100, 999),
  66. licVerification = -1,
  67. parkingFee = (short)rnd.Next(100, 999),
  68. userType = (short)rnd.Next(100, 999)
  69. };
  70. //模拟终端写数据
  71. TerminalStru tsTerminal = new TerminalStru
  72. {
  73. terminalID = 1,
  74. terminalStatus = (short)rnd.Next(100, 999),
  75. btnStatus = (short)rnd.Next(100, 999),
  76. licenseCodeA = (short)rnd.Next(100, 999),
  77. licenseCodeB = -1,
  78. licenseCodeC = (short)rnd.Next(100, 999),
  79. receiptNum = (short)rnd.Next(100, 999)
  80. };
  81. //pl.WriteToPLC(tsCentral, PLCDataType.central);
  82. pl.WriteToPLC(tsTerminal, PLCDataType.terminal);
  83. }
  84. }
  85. private void button1_Click(object sender, EventArgs e)
  86. {
  87. PLCDataType type = PLCDataType.parkingSpace;
  88. int offset = 0;
  89. int value = 0;
  90. try
  91. {
  92. offset = Int32.Parse(textBox2.Text);
  93. value = Int32.Parse(textBox3.Text);
  94. }
  95. catch { MessageBox.Show("异常"); return; }
  96. switch (textBox4.Text)
  97. {
  98. case "中控":
  99. type = PLCDataType.central;
  100. break;
  101. case "终端1":
  102. type = PLCDataType.terminal;
  103. break;
  104. case "终端2":
  105. type = PLCDataType.terminal;
  106. offset += 42;
  107. break;
  108. case "终端3":
  109. type = PLCDataType.terminal;
  110. offset += 42 * 2;
  111. break;
  112. case "车位1":
  113. type = PLCDataType.parkingSpace;
  114. break;
  115. }
  116. if (value < 32768)
  117. {
  118. pl.WriteAccordingToOffset(type, offset, (short)value);
  119. }
  120. else
  121. {
  122. pl.WriteAccordingToOffset(type, offset, value);
  123. }
  124. }
  125. private void button2_Click(object sender, EventArgs e)
  126. {
  127. //PLCDataType type = PLCDataType.parkingSpace;
  128. //int id = -1;
  129. //switch ((string)textBox5.Text)
  130. //{
  131. // case "中控":
  132. // type = PLCDataType.central; id = 0;
  133. // break;
  134. // case "终端1":
  135. // type = PLCDataType.terminal; id = 1;
  136. // break;
  137. // case "终端2":
  138. // type = PLCDataType.terminal; id = 2;
  139. // break;
  140. // case "终端3":
  141. // type = PLCDataType.terminal; id = 3;
  142. // break;
  143. // case "车位1":
  144. // type = PLCDataType.parkingSpace; id = 4;
  145. // break;
  146. //}
  147. //string result = "";
  148. //try
  149. //{
  150. // if (id == 4)
  151. // {
  152. // List<object> list = pl.ReadFromPLC(type, 1);
  153. // result = ((ParkingSpaceStru)list[0]).ToString();
  154. // }
  155. // else if (id > 0)
  156. // {
  157. // List<object> list = pl.ReadFromPLC(type, id);
  158. // result = ((TerminalStru)list[0]).ToString();
  159. // }
  160. // else if (id == 0)
  161. // {
  162. // List<object> list = pl.ReadFromPLC(type, id);
  163. // result = ((MainBlockStru)list[0]).ToString();
  164. // }
  165. //}
  166. //catch { Console.WriteLine("异常"); return; }
  167. //textBox1.Text = result.Replace(",", "\r\n");
  168. display(textBox5.Text, textBox1);
  169. }
  170. private void button3_Click(object sender, EventArgs e)
  171. {
  172. //PLCDataType type = PLCDataType.parkingSpace;
  173. //int id = -1;
  174. //switch ((string)textBox6.Text)
  175. //{
  176. // case "中控":
  177. // type = PLCDataType.central; id = 0;
  178. // break;
  179. // case "终端1":
  180. // type = PLCDataType.terminal; id = 1;
  181. // break;
  182. // case "终端2":
  183. // type = PLCDataType.terminal; id = 2;
  184. // break;
  185. // case "终端3":
  186. // type = PLCDataType.terminal; id = 3;
  187. // break;
  188. // case "车位1":
  189. // type = PLCDataType.parkingSpace; id = 4;
  190. // break;
  191. //}
  192. //string result = "";
  193. //try
  194. //{
  195. // if (id == 4)
  196. // {
  197. // List<object> list = pl.ReadFromPLC(type, 1);
  198. // result = ((ParkingSpaceStru)list[0]).ToString();
  199. // }
  200. // else if (id > 0)
  201. // {
  202. // List<object> list = pl.ReadFromPLC(type, id);
  203. // result = ((TerminalStru)list[0]).ToString();
  204. // }
  205. // else if (id == 0)
  206. // {
  207. // List<object> list = pl.ReadFromPLC(type, id);
  208. // result = ((MainBlockStru)list[0]).ToString();
  209. // }
  210. //}
  211. //catch { Console.WriteLine("异常"); return; }
  212. //textBox7.Text = result.Replace(",", "\r\n");
  213. display(textBox6.Text, textBox7);
  214. }
  215. private void button4_Click(object sender, EventArgs e)
  216. {
  217. //PLCDataType type = PLCDataType.parkingSpace;
  218. //int id = -1;
  219. //switch ((string)textBox8.Text)
  220. //{
  221. // case "中控":
  222. // type = PLCDataType.central; id = 0;
  223. // break;
  224. // case "终端1":
  225. // type = PLCDataType.terminal; id = 1;
  226. // break;
  227. // case "终端2":
  228. // type = PLCDataType.terminal; id = 2;
  229. // break;
  230. // case "终端3":
  231. // type = PLCDataType.terminal; id = 3;
  232. // break;
  233. // case "车位1":
  234. // type = PLCDataType.parkingSpace; id = 4;
  235. // break;
  236. //}
  237. //string result = "";
  238. //try
  239. //{
  240. // if (id == 4)
  241. // {
  242. // //List<object> list = pl.ReadFromPLC(type, 1);
  243. // result = Monitor.Monitor.parkingSpaceInfo[1];//((ParkingSpaceStru)list[0]).ToString();
  244. // }
  245. // else if (id > 0)
  246. // {
  247. // List<object> list = pl.ReadFromPLC(type, id);
  248. // result = ((TerminalStru)list[0]).ToString();
  249. // }
  250. // else if (id == 0)
  251. // {
  252. // List<object> list = pl.ReadFromPLC(type, id);
  253. // result = ((MainBlockStru)list[0]).ToString();
  254. // }
  255. //}
  256. //catch { Console.WriteLine("异常"); return; }
  257. //textBox9.Text = result.Replace(",", "\r\n");
  258. display(textBox8.Text, textBox9);
  259. }
  260. private void display(string target, TextBox tb)
  261. {
  262. if (target == "")
  263. return;
  264. string result = "";
  265. PLCDataType type = PLCDataType.parkingSpace;
  266. int id = -1;
  267. int index = 0;
  268. try
  269. {
  270. if (target.Length > 2)
  271. index = int.Parse(target.Substring(2, target.Length - 2));
  272. }
  273. catch (Exception ex) { index = 0; Console.WriteLine("aaaaa"); }
  274. try
  275. {
  276. string block = target.Substring(0, 2);
  277. switch (block)
  278. {
  279. case "中控":
  280. type = PLCDataType.central; id = 1001;
  281. break;
  282. case "终端":
  283. type = PLCDataType.terminal;
  284. id = 2000 + (index > 0 ? index - 1 : 0);
  285. break;
  286. case "车位":
  287. type = PLCDataType.parkingSpace;
  288. id = 3000 + (index > 0 ? index - 1 : 0);
  289. break;
  290. case "错误":
  291. type = PLCDataType.errorInfo;
  292. break;
  293. }
  294. }
  295. catch { tb.Text = result.Replace(",", "\r\n"); return; }
  296. try
  297. {
  298. if (id == 1001)
  299. {
  300. result = Monitor.Monitor.mainBlockInfo.ToString();
  301. }
  302. else if (id >= 3000)
  303. {
  304. result = Monitor.Monitor.parkingSpaceInfo[id - 3000].ToString();
  305. }
  306. else if (id >= 2000)
  307. {
  308. result = Terminal.Terminal.terminalInfo[id - 2000].ToString();
  309. }
  310. else if(type == PLCDataType.errorInfo)
  311. {
  312. result = Monitor.Monitor.PLCErrorInfo.ToString();
  313. }
  314. else
  315. {
  316. Console.WriteLine("无法识别输入"); return;
  317. }
  318. }
  319. catch { Console.WriteLine("异常"); return; }
  320. tb.Text = result.Replace(",", "\r\n");
  321. }
  322. private void btn_refresh_Click(object sender, EventArgs e)
  323. {
  324. Timer timer1 = new Timer();
  325. timer1.Interval = 500;
  326. timer1.Tick += new EventHandler(button2_Click);
  327. timer1.Start();
  328. Timer timer2 = new Timer();
  329. timer2.Interval = 500;
  330. timer2.Tick += new EventHandler(button3_Click);
  331. timer2.Start();
  332. Timer timer3 = new Timer();
  333. timer3.Interval = 500;
  334. timer3.Tick += new EventHandler(button4_Click);
  335. timer3.Start();
  336. }
  337. private void btn_clear_Click(object sender, EventArgs e)
  338. {
  339. TerminalStru tsFromCentral = new TerminalStru
  340. {
  341. terminalID = 1,
  342. parkingFee = (short)32767,
  343. paymentStatus = (short)0,
  344. licVerification = (short)0,
  345. userType = (short)0,
  346. };
  347. TerminalStru tsFromTerminal = new TerminalStru
  348. {
  349. terminalID = 1,
  350. btnStatus = (short)0,
  351. cmd = (short)0,
  352. receiptNum = (short)0,
  353. };
  354. pl.WriteToPLC(tsFromCentral, PLCDataType.central);
  355. pl.WriteToPLC(tsFromTerminal, PLCDataType.terminal);
  356. }
  357. /// <summary>
  358. /// 凭证号写入车位
  359. /// </summary>
  360. /// <param name="sender"></param>
  361. /// <param name="e"></param>
  362. private void button5_Click(object sender, EventArgs e)
  363. {
  364. List<object> list = pl.ReadFromPLC(PLCDataType.terminal, 1);
  365. int receipt = ((TerminalStru)list[0]).receiptNum;
  366. pl.WriteAccordingToOffset(PLCDataType.parkingSpace, 14, receipt);
  367. }
  368. /// <summary>
  369. /// 启动地感
  370. /// </summary>
  371. /// <param name="sender"></param>
  372. /// <param name="e"></param>
  373. private void button7_Click(object sender, EventArgs e)
  374. {
  375. List<object> list = pl.ReadFromPLC(PLCDataType.terminal, 1);
  376. short ground = ((TerminalStru)list[0]).groundStatus;
  377. if (ground == (short)1)
  378. {
  379. pl.WriteAccordingToOffset(PLCDataType.terminal, 40, (short)0);
  380. }
  381. else
  382. {
  383. pl.WriteAccordingToOffset(PLCDataType.terminal, 40, (short)1);
  384. }
  385. }
  386. /// <summary>
  387. /// 启动号牌机
  388. /// </summary>
  389. /// <param name="sender"></param>
  390. /// <param name="e"></param>
  391. private void button8_Click(object sender, EventArgs e)
  392. {
  393. pl.WriteAccordingToOffset(PLCDataType.central, 2, (short)1);
  394. }
  395. /// <summary>
  396. /// 清空中控db
  397. /// </summary>
  398. /// <param name="sender"></param>
  399. /// <param name="e"></param>
  400. private void button9_Click(object sender, EventArgs e)
  401. {
  402. try
  403. {
  404. List<object> list = pl.ReadFromPLC(PLCDataType.central, 0);
  405. short complete = ((MainBlockStru)list[0]).processCompleted;
  406. if (complete == (short)0)
  407. {
  408. pl.WriteAccordingToOffset(PLCDataType.central, 2, (short)0);
  409. pl.WriteAccordingToOffset(PLCDataType.central, 16, (short)1);
  410. pl.WriteAccordingToOffset(PLCDataType.central, 52, (short)0);
  411. }
  412. else
  413. {
  414. pl.WriteAccordingToOffset(PLCDataType.central, 2, (short)0);
  415. pl.WriteAccordingToOffset(PLCDataType.central, 16, (short)0);
  416. pl.WriteAccordingToOffset(PLCDataType.central, 52, (short)0);
  417. }
  418. }
  419. catch { }
  420. }
  421. /// <summary>
  422. /// 切换中控停取模式
  423. /// </summary>
  424. /// <param name="sender"></param>
  425. /// <param name="e"></param>
  426. private void button10_Click(object sender, EventArgs e)
  427. {
  428. try
  429. {
  430. List<object> list = pl.ReadFromPLC(PLCDataType.central, 0);
  431. short park = ((MainBlockStru)list[0]).parkingRunning;
  432. if (park == (short)0)
  433. {
  434. pl.WriteAccordingToOffset(PLCDataType.central, 12, (short)1);
  435. pl.WriteAccordingToOffset(PLCDataType.central, 14, (short)0);
  436. }
  437. else
  438. {
  439. pl.WriteAccordingToOffset(PLCDataType.central, 12, (short)0);
  440. pl.WriteAccordingToOffset(PLCDataType.central, 14, (short)1);
  441. }
  442. }
  443. catch { }
  444. }
  445. /// <summary>
  446. /// 切换终端停取模式
  447. /// </summary>
  448. /// <param name="sender"></param>
  449. /// <param name="e"></param>
  450. private void button11_Click(object sender, EventArgs e)
  451. {
  452. try
  453. {
  454. List<object> list = pl.ReadFromPLC(PLCDataType.terminal, 1);
  455. short termStatus = ((TerminalStru)list[0]).terminalStatus;
  456. if (termStatus == (short)1)
  457. {
  458. pl.WriteAccordingToOffset(PLCDataType.terminal, 2, (short)2);
  459. }
  460. else
  461. {
  462. pl.WriteAccordingToOffset(PLCDataType.terminal, 2, (short)1);
  463. }
  464. }
  465. catch { }
  466. }
  467. private void btn_PLCLink_Click(object sender, EventArgs e)
  468. {
  469. if (pl == null)
  470. {
  471. //pl = new PLCLinker(CpuType.S71500, ip, rack, slot, int.Parse(blockIds[0]), int.Parse(blockIds[1]), int.Parse(blockIds[2]), 6, 200);
  472. pl = (PLCLinker)Monitor.Monitor.PLC;
  473. }
  474. if (pl == null)
  475. return;
  476. else if (!pl.isConnected)
  477. {
  478. pl.PLCConnect();
  479. }
  480. else
  481. {
  482. Control.ControlCollection cc = this.Controls;
  483. foreach (Control c in cc)
  484. {
  485. c.Enabled = pl.isConnected;
  486. }
  487. }
  488. }
  489. }
  490. }