Form1.cs 21 KB

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