Form1.cs 18 KB

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