FormPLCConf.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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;
  281. id = 1001;
  282. break;
  283. case "终端":
  284. type = PLCDataType.terminal;
  285. id = 2000 + (index > 0 ? index - 1 : 0);
  286. break;
  287. case "车位":
  288. type = PLCDataType.parkingSpace;
  289. id = 3000 + (index > 0 ? index - 1 : 0);
  290. break;
  291. case "错误":
  292. type = PLCDataType.errorInfo;
  293. break;
  294. case "扫描":
  295. type = PLCDataType.scanInfo;
  296. break;
  297. }
  298. }
  299. catch { tb.Text = result.Replace(",", "\r\n"); return; }
  300. try
  301. {
  302. if (id == 1001)
  303. {
  304. result = Monitor.Monitor.mainBlockInfo.ToString();
  305. }
  306. else if (id >= 3000)
  307. {
  308. result = Monitor.Monitor.parkingSpaceInfo[id - 3000].ToString();
  309. }
  310. else if (id >= 2000)
  311. {
  312. result = Terminal.Terminal.terminalInfo[id - 2000].ToString();
  313. }
  314. else if(type == PLCDataType.errorInfo)
  315. {
  316. result = Monitor.Monitor.PLCErrorInfo.ToString();
  317. }
  318. else if(type == PLCDataType.scanInfo)
  319. {
  320. result = Monitor.Monitor.PLCScanInfo.ToString();
  321. }
  322. else
  323. {
  324. Console.WriteLine("无法识别输入"); return;
  325. }
  326. }
  327. catch { Console.WriteLine("异常"); return; }
  328. tb.Text = result.Replace(",", "\r\n");
  329. }
  330. private void btn_refresh_Click(object sender, EventArgs e)
  331. {
  332. Timer timer1 = new Timer();
  333. timer1.Interval = 500;
  334. timer1.Tick += new EventHandler(button2_Click);
  335. timer1.Start();
  336. Timer timer2 = new Timer();
  337. timer2.Interval = 500;
  338. timer2.Tick += new EventHandler(button3_Click);
  339. timer2.Start();
  340. Timer timer3 = new Timer();
  341. timer3.Interval = 500;
  342. timer3.Tick += new EventHandler(button4_Click);
  343. timer3.Start();
  344. }
  345. private void btn_clear_Click(object sender, EventArgs e)
  346. {
  347. TerminalStru tsFromCentral = new TerminalStru
  348. {
  349. terminalID = 1,
  350. parkingFee = (short)32767,
  351. paymentStatus = (short)0,
  352. licVerification = (short)0,
  353. userType = (short)0,
  354. };
  355. TerminalStru tsFromTerminal = new TerminalStru
  356. {
  357. terminalID = 1,
  358. btnStatus = (short)0,
  359. cmd = (short)0,
  360. receiptNum = (short)0,
  361. };
  362. pl.WriteToPLC(tsFromCentral, PLCDataType.central);
  363. pl.WriteToPLC(tsFromTerminal, PLCDataType.terminal);
  364. }
  365. /// <summary>
  366. /// 凭证号写入车位
  367. /// </summary>
  368. /// <param name="sender"></param>
  369. /// <param name="e"></param>
  370. private void button5_Click(object sender, EventArgs e)
  371. {
  372. List<object> list = pl.ReadFromPLC(PLCDataType.terminal, 1);
  373. int receipt = ((TerminalStru)list[0]).receiptNum;
  374. pl.WriteAccordingToOffset(PLCDataType.parkingSpace, 14, receipt);
  375. }
  376. /// <summary>
  377. /// 启动地感
  378. /// </summary>
  379. /// <param name="sender"></param>
  380. /// <param name="e"></param>
  381. private void button7_Click(object sender, EventArgs e)
  382. {
  383. List<object> list = pl.ReadFromPLC(PLCDataType.terminal, 1);
  384. short ground = ((TerminalStru)list[0]).groundStatus;
  385. if (ground == (short)1)
  386. {
  387. pl.WriteAccordingToOffset(PLCDataType.terminal, 40, (short)0);
  388. }
  389. else
  390. {
  391. pl.WriteAccordingToOffset(PLCDataType.terminal, 40, (short)1);
  392. }
  393. }
  394. /// <summary>
  395. /// 启动号牌机
  396. /// </summary>
  397. /// <param name="sender"></param>
  398. /// <param name="e"></param>
  399. private void button8_Click(object sender, EventArgs e)
  400. {
  401. pl.WriteAccordingToOffset(PLCDataType.central, 2, (short)1);
  402. }
  403. /// <summary>
  404. /// 清空中控db
  405. /// </summary>
  406. /// <param name="sender"></param>
  407. /// <param name="e"></param>
  408. private void button9_Click(object sender, EventArgs e)
  409. {
  410. try
  411. {
  412. List<object> list = pl.ReadFromPLC(PLCDataType.central, 0);
  413. short complete = ((MainBlockStru)list[0]).processCompleted;
  414. if (complete == (short)0)
  415. {
  416. pl.WriteAccordingToOffset(PLCDataType.central, 2, (short)0);
  417. pl.WriteAccordingToOffset(PLCDataType.central, 16, (short)1);
  418. pl.WriteAccordingToOffset(PLCDataType.central, 52, (short)0);
  419. }
  420. else
  421. {
  422. pl.WriteAccordingToOffset(PLCDataType.central, 2, (short)0);
  423. pl.WriteAccordingToOffset(PLCDataType.central, 16, (short)0);
  424. pl.WriteAccordingToOffset(PLCDataType.central, 52, (short)0);
  425. }
  426. }
  427. catch { }
  428. }
  429. /// <summary>
  430. /// 切换中控停取模式
  431. /// </summary>
  432. /// <param name="sender"></param>
  433. /// <param name="e"></param>
  434. private void button10_Click(object sender, EventArgs e)
  435. {
  436. try
  437. {
  438. List<object> list = pl.ReadFromPLC(PLCDataType.central, 0);
  439. short park = ((MainBlockStru)list[0]).parkingRunning;
  440. if (park == (short)0)
  441. {
  442. pl.WriteAccordingToOffset(PLCDataType.central, 12, (short)1);
  443. pl.WriteAccordingToOffset(PLCDataType.central, 14, (short)0);
  444. }
  445. else
  446. {
  447. pl.WriteAccordingToOffset(PLCDataType.central, 12, (short)0);
  448. pl.WriteAccordingToOffset(PLCDataType.central, 14, (short)1);
  449. }
  450. }
  451. catch { }
  452. }
  453. /// <summary>
  454. /// 切换终端停取模式
  455. /// </summary>
  456. /// <param name="sender"></param>
  457. /// <param name="e"></param>
  458. private void button11_Click(object sender, EventArgs e)
  459. {
  460. try
  461. {
  462. List<object> list = pl.ReadFromPLC(PLCDataType.terminal, 1);
  463. short termStatus = ((TerminalStru)list[0]).terminalStatus;
  464. if (termStatus == (short)1)
  465. {
  466. pl.WriteAccordingToOffset(PLCDataType.terminal, 2, (short)2);
  467. }
  468. else
  469. {
  470. pl.WriteAccordingToOffset(PLCDataType.terminal, 2, (short)1);
  471. }
  472. }
  473. catch { }
  474. }
  475. private void btn_PLCLink_Click(object sender, EventArgs e)
  476. {
  477. if (pl == null)
  478. {
  479. //pl = new PLCLinker(CpuType.S71500, ip, rack, slot, int.Parse(blockIds[0]), int.Parse(blockIds[1]), int.Parse(blockIds[2]), 6, 200);
  480. pl = (PLCLinker)Monitor.Monitor.PLC;
  481. }
  482. if (pl == null)
  483. return;
  484. else if (!pl.isConnected)
  485. {
  486. pl.PLCConnect();
  487. }
  488. else
  489. {
  490. Control.ControlCollection cc = this.Controls;
  491. foreach (Control c in cc)
  492. {
  493. c.Enabled = pl.isConnected;
  494. }
  495. }
  496. }
  497. }
  498. }