Form1.cs 17 KB

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