FormPLCConf.cs 15 KB

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