FormPLCConf.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. pl = (PLCLinker)Monitor.Monitor.PLC;
  32. }
  33. catch (Exception e) { MessageBox.Show("配置文件异常"); }
  34. Control.ControlCollection cc = this.Controls;
  35. foreach (Control c in cc)
  36. {
  37. c.Enabled = false;
  38. }
  39. btn_PLCLink.Enabled = true;
  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. }
  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. }
  160. string result = "";
  161. try
  162. {
  163. if (id == 4)
  164. {
  165. List<object> list = pl.ReadFromPLC(type, 1);
  166. result = ((ParkingSpaceStru)list[0]).ToString();
  167. }
  168. else if (id > 0)
  169. {
  170. List<object> list = pl.ReadFromPLC(type, id);
  171. result = ((TerminalStru)list[0]).ToString();
  172. }
  173. else if (id == 0)
  174. {
  175. List<object> list = pl.ReadFromPLC(type, id);
  176. result = ((MainBlockStru)list[0]).ToString();
  177. }
  178. }
  179. catch { Console.WriteLine("异常"); return; }
  180. textBox1.Text = result.Replace(",", "\r\n");
  181. }
  182. private void button3_Click(object sender, EventArgs e)
  183. {
  184. PLCDataType type = PLCDataType.parkingSpace;
  185. int id = -1;
  186. switch ((string)textBox6.Text)
  187. {
  188. case "中控":
  189. type = PLCDataType.central; id = 0;
  190. break;
  191. case "终端1":
  192. type = PLCDataType.terminal; id = 1;
  193. break;
  194. case "终端2":
  195. type = PLCDataType.terminal; id = 2;
  196. break;
  197. case "终端3":
  198. type = PLCDataType.terminal; id = 3;
  199. break;
  200. case "车位1":
  201. type = PLCDataType.parkingSpace; id = 4;
  202. break;
  203. }
  204. string result = "";
  205. try
  206. {
  207. if (id == 4)
  208. {
  209. List<object> list = pl.ReadFromPLC(type, 1);
  210. result = ((ParkingSpaceStru)list[0]).ToString();
  211. }
  212. else if (id > 0)
  213. {
  214. List<object> list = pl.ReadFromPLC(type, id);
  215. result = ((TerminalStru)list[0]).ToString();
  216. }
  217. else if (id == 0)
  218. {
  219. List<object> list = pl.ReadFromPLC(type, id);
  220. result = ((MainBlockStru)list[0]).ToString();
  221. }
  222. }
  223. catch { Console.WriteLine("异常"); return; }
  224. textBox7.Text = result.Replace(",", "\r\n");
  225. }
  226. private void button4_Click(object sender, EventArgs e)
  227. {
  228. PLCDataType type = PLCDataType.parkingSpace;
  229. int id = -1;
  230. switch ((string)textBox8.Text)
  231. {
  232. case "中控":
  233. type = PLCDataType.central; id = 0;
  234. break;
  235. case "终端1":
  236. type = PLCDataType.terminal; id = 1;
  237. break;
  238. case "终端2":
  239. type = PLCDataType.terminal; id = 2;
  240. break;
  241. case "终端3":
  242. type = PLCDataType.terminal; id = 3;
  243. break;
  244. case "车位1":
  245. type = PLCDataType.parkingSpace; id = 4;
  246. break;
  247. }
  248. string result = "";
  249. try
  250. {
  251. if (id == 4)
  252. {
  253. List<object> list = pl.ReadFromPLC(type, 1);
  254. result = ((ParkingSpaceStru)list[0]).ToString();
  255. }
  256. else if (id > 0)
  257. {
  258. List<object> list = pl.ReadFromPLC(type, id);
  259. result = ((TerminalStru)list[0]).ToString();
  260. }
  261. else if (id == 0)
  262. {
  263. List<object> list = pl.ReadFromPLC(type, id);
  264. result = ((MainBlockStru)list[0]).ToString();
  265. }
  266. }
  267. catch { Console.WriteLine("异常"); return; }
  268. textBox9.Text = result.Replace(",", "\r\n");
  269. }
  270. private void btn_refresh_Click(object sender, EventArgs e)
  271. {
  272. Timer timer1 = new Timer();
  273. timer1.Interval = 500;
  274. timer1.Tick += new EventHandler(button2_Click);
  275. timer1.Start();
  276. Timer timer2 = new Timer();
  277. timer2.Interval = 500;
  278. timer2.Tick += new EventHandler(button3_Click);
  279. timer2.Start();
  280. Timer timer3 = new Timer();
  281. timer3.Interval = 500;
  282. timer3.Tick += new EventHandler(button4_Click);
  283. timer3.Start();
  284. }
  285. private void btn_clear_Click(object sender, EventArgs e)
  286. {
  287. TerminalStru tsFromCentral = new TerminalStru
  288. {
  289. terminalID = 1,
  290. parkingFee = (short)32767,
  291. paymentStatus = (short)0,
  292. licVerification = (short)0,
  293. userType = (short)0,
  294. };
  295. TerminalStru tsFromTerminal = new TerminalStru
  296. {
  297. terminalID = 1,
  298. btnStatus = -1,
  299. cmd = (short)0,
  300. receiptNum = (short)0,
  301. };
  302. pl.WriteToPLC(tsFromCentral, PLCDataType.central);
  303. pl.WriteToPLC(tsFromTerminal, PLCDataType.terminal);
  304. }
  305. /// <summary>
  306. /// 凭证号写入车位
  307. /// </summary>
  308. /// <param name="sender"></param>
  309. /// <param name="e"></param>
  310. private void button5_Click(object sender, EventArgs e)
  311. {
  312. List<object> list = pl.ReadFromPLC(PLCDataType.terminal, 1);
  313. int receipt = ((TerminalStru)list[0]).receiptNum;
  314. pl.WriteAccordingToOffset(PLCDataType.parkingSpace, 14, receipt);
  315. }
  316. /// <summary>
  317. /// 启动地感
  318. /// </summary>
  319. /// <param name="sender"></param>
  320. /// <param name="e"></param>
  321. private void button7_Click(object sender, EventArgs e)
  322. {
  323. List<object> list = pl.ReadFromPLC(PLCDataType.terminal, 1);
  324. short ground = ((TerminalStru)list[0]).groundStatus;
  325. if (ground == (short)1)
  326. {
  327. pl.WriteAccordingToOffset(PLCDataType.terminal, 40, (short)0);
  328. }
  329. else
  330. {
  331. pl.WriteAccordingToOffset(PLCDataType.terminal, 40, (short)1);
  332. }
  333. }
  334. /// <summary>
  335. /// 启动号牌机
  336. /// </summary>
  337. /// <param name="sender"></param>
  338. /// <param name="e"></param>
  339. private void button8_Click(object sender, EventArgs e)
  340. {
  341. pl.WriteAccordingToOffset(PLCDataType.central, 2, (short)1);
  342. }
  343. /// <summary>
  344. /// 清空中控db
  345. /// </summary>
  346. /// <param name="sender"></param>
  347. /// <param name="e"></param>
  348. private void button9_Click(object sender, EventArgs e)
  349. {
  350. try
  351. {
  352. List<object> list = pl.ReadFromPLC(PLCDataType.central, 0);
  353. short complete = ((MainBlockStru)list[0]).processCompleted;
  354. if (complete == (short)0)
  355. {
  356. pl.WriteAccordingToOffset(PLCDataType.central, 2, (short)0);
  357. pl.WriteAccordingToOffset(PLCDataType.central, 16, (short)1);
  358. pl.WriteAccordingToOffset(PLCDataType.central, 52, (short)0);
  359. }
  360. else
  361. {
  362. pl.WriteAccordingToOffset(PLCDataType.central, 2, (short)0);
  363. pl.WriteAccordingToOffset(PLCDataType.central, 16, (short)0);
  364. pl.WriteAccordingToOffset(PLCDataType.central, 52, (short)0);
  365. }
  366. }
  367. catch { }
  368. }
  369. /// <summary>
  370. /// 切换中控停取模式
  371. /// </summary>
  372. /// <param name="sender"></param>
  373. /// <param name="e"></param>
  374. private void button10_Click(object sender, EventArgs e)
  375. {
  376. try
  377. {
  378. List<object> list = pl.ReadFromPLC(PLCDataType.central, 0);
  379. short park = ((MainBlockStru)list[0]).parkingRunning;
  380. if (park == (short)0)
  381. {
  382. pl.WriteAccordingToOffset(PLCDataType.central, 12, (short)1);
  383. pl.WriteAccordingToOffset(PLCDataType.central, 14, (short)0);
  384. }
  385. else
  386. {
  387. pl.WriteAccordingToOffset(PLCDataType.central, 12, (short)0);
  388. pl.WriteAccordingToOffset(PLCDataType.central, 14, (short)1);
  389. }
  390. }
  391. catch { }
  392. }
  393. /// <summary>
  394. /// 切换终端停取模式
  395. /// </summary>
  396. /// <param name="sender"></param>
  397. /// <param name="e"></param>
  398. private void button11_Click(object sender, EventArgs e)
  399. {
  400. try
  401. {
  402. List<object> list = pl.ReadFromPLC(PLCDataType.terminal, 1);
  403. short termStatus = ((TerminalStru)list[0]).terminalStatus;
  404. if (termStatus == (short)1)
  405. {
  406. pl.WriteAccordingToOffset(PLCDataType.terminal, 2, (short)2);
  407. }
  408. else
  409. {
  410. pl.WriteAccordingToOffset(PLCDataType.terminal, 2, (short)1);
  411. }
  412. }
  413. catch { }
  414. }
  415. private void btn_PLCLink_Click(object sender, EventArgs e)
  416. {
  417. if (pl == null)
  418. {
  419. //pl = new PLCLinker(CpuType.S71500, ip, rack, slot, int.Parse(blockIds[0]), int.Parse(blockIds[1]), int.Parse(blockIds[2]), 6, 200);
  420. pl = (PLCLinker)Monitor.Monitor.PLC;
  421. }
  422. if (!pl.isConnected)
  423. {
  424. pl.PLCConnect();
  425. }
  426. Control.ControlCollection cc = this.Controls;
  427. foreach (Control c in cc)
  428. {
  429. c.Enabled = pl.isConnected;
  430. }
  431. }
  432. }
  433. }