Form1.cs 15 KB

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