FormPLCConf.cs 17 KB

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